Example #1
0
 public ReversiGameMenu()
 {
     Application.EnableVisualStyles();
     m_BlackPlayer = new Player();
     m_SettingsForm = new GameSettingsForm();
     m_SettingsForm.ShowDialog();
     m_BoardSize = m_SettingsForm.BoardSize + 2;
     m_WhitePlayer = new Player(m_SettingsForm.PlayerType, eKindOfCell.White);
     m_ReversiGameManager = new ReversiGameManager(m_BlackPlayer, m_WhitePlayer, m_BoardSize);
 }
 public ReversiGameManager(Player i_BlackPlayer, Player i_WhitePlayer, int i_BoardSize)
 {
     m_BlackPlayer = i_BlackPlayer;
     m_WhitePlayer = i_WhitePlayer;
     m_BoardSize = i_BoardSize;
     m_BoardMatrix = new Cell[m_BoardSize, m_BoardSize];
     m_TurnInPlay = k_TurnOfBlacks;
     initializeMatrix();
     initiateGame();
 }
        private void updateBoardAfterAMove(PickedCell i_CellToUpdate, Player i_Player)
        {
            eKindOfCell playersColor = i_Player.DiskColor;
            List<Direction> foundDirections;

            foundDirections = findFoeNeighbourDirection(playersColor, i_CellToUpdate.PickARow, i_CellToUpdate.PickAColumn);
            flipFoeDisks(foundDirections, i_CellToUpdate.PickARow, i_CellToUpdate.PickAColumn, playersColor);
            m_BoardMatrix[i_CellToUpdate.PickARow, i_CellToUpdate.PickAColumn].CellKind = i_Player.DiskColor;

            UpdateAvailableMovesList(m_BlackPlayer);
            UpdateAvailableMovesList(m_WhitePlayer);
        }
        private PickedCell chooseCellRandomaly(Player i_Player)
        {
            PickedCell randomCell;
            int randomIndex;

            randomIndex = s_RandomCell.Next(i_Player.AvailableMoves.Count);
            randomCell = i_Player.AvailableMoves[randomIndex];
            return randomCell;
        }
        private bool checkSkipOfTurnOrEndOfGame(Player i_Player)
        {
            bool isEndOfGame = false;
            string message;
            if (isBoardFull())
            {
                isEndOfGame = true;
            }
            else
            {
                if (!areThereAvailableMovesForAnyPlayer())
                {
                    message = "No moves at all!";
                    ShowMessage(message);
                    isEndOfGame = true;
                }
                else
                {
                    if (!areThereAvailableMovesForPlayer(i_Player))
                    {
                        if (i_Player.PlayerType == ePlayerType.Human)
                        {
                            message = string.Format("There are no available moves for {0}, skip this move", i_Player.PlayerName);
                            ShowMessage(message);
                        }
                        else if (i_Player.PlayerType == ePlayerType.Computer)
                        {
                            message = string.Format("There are no available moves for {0}, it will skip this move", i_Player.PlayerName);
                            ShowMessage(message);
                        }

                        m_TurnInPlay = !m_TurnInPlay;
                    }
                }
            }

            return isEndOfGame;
        }
        private bool areThereAvailableMovesForPlayer(Player i_Player)
        {
            bool areAvailable = false;

            if (i_Player.AvailableMoves.Count != 0)
            {
                areAvailable = true;
            }

            return areAvailable;
        }
        public void UpdateAvailableMovesList(Player i_Player)
        {
            eKindOfCell playersColor = i_Player.DiskColor;
            List<Direction> foundDirection;
            bool isMoveAvailable;
            PickedCell foundCell = new PickedCell(0, 0);

            if (i_Player.AvailableMoves.Count != 0)
            {
                i_Player.ClearAvailableMovesList();
            }

            for (int i = 1; i < m_BoardMatrix.GetLength(1) - 1; i++)
            {
                for (int j = 1; j < m_BoardMatrix.GetLength(1) - 1; j++)
                {
                    if (m_BoardMatrix[i, j].CellKind == eKindOfCell.Empty)
                    {
                        // list of neighbours, potential for move
                        foundDirection = findFoeNeighbourDirection(playersColor, i, j);
                        if (foundDirection.Count != 0)
                        {
                            isMoveAvailable = findIfThereIsAMove(foundDirection, i, j, playersColor);
                            if (isMoveAvailable)
                            {
                                foundCell.PickARow = i;
                                foundCell.PickAColumn = j;
                                i_Player.SetNewAvailableMove(foundCell);
                            }
                        }
                    }
                }
            }
        }
        public bool PlayMoveFromBoard(int i_Row, int i_Column, Player i_Player)
        {
            Player nextPlayer;
            bool isEndOfGame;
            PickedCell pickedCell = new PickedCell(i_Row, i_Column);

            updateBoardAfterAMove(pickedCell, i_Player);
            m_TurnInPlay = !m_TurnInPlay;
            nextPlayer = WhoPlaysNext();

            isEndOfGame = checkSkipOfTurnOrEndOfGame(nextPlayer);

            if (isEndOfGame == false)
            {
                nextPlayer = WhoPlaysNext();
                if (nextPlayer.PlayerType == ePlayerType.Computer)
                {
                    pickedCell = chooseCellRandomaly(nextPlayer);
                    updateBoardAfterAMove(pickedCell, nextPlayer);
                    m_TurnInPlay = !m_TurnInPlay;
                    nextPlayer = WhoPlaysNext();

                    if (isBoardFull() || !areThereAvailableMovesForAnyPlayer())
                    {
                        isEndOfGame = true;
                        PlayersScoreWhenGameEnded();
                    }
                    else if (!areThereAvailableMovesForPlayer(nextPlayer))
                    {
                        m_TurnInPlay = !m_TurnInPlay;
                    }
                }
            }
            else
            {
                PlayersScoreWhenGameEnded();
            }

            return isEndOfGame;
        }