/** * This function first calls the AI to get a space to move to. * Next it updates the gui * Finally makes a call to determineEndgame to check if the game is over or not. */ void makeComputerMove() { computersMove = true; int compMove = TTTAI.getMove(buttons, 'O', 'X', this.difficulty); // Get computer's move. bool error = true; switch (compMove) // Update the board to reflect computer's move. { case 0: error = killButton(this.button1, 0); break; case 1: error = killButton(this.button2, 1); break; case 2: error = killButton(this.button3, 2); break; case 3: error = killButton(this.button4, 3); break; case 4: error = killButton(this.button5, 4); break; case 5: error = killButton(this.button6, 5); break; case 6: error = killButton(this.button7, 6); break; case 7: error = killButton(this.button8, 7); break; case 8: error = killButton(this.button9, 8); break; } if (!error) // An error occurred, the computer did not make a move.!!! { Form dd = new TTTDialogForm("ERROR"); dd.ShowDialog(); } determineEndgame(false); }
/** * @return: true if the game is over, false if it is not. * * This function is used to determine if the game is over. * First it uses a function in the AI to determine if there is a winner. * If there is, set the winner & return true. If there's not, check to see if more moves * can be made. If the board is filled up return true, else return false. */ bool isGameOver() { char temp = TTTAI.checkForAWinner(buttons); if (temp != '\0') { this.winner = temp; return(true); } // There isn't a winner, but the game is over if all spaces on the board are occupied. for (int i = 0; i < this.buttons.Length; i++) { if (this.buttons[i] == '\0') { return(false); // A move can still be made; thus game goes on! } } return(true); // No more moves can be made; the game is over! }