// this checks if it's the computer's turn. if so it will choose a cell to give it a highlight // as if computer is mousing over it, and then start a timer to choose in a fraction of a second later private void HandlePossibleComputerTurn(object sender, EventArgs e) { // if current turn is a computer's turn if (TileTacToe.Players[TileTacToe.CurrentTurn].IsComputer) { // not claiming cell just yet, saving a cell to choose computerChosenCell = GameLogic.ChooseACell(); // then highlighting it computerChosenCell.BackColor = TileTacToe.Players[TileTacToe.CurrentTurn].TileColor.Lightest; // and starting a timer to actually claim it during the Tick computerTimer.Enabled = true; } }
// called from MainForm after Cells[,,] are instantiated (since cells in the lines are references within that array) public static void BuildCellLines() { // from wikipedia: en.wikipedia.org/wiki/3D_tic-tac-toe // "There are 76 winning lines. On each of the four 4x4 boards, or horizontal planes, there are four columns, // four rows, and two diagonals, accounting for 40 lines. There are 16 vertical lines, each ascending from // a cell on the bottom board through the corresponding cells on the other boards. There are eight vertically- // oriented planes parallel to the sides of the boards, each of these adding two more diagonals (the horizontal // and vertical lines of these planes have already been counted). Finally, there are two vertically-oriented // planes that include the diagonal lines of the 4x4 boards, and each of these contributes two more diagonal // lines—each of these including two corners and two internal cells." // this function builds all lines mentioned above. each list item is an array of GameInfo.Cell references. // (this also supports any sized board, but comments will assume a 4x4x4 board) // also, as cells are added, their weights are incremented so its Weight is equal to the number of its lines // this function builds all 76 lines mentioned above. each line is an array of GameInfo.Cell references. int size = TileTacToe.BOARD_SIZE; // to save some typing int index = 0; // index into Lines, incremented after a line is complete // "On each of the four 4x4 boards, or horizontal planes, there are four columns, // four rows, and two diagonals, accounting for 40 lines." for (int z = 0; z < size; z++) // for each horizontal plane { for (int x = 0; x < size; x++) // there are four columns { cellLines[index] = new CellButton[size]; for (int y = 0; y < size; y++) // gather the column into a Lines entry { cellLines[index][y] = AddCell(x, y, z); } index++; } } for (int z = 0; z < size; z++) // for each horizontal plane { for (int y = 0; y < size; y++) // there are four rows { cellLines[index] = new CellButton[size]; for (int x = 0; x < size; x++) // gather the row into a Lines entry { cellLines[index][x] = AddCell(x, y, z); } index++; } } // two diagonals for each horizontal plane for (int z = 0; z < size; z++) // for each horizontal plane { // first diagonal from 0,0,z -> 1,1,z -> 2,2,z -> 3,3,z cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) // gather first diagonal into a Lines entry { cellLines[index][i] = AddCell(i, i, z); } index++; // second diagonal from 0,3,z -> 1,2,z -> 2,1,z -> 3,0,z cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) // gather second diagonal into a Lines entry { cellLines[index][i] = AddCell(i, size - i - 1, z); } index++; } // for a 4x4x4 board there are 40 lines at this point // "There are 16 vertical lines, each ascending from a cell on the bottom board through the // corresponding cells on the other boards." for (int x = 0; x < size; x++) // for each column { for (int y = 0; y < size; y++) // and row { cellLines[index] = new CellButton[size]; for (int z = 0; z < size; z++) // gather the vertical line into a Lines entry { cellLines[index][z] = AddCell(x, y, z); } index++; } } // for a 4x4x4 board there are 56 lines at this point // "There are eight vertically-oriented planes parallel to the sides of the boards, each of these // adding two more diagonals (the horizontal and vertical lines of these planes have already been counted)." // four vertical diagonals along each column for (int x = 0; x < size; x++) { // first diagonal from x,0,0 -> x,1,1 -> x,2,2 -> x,3,3 cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) // gather first diagonal along each vertical column { cellLines[index][i] = AddCell(x, i, i); } index++; // second diagonal from x,0,3 -> x,1,2 -> x,2,1 -> x,3,0 cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) // gather second diagonal along each vertical column { cellLines[index][i] = AddCell(x, i, size - i - 1); } index++; } // four vertical diagonals along each row for (int y = 0; y < size; y++) { // first diagonal from 0,y,0 -> 1,y,1 -> 2,y,2 -> 3,y,3 cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) { cellLines[index][i] = AddCell(i, y, i); } index++; // second diagonal from 0,y,3 -> 1,y,2 -> 2,y,1 -> 3,y,0 cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) { cellLines[index][i] = AddCell(i, y, size - i - 1); } index++; } // for a 4x4x4 board there are 72 lines at this point // "Finally, there are two vertically-oriented planes that include the diagonal lines of the // 4x4 boards, and each of these contributes two more diagonal lines—each of these including // two corners and two internal cells." // diagonal 0,0,0 -> 1,1,1 -> 2,2,2 -> 3,3,3 cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) { cellLines[index][i] = AddCell(i, i, i); } index++; // diagonal 0,0,3 -> 1,1,2 -> 2,2,1 -> 3,3,0 cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) { cellLines[index][i] = AddCell(i, i, size - i - 1); } index++; // diagonal 0,3,0 -> 1,2,1 -> 2,1,2 -> 3,0,3 cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) { cellLines[index][i] = AddCell(i, size - i - 1, i); } index++; // diagonal 3,0,0 -> 2,1,1 -> 1,2,2 -> 0,3,3 cellLines[index] = new CellButton[size]; for (int i = 0; i < size; i++) { cellLines[index][i] = AddCell(size - i - 1, i, i); } index++; // done! at this point there are 76 lines for a 4x4x4 board // the four corner cells of the cube and the inner four are involved in 7 lines and have a Weight of 7 // the rest of the cells are involved in 4 lines and have a Weight of 4 // testing at other sizes: // for a 3x3x3 board there are 49 lines // for a 5x5x5 board there are 109 lines } // end BuildLines