private static void Demo(IChessBoard board) { board.SetPiece(new Tuple <int, int>(0, 7), new Knight(Utils.ColorsEnum.Black, new Tuple <int, int>(0, 7))); board.ShowBoard(); board.MovePiece(new Tuple <int, int>(0, 7), new Tuple <int, int>(2, 6)); board.ShowBoard(); board.MovePiece(new Tuple <int, int>(0, 7), new Tuple <int, int>(2, 3)); board.ShowBoard(); }
public void MovePiece(CellId source, CellId target) { if (!IsValidMove(source, target)) { throw new InvalidOperationException("Invalid move!"); } // Figure out if it is a castle operation. // var sourceCell = _chessboard.GetCell(source); var targetCell = _chessboard.GetCell(target); // We know it is a valid move. We just have to identify if it is a castle // operation. // if (sourceCell.PieceType == EChessPieceType.King && targetCell.PieceType == EChessPieceType.Rook && sourceCell.PieceColor == targetCell.PieceColor) { _chessboard.SwapPiece(source, target); } else { _chessboard.MovePiece(source, target); } nextTurn(); }
private static void ProgramLoop(IChessBoard board) { Console.WriteLine("To move select your chess piece by command 'select x,y' where x is column and y is row."); Console.WriteLine("Next move by command 'move x,y' where x is column and y is row."); Console.WriteLine("Move:"); input = string.Empty; while (!input.Equals("exit")) { input = Console.ReadLine().Trim().ToLower(); if (input.Contains("move")) { if (selected == null) { Console.WriteLine("No chess piece selected."); continue; } if (!board.MovePiece(selected.Position, GetCoordinates(input))) { Console.WriteLine("Invalid move."); } board.ShowBoard(); selected = null; continue; } else if (input.Contains("select")) { try { selected = board.GetSquare(GetCoordinates(input)); if (selected == null) { Console.WriteLine("No chess piece selected."); } } catch (ArgumentException e) { Console.WriteLine(e.Message); } continue; } if (input.Contains("exit")) { break; } Console.WriteLine("Invalid command. Try again."); } }
public void MovePiece(CellId source, CellId target) { if (!IsValidMove(source, target)) { throw new InvalidOperationException("Invalid move!"); } // Figure out if it is a castle operation. // var sourceCell = _chessboard.GetCell(source); var targetCell = _chessboard.GetCell(target); // We know it is a valid move. We just have to identify if it is a castle // operation. // if (sourceCell.PieceType == EChessPieceType.King && targetCell.PieceType == EChessPieceType.Rook && sourceCell.PieceColor == targetCell.PieceColor) { _chessboard.SwapPiece(source, target); } else { if (targetCell.PieceColor != sourceCell.PieceColor) // The player captured a piece { PieceCaptured(this, new PieceCapturedEventArgs(targetCell.PieceType, targetCell.PieceColor)); // Check if the king was captured. // if (targetCell.PieceType == EChessPieceType.King) { GameFinished(this, new GameEndedEventArgs(ActivePlayer)); } } _chessboard.MovePiece(source, target); } nextTurn(); }