private void timer1_Tick(object sender, EventArgs e) { int[][] board = game.GetBoard(); if (board[0][0] != 0) { label1.Text = board[0][0] == 1?"X":"0"; } if (board[0][1] != 0) { label2.Text = board[0][1] == 1?"X":"0"; } if (board[0][2] != 0) { label3.Text = board[0][2] == 1?"X":"0"; } if (board[1][0] != 0) { label4.Text = board[1][0] == 1?"X":"0"; } if (board[1][1] != 0) { label5.Text = board[1][1] == 1?"X":"0"; } if (board[1][2] != 0) { label6.Text = board[1][2] == 1?"X":"0"; } if (board[2][0] != 0) { label7.Text = board[2][0] == 1?"X":"0"; } if (board[2][1] != 0) { label8.Text = board[2][1] == 1?"X":"0"; } if (board[2][2] != 0) { label9.Text = board[2][2] == 1?"X":"0"; } }
public override char Move() { // First check if threathened int[][] grid = game.GetBoard(); int gridSize = game.GetGridSize(); int mark = GetMark(); int otherMark; char defendChoice = ' '; char winningChoice = ' '; char choice = ' '; if (mark == 1) { otherMark = 2; } else { otherMark = 1; } // Players play either '1' or '2' int count; // Check rows for (int row = 0; row < gridSize; row++) { count = 0; for (int col = 0; col < gridSize; col++) { if (grid[row][col] == otherMark) { count++; } } if (count == gridSize - 1) { // Defense for (int col = 0; col < gridSize; col++) { if (grid[row][col] == 0) { defendChoice = (char)(row * gridSize + col + 97); } } } } // Check columns for (int col = 0; col < gridSize; col++) { count = 0; for (int row = 0; row < gridSize; row++) { if (grid[row][col] == otherMark) { count++; } } if (count == gridSize - 1) { // Defense for (int row = 0; row < gridSize; row++) { if (grid[row][col] == 0) { defendChoice = (char)(row * gridSize + col + 97); } } } } // Check positive diagonal count = 0; for (int row = 0, col = 0; (row < gridSize && col < gridSize); row++, col++) { if (grid[row][col] == otherMark) { count++; } } if (count == gridSize - 1) { // Defense for (int row = 0, col = 0; (row < gridSize && col < gridSize); row++, col++) { if (grid[row][col] == 0) { defendChoice = (char)(row * gridSize + col + 97); } } } // Check negative diagonal count = 0; for (int row = 0, col = gridSize - 1; (row < gridSize && col >= 0); row++, col--) { if (grid[row][col] == otherMark) { count++; } } if (count == gridSize - 1) { // Defense for (int row = 0, col = gridSize - 1; (row < gridSize && col >= 0); row++, col--) { if (grid[row][col] == 0) { defendChoice = (char)(row * gridSize + col + 97); } } } // See if we have learnt anything in the past ArrayList alPossibilities = (ArrayList)memory.getChildTries(game.GetMoves()); if (alPossibilities != null) { if (alPossibilities.Count > 0) { // For the time being, select the first possibility Trie t = (Trie)alPossibilities[new Random().Next(alPossibilities.Count)]; if (t.getValue() != null) { winningChoice = t.getChar(); } else { choice = t.getChar(); } } else { } } else { // Go for a random placement while (true) { choice = (char)(new Random().Next(game.GetGridSize() * game.GetGridSize()) + 97); if (game.IsEmpty(choice)) { break; } } } if (winningChoice != ' ') { choice = winningChoice; } else if (defendChoice != ' ') { choice = defendChoice; } return(choice); }