shape.zig 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const std = @import("std");
  2. pub const kShapeSize: i32 = 3;
  3. pub const Shape = struct {
  4. data: [9]u8,
  5. pub fn getCell(self: *const Shape, row: i32, col: i32) u8 {
  6. return self.data[@abs(row*kShapeSize + col)];
  7. }
  8. pub fn setCell(self: *Shape, row: i32, col: i32, cell: u8) void {
  9. self.data[@abs(row*kShapeSize + col)] = cell;
  10. }
  11. pub fn flipHor(self: *Shape) void {
  12. for (0 .. kShapeSize) |row| {
  13. for (0 .. kShapeSize/2) |col| {
  14. const t = self.getCell(@intCast(row), @intCast(col));
  15. self.setCell(@intCast(row), @intCast(col),
  16. self.getCell(@intCast(row),
  17. @intCast(kShapeSize - 1 - col)));
  18. self.setCell(@intCast(row), @intCast(kShapeSize - 1 - col), t);
  19. }
  20. }
  21. }
  22. pub fn flipVer(self: *Shape) void {
  23. for (0 .. kShapeSize) |col| {
  24. for (0 .. kShapeSize/2) |row| {
  25. const t = self.getCell(@intCast(row), @intCast(col));
  26. self.setCell(@intCast(row), @intCast(col),
  27. self.getCell(@intCast(kShapeSize - 1 - row),
  28. @intCast(col)));
  29. self.setCell(@intCast(kShapeSize - 1 - row), @intCast(col), t);
  30. }
  31. }
  32. }
  33. pub fn rotate(self: *Shape) void {
  34. for (0 .. kShapeSize-1) |row| {
  35. for (row+1 .. kShapeSize) |col| {
  36. const t = self.getCell(@intCast(row), @intCast(col));
  37. self.setCell(@intCast(row), @intCast(col),
  38. self.getCell(@intCast(col), @intCast(row)));
  39. self.setCell(@intCast(col), @intCast(row), t);
  40. }
  41. }
  42. self.flipHor();
  43. }
  44. pub fn getAllTransforms(self: * const Shape, allocator: std.mem.Allocator) !std.AutoHashMap(Shape, bool) {
  45. var allTransforms = std.AutoHashMap(Shape, bool).init(allocator);
  46. try allTransforms.put(self.*, true);
  47. var shapeHFlip = self.*;
  48. shapeHFlip.flipHor();
  49. for (0 .. 4) |_| {
  50. try allTransforms.put(shapeHFlip, true);
  51. shapeHFlip.rotate();
  52. }
  53. var shapeVFlip = self.*;
  54. shapeVFlip.flipVer();
  55. for (0 .. 4) |_| {
  56. try allTransforms.put(shapeVFlip, true);
  57. shapeVFlip.rotate();
  58. }
  59. var shapeHVFlip = self.*;
  60. shapeHVFlip.flipHor();
  61. shapeHVFlip.flipVer();
  62. for (0 .. 4) |_| {
  63. try allTransforms.put(shapeHVFlip, true);
  64. shapeHVFlip.rotate();
  65. }
  66. var shapeVHFlip = self.*;
  67. shapeVHFlip.flipVer();
  68. shapeVHFlip.flipHor();
  69. for (0 .. 4) |_| {
  70. try allTransforms.put(shapeVHFlip, true);
  71. shapeVHFlip.rotate();
  72. }
  73. return allTransforms;
  74. }
  75. pub fn print(self: *const Shape, allocator: std.mem.Allocator) !void {
  76. for (0..kShapeSize) |row| {
  77. for (0..kShapeSize) |col| {
  78. const cell = self.getCell(@intCast(row), @intCast(col));
  79. try std.fs.File.stdout().writeAll(
  80. try std.fmt.allocPrint(allocator, "{c}", .{cell}));
  81. }
  82. try std.fs.File.stdout().writeAll("\n");
  83. }
  84. }
  85. };