示例#1
0
 public void AddPlayerToSquare(GameBoard.PLAYERS_ID playerNum, int boardRow, int boardColumn)
 {
     isOccupied = true;
     playerID   = playerNum;
     row        = boardRow;
     column     = boardColumn;
 }
示例#2
0
    public void AddMovetoGameMoveBoard(Move nextMove, GameBoard.PLAYERS_ID nextMovePlayer)
    {
        int nextMoveRow    = nextMove.Row;
        int nextMoveColumn = nextMove.Column;

        //update the board with the move by the currentplayer
        gameMoveBoard[nextMoveRow, nextMoveColumn].setOccupied(nextMovePlayer);
    }
示例#3
0
    public void makeMove(Move nextMove, GameBoard.PLAYERS_ID nextMovePlayer)
    {
        //add move to our scratch pad game board
        AddMovetoGameMoveBoard(nextMove, nextMovePlayer);

        nextMove.setOccupied(nextMovePlayer);

        //if the nextMove is made then create a list of children with the remaining possible moves
        AddPossibleMovesForNextMove(nextMove);
    }
示例#4
0
 public void initMove(int row, int column)
 {
     score      = 0;
     rowPos     = row;
     colPos     = column;
     isOccupied = false;
     children.Clear();
     occupiedByPlayer = GameBoard.PLAYERS_ID.PLAYER_NONE;
     currentDepth     = 0;
 }
示例#5
0
 public void InitPlayerSquare(int boardRow, int boardColumn)
 {
     isOccupied = false;
     playerID   = GameBoard.PLAYERS_ID.PLAYER_NONE;
     row        = boardRow;
     column     = boardColumn;
     SetGamePiecePositions();
     SetParameters(5.0f, 10.0f, boardRow, boardColumn);
     GetComponent <Renderer>().material.color = new Color(255, 255, 0);
 }
示例#6
0
    static Move getBestMove(GameBoard.PLAYERS_ID player)
    {
        //make a moveBoard of the current state of the board
        moveBoard currentMoveBoard = new moveBoard();

        //this move board is used as a scratch pad to examine possible next moves
        currentMoveBoard.setCurrentGameBoardToMoveBoard(player);

        //using the moveboard the minimax function finds the next BEST move
        Move bestMove = miniMax(currentMoveBoard, currentMoveBoard.Root, player, MAX_DEPTH, 0);

        return(bestMove);
    }
示例#7
0
    public void initMoveBoard()
    {
        rootMove.ClearChildrenList();
        isMoveGameFinished = false;
        currentPlayer      = GameBoard.PLAYERS_ID.PLAYER_NONE;

        for (int i = 0; i < GameBoard.MAX_ROWS; i++)
        {
            for (int j = 0; j < GameBoard.MAX_COLUMNS; j++)
            {
                gameMoveBoard[i, j]       = new Move(i, j);
                gameMoveBoard[i, j].Score = 0;
            }
        }
    }
示例#8
0
    public void setCurrentGameBoardToMoveBoard(GameBoard.PLAYERS_ID initPlayer)
    {
        for (int i = 0; i < GameBoard.MAX_ROWS; i++)
        {
            for (int j = 0; j < GameBoard.MAX_COLUMNS; j++)
            {
                gameMoveBoard[i, j].Occupied = GameBoard.gameBoard[i, j].Occupied;
                gameMoveBoard[i, j].PlayerID = GameBoard.gameBoard[i, j].PlayerID;
            }
        }

        //set up the root node to match the current game board
        CurrentPlayer     = initPlayer;
        rootMove.PlayerID = initPlayer;
        rootMove.Score    = NO_SCORE;
        AddPossibleMovesForNextMove(rootMove);
    }
示例#9
0
    private bool CheckPatternBelowRight(Move movePiece, ref int foundMatchCount, GameBoard.PLAYERS_ID playerTurnID, int maxConnect)
    {
        bool bFoundPattern = false;

        if (movePiece.PlayerID == playerTurnID)
        {
            foundMatchCount++;
            if (foundMatchCount == maxConnect)
            {
                bFoundPattern = true;
            }
            else if (movePiece.Row < (GameBoard.MAX_ROWS - 1) && movePiece.Column < (GameBoard.MAX_COLUMNS - 1) && foundMatchCount < maxConnect)
            {
                bFoundPattern = CheckPatternBelowRight(gameMoveBoard[movePiece.Row + 1, movePiece.Column + 1], ref foundMatchCount, playerTurnID, maxConnect);
            }
        }

        return(bFoundPattern);
    }
示例#10
0
    private bool CheckPatternAboveLeft(Move movePiece, ref int foundMatchCount, GameBoard.PLAYERS_ID playerTurnID, int maxConnect)
    {
        bool bFoundPattern = false;

        if (movePiece.PlayerID == playerTurnID)
        {
            foundMatchCount++;
            if (foundMatchCount == maxConnect)
            {
                bFoundPattern = true;
            }
            else if (movePiece.Row > 0 && movePiece.Column > 0 && foundMatchCount < maxConnect)
            {
                bFoundPattern = CheckPatternAboveLeft(gameMoveBoard[movePiece.Row - 1, movePiece.Column - 1], ref foundMatchCount, playerTurnID, maxConnect);
            }
        }

        return(bFoundPattern);
    }
示例#11
0
    private bool CheckPatternRight(Move movePiece, ref int foundMatchCount, GameBoard.PLAYERS_ID playerTurnID, int maxConnect)
    {
        bool bFoundPattern = false;

        if (movePiece.PlayerID != GameBoard.PLAYERS_ID.PLAYER_NONE && movePiece.PlayerID == playerTurnID)
        {
            foundMatchCount++;
            if (foundMatchCount == maxConnect)
            {
                bFoundPattern = true;
            }
            else if (movePiece.Column < (GameBoard.MAX_COLUMNS - 1) && foundMatchCount < maxConnect)
            {
                //if haven't found a match check the next squre to the right
                bFoundPattern = CheckPatternRight(gameMoveBoard[movePiece.Row, movePiece.Column + 1], ref foundMatchCount, playerTurnID, maxConnect);
            }
        }

        return(bFoundPattern);
    }
示例#12
0
    private bool CheckPatternAbove(Move movePiece, ref int foundMatchCount, GameBoard.PLAYERS_ID playerTurnID, int maxConnect)
    {
        bool bFoundPattern = false;

        //Piece must be current players value
        if (movePiece.PlayerID != GameBoard.PLAYERS_ID.PLAYER_NONE && movePiece.PlayerID == playerTurnID)
        {
            foundMatchCount++;

            if (foundMatchCount == maxConnect)
            {
                bFoundPattern = true;
            }
            else if (movePiece.Row > 0 && foundMatchCount < maxConnect)
            {
                //if haven't found a match check the next squre above
                bFoundPattern = CheckPatternAbove(gameMoveBoard[movePiece.Row - 1, movePiece.Column], ref foundMatchCount, playerTurnID, maxConnect);
            }
        }

        return(bFoundPattern);
    }
示例#13
0
    public bool CheckForWinner(GameBoard.PLAYERS_ID playerTurnID, int maxConnect)
    {
        bool bWon = false;

        for (int i = 0; i < GameBoard.MAX_ROWS; i++)
        {
            for (int j = 0; j < GameBoard.MAX_COLUMNS; j++)
            {
                Move movePiece = gameMoveBoard[i, j];

                if (playerTurnID == movePiece.PlayerID)
                {
                    int foundMatchCount = 0;

                    //Check above,
                    if (CheckPatternAbove(movePiece, ref foundMatchCount, playerTurnID, maxConnect))
                    {
                        bWon = true;
                        //found a winner
                        break;
                    }

                    foundMatchCount = 0;
                    //Check below,
                    if (CheckPatternBelow(movePiece, ref foundMatchCount, playerTurnID, maxConnect))
                    {
                        //found a winner
                        bWon = true;
                        break;
                    }
                    foundMatchCount = 0;

                    //Check right,
                    if (CheckPatternRight(movePiece, ref foundMatchCount, playerTurnID, maxConnect))
                    {
                        //found a winner
                        bWon = true;
                        break;
                    }

                    foundMatchCount = 0;

                    //Check Left,
                    if (CheckPatternLeft(movePiece, ref foundMatchCount, playerTurnID, maxConnect))
                    {
                        //found a winner
                        bWon = true;
                        break;
                    }

                    foundMatchCount = 0;

                    //check upper right
                    if (CheckPatternAboveRight(movePiece, ref foundMatchCount, playerTurnID, maxConnect))
                    {
                        //found a winner
                        bWon = true;
                        break;
                    }

                    foundMatchCount = 0;

                    //check upper left
                    if (CheckPatternAboveLeft(movePiece, ref foundMatchCount, playerTurnID, maxConnect))
                    {
                        //found a winner
                        bWon = true;
                        break;
                    }

                    foundMatchCount = 0;

                    //check lower right
                    if (CheckPatternBelowRight(movePiece, ref foundMatchCount, playerTurnID, maxConnect))
                    {
                        //found a winner
                        bWon = true;
                        break;
                    }

                    foundMatchCount = 0;

                    //check lower left
                    if (CheckPatternBelowLeft(movePiece, ref foundMatchCount, playerTurnID, maxConnect))
                    {
                        //found a winner
                        bWon = true;
                        break;
                    }
                }
            }

            if (bWon)
            {
                break;
            }
        }

        return(bWon);
    }
示例#14
0
 public void clearOccupied()
 {
     isOccupied       = false;
     occupiedByPlayer = GameBoard.PLAYERS_ID.PLAYER_NONE;
 }
示例#15
0
 public void setOccupied(GameBoard.PLAYERS_ID occupiedBy)
 {
     isOccupied       = true;
     occupiedByPlayer = occupiedBy;
 }
示例#16
0
    //
    //This function implements the Minimax algorithm
    //
    //moveboard board   --represents the current game board as a scratch pad to make moves on (before actually making the move)
    //                  --board also holds the actual player's turn for the game (first player in the tree)
    //Move nextMove     --represents the next move to be made on the game board
    //                  --nexMove also holds a score value for the move
    //evaluatePlayer    --represents the players turn on the tree ( it will change as we traverse each level of the tree )
    //
    //maxDepth          --represents the number of levels to use in the minimax algorithm (how far down the tree do we want to go?)
    //
    //currentDepth      --represents the current level we are evaluating
    //
    //return            --this function returns the best move to make on the board

    static public Move miniMax(moveBoard board, Move nextMove, GameBoard.PLAYERS_ID evaluatePlayer, int maxDepth, int currentDepth)
    {
        //
        //create a bestmove to return
        //
        Move bestMove = new Move();

        //
        //if game is over ( no more moves left/win/loss ) || we have reached our max depth to search || there are no children for this move and it isnt the very first move
        //
        if (board.isGameOver() || currentDepth == maxDepth || ((currentDepth != 0) && nextMove.PossibleMoves.Count == 0))
        {
            //
            //evaluate the board and return the best move for this board, the move data structure also holds a score value for the board
            //
            Move eMove = board.evaluate(board, nextMove);
            eMove.Depth = currentDepth;

            //
            //copy the evaluated move into bestMove
            //
            eMove.Copy(bestMove);
        }
        else
        {
            //
            //if we haven't reached a terminal node traverse the children and call minimax
            //
            Move currentBubbledUpScore = null;

            //
            //Initialize the bestmove object
            //
            nextMove.Copy(bestMove);
            bestMove.Score = INFINITY;

            //
            //Ask: whose turn is it on the tree? Keep track of the max or min score for all the possible moves (children)
            //
            if (board.CurrentPlayer == evaluatePlayer)
            {
                bestMove.Score = -INFINITY;
            }

            //
            //Loop through all the possible moves from nextMove
            //
            foreach (Move move in nextMove.PossibleMoves)
            {
                //
                //make the move ( take the turn ) and then call minimax for the next move
                //makeMove also adds the move to our move game board scratch pad
                //
                board.makeMove(move, evaluatePlayer);
                currentBubbledUpScore = miniMax(board, move, GetNextPlayerID(evaluatePlayer), maxDepth, currentDepth + 1);

                //
                //remove move from our game board (game board scratch pad)
                //
                board.RemoveMoveFromGameMoveBoard(move);

                //
                //if we are the current player, then take the highest score found of all the moves
                //
                if (board.CurrentPlayer == evaluatePlayer)
                {
                    if (currentBubbledUpScore.Score > bestMove.Score)
                    {
                        bestMove.Row    = move.Row;
                        bestMove.Column = move.Column;
                        bestMove.Score  = currentBubbledUpScore.Score;
                        bestMove.Depth  = currentBubbledUpScore.Depth;
                    }
                    else if (currentBubbledUpScore.Score == bestMove.Score)
                    {
                        if (currentBubbledUpScore.Depth < bestMove.Depth)
                        {
                            bestMove.Row    = move.Row;
                            bestMove.Column = move.Column;
                            bestMove.Score  = currentBubbledUpScore.Score;
                            bestMove.Depth  = currentBubbledUpScore.Depth;
                        }
                    }
                }
                else
                {
                    if (currentBubbledUpScore.Score < bestMove.Score)
                    {
                        bestMove.Row    = move.Row;
                        bestMove.Column = move.Column;
                        bestMove.Score  = currentBubbledUpScore.Score;
                        bestMove.Depth  = currentBubbledUpScore.Depth;
                    }
                }

                //
                // the opponent is the current player then take the lowest score of all the moves
                //
            }
        }

        return(bestMove);
    }