Ejemplo n.º 1
0
 public Games(Board b, BoardPresenter bp, MoveValidator mv, MoveExecutor me)
 {
     Board          = b;
     BoardPresenter = bp;
     MoveValidator  = mv;
     MoveExecutor   = me;
     ActivePlayer   = EPlayer.White;
 }
Ejemplo n.º 2
0
 public Games(Board b, BoardPresenter bp, MoveExecutor me)
 {
     Board          = b;
     BoardPresenter = bp;
     //MoveValidator = mv;
     MoveExecutor      = me;
     ActivePlayer      = EPlayer.White;
     CapturedPresenter = new CapturedPresenter();
 }
Ejemplo n.º 3
0
        public void Move(Cell src, Cell dst)
        {
            Console.WriteLine($"move from {src} to {dst}");
            var piece = Board.ContainsKey(src) ? Board[src] : null;

            if (piece == null)
            {
                Console.WriteLine($"no piece at {src}");
            }
            else
            {
                var context = new MoveValidationContext {
                    ActivePlayer = ActivePlayer, Src = src, Board = Board, Piece = piece
                };
                piece.ValidatingMovement(context);
                var moveOutput = context.Find(dst);
                if (moveOutput != null)
                {
                    MoveExecutor.ExecuteMove
                    (
                        new MoveExecutionContext
                    {
                        Type = moveOutput.Type, Src = src, Dst = moveOutput.Cell, Piece = piece, Board = Board, Captured = Captured
                    }
                    );
                    TogglePlayerTurn();

                    if (WhiteKingCaptured())
                    {
                        Winner = EPlayer.Black;
                    }
                    else if (BlackKingCaptured())
                    {
                        Winner = EPlayer.White;
                    }
                }
                else
                {
                    Console.WriteLine($"no valid move from {src} to {dst}, reason: {context.InvalidMessage}");
                }
            }
            BoardPresenter.Print(Board);
            CapturedPresenter.Print(Captured);
        }
Ejemplo n.º 4
0
        public void Move(Cell src, Cell dst)
        {
            Console.WriteLine($"move from {src} to {dst}");
            //get piece
            var piece = Board.ContainsKey(src)
                ? Board[src]
                : null;

            if (piece == null)
            {
                Console.WriteLine($"no piece at {src}");
            }
            else
            {
                //is move valid?
                var validationOutput = MoveValidator.IsMoveValid(ActivePlayer, piece, src, dst, Board);
                if (!validationOutput.Valid)
                {
                    Console.WriteLine($"piece at {src} cannot move to {dst}, {validationOutput.InvalidMessage}");
                }
                else
                {
                    //execute move
                    MoveExecutor.ExecuteMove(validationOutput, piece, src, dst, Board, Captured);
                    //toggle player
                    ActivePlayer = ActivePlayer == EPlayer.White ? EPlayer.Black : EPlayer.White;

                    if (Captured.Count(x => x.Player == EPlayer.White && x is King) == 1)
                    {
                        Winner = EPlayer.Black;
                    }
                    else if (Captured.Count(x => x.Player == EPlayer.White && x is King) == 1)
                    {
                        Winner = EPlayer.White;
                    }
                }
            }
            BoardPresenter.Print(Board);
        }