Esempio n. 1
0
 public void AddChessChange(ChessChange chessChange)
 {
     this.Availability = false;
     try
     {
         PerformChessChange(chessChange);
         if (chessChange.NewChess == null)
         {
             // remove key-value
             if (this.LocationChess.ContainsKey(chessChange.Position))
             {
                 this.LocationChess.Remove(chessChange.Position);
             }
         }
         else
         {
             // change value
             this.LocationChess[chessChange.Position] = chessChange.NewChess;
         }
         this.NewChessLocation = chessChange.Position;
     }
     finally
     {
         this.Availability = true;
     }
 }
Esempio n. 2
0
 public void AddChessChange(ChessChange chessChange)
 {
     lock (this)
     {
         SaveHistory();
         try
         {
             this.Board.AddChessChange(chessChange);
             // we are in the new branch of history, the old history should be removed
             this.BoardRedo.Clear();
         }
         catch (InvalidChessOperation)
         {
             UndoHistory();
         }
     }
 }
        private void PerformChessChange(ChessChange instruction)
        {
            // chess if there has a chess
            if (instruction.NewChess == null)
            {
                throw new InvalidChessOperation();
            }
            // check who's turn
            if (instruction.NewChess?.Side != this.WhosTurn)
            {
                throw new InvalidChessOperation();
            }
            // check chess type
            ChessType[] validChessType = new ChessType[]
            {
                ChessType.Bishop, ChessType.Knight, ChessType.Queen, ChessType.Rook
            };
            if (!validChessType.Contains(instruction.NewChess.Type))
            {
                throw new InvalidChessOperation();
            }

            // promotion
            if (instruction.Position.Y == 0 && instruction.NewChess?.Side == PlayerSide.White ||
                instruction.Position.Y == 7 && instruction.NewChess?.Side == PlayerSide.Black)
            {
            }
            else
            {
                throw new InvalidChessOperation();
            }

            // switch turn
            this.WhosTurn = GetOppositeSide(instruction.NewChess.Side);

            this.IsDuringPromotion = false;
            // ~~mark check~~
        }