Example #1
0
        private IEnumerable <Move> GetAvailableMovesForColor(PawnColor color)
        {
            List <Move> result      = new List <Move>();
            bool        hasAnyJumps = false;

            foreach (var field in Fields)
            {
                if (field.Pawn?.Color != color)
                {
                    continue;
                }
                var directions = GetPawnDirections(field.Index);
                var jumps      = GetJumpsForIndex(field.Index, directions);
                result.AddRange(jumps);
                if (jumps.Any())
                {
                    hasAnyJumps = true;
                    result.RemoveAll(x => !x.IsJump);
                }
                if (!hasAnyJumps)
                {
                    var simpleMoves = GetSimpleMovesForIndex(field.Index, directions);
                    result.AddRange(simpleMoves);
                }
            }
            return(result);
        }
Example #2
0
 public static Player Create(Guid id, string name, PawnColor color)
 {
     if (string.IsNullOrEmpty(name))
     {
         throw new ArgumentException("New name can not be null", nameof(name));
     }
     return(new Player
     {
         Id = id,
         Name = name,
         Color = color
     });
 }
Example #3
0
        public static Board FromStanpshot(BoardDbModel dbModel)
        {
            var board = new Board
            {
                Size = dbModel.Size,
                isLongMoveAllowed = dbModel.LongMoveAllowed,
                CurrentTurn       = PawnColor.FromName(dbModel.CurrentTurn)
            };

            board.InitializeEmptyFields();
            foreach (PawnDbModel pawn in dbModel.Pawns)
            {
                var color = PawnColor.FromName(pawn.Color);
                board.Fields[pawn.Index].PutPawn(new Pawn(color, pawn.IsKing));
            }
            return(board);
        }
Example #4
0
        public void AddPlayer(Guid playerId, string playerName)
        {
            if (_hasStarted || _players.Count > 1)
            {
                throw new InvalidGameOperation($"Cannot join a game a running game");
            }

            PawnColor randomColor = GetRandomPlayerColor();

            if (_players.ContainsKey(playerId))
            {
                return;
            }
            Player newPlayer = Player.Create(playerId, playerName, randomColor);

            _players.Add(playerId, newPlayer);
            if (BlackPlayer != null && WhitePlayer != null)
            {
                _hasStarted = true;
            }
        }
Example #5
0
 public int GetNumberOfPawns(PawnColor color)
 {
     return(Fields.Where(f => !Field.IsEmpty(f) && f.Pawn.Color.Equals(color)).Count());
 }
Example #6
0
 private void SwitchTurns()
 {
     CurrentTurn         = CurrentTurn.Equals(PawnColor.White) ? PawnColor.Black : PawnColor.White;
     _multipleMovesIndex = null;
 }
Example #7
0
 public Pawn(PawnColor color, bool isKing = false)
 {
     Color  = color;
     IsKing = isKing;
 }