private List<Move> getPieceMoves(Square square) { List<Move> list = new List<Move>(); Piece piece = getPiece(square), tmp; Square stmp; if (piece == null) return list; switch (piece.PieceType) { case Piece.Type.b: #region BishopMoves //Do up, down, left, right stmp = new Square(square.x, square.y + 1); tmp = getPiece(stmp); if (tmp == null && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x, square.y - 1); tmp = getPiece(stmp); if (tmp == null && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x + 1, square.y); tmp = getPiece(stmp); if (tmp == null && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 1, square.y); tmp = getPiece(stmp); if (tmp == null && isValidSquare(stmp)) list.Add(new Move(stmp, square)); list.AddRange(this.getDiagonalMoves(square)); #endregion break; case Piece.Type.k: #region KingMoves stmp = new Square(square.x, square.y + 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x, square.y - 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x + 1, square.y); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 1, square.y); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); //Diags stmp = new Square(square.x + 1, square.y + 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x + 1, square.y - 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 1, square.y + 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 1, square.y - 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); #endregion break; case Piece.Type.n: #region KnightMoves stmp = new Square(square.x + 1, square.y + 2); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x + 1, square.y - 2); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 1, square.y + 2); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 1, square.y - 2); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x + 2, square.y + 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x + 2, square.y - 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 2, square.y + 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 2, square.y - 1); tmp = getPiece(stmp); if ((tmp == null || tmp.PieceColor != piece.PieceColor) && isValidSquare(stmp)) list.Add(new Move(stmp, square)); #endregion break; case Piece.Type.p: #region PawnMoves if (piece.PieceColor == Piece.Color.White) { stmp = new Square(square.x - 1, square.y); tmp = getPiece(stmp); if (tmp == null && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 1, square.y - 1); tmp = getPiece(stmp); if (tmp != null && tmp.PieceColor != piece.PieceColor) list.Add(new Move(stmp, square)); stmp = new Square(square.x - 1, square.y + 1); tmp = getPiece(stmp); if (tmp != null && tmp.PieceColor != piece.PieceColor) list.Add(new Move(stmp, square)); } else { stmp = new Square(square.x + 1, square.y); tmp = getPiece(stmp); if (tmp == null && isValidSquare(stmp)) list.Add(new Move(stmp, square)); stmp = new Square(square.x + 1, square.y - 1); tmp = getPiece(stmp); if (tmp != null && tmp.PieceColor != piece.PieceColor) list.Add(new Move(stmp, square)); stmp = new Square(square.x + 1, square.y + 1); tmp = getPiece(stmp); if (tmp != null && tmp.PieceColor != piece.PieceColor) list.Add(new Move(stmp, square)); } #endregion break; case Piece.Type.q: list.AddRange(this.getDiagonalMoves(square)); list.AddRange(this.getOrthMoves(square)); break; case Piece.Type.r: list.AddRange(this.getOrthMoves(square)); break; } return list; }
private bool isValidSquare(Square square) { return (square.x >= 0 && square.x < HEIGHT && square.y >= 0 && square.y < WIDTH); }
private List<Move> getOrthMoves(Square square) { List<Move> list = new List<Move>(); Square stmp; Piece tmp, piece = getPiece(square); if (piece == null) return list; Boolean done = false; stmp = square; while (!done) { stmp = new Square(stmp.x + 1, stmp.y); if (isValidSquare(stmp)) { tmp = getPiece(stmp); if (tmp == null) { list.Add(new Move(stmp, square)); continue; } if (tmp.PieceColor != piece.PieceColor) { list.Add(new Move(stmp, square)); done = true; } else { done = true; } } else { done = true; } } done = false; stmp = square; while (!done) { stmp = new Square(stmp.x - 1, stmp.y); if (isValidSquare(stmp)) { tmp = getPiece(stmp); if (tmp == null) { list.Add(new Move(stmp, square)); continue; } if (tmp.PieceColor != piece.PieceColor) { list.Add(new Move(stmp, square)); done = true; } else { done = true; } } else { done = true; } } done = false; stmp = square; while (!done) { stmp = new Square(stmp.x, stmp.y + 1); if (isValidSquare(stmp)) { tmp = getPiece(stmp); if (tmp == null) { list.Add(new Move(stmp, square)); continue; } if (tmp.PieceColor != piece.PieceColor) { list.Add(new Move(stmp, square)); done = true; } else { done = true; } } else { done = true; } } done = false; stmp = square; while (!done) { stmp = new Square(stmp.x, stmp.y - 1); if (isValidSquare(stmp)) { tmp = getPiece(stmp); if (tmp == null) { list.Add(new Move(stmp, square)); continue; } if (tmp.PieceColor != piece.PieceColor) { list.Add(new Move(stmp, square)); done = true; } else { done = true; } } else { done = true; } } return list; }
public Boolean setPiece(Square square, Piece piece) { if (isValidSquare(square)) { this.board[square.x, square.y] = piece; return true; } return false; }
public Piece getPiece(Square square) { if (isValidSquare(square)) return this.board[square.x, square.y]; return null; }
public void fromString(String m) { string[] parts = m.Split("-".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); this.From = new Square(parts[0]); this.To = new Square(parts[1]); }
public Move(Square to, Square from) { this.To = to; this.From = from; }