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); }
public void handleInput(int input) { console.text += "::"; if (numberOfMoves < 9 && !victory) { int boardIndex = 0; if (currentPlayer.human) { //HUMAN console.text += "Human\n"; boardIndex = input; } else { //AI SECTION IN-PROGRESS console.text += "AI\n"; currentPlayer.console = console; boardIndex = currentPlayer.getMoveFromComputer(board); } bool worked = board.attemptMove(boardIndex, currentPlayer.getMarker()); if (worked) { numberOfMoves++; victory = board.checkVictory(); if (currentPlayer.getMarker() == 'X') { currentPlayer = player_o; } else { currentPlayer = player_x; } console.text += "Move Completed\n"; } else { console.text += "Move Failed\n"; } console.text += board.getBoard(); if (victory) { console.text += "Player " + currentPlayer.getMarker() + " Lost!\nSay \"New Game\" to play again.\n"; if (!currentPlayer.human) { currentPlayer.getMoveFromComputer(board); } //Update AI with the loss } else if (numberOfMoves == 9) { console.text += "Cat's Game!\nSay \"New Game\" to play again.\n"; } else if (!currentPlayer.human) { handleInput(-1); }//Don't wait for human command to take AI's Turn... else { console.text += "Player " + currentPlayer.getMarker() + ", select your move.\n"; } //Prompt Human Player } else { console.text += "Cat's Game!\nSay \"New Game\" to play again.\n"; } // console.text += gameText; }