ASCII representation of chess board
I am writing a chess engine in Java, and I would like to print the board with ASCII characters (ie: # for an empty square, K for a king, Q for a queen etc ..). The represention should differentiate black and white pieces.
I am looking for method which will have for input a list (or map or array) of Pieces and which will output a string representing the board.
I have already a Board object, containting a list of Pieces. Piece is an abstract class and has a position (x,y) and a color (black or white). Then King, Queen, Knight are classes implementing the Piece class.
Thanks
I guess you represent each individual board location with an Object
. So you can use a mapper to convert your Object layout (ie the board) to a character matrix.
Map<Object, Character> mapper = new HashMap<Object, Character>();
mapper.put(new Queen(), 'Q');
...
While printing the board, you should simply iterate over the board squares and print their corresponding character from the mapper populated above.
I think you can use a Abstract- Class (or Interface) for it:
public abstract class ChessObject {
private ChessColor color;
public abstract String getCharRepresentation();
public ChessObject(ChessColor color){
this.color = color;
}
public ChessColor getColor() {
return color;
}
public void setColor(ChessColor color) {
this.color = color;
}
}
Implementation:
public class King extends ChessObject{
public King(ChessColor color){
super(color);
}
@Override
public String getCharRepresentation() {
return "K";
}
}
Enum:
public enum ChessColor {
WHITE, BLACK;
}
Main- Example:
public static void main(String[] args) {
List<ChessObject> objects = new ArrayList<ChessObject>();
objects.add(new King());
for(ChessObject obj : objects){
System.out.println(obj.getCharRepresentation());
}
}
First of all: If your intention is to write a chess engine, then you should throughoutly read the Chess Programming Wiki.
If you then still want to give it a try, you should be aware of the fact that the usual best practices of object oriented programming may not necessarily be applied for chess programming. Many aspects of OOP aim at goals that simply do not exist for chess. For example, extensibility. You'll never have other pieces, except for the ones that exist right from the beginning. For an efficient chess engine, you'll probably not use an explicit representation of the pieces in form of classes at all. And for the board, you'd rather not use a List<Piece>
, but instead, a special array (see Board Representation for lots of (lots of!) details).
However, based on your current description, there seems to be no way to distinguish between the pieces, except for checking their class via instanceof
checks. Of course, this is not so nice, but unless you extend the Piece
class with something like a getType()
method (rendering the whole inheritance useless), there's hardly any other option.
I sketched this approach below. The notation is roughly based on the algebraic move notation: The letters
K = Knight
Q = Queen
B = Bishop
N = Knight
R = Rook
P = Pawn
are commonly used to denote the types of pieces. In order to distinguish between black and white pieces, you simply can use an uppercase letter for the white pieces and a lowercase letter for the black ones.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ChessBoardString
{
public static void main(String[] args)
{
Board board = new Board();
String s = createString(board);
System.out.println(s);
}
private static String createString(Board board)
{
char empty = '.';
char chars[] = new char[8*8+8];
Arrays.fill(chars, empty);
for (int y=0; y<8; y++)
{
chars[y*9+8] = 'n';
}
List<Piece> pieces = board.getPieces();
for (Piece piece : pieces)
{
int x = piece.getX();
int y = piece.getY();
char c = charFor(piece);
chars[x+y*9] = c;
}
String s = new String(chars);
return s;
}
private static char charFor(Piece p)
{
char c = ' ';
if (p instanceof King)
{
c = 'k';
}
else if (p instanceof Queen)
{
c = 'q';
}
else if (p instanceof Bishop)
{
c = 'b';
}
else if (p instanceof Knight)
{
c = 'n';
}
else if (p instanceof Rook)
{
c = 'r';
}
else if (p instanceof Pawn)
{
c = 'p';
}
if (p.getColor() == Color.WHITE)
{
c = Character.toUpperCase(c);
}
return c;
}
}
class Board
{
List<Piece> getPieces()
{
List<Piece> pieces = new ArrayList<Piece>();
pieces.add(new King(Color.WHITE,3,4));
pieces.add(new King(Color.BLACK,5,6));
pieces.add(new Queen(Color.WHITE,7,2));
pieces.add(new Queen(Color.BLACK,2,0));
pieces.add(new Bishop(Color.WHITE,1,2));
pieces.add(new Bishop(Color.BLACK,5,4));
pieces.add(new Knight(Color.WHITE,5,1));
pieces.add(new Knight(Color.BLACK,0,7));
pieces.add(new Rook(Color.WHITE,2,2));
pieces.add(new Rook(Color.BLACK,1,4));
pieces.add(new Pawn(Color.WHITE,6,1));
pieces.add(new Pawn(Color.BLACK,2,3));
return pieces;
}
}
enum Color
{
BLACK,
WHITE
}
abstract class Piece
{
private Color color;
private int x;
private int y;
Piece(Color color, int x, int y)
{
this.color = color;
this.x = x;
this.y = y;
}
Color getColor()
{
return color;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
}
class King extends Piece
{
King(Color color, int x, int y)
{
super(color, x, y);
}
}
class Queen extends Piece
{
Queen(Color color, int x, int y)
{
super(color, x, y);
}
}
class Bishop extends Piece
{
Bishop(Color color, int x, int y)
{
super(color, x, y);
}
}
class Knight extends Piece
{
Knight(Color color, int x, int y)
{
super(color, x, y);
}
}
class Rook extends Piece
{
Rook(Color color, int x, int y)
{
super(color, x, y);
}
}
class Pawn extends Piece
{
Pawn(Color color, int x, int y)
{
super(color, x, y);
}
}
链接地址: http://www.djcxy.com/p/84616.html
上一篇: 如何在国际象棋中移动棋子?
下一篇: 国际象棋棋盘的ASCII码表示