public int getMoveFromComputer_old(TTTBoard currentBoard) { TTTBoard testBoard = new TTTBoard(); testBoard.resetBoard(); char testmarker = marker; for (int magic = 0; magic < 2; magic++) { for (int i = 0; i < 9; i++) { for (int y = 0; y < 9; y++) { testBoard.board[y] = currentBoard.board[y]; } if (testBoard.attemptMove(i, testmarker)) { if (testBoard.checkVictory()) { return(i); } } } if (testmarker == 'X') { testmarker = 'O'; } else { testmarker = 'X'; } } //Move in center if that is available if (currentBoard.checkMove(4)) { return(4); } //Move in corner if available (center is already taken...) Random random = new Random(); for (int ctr = 0; ctr < 5; ctr++) { //int move = random.Next(0, 4); int move = ctr; move *= 2; //ensure move is even (0,2,4,6,8) if (currentBoard.checkMove(move)) { return(move); } } return(random.Next(0, 9)); }
}//Returns the last move (Other Player's Move). -1 denotes no change (eg. blank board) //-- SELECT NEXT AI MOVE -- public int getMove() { //GET POSSIBLE MOVES (Get Current Memory Cell) MemoryCell currentCell = memory.getCellForIndex(moves_memory, movesIndex); //EVALUATE POSSIBLE MOVES (Get Best Move) int bestIndex = 0; int retval = currentCell.possibleMoves[bestIndex].move; for (int ctr = 1; ctr < currentCell.possibleMoves.Length; ctr++) //index 0 is assummed to be best initially... { int newScore = currentCell.possibleMoves[ctr].score; if (currentCell.possibleMoves[ctr].score > currentCell.possibleMoves[bestIndex].score) { bestIndex = ctr; retval = currentCell.possibleMoves[bestIndex].move; } } //CONFIRM BEST MOVE (-1 => next valid move) for (int ctr = 0; retval == -1 && ctr < 9; ctr++) //This will only run if move is unkown... { if (board.checkMove(ctr)) { retval = ctr; currentCell.possibleMoves[bestIndex].move = retval;//Update the MemoryCell so it will be known next time. } } //Make move (on AI's board only... real move is handled by TTTGame object.) board.attemptMove(retval, mark);//AI should never attempt illeagal moves... moves_board[movesIndex] = retval; moves_memory[movesIndex] = bestIndex; currentCell = currentCell.possibleMoves[bestIndex];//Advance current cell (they moved) //CHECK VICTORY? (If so, update memory to denote this is a victorious move sequence) if (board.checkVictory()) //Update MemoryCell score if AI won. { currentCell.score = (int)Scores.Victory; } //UPDATE MOVES INDEX movesIndex++; return(retval); }