public static IEnumerable<Position> GetRookPositionIterator(Position p) { for (var x = -BoardRows; x <= BoardRows; x++) { if (x == 0) { continue; } var newPosition = new Position(p.X + x, p.Y); if (IsValid(newPosition)) { yield return newPosition; } } for (var y = -BoardColumns; y <= BoardColumns; y++) { if (y == 0) { continue; } var newPosition = new Position(p.X, p.Y + y); if (IsValid(newPosition)) { yield return newPosition; } } }
public Pawn(Position p, Color c) : base(p, c) { if (base.getColor() == Color.Black) { base.setImage(new Bitmap(Board.getInstance().getPath() + "//BlackPawn.gif")); } else { base.setImage(new Bitmap(Board.getInstance().getPath() + "//WhitePawn.gif")); } }
public override string ToString() { String repr = String.Empty; int col; int row; for (row = BOARD_DIMENSION - 1; row >= 0; row--) { repr += " -"; for (col = 0; col < BOARD_DIMENSION; col++) { repr += "----"; } repr += "\n"; repr += String.Format(" {0} |", row + 1); for (col = 0; col < BOARD_DIMENSION; col++) { Position p = new Position(col, row); if (isPieceAtPosition(p)) { repr += String.Format(" {0} |", getPieceAtPosition(p).Avatar); } else { repr += " |"; } } repr += "\n"; } repr += " -"; for (col = 0; col < BOARD_DIMENSION; col++) { repr += "----"; } repr += "\n"; repr += " "; Char colChar = 'a'; for (col = 0; col < BOARD_DIMENSION; col++) { repr += String.Format(" {0} ", colChar); colChar++; } repr += "\n"; return repr; }
/* * Checks whether a given position is valid as a next move. */ public bool isvalidMove(Position newPosition) { Iterator iterator = allowedMovements.getIterator(); while (iterator.MoveNext()) { Position p = iterator.Current as Position; if (p.Equals(newPosition)) { return true; } } return false; }
public static Boolean tryParse(String input, out Position result) { char col; int row; if ((input.Length >= 2) && (input[0] >= 'a' && input[0] <= 'h') && (Int32.TryParse(input.Substring(1, 1), out row))) { col = input[0]; result = new Position(col, row); return true; } else { result = null; return false; } }
static void Main(string[] args) { var position = new Position(5, 5); Console.WriteLine("Starting position: " + position); var knightMoves = Position.GetKnightPositionIterator(position); Console.WriteLine("---- Knight moves ----"); foreach (var p in knightMoves) { Console.WriteLine(p.ToString()); } Console.WriteLine(); var rookMoves = Position.GetRookPositionIterator(position); Console.WriteLine("---- Rook moves ----"); foreach (var p in rookMoves) { Console.WriteLine(p.ToString()); } Console.WriteLine(); var bishopMoves = Position.GetBishopPositionIterator(position); Console.WriteLine("---- Bishop moves ----"); foreach (var p in bishopMoves) { Console.WriteLine(p.ToString()); } Console.WriteLine(); var quennMoves = Position.GetQueenPositionIterator(position); Console.WriteLine("---- Queen moves ----"); foreach (var p in quennMoves) { Console.WriteLine(p.ToString()); } Console.WriteLine(); Console.Read(); }
/** return an iterator that will return all positions * that a knight may reach from a given starting position. */ public static IEnumerable<Position> GetKnightPositionIterator(Position p) { for (var x = -2; x <= 2; x++) { for (var y = -2; y <= 2; y++) { if (Math.Abs(x) + Math.Abs(y) != 3) { continue; } var newPosition = new Position(p.X + x, p.Y + y); if (IsValid(newPosition)) { yield return newPosition; } } } }
/* * This constructor is used in the clone method */ public List(ArrayList l) { list = new ArrayList(); foreach (Piece p in l) { Position pos = new Position(p.getPosition().getRow(), p.getPosition().getColumn()); Color color = p.getColor(); Image image = p.getImage(); Piece piece = null; if (p is Bishop) { piece = new Bishop(pos, color); } else if (p is Knight) { piece = new Knight(pos, color); } else if (p is Pawn) { piece = new Pawn(pos, color); } else if (p is Queen) { piece = new Queen(pos, color); } else if (p is Rook) { piece = new Rook(pos, color); } else if (p is King) { piece = new King(pos, color); } list.Add(piece); } }
public static IEnumerable<Position> GetBishopPositionIterator(Position p) { for (var x = -BoardRows; x <= BoardRows; x++) { if (x == 0) { continue; } var newPosition = new Position(p.X + x, p.Y + x); if (IsValid(newPosition)) { yield return newPosition; } newPosition = new Position(p.X + x, p.Y - x); if (IsValid(newPosition)) { yield return newPosition; } } }
public King(Color color, Position position) : base(color, position) { character = (color == Color.Black) ? '\u265A' : '\u2654'; }
public Bishop(Color color, Position position) : base(color, position) { character = (color == Color.Black) ? '\u265D' : '\u2657'; }
public Square(Position p) { position = p; }
public Piece GetPieceAtPosition(Position position) { return piecesinplay.Where(i => i.Position.Equals(position)) .Select(i => i.Piece) .FirstOrDefault(); }
Boolean isPositionOnBoard(Position position) { if (position.Column < 0 || position.Column >= BOARD_DIMENSION) return false; if (position.Row < 0 || position.Row >= BOARD_DIMENSION) return false; return true; }
public InPlay(Piece piece, Position position) { Piece = piece; Position = position; }
public void PutPieceInPlay(Piece piece, Position position) { piecesinplay.Add(new InPlay(piece,position)); }
void Announce(Piece piece, Position position) { Message = string.Format("{0} to {1}", piece, position); }
public void MovePieceTo(Piece piece,Position to) { var toMove = piecesinplay.Where(p => p.Piece.Equals(piece)) .FirstOrDefault(); if (toMove == null) throw new InvalidOperationException("No such piece in play " + piece); var move = new Move(toMove.Position,to,piece,isfirst); if(move.Valid()) { move.Apply(piecesinplay); Announce(piece, to); } else AnnounceIllegalMove(); isfirst = false; }
public Position.Placement validPosition(IPiece piece, Position position) { if (!isPositionOnBoard(position)) return Position.Placement.ILLEGAL; if (isPieceAtPosition(position)) { IPiece occupier = getPieceAtPosition(position); if (piece.isFriendly(occupier)) return Position.Placement.BLOCKED; else return Position.Placement.CAPTURE; } return Position.Placement.VALID; }
public IPiece getPieceAtPosition(Position p) { return board[p.Column, p.Row]; }
/* * Checks whether there is a piece in a given position */ public Piece consultPiece(Position p, bool white) { Iterator it; if (white) { it = whitePieces.getIterator(); } else { it = blackPieces.getIterator(); } while (it.MoveNext()) { Piece piece = (it.Current as Piece); if (piece.getPosition().Equals(p)) { return piece; } } return null; }
/* * Deletes a piece from a given position. In case of "pawn promotion", do not put into deleted pieces set. */ public Piece killPiece(Position pos, bool promotion) { bool found = false; Piece piece = null; Iterator it = whitePieces.getIterator(); while (it.MoveNext()) { Piece p = (it.Current as Piece); if (p.getPosition().Equals(pos)) { piece = p; if (!promotion) { mainForm.getWhiteImages().Images.Add(p.getImage()); } whitePieces.remove(p); found = true; break; } } if (!found) { it = blackPieces.getIterator(); while (it.MoveNext()) { Piece p = (it.Current as Piece); if (p.getPosition().Equals(pos)) { piece = p; if (!promotion) { mainForm.getBlackImages().Images.Add(p.getImage()); } blackPieces.remove(p); break; } } } return piece; }
/* * This method updates the set of allowed movements. Implememts an abstract method from Piece.cs */ public override void updateAllowedMovements() { this.allowedMovements.clear(); int row = position.getRow(); int column = position.getColumn(); int i, j; Position currentPos; if (this.color == Color.Black) //Black pawn { //Down i = row + 1; j = column; if (i <= 7) { currentPos = new Position(i, j); if (Board.getInstance().consultPiece(currentPos, true) == null && Board.getInstance().consultPiece(currentPos, false) == null) { this.allowedMovements.insert(currentPos); } } //2 positions down for first movement if (row == 1 && Board.getInstance().consultPiece(new Position(2, column), true) == null && Board.getInstance().consultPiece(new Position(2, column), false) == null && Board.getInstance().consultPiece(new Position(3, column), true) == null) { checkSquare(new Position(row + 2, column)); } //Down-right diagonal, only to atack i = row + 1; j = column + 1; if (i < Board.NUM_ROWS && j < Board.NUM_ROWS) { currentPos = new Position(i, j); if (Board.getInstance().consultPiece(currentPos, true) != null) //Opponent's piece { this.allowedMovements.insert(currentPos); } } //Down-left diagonal, only to atack i = row + 1; j = column - 1; if (i < Board.NUM_ROWS && j >= 0) { currentPos = new Position(i, j); if (Board.getInstance().consultPiece(currentPos, true) != null) //Opponent's piece { this.allowedMovements.insert(currentPos); } } } else { // White pawn //Up i = row - 1; j = column; if (i >= 0) { currentPos = new Position(i, j); if (Board.getInstance().consultPiece(currentPos, true) == null && Board.getInstance().consultPiece(currentPos, false) == null) { this.allowedMovements.insert(currentPos); } } //Two positions up for first movement if (row == 6 && Board.getInstance().consultPiece(new Position(5, column), true) == null && Board.getInstance().consultPiece(new Position(5, column), false) == null && Board.getInstance().consultPiece(new Position(4, column), false) == null) { checkSquare(new Position(row - 2, column)); } //Up-right diagonal, only to attack i = row - 1; j = column + 1; if (i >= 0 && j < Board.NUM_ROWS) { currentPos = new Position(i, j); if (Board.getInstance().consultPiece(currentPos, false) != null) //Opponent's piece { this.allowedMovements.insert(currentPos); } } //Up-left diagonal, only to attack i = row - 1; j = column - 1; if (i >= 0 && j >= 0) { currentPos = new Position(i, j); if (Board.getInstance().consultPiece(currentPos, false) != null) // Opponent's piece { this.allowedMovements.insert(currentPos); } } } }
public bool IsValid(Position from, Position to) { return (from.y - to.y) < yoffset; }
public static bool IsValid(Position p) { return 0 <= p.X && p.X <= BoardRows && 0 <= p.Y && p.Y <= BoardColumns; }
public bool Equals(Position other) { return other.x == x && other.y == y; }
public void MoveTo(Position position) { Position = position; }
Boolean isPieceAtPosition(Position p) { return (getPieceAtPosition(p) != null); }
public static IEnumerable<Position> GetQueenPositionIterator(Position p) { return GetRookPositionIterator(p).Concat(GetBishopPositionIterator(p)); }
public Move(Position from,Position to, Piece piece,bool isfirst) { this.from = from; this.to = to; this.piece = piece; this.isfirst = isfirst; }