예제 #1
0
파일: AI.cs 프로젝트: Qazebulon/Repository
        //-- UPDATE BOARD (Other Player Moved) --
        public int nextMove(TTTBoard updatedBoard)
        {
///            console.text += "\nDo I Remember? " + testMemory + "\n\n";

            MemoryCell currentCell = memory.getCellForIndex(moves_memory, movesIndex);//MCell when other player moved started their turn

            console.text += currentCell.outputMemoryCell();

            int change = boardDiff(updatedBoard);

            console.text += "Change == " + change + " @MovesIndex:" + movesIndex + "\n"; //DEBUG

            if (change >= 0)                                                             //Skip if AI is making the first move (-1 denotes no change) //DEBUG
            {
                moves_board[movesIndex]  = boardDiff(updatedBoard);
                moves_memory[movesIndex] = currentCell.getMemoryIndexForMove(change);
                console.text            += "Moves:\n";    //Debug
                for (int ctr = 0; ctr <= movesIndex; ctr++)
                {
                    console.text += "MB_" + ctr + ": " + moves_board[ctr] + "\t";               //Debug
                    console.text += "MM_" + ctr + ": " + moves_memory[ctr] + "\n";              //Debug
                }

                //CHECK TO SEE IF OTHER'S INPUT NEEDS MAPPING...
                int memoryIndex = moves_memory[movesIndex];
                if (memoryIndex == -1)  //Needs mapping
                {
                    for (int ctr = 0; memoryIndex == -1 && ctr < currentCell.possibleMoves.Length; ctr++)
                    {
                        if (currentCell.possibleMoves[ctr].move == -1)
                        {
                            memoryIndex = ctr;
                            moves_memory[movesIndex] = movesIndex;
                            currentCell.possibleMoves[memoryIndex].move = moves_board[movesIndex];
                        }
                    }
                }//new input mapped...

                currentCell = currentCell.possibleMoves[moves_memory[movesIndex]];//Advance current cell (they moved)
                movesIndex++;   //AI's Turn now
                board = updatedBoard.deepCopyBoard();

                //Check Defeat
                if (board.checkVictory())
                {                                                                             //Update MemoryCell score if AI lost.
                    console.text     += "AI LOST :( ";
                    currentCell       = memory.getCellForIndex(moves_memory, movesIndex - 2); //movesIndex is current move, -1 opponent's last move, -2 AI's last.
                    currentCell.score = (int)Scores.Loss;
                    console.text     += "score is now " + currentCell.score + "\n";           //Debug
///                    testMemory = true;
                }
            }

            //Get Next Move
            int retval = getMove();//Get Next Move

            console.text += "AI Move Retval: " + retval + "\n";

            return(retval);
        }
예제 #2
0
        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));
        }
예제 #3
0
파일: AI.cs 프로젝트: Qazebulon/Repository
        }//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);
        }
예제 #4
0
        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;
        }