예제 #1
0
        // Violating SRP by Checking for game over and setting up winner in the same method
        // but it makes sense to keep them together
        public bool IsGameOver()
        {
            bool hasAPlayerWon;
            var  result = _gameBoard.IsGameOver(out hasAPlayerWon);

            if (hasAPlayerWon)
            {
                Winner = CurrentPlayer;
            }

            return(result);
        }
예제 #2
0
        //Play a position
        public void Play(PlayPosition playPos)
        {
            _nosMoves++;

            _lastTurn = _playTurn;

            if (_playTurn == PlayerNumber.FirstPlayer)//First Player turn
            {
                _gameBoard.SetAt(playPos.X, playPos.Y, (char)PlayChars.First);
                _playTurn = PlayerNumber.SecondPlayer;
            }
            else if (_playTurn == PlayerNumber.SecondPlayer)//Second player turn
            {
                _gameBoard.SetAt(playPos.X, playPos.Y, (char)PlayChars.Second);
                _playTurn = PlayerNumber.FirstPlayer;
            }

            //Check game is over only after 5 play moves.
            if (_nosMoves >= 5)
            {
                if (_gameBoard.IsGameOver())
                {
                    _gameOver = true;
                }
            }

            //If game is over then update WhoWon and score
            if (_nosMoves == 9 || _gameOver)
            {
                _gameOver = true;

                if (_gameBoard.whoWon == (char)PlayChars.First)
                {
                    _wonPlayer = PlayerNumber.FirstPlayer;
                }
                else if (_gameBoard.whoWon == (char)PlayChars.Second)
                {
                    _wonPlayer = PlayerNumber.SecondPlayer;
                }

                if (_wonPlayer > 0)
                {
                    _players[((int)_wonPlayer) - 1].Score++;
                }
            }
        }
        private int ScoreSingleBoard(IGameBoard gb)
        {
            int score = gb.Score * scoreMult;
            score += gb.MaxValue * maxValMult;
            score += gb.GetEmptySquareCount() * emptySquareMult;

            if (gb.IsGameOver())
                score += gameOverPen;

            return score;
        }