コード例 #1
0
    //Generates all children (results of all moves) from this state.
    public override List <AIState> generateChildren()
    {
        //List of children
        children = new List <AIState> ();
        //If the game is already over there are no children
        if (getWinner() >= 0)
        {
            return(children);
        }
        //Swap the player
        int newPIndx = (playerIndex == 0) ? 1 : 0;

        //Loop through all of the board pieces
        for (int i = 0; i < 9; i++)
        {
            //if it is 0 (therefore empty)
            if (stateRep[i] == 0)
            {
                //We have a possible peice to play so clone the board
                int[] newBoard = (int[])stateRep.Clone();
                //and simululate playing a piece
                newBoard [i] = playerIndex + 1;
                TTTAIState childAIState = new TTTAIState(newPIndx, this, depth + 1, newBoard);
                //And add this state as a child
                children.Add(childAIState);
            }
        }
        //return it.
        return(children);
    }
コード例 #2
0
    //Handles the player clicking a tile.
    public override void handlePlayerAt(int x, int y)
    {
        //Get the staterep location and updating it with the correct value
        latestStateRep[x * 3 + y] = playerIndx == 0 ? 2 : 1;
        //Change the players turn
        currentPlayersTurn = (currentPlayersTurn + 1) % 2;
        //Update the number of moves
        numbMovesPlayed++;
        //Set up the last state
        latestAIState = new TTTAIState(playerIndx, null, 0, latestStateRep);
        //Find out the result of the board
        int result = latestAIState.getWinner();

        //And the end game as such
        if (result >= 0)
        {
            if (result == 2)
            {
                winlose.text = "You drew!";
            }
            else if (result == playerIndx)
            {
                winlose.text = "You won!";
            }
            else
            {
                winlose.text = "You lost!";
            }
            gamePlaying = false;
            EndGame.SetActive(true);
        }
    }
コード例 #3
0
 //Called each time the update loop checks the AI progress
 public override int checkAI()
 {
     //If the AI has not stated
     if (!ai.started)
     {
         //Start it with the current state.
         AIState currentState = new TTTAIState((currentPlayersTurn + 1) % 2, null, 0, latestStateRep);
         ai.run(currentState);
     }
     //Otherwise if the AI is done
     else if (ai.done)
     {
         //Get the next state (after the AI has moved)
         TTTAIState nextAIState = (TTTAIState)ai.next;
         //Unpack the state
         latestAIState  = nextAIState;
         latestStateRep = nextAIState.stateRep;
         //Reset the AI
         ai.reset();
         //Switch which player is playing
         currentPlayersTurn = (currentPlayersTurn + 1) % 2;
         //Update the graphical rep of the board
         updateBoard();
         //And increment the number of moves
         numbMovesPlayed++;
     }
     //Return who the winner is
     return(latestAIState == null ? -1 :(latestAIState.getWinner()));
 }
コード例 #4
0
 public TicTacToe()
 {
     //Make a blank state
     latestAIState      = new TTTAIState();
     currentPlayersTurn = 0;
 }