Beispiel #1
0
        public IReadOnlyList <Position> PlayerMakesMove(Player player, Position position)
        {
            var activePlayer = GetActivePlayer(player);

            if (!IsPlayersTurn(activePlayer))
            {
                return(null);
            }

            var move         = new Move(position, activePlayer.Disc);
            var piecesToFlip = Board.TryDoMove(move);

            if (piecesToFlip == null || piecesToFlip.Count == 0)
            {
                return(null);
            }

            var nextDisc = ToggleDisc(activePlayer);

            if (nextDisc == null)
            {
                GameState = GameState.Finished;
            }

            _lastValidMove     = move;
            _lastPiecesFlipped = piecesToFlip;
            _discOfNextMove    = nextDisc;

            OnGameStateChanged();
            return(piecesToFlip);
        }
Beispiel #2
0
 public void RemoveDiscForColor(DiscColor disc)
 {
     for (var i = 0; i < EdgeSize; i++)
     {
         for (var j = 0; j < EdgeSize; j++)
         {
             if (_positions[i, j] == disc.Color)
             {
                 _positions[i, j] = 0;
             }
         }
     }
 }
Beispiel #3
0
        internal Game(BlackPlayer player, Board board)
        {
            GameId    = Guid.NewGuid();
            Board     = board;
            GameState = GameState.WaitingForOpponent;

            _blackPlayer       = player;
            _discOfNextMove    = DiscColor.Black;
            _lastPiecesFlipped = null;
            _lastValidMove     = null;

            _observers = new List <Player> {
                player
            };
        }
Beispiel #4
0
        /// <summary>
        /// Checks if there is any valid moves for a given disc
        /// </summary>
        /// <param name="disc">The disc to check for</param>
        /// <returns>True if there exists any valid moves for the disc, false otherwise</returns>
        public bool HasMoves(DiscColor disc)
        {
            for (var i = 0; i < EdgeSize; i++)
            {
                for (var j = 0; j < EdgeSize; j++)
                {
                    if (IsMoveValid(new Move(i, j, disc)))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Beispiel #5
0
        public GameStatus(
            Guid gameId,
            GameState gameState,
            Board board,
            DiscColor discOfNextMove,
            IReadOnlyList <Position> discsFlipped,
            Move lastValidMove,
            BlackPlayer blackPlayer,
            WhitePlayer whitePlayer
            )
        {
            GameId       = gameId;
            CurrentState = board.CurrentState;
            GameState    = gameState;
            Debug.WriteLine(board);
            DiscOfNextMove = discOfNextMove;
            DiscsFlipped   = discsFlipped;
            LastValidMove  = lastValidMove;

            int blackPlayerScore = 0;
            int whitePlayerScore = 0;

            for (var i = 0; i < Board.EdgeSize; i++)
            {
                for (var j = 0; j < Board.EdgeSize; j++)
                {
                    if (CurrentState[i, j] == -1)
                    {
                        blackPlayerScore += 1;
                    }
                    else if (CurrentState[i, j] == 1)
                    {
                        whitePlayerScore += 1;
                    }
                }
            }

            BlackPlayerStatus = new PlayerStatus(blackPlayer.Name, blackPlayerScore);
            WhitePlayerStatus = new PlayerStatus(whitePlayer?.Name ?? "No player", whitePlayerScore);
        }
Beispiel #6
0
 protected ActivePlayer(string name, DiscColor disc) : base(name)
 {
     Disc = disc;
 }