Пример #1
0
        public GameLogic(Board i_GameBoard, Player.eColor i_PlayerTurn)
        {
            Board copiedBoard = i_GameBoard;

            m_GameBoard  = copiedBoard;
            m_PlayerTurn = i_PlayerTurn;
        }
Пример #2
0
        public bool ManageTurnChanging()
        {
            // this method is managing the players turn changing and return true if the turn has been changed.
            bool isTurnChanged = true;

            if (m_PlayerTurn == Player.eColor.Red && m_YellowPlayerOptions.Count > 0)
            {
                m_PlayerTurn = Player.eColor.Yellow;
            }
            else if (m_PlayerTurn == Player.eColor.Yellow && m_RedPlayerOptions.Count > 0)
            {
                m_PlayerTurn = Player.eColor.Red;
            }
            else if (IsGameOver())
            {
                m_PlayerTurn = m_PlayerTurn == Player.eColor.Red ? Player.eColor.Yellow : Player.eColor.Red;
            }
            else if ((m_PlayerTurn == Player.eColor.Red && m_YellowPlayerOptions.Count == 0) ||
                     (m_PlayerTurn == Player.eColor.Yellow && m_RedPlayerOptions.Count == 0))
            {
                isTurnChanged = false;
            }

            return(isTurnChanged);
        }
Пример #3
0
        public bool MoveIsLegal(int i_Row, int i_Column, Player.eColor i_PlayerColor, out Move o_Move)
        {
            o_Move = null;
            bool v_MoveIsLegal = false;
            Cell cellToCheck   = new Cell(i_Row, i_Column);

            Directions[] allDirections = AllDirections.GetAllDirections();

            if (cellIsEmpty(cellToCheck))
            {
                o_Move = new Move(i_Row, i_Column);
                foreach (Directions direction in allDirections)
                {
                    Cell startCell = new Cell(i_Row, i_Column);
                    Cell endCell;
                    if (checkDirection(startCell, i_PlayerColor, direction, out endCell))
                    {
                        o_Move.AddValidDirection(direction, endCell);
                    }
                }

                v_MoveIsLegal = o_Move.HasValidMove();
            }

            return(v_MoveIsLegal);
        }
Пример #4
0
        public void updatePlayersOptions()
        {
            // this method is updating the Lists of the players options
            List <Cell> cellList = new List <Cell>();

            Player.eColor lastPlayerTurn;
            bool          isCellAnOption, shouldMethodAddCellsToUpdateList;

            m_YellowPlayerOptions.Clear();
            m_RedPlayerOptions.Clear();
            lastPlayerTurn = m_PlayerTurn;
            shouldMethodAddCellsToUpdateList = false;
            foreach (Cell cellIteator in m_GameBoard.Matrix)
            {
                if (cellIteator.Sign == Cell.k_Empty)
                {
                    m_PlayerTurn   = Player.eColor.Yellow;
                    isCellAnOption = isPlayerMoveBlockingEnemy(cellIteator, ref cellList, shouldMethodAddCellsToUpdateList);
                    if (isCellAnOption)
                    {
                        m_YellowPlayerOptions.Add(cellIteator);
                    }

                    m_PlayerTurn   = Player.eColor.Red;
                    isCellAnOption = isPlayerMoveBlockingEnemy(cellIteator, ref cellList, shouldMethodAddCellsToUpdateList);
                    if (isCellAnOption)
                    {
                        m_RedPlayerOptions.Add(cellIteator);
                    }
                }
            }

            // restore the turn of the last player
            m_PlayerTurn = lastPlayerTurn;
        }
Пример #5
0
        private bool checkDirection(Cell i_StartCell, Player.eColor i_CurrentPlayerColor,
                                    Directions i_Direction, out Cell o_EndCell)
        {
            Player.eColor oponentColor = GetOponentColor(i_CurrentPlayerColor);
            i_StartCell.MoveToDirection(i_Direction);
            bool cellContainsCurrentPlayerColor = false;
            bool cellContainsOponentColor       = false;
            bool cellOutOfBoundary = CellOutOfBoundary(i_StartCell);

            if (!cellOutOfBoundary)
            {
                cellContainsOponentColor = CellContainsColor(i_StartCell, oponentColor);
            }

            while (!cellOutOfBoundary && cellContainsOponentColor)
            {
                i_StartCell.MoveToDirection(i_Direction);
                cellOutOfBoundary = CellOutOfBoundary(i_StartCell);
                if (!cellOutOfBoundary)
                {
                    cellContainsOponentColor       = CellContainsColor(i_StartCell, oponentColor);
                    cellContainsCurrentPlayerColor = CellContainsColor(i_StartCell, i_CurrentPlayerColor);
                }
            }
            o_EndCell = new Cell(i_StartCell.Row, i_StartCell.Col);

            return(!cellOutOfBoundary && cellContainsCurrentPlayerColor);
        }
Пример #6
0
 public void Initialize()
 {
     // this method is initializing the player options, scores and board.
     m_GameBoard.Initialize();
     initializePlayersOptions();
     initializePlayersCurrentRoundScores();
     m_PlayerTurn = Player.eColor.Yellow;
 }
Пример #7
0
        private int playerIndex(Player.eColor i_PlayerColor)
        {
            // this method recieves a player color and return the index by int
            int index;

            index = (char)(i_PlayerColor) - '0';

            return(index);
        }
Пример #8
0
        private bool isCellAnEnemy(Cell i_CellIterator, Player.eColor i_CurrentPlayerTurn)
        {
            // this method return true if the sign of the input cell is different from i_CurrentPlayerTurn
            bool isCellEnemy;

            isCellEnemy = i_CellIterator.Sign != (char)i_CurrentPlayerTurn && i_CellIterator.Sign != Cell.k_Empty;

            return(isCellEnemy);
        }
Пример #9
0
        public void UpdateBoard(List <Cell> i_CellsToUpdate, Player.eColor i_PlayingPlayer)
        {
            // this method recieves a list of cells and a player color and put the correct sign in those cells.
            foreach (Cell currentCell in i_CellsToUpdate)
            {
                m_Board[currentCell.Row, currentCell.Column].Sign = (char)i_PlayingPlayer;
            }

            i_CellsToUpdate.Clear();
        }
Пример #10
0
        private void enableAllLegalPlayerPictureBoxs(Player.eColor i_Turn)
        {
            // this method is enabling all the picture boxes which represents the possible cells for the current turn
            List <Cell> currentPlayerOptionList;

            currentPlayerOptionList = i_Turn == Player.eColor.Yellow ? m_GameLogic.YellowPlayerOptions : m_GameLogic.RedPlayerOptions;
            foreach (Cell cell in currentPlayerOptionList)
            {
                enableRepresentingPictureBox(cell);
            }
        }
Пример #11
0
        private static int heuristic(Board i_Board, Player.eColor i_playerTurn)
        {
            // heuristic method for Minimax algorithm
            int  heuristicResult;
            int  differencePCHuman;
            char playerTurnSign;

            heuristicResult   = 0;
            differencePCHuman = differencePCScoreHumanScore(i_Board);
            playerTurnSign    = i_playerTurn == Player.eColor.Red ? (char)Player.eColor.Red : (char)Player.eColor.Yellow;
            heuristicResult  += getCornersHeuristic(i_Board, playerTurnSign);
            return(heuristicResult + differencePCHuman);
        }
Пример #12
0
        public void UpdateBoardCauseLegalMove(Move i_Move, Player.eColor i_Color)
        {
            List <Directions> MoveValidDirections = i_Move.ValidDirections;
            List <Cell>       MoveValidEndCells   = i_Move.ValidEndCells;

            Cell startCell;

            for (int i = 0; i < MoveValidDirections.Count; i++)
            {
                startCell = new Cell(i_Move.Row, i_Move.Col);
                Directions validDirection       = MoveValidDirections[i];
                Cell       finalCellInDirection = MoveValidEndCells[i];
                fillsCells(startCell, validDirection, finalCellInDirection, i_Color);
            }
        }
Пример #13
0
        private bool isPlayerOptionEmpty(Player.eColor i_PlayerColor)
        {
            // this method recieve a player color and return true if his options list is empty.
            bool isOptionListEmpty;

            if (i_PlayerColor == Player.eColor.Red)
            {
                isOptionListEmpty = m_RedPlayerOptions.Count == 0;
            }
            else
            {
                // if not red player - than its a yellow player.
                isOptionListEmpty = m_YellowPlayerOptions.Count == 0;
            }

            return(isOptionListEmpty);
        }
Пример #14
0
        public int AmountOfColorInBoard(Player.eColor i_Color)
        {
            char colorToCheck = (char)i_Color;
            int  counterColor = 0;

            for (int i = 0; i < r_Height; i++)
            {
                for (int j = 0; j < r_Width; j++)
                {
                    if (m_Board[i, j] == colorToCheck)
                    {
                        counterColor++;
                    }
                }
            }

            return(counterColor);
        }
Пример #15
0
        private static bool isGameOver(Board i_GameBoardState, Player.eColor i_MaximizingPlayer)
        {
            // this method passing all cell in the list and check if their is an option for maximizingPlayer
            GameLogic   tempGameManager = new GameLogic(i_GameBoardState, i_MaximizingPlayer);
            List <Cell> cellLists = new List <Cell>();
            bool        addToCellsList, isCellAnOption, currentGameNotOver;

            addToCellsList     = false;
            currentGameNotOver = false;
            foreach (Cell cellIteator in i_GameBoardState.Matrix)
            {
                isCellAnOption = tempGameManager.isPlayerMoveBlockingEnemy(cellIteator, ref cellLists, addToCellsList);
                if (isCellAnOption)
                {
                    return(currentGameNotOver);
                }
            }

            currentGameNotOver = true;

            return(currentGameNotOver);
        }
Пример #16
0
 public PcPlayer(Player.eColor i_PlayerColor)
 {
     // PcPlayer c'tor
     m_PlayerColor = i_PlayerColor;
 }
Пример #17
0
 public HumanPlayer(Player.eColor i_PlayerColor)
 {
     // Human player c'tor
     m_PlayerColor = i_PlayerColor;
 }
Пример #18
0
 private void fillsCells(Cell i_FromCell, Directions i_Direction, Cell i_ToCell, Player.eColor i_Color)
 {
     while (!i_FromCell.AtSameLocationLikeCell(i_ToCell))
     {
         PlacePiece(i_FromCell.Row, i_FromCell.Col, i_Color);
         i_FromCell.MoveToDirection(i_Direction);
     }
 }
Пример #19
0
 public void PlacePiece(int i_Height, int i_Width, Player.eColor i_Color)
 {
     m_Board[i_Height, i_Width] = (char)i_Color;
 }
Пример #20
0
 Player.eColor GetOponentColor(Player.eColor i_Color)
 {
     return(i_Color == Player.eColor.White? Player.eColor.Black : Player.eColor.White);
 }
Пример #21
0
        bool CellContainsColor(Cell i_CellToCheck, Player.eColor i_Color)
        {
            char cellValueInBoard = GetPiece(i_CellToCheck.Row, i_CellToCheck.Col);

            return(cellValueInBoard == (char)i_Color);
        }
Пример #22
0
        public static int Minimax(Board i_GameBoardState, int i_Depth, Player.eColor i_MaximizingPlayer, ref Cell io_Cell, ref List <KeyValuePair <int, List <Cell> > > io_ListOfKeyValue)
        {
            // this method return a List of pairs < heuristic score , list of cells that lead to this score >
            // using Minimax algorithm, it return that list of pair I described by ref
            GameLogic   gameMangaerAI    = new GameLogic(i_GameBoardState, i_MaximizingPlayer);
            List <Cell> playerOptionList = new List <Cell>();
            List <Cell> playerMovesList  = new List <Cell>();
            KeyValuePair <int, List <Cell> > scoreAndCellsListPair;
            Board copiedBoard;
            int   eval, minEval, maxEval;

            if (i_Depth == 0 || isGameOver(i_GameBoardState, i_MaximizingPlayer))
            {
                return(heuristic(i_GameBoardState, i_MaximizingPlayer));
            }
            else
            {
                gameMangaerAI.updatePlayersOptions();
                if (i_MaximizingPlayer == Player.eColor.Red)
                {
                    // this is PC's turn - Choose max value
                    maxEval = int.MinValue;
                    foreach (Cell cellIteator in gameMangaerAI.RedPlayerOptions)
                    {
                        copiedBoard = gameMangaerAI.GameBoard.Clone();
                        gameMangaerAI.isPlayerMoveBlockingEnemy(cellIteator, ref playerOptionList);
                        copiedBoard.UpdateBoard(playerOptionList, i_MaximizingPlayer);
                        eval = Minimax(copiedBoard, i_Depth - 1, Player.eColor.Yellow, ref io_Cell, ref io_ListOfKeyValue);
                        if (eval > maxEval)
                        {
                            playerMovesList.Add(io_Cell);
                            scoreAndCellsListPair = new KeyValuePair <int, List <Cell> >(eval, playerMovesList);
                            io_ListOfKeyValue.Add(scoreAndCellsListPair);
                            io_Cell.Row    = cellIteator.Row;
                            io_Cell.Column = cellIteator.Column;
                            maxEval        = eval;
                        }
                    }

                    return(maxEval);
                }
                else
                {
                    // this is Human player - Choose min value
                    minEval = int.MaxValue;
                    foreach (Cell cellIteator in gameMangaerAI.YellowPlayerOptions)
                    {
                        copiedBoard = gameMangaerAI.GameBoard.Clone();
                        gameMangaerAI.isPlayerMoveBlockingEnemy(cellIteator, ref playerOptionList);
                        copiedBoard.UpdateBoard(playerOptionList, i_MaximizingPlayer);
                        eval = Minimax(copiedBoard, i_Depth - 1, Player.eColor.Red, ref io_Cell, ref io_ListOfKeyValue);
                        if (eval < minEval)
                        {
                            playerMovesList.Add(io_Cell);
                            scoreAndCellsListPair = new KeyValuePair <int, List <Cell> >(eval, playerMovesList);
                            io_ListOfKeyValue.Add(scoreAndCellsListPair);
                            minEval = eval;
                        }
                    }

                    return(minEval);
                }
            }
        }