Guest User

Untitled

a guest
Apr 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. public class GameGrid<T>
  2. {
  3. protected class Coordinates
  4. {
  5. public int Row { get; set; }
  6. public int Col { get; set; }
  7. public int RightDiagRow { get; set; }
  8. public int RightDiagPos { get; set; }
  9. public int LeftDiagRow { get; set; }
  10. public int LeftDiagPos { get; set; }
  11. public Coordinates()
  12. {
  13. Row = -1;
  14. Col = -1;
  15. RightDiagRow = -1;
  16. RightDiagPos = -1;
  17. LeftDiagRow = -1;
  18. LeftDiagPos = -1;
  19. }
  20. public Coordinates(int row, int col, int rightDiagRow, int rightDiagPos, int leftDiagRow, int leftDiagPos)
  21. {
  22. Row = row;
  23. Col = col;
  24. RightDiagRow = rightDiagRow;
  25. RightDiagPos = rightDiagPos;
  26. LeftDiagRow = leftDiagRow;
  27. LeftDiagPos = leftDiagPos;
  28. }
  29. }
  30. protected class Cell
  31. {
  32. public T Value { get; set; }
  33. public Coordinates Location { get; set; }
  34. public Cell()
  35. {
  36. Value = default(T);
  37. Location = new Coordinates();
  38. }
  39. public Cell(int row, int col,int rightDiagRow,int rightDiagPos, int leftDiagRow,int leftDiagPos)
  40. {
  41. Value = default(T);
  42. Location = new Coordinates(row, col, rightDiagRow, rightDiagPos, leftDiagRow, leftDiagPos);
  43. }
  44. public override string ToString()
  45. {
  46. return Value.ToString();
  47. }
  48. }
  49. const int DEFAULT_GRID_SIZE = 3;
  50. protected int GridSize { get; set; }
  51. protected Cell[][] Rows { get; set; }
  52. protected Cell[][] Cols { get; set; }
  53. protected Cell[][] RightDiagRows { get; set; }
  54. protected Cell[][] LeftDiagRows { get; set; }
  55.  
  56. /// <summary>
  57. /// New GameGrid set to a default size of 3
  58. /// </summary>
  59. protected GameGrid()
  60. {
  61. GridSize = DEFAULT_GRID_SIZE;
  62. InitGrid();
  63. }
  64. /// <summary>
  65. /// New GameGrid set to gridSize
  66. /// </summary>
  67. protected GameGrid(int gridSize)
  68. {
  69. GridSize = gridSize;
  70. InitGrid();
  71. }
  72. private void InitGrid()
  73. {
  74. Rows = new Cell[GridSize][];
  75. Cols = new Cell[GridSize][];
  76. int diagSize = (GridSize * 2) - 1;
  77. RightDiagRows = new Cell[diagSize][];
  78. LeftDiagRows = new Cell[diagSize][];
  79. for(int i = 0; i < GridSize;i++)
  80. {
  81. Rows[i] = new Cell[GridSize];
  82. Cols[i] = new Cell[GridSize];
  83. RightDiagRows[i] = new Cell[GridSize];
  84. LeftDiagRows[i] = new Cell[GridSize];
  85. }
  86. for(int i = GridSize; i < diagSize;i++)
  87. {
  88. RightDiagRows[i] = new Cell[GridSize];
  89. LeftDiagRows[i] = new Cell[GridSize];
  90. }
  91. for (int row = 0; row < GridSize; row++)
  92. {
  93. for (int col = 0; col < GridSize; col++)
  94. {
  95. int rightDiagRow = ((GridSize * 2) - 2) - (row + col);
  96. int rightDiagPos = rightDiagRow < GridSize ? (GridSize - 1) - col : row;
  97. int leftDiagRow = Math.Abs((row - col) - (GridSize - 1));
  98. int leftDiagPos = leftDiagRow < GridSize ? col : row;
  99. Cell newCell = new Cell(row, col, rightDiagRow, rightDiagPos, leftDiagRow, leftDiagPos);
  100. Rows[row][col] = newCell;
  101. Cols[col][row] = newCell;
  102. RightDiagRows[rightDiagRow][rightDiagPos] = newCell;
  103. LeftDiagRows[leftDiagRow][leftDiagPos] = newCell;
  104. }
  105. }
  106. for(int i = 0; i < RightDiagRows.Length;i++)
  107. {
  108. RightDiagRows[i] = RightDiagRows[i].Where(x => x != null).ToArray();
  109. LeftDiagRows[i] = LeftDiagRows[i].Where(x => x != null).ToArray();
  110. }
  111. }
  112. }
  113.  
  114. public class TicTacToe : GameGrid<char>
  115.  
  116. public TicTacToe():base()
Add Comment
Please, Sign In to add comment