Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GameGrid<T>
- {
- protected class Coordinates
- {
- public int Row { get; set; }
- public int Col { get; set; }
- public int RightDiagRow { get; set; }
- public int RightDiagPos { get; set; }
- public int LeftDiagRow { get; set; }
- public int LeftDiagPos { get; set; }
- public Coordinates()
- {
- Row = -1;
- Col = -1;
- RightDiagRow = -1;
- RightDiagPos = -1;
- LeftDiagRow = -1;
- LeftDiagPos = -1;
- }
- public Coordinates(int row, int col, int rightDiagRow, int rightDiagPos, int leftDiagRow, int leftDiagPos)
- {
- Row = row;
- Col = col;
- RightDiagRow = rightDiagRow;
- RightDiagPos = rightDiagPos;
- LeftDiagRow = leftDiagRow;
- LeftDiagPos = leftDiagPos;
- }
- }
- protected class Cell
- {
- public T Value { get; set; }
- public Coordinates Location { get; set; }
- public Cell()
- {
- Value = default(T);
- Location = new Coordinates();
- }
- public Cell(int row, int col,int rightDiagRow,int rightDiagPos, int leftDiagRow,int leftDiagPos)
- {
- Value = default(T);
- Location = new Coordinates(row, col, rightDiagRow, rightDiagPos, leftDiagRow, leftDiagPos);
- }
- public override string ToString()
- {
- return Value.ToString();
- }
- }
- const int DEFAULT_GRID_SIZE = 3;
- protected int GridSize { get; set; }
- protected Cell[][] Rows { get; set; }
- protected Cell[][] Cols { get; set; }
- protected Cell[][] RightDiagRows { get; set; }
- protected Cell[][] LeftDiagRows { get; set; }
- /// <summary>
- /// New GameGrid set to a default size of 3
- /// </summary>
- protected GameGrid()
- {
- GridSize = DEFAULT_GRID_SIZE;
- InitGrid();
- }
- /// <summary>
- /// New GameGrid set to gridSize
- /// </summary>
- protected GameGrid(int gridSize)
- {
- GridSize = gridSize;
- InitGrid();
- }
- private void InitGrid()
- {
- Rows = new Cell[GridSize][];
- Cols = new Cell[GridSize][];
- int diagSize = (GridSize * 2) - 1;
- RightDiagRows = new Cell[diagSize][];
- LeftDiagRows = new Cell[diagSize][];
- for(int i = 0; i < GridSize;i++)
- {
- Rows[i] = new Cell[GridSize];
- Cols[i] = new Cell[GridSize];
- RightDiagRows[i] = new Cell[GridSize];
- LeftDiagRows[i] = new Cell[GridSize];
- }
- for(int i = GridSize; i < diagSize;i++)
- {
- RightDiagRows[i] = new Cell[GridSize];
- LeftDiagRows[i] = new Cell[GridSize];
- }
- for (int row = 0; row < GridSize; row++)
- {
- for (int col = 0; col < GridSize; col++)
- {
- int rightDiagRow = ((GridSize * 2) - 2) - (row + col);
- int rightDiagPos = rightDiagRow < GridSize ? (GridSize - 1) - col : row;
- int leftDiagRow = Math.Abs((row - col) - (GridSize - 1));
- int leftDiagPos = leftDiagRow < GridSize ? col : row;
- Cell newCell = new Cell(row, col, rightDiagRow, rightDiagPos, leftDiagRow, leftDiagPos);
- Rows[row][col] = newCell;
- Cols[col][row] = newCell;
- RightDiagRows[rightDiagRow][rightDiagPos] = newCell;
- LeftDiagRows[leftDiagRow][leftDiagPos] = newCell;
- }
- }
- for(int i = 0; i < RightDiagRows.Length;i++)
- {
- RightDiagRows[i] = RightDiagRows[i].Where(x => x != null).ToArray();
- LeftDiagRows[i] = LeftDiagRows[i].Where(x => x != null).ToArray();
- }
- }
- }
- public class TicTacToe : GameGrid<char>
- public TicTacToe():base()
Add Comment
Please, Sign In to add comment