Exemplo n.º 1
0
 //When the tile is clicked
 void OnMouseDown()
 {
     if (boardBehaviour.currentPlayer == 1 && boardBehaviour.GetTileState(x, y) == 0 && player1.enabled && boardBehaviour.CanPlaceTile(x, y, 1))
     {
         boardBehaviour.SetTileState(x, y, 1);
         boardBehaviour.TurnComplete();
     }
     else if (boardBehaviour.currentPlayer == 2 && boardBehaviour.GetTileState(x, y) == 0 && player2.enabled && boardBehaviour.CanPlaceTile(x, y, 2))
     {
         boardBehaviour.SetTileState(x, y, 2);
         boardBehaviour.TurnComplete();
     }
 }
Exemplo n.º 2
0
    public void UpdateAI()
    {
        Node worstNode;
        Node bestNode;


        if (boardBehaviour.currentPlayer == playerNumber)
        {
            //The AI simulates it's own turn, then the possible responses from the player.
            Node tree = new Node();
            tree.board = new int[8, 8];
            if (playerNumber == 1)
            {
                tree.myScore    = boardBehaviour.player1Score;
                tree.enemyScore = boardBehaviour.player2Score;
            }
            else
            {
                tree.myScore    = boardBehaviour.player2Score;
                tree.enemyScore = boardBehaviour.player1Score;
            }
            Array.Copy(boardBehaviour.board, tree.board, boardBehaviour.board.Length);

            //Create a tree containing every possible move the AI can make right now and every subsequent move the enemy could make.
            tree.next = GetBranches(tree, 1);

            Node[] BestWorstNode = FindWorstAndBestNode(tree);

            bestNode  = BestWorstNode [0];
            worstNode = BestWorstNode [1];

            boardBehaviour.SetTileState(bestNode.x, bestNode.y, playerNumber, true);
            boardBehaviour.TurnComplete();
        }
    }
Exemplo n.º 3
0
    // Method to place a tile on the board.
    public void UpdateAI()
    {
        int highestX = -1;              //The x co-ordinate of the current highest scoring tile
        int highestY = -1;              //The y co-ordinate of the current highest scoring tile

        int highestScore = 0;           //The highest score found so far
        int currentScore;               //The score of the current tile being compared

        if (boardBehaviour.currentPlayer == playerNumber)
        {
            for (int y = 0; y < 8; y++)
            {
                for (int x = 0; x < 8; x++)
                {
                    if (boardBehaviour.CanPlaceTile(x, y, playerNumber))
                    {
                        currentScore = boardBehaviour.GetTileScore(x, y, playerNumber);

                        if (currentScore > highestScore && boardBehaviour.GetTileState(x, y) == 0)
                        {
                            highestScore = currentScore;
                            highestX     = x;
                            highestY     = y;
                        }
                    }
                }
            }

            if (highestX != -1 && highestY != -1)
            {
                boardBehaviour.SetTileState(highestX, highestY, playerNumber);
            }
            boardBehaviour.TurnComplete();
        }
    }
Exemplo n.º 4
0
    // Method to place a tile on the board.
    public void UpdateAI()
    {
        int bestX = 0;                  //The x co-ordinate of the best tile
        int bestY = 0;                  //The y co-ordinate of the best tile

        int highestScore = -1;          //The highest score found so far
        int currentScore = 0;           //The score of the current tile being compared

        if (boardBehaviour.currentPlayer == playerNumber)
        {
            //Find all tiles that can be placed
            Placement[] firstTurnPlacements = GetPossiblePlacements(boardBehaviour.board, playerNumber);

            if (firstTurnPlacements != null)
            {
                //For each placement, assume the enemy picks the best scoring tile and calculate the result accordingly
                for (int i = 0; i < firstTurnPlacements.Length; i++)
                {
                    firstTurnPlacements [i] = FindHighestScoringMove(firstTurnPlacements [i].board, enemyNumber, firstTurnPlacements [i].x, firstTurnPlacements [i].y);
                }
                //We now have an array containing the state of the gameboard corresponding to the possible move after 1 turn.
                //Next, we will simulate all possible moves stemming from each of these first moves.

                //The secondTurnPlacements array is jagged to allow an array corresponding to each first move to be associated with each firstTurnPlacement
                Placement[][] secondTurnPlacements = new Placement[firstTurnPlacements.Length][];

                //Get all possible placements for each first turn placement
                for (int i = 0; i < firstTurnPlacements.Length; i++)
                {
                    secondTurnPlacements [i] = GetPossiblePlacements(firstTurnPlacements [i].board, playerNumber);
                    if (secondTurnPlacements [i] != null)
                    {
                        //Next, for each second turn placement, assume the enemy picks the best scoring tile and calculate the result accordingly
                        for (int c = 0; c < secondTurnPlacements[i].Length; c++)
                        {
                            secondTurnPlacements [i] [c] = FindHighestScoringMove(secondTurnPlacements [i] [c].board, enemyNumber, secondTurnPlacements [i] [c].x, secondTurnPlacements [i] [c].y);
                        }
                    }
                }

                //Check to see if the first element of secondTurnPlacements is null. If this is the case, it means that the next move is the last that can be played.
                //We will therefore then just play the first move in the firstTurnPlacements.

                if (secondTurnPlacements [0] == null)
                {
                    boardBehaviour.SetTileState(firstTurnPlacements [0].x, firstTurnPlacements [0].y, playerNumber);
                    boardBehaviour.TurnComplete();
                }
                else
                {
                    //We now have a tree of sorts that contains all possible states of the game after two moves have been made, assuming the enemy player picks the highest scoring tile.
                    //Next, we will look at the percentage difference in score that each of the first choices could potentially lead to.
                    //We do this by going through every second turn scenario, finding the average score (positive or negative) and then comparing this to the current score.

                    float[] potentialScores = new float[firstTurnPlacements.Length];

                    for (int i = 0; i < firstTurnPlacements.Length; i++)
                    {
                        float scoreTotal = 0;

                        for (int c = 0; c < secondTurnPlacements[i].Length; c++)
                        {
                            scoreTotal += GetScore(secondTurnPlacements [i] [c].board, playerNumber) - GetScore(secondTurnPlacements [i] [c].board, enemyNumber);
                        }

                        //Get the average score.
                        potentialScores [i] = scoreTotal / secondTurnPlacements [i].Length;
                    }

                    Debug.Log("Scores: " + potentialScores);

                    //Now that we know all of the average scores that will result from each possible move, we can pick the move that will give us the best average score.

                    float tempBestScore     = potentialScores [0];
                    int   currentTurnChoice = 0;

                    for (int i = 0; i < potentialScores.Length; i++)
                    {
                        if (potentialScores [i] > tempBestScore)
                        {
                            currentTurnChoice = i;
                            tempBestScore     = potentialScores [i];
                        }
                    }

                    //Now, let's extract the information required to take the turn

                    boardBehaviour.SetTileState(firstTurnPlacements [currentTurnChoice].x, firstTurnPlacements [currentTurnChoice].y, playerNumber);
                    boardBehaviour.TurnComplete();
                }

                /*
                 * for (int y = 0; y < 8; y ++) {
                 * for (int x = 0; x < 8; x++) {
                 *      if (boardBehaviour.CanPlaceTile (x, y, playerNumber)) {
                 *
                 *              //Simulate what would happen if the ai placed a tile here and then the opponent picked the highest scoring tile.
                 *
                 *              gameState.board = new int[8, 8];
                 *
                 *              int tempHighestX = 0;
                 *              int tempHighestY = 0;
                 *              int tempHighestScore = 0;
                 *              currentScore = 0;
                 *
                 *              Array.Copy (boardBehaviour.board, gameState.board, boardBehaviour.board.Length);
                 *              gameState.board = boardBehaviour.SetTileState (x, y, playerNumber, gameState.board);
                 *
                 *              for (int x2 = 0; x2 < 8; x2++) {
                 *                      for (int y2 = 0; y2 < 8; y2++) {
                 *                              if (boardBehaviour.GetTileScore (x2, y2, enemyNumber) > tempHighestScore) {
                 *                                      tempHighestX = x2;
                 *                                      tempHighestY = y2;
                 *                                      tempHighestScore = boardBehaviour.GetTileScore (x2, y2, enemyNumber, gameState.board);
                 *                              }
                 *                      }
                 *              }
                 *
                 *              gameState.board = boardBehaviour.SetTileState (x, y, enemyNumber, gameState.board);
                 *
                 *              //Now, repeat this process again.
                 *
                 *              //Calculate the score
                 *
                 *              for (int y2 = 0; y2 < 7; y2++) {
                 *                      for (int x2 = 0; x2 < 7; x2++) {
                 *                              if (gameState.board [x2, y2] == playerNumber)
                 *                                      currentScore++;
                 *                      }
                 *              }
                 *
                 *              if (currentScore > highestScore) {
                 *                      bestX = x;
                 *                      bestY = y;
                 *                      highestScore = currentScore;
                 *              }
                 *      }
                 * }
                 * }
                 */
                //boardBehaviour.SetTileState (bestX, bestY, playerNumber);
                //boardBehaviour.TurnComplete ();
            }
            else
            {
                boardBehaviour.TurnComplete();
            }
        }
    }