示例#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)
        {
            this.children = children;
            return(children);
        }
        //Swap the player
        int newPIndx = (playerIndex + 1) % 2;
        //Increment the number of peices played
        int newNumbPieces = numbPiecesPlayed + 1;

        //Loop through all of the board pieces
        for (int i = 0; i < stateRep.Length; i++)
        {
            //if it is 0 (therefore empty)
            if (stateRep[i] == 0)
            {
                //We have a possible peice to play so clone the board
                int[] newStateRep = (int[])stateRep.Clone();
                //and simululate playing a piece
                newStateRep [i] = playerIndex + 1;
                HexAIState childAIState = new HexAIState(newPIndx, this, depth + 1, newStateRep, newNumbPieces);
                //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 * 9 + 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 HexAIState(playerIndx, null, 0, latestStateRep, numbMovesPlayed);
        //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 HexAIState((currentPlayersTurn + 1) % 2, null, 0, latestStateRep, numbMovesPlayed);
         ai.run(currentState);
     }
     //Otherwise if the AI is done
     else if (ai.done)
     {
         //Get the next state (after the AI has moved)
         HexAIState nextAIState = (HexAIState)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.getWinner());
 }
示例#4
0
 public Hex()
 {
     //Make a blank state
     latestAIState      = new HexAIState();
     currentPlayersTurn = 0;
 }