Example #1
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();
        }
    }
    /// <summary>
    /// Gets the possible placements on the passed in board and returns them as an array of Placement structures.
    /// </summary>
    /// <returns>The possible placements.</returns>
    /// <param name="gameBoard">Game board.</param>
    /// <param name="player">Player.</param>
    Placement[] GetPossiblePlacements(int[,] gameBoard, int player)
    {
        Placement[] possiblePlacements;
        int         numberOfPossiblePlacements = 0;

        for (int y = 0; y < 8; y++)
        {
            for (int x = 0; x < 8; x++)
            {
                if (boardBehaviour.CanPlaceTile(x, y, playerNumber, gameBoard))
                {
                    numberOfPossiblePlacements++;
                }
            }
        }

        if (numberOfPossiblePlacements == 0)
        {
            return(null);
        }

        possiblePlacements = new Placement[numberOfPossiblePlacements];
        int currentPlacementNumber = 0;

        for (int y = 0; y < 8; y++)
        {
            for (int x = 0; x < 8; x++)
            {
                if (boardBehaviour.CanPlaceTile(x, y, playerNumber, gameBoard))
                {
                    possiblePlacements [currentPlacementNumber]   = new Placement();
                    possiblePlacements [currentPlacementNumber].x = x;
                    possiblePlacements [currentPlacementNumber].y = y;

                    possiblePlacements [currentPlacementNumber].board = new int[8, 8];
                    int[,] tempBoard = boardBehaviour.SetTileState(x, y, player, gameBoard);
                    Array.Copy(tempBoard, possiblePlacements [currentPlacementNumber].board, tempBoard.Length);

                    currentPlacementNumber++;
                }
            }
        }

        return(possiblePlacements);
    }
Example #3
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();
     }
 }
Example #4
0
    Node[] GetBranches(Node root, int depth)
    {
        Node[] branches;
        int    currentPlayer;
        int    numberOfBranches = 0;

        //Prevents the simulation from running too deep.
        if (depth > 2)
        {
            return(null);
        }

        //Work out who the current player is
        if (playerNumber == 1)
        {
            if (depth == 1)
            {
                currentPlayer = 1;
            }
            else
            {
                currentPlayer = 2;
            }
        }
        else
        if (depth == 1)
        {
            currentPlayer = 2;
        }
        else
        {
            currentPlayer = 1;
        }

        //Calculate how many possible moves there are and thus how many branches we need
        for (int y = 0; y < 7; y++)
        {
            for (int x = 0; x < 7; x++)
            {
                if (boardBehaviour.CanPlaceTile(x, y, currentPlayer, root.board))
                {
                    numberOfBranches++;
                }
            }
        }

        int currentBranch = 0;

        branches = new Node[numberOfBranches];

        //For each possible branch, simulate a turn
        for (int y = 0; y < 7; y++)
        {
            for (int x = 0; x < 7; x++)
            {
                if (boardBehaviour.CanPlaceTile(x, y, currentPlayer, root.board))
                {
                    Debug.Log("Simulating move at " + x + "," + y + ", move " + currentBranch + "/" + numberOfBranches);
                    branches [currentBranch]       = new Node();
                    branches [currentBranch].board = new int[8, 8];
                    branches [currentBranch]       = SimulateTurn(x, y, currentPlayer, root);
                    Debug.Log("Successful run, depth " + depth);
                    currentBranch++;
                }
            }
        }

        //For each possible branch, get their branches
        for (int i = 0; i < numberOfBranches; i++)
        {
            branches [i].next = GetBranches(branches [i], depth + 1);
        }

        return(branches);
    }