Exemple #1
0
        //-- 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);
        }
Exemple #2
0
    void Start()
    {
        TTT.TTTBoard aBoard = new TTT.TTTBoard(3, 3);

        Debug.Log(aBoard.availableMoves);
        StartCoroutine(cpu.GetDecision(aBoard));
    }
Exemple #3
0
        public int getMoveFromComputer(TTTBoard currentBoard)
        {
            int retval = -1;

            computer.console = console;
            retval           = computer.nextMove(currentBoard);
            return(retval);
        }
Exemple #4
0
        }//Fills board array with initial values of ' ' (space).

        public TTTBoard deepCopyBoard()
        {
            TTTBoard copy = new TTTBoard();

            for (int ctr = 0; ctr < 9; ctr++)
            {
                copy.board[ctr] = board[ctr];
            }
            return(copy);
        }//Return a deep copy of the board.
Exemple #5
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));
        }
Exemple #6
0
    public void StartGame(AbstractPlayer p1, AbstractPlayer p2)
    {
        //Deactivate the options to start a game
        uiCanvas.SetActive(false);
        winLossText.gameObject.SetActive(false);

        //Remove any previous board pieces
        for (int i = 0; i < squares.Count; ++i)
        {
            squares[i].DumpPieces();
        }

        for (int i = 0; i < pieces.Count; ++i)
        {
            Destroy(pieces[i].gameObject);
        }

        //Flush queue
        playerTurns = new Queue <AbstractPlayer>();
        winner      = null;

        Debug.Log("RESETING");
        //Reset important data
        p1.ResetPlayer(TTT.SquareState.X);
        p2.ResetPlayer(TTT.SquareState.O);

        //Enqueue both players to make moves
        playerTurns.Enqueue(p1);
        playerTurns.Enqueue(p2);

        gameBoard = new TTT.TTTBoard(3, 3);

        started = true;

        //Undo previous table-flips
        OpponentAnimationController oac = null;

        if (player1.TryGetComponent(out oac))
        {
            oac.UN_YEET();
        }

        oac = null;

        if (player2.TryGetComponent(out oac))
        {
            oac.UN_YEET();
        }
    }
Exemple #7
0
        private int boardDiff(TTTBoard updatedBoard)
        {
            int  change         = 0;
            bool changeNotFound = true;

            for (; change < 9 && changeNotFound; change++)
            {
                if (board.board[change] != updatedBoard.board[change])
                {
                    changeNotFound = false;
                }
            }
            change--;//tmp test...
            if (changeNotFound)
            {
                change = -1;
            }
            return(change);
        }//Returns the last move (Other Player's Move). -1 denotes no change (eg. blank board)
Exemple #8
0
        //Constructor to duplicate boards, such that the AI does not modify the existing board
        public TTTBoard(TTTBoard toDuplicate)
        {
            amountForWin = toDuplicate.amountForWin;
            boardLength  = toDuplicate.boardLength;
            board        = new TTTSquare[toDuplicate.board.Length][];

            //Sets the board squares to be the same as the previous board
            for (int i = 0; i < boardLength; ++i)
            {
                board[i] = new TTTSquare[boardLength];

                for (int j = 0; j < boardLength; ++j)
                {
                    board[i][j] = new TTTSquare(toDuplicate.board[i][j]);
                }
            }

            availableMoves = toDuplicate.availableMoves;

            SetWinConditions();

            SetSymmetries();
        }