diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..70dd2cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.idea +*.iml +out + diff --git a/Bishop.java b/Bishop.java index 161cf05..6d3037b 100644 --- a/Bishop.java +++ b/Bishop.java @@ -6,4 +6,9 @@ public class Bishop extends ChessPiece { public Bishop(int posX, int posY) { super(posX, posY); } + + @Override + public String toString() { + return "B"; + } } diff --git a/ChessBoard.java b/ChessBoard.java index 67ba7a2..ee5773d 100644 --- a/ChessBoard.java +++ b/ChessBoard.java @@ -1,6 +1,9 @@ //Uses mapping of 8x8 chess board //Checks if piece is on board, remove if lands on piece +import java.util.stream.IntStream; +import java.util.stream.Stream; + public class ChessBoard { public final int LENGTH = 8; @@ -9,22 +12,39 @@ public class ChessBoard { public ChessBoard(){ squares = new ChessSquare[LENGTH][WIDTH]; - for(int i = 0; i <= LENGTH - 1; i++){ for(int j = 0; j <= WIDTH - 1; j++){ squares[i][j] = new ChessSquare(i, j, null); } } - - squares[0][1].piece = new Pawn(0, 1); } public void printBoard(){ - for(int i = 0; i <= squares.length - 1; i++){ - for(int j = 0; j <= squares.length - 1; j++) { - squares[i][j].printPosition(); - } - } + final StringBuilder sb = new StringBuilder(); + + // Generate squares and y axis + Stream.of(squares).flatMap(i -> + Stream.concat( Stream.of( String.valueOf( 8 - i[0].getPosX()) + " | " ), + Stream.concat( Stream.of(i).map(j -> j.getGraphic() + " "), Stream.of("\n"))) + ).forEach(s -> sb.append(s)); + + // Generate x axis + sb.append(" ---------------\n "); + IntStream.range(0, 8).mapToObj(i -> String.format("%c ", 'A' + i)).forEach(s -> sb.append(s)); + + System.out.println(sb); + } + + public ChessPiece getPiece(final String positionStart) { + return this.getSquare(positionStart).getPiece(); } + public void setPiece(final ChessPiece chessPiece) { + this.squares[chessPiece.posX][chessPiece.posY].setPiece(chessPiece); + } + + public ChessSquare getSquare (final String positionStart) { + final PositionTuple position = new PositionTuple(positionStart); + return squares[position.x][position.y]; + } } diff --git a/ChessGame.java b/ChessGame.java index 56e8a3b..26f1db8 100644 --- a/ChessGame.java +++ b/ChessGame.java @@ -2,11 +2,21 @@ * Created by Perry on 8/30/2014. */ public class ChessGame { + private ChessBoard chessBoard; + + public ChessGame(ChessBoard chessBoard) { + this.chessBoard = chessBoard; + ChessGame.setupBoard(this.chessBoard); + } public static void main(String[] args){ + final ChessBoard board = new ChessBoard(); + final ChessGame chessGame = new ChessGame(board); - ChessBoard board = new ChessBoard(); board.printBoard(); + } + private static void setupBoard(final ChessBoard board) { + board.setPiece(new King(0, 1)); } } diff --git a/ChessPiece.java b/ChessPiece.java index 8d1a738..643b043 100644 --- a/ChessPiece.java +++ b/ChessPiece.java @@ -1,27 +1,46 @@ /** * Created by Perry on 8/8/2014. */ -public class ChessPiece { +public abstract class ChessPiece { public boolean alive; - public int posX; - public int posY; + protected int posX; + protected int posY; public ChessPiece(int posX, int posY){ - alive = true; - posX = posX; - posY = posY; + this.alive = true; + this.posX = posX; + this.posY = posY; } public void move(int newX, int newY){ - posX = newX; - posY = newY; + this.posX = newX; + this.posY = newY; + } + + public void move(final PositionTuple position) { + move(position.x, position.y); + } + + public void move(final String destination) { + final PositionTuple position = new PositionTuple(destination); + move(position); } public void kill(){ - alive = false; + this.alive = false; } public boolean isAlive(){ - return alive; + return this.alive; } + + public int getPosX() { + return posX; + } + + public int getPosY() { + return posY; + } + + public String toString; } diff --git a/ChessSquare.java b/ChessSquare.java index ad1f38f..c16b8b5 100644 --- a/ChessSquare.java +++ b/ChessSquare.java @@ -5,30 +5,61 @@ public class ChessSquare { public int posX; public int posY; public ChessPiece piece; + public String graphic; - public ChessSquare(int posX, int posY, ChessPiece piece){ + public ChessSquare(int posX, int posY, ChessPiece piece) { this.posX = posX; this.posY = posY; this.piece = piece; } - public ChessPiece getPiece(){ + public int getPosX() { + return posX; + } + + public void setPosX(int posX) { + this.posX = posX; + } + + public int getPosY() { + return posY; + } + + public void setPosY(int posY) { + this.posY = posY; + } + + public ChessPiece getPiece() { return piece; } - public String getPosition(){ - return posX + ", " + posY; + public void setPiece(ChessPiece piece) { + this.piece = piece; } - public int getPosX(){ - return posX; + public String getGraphic() { + return this.piece != null ? this.piece.toString() : "X"; } - public int getPosY(){ - return posY; + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ChessSquare)) return false; + + ChessSquare that = (ChessSquare) o; + + if (posX != that.posX) return false; + if (posY != that.posY) return false; + if (piece != null ? !piece.equals(that.piece) : that.piece != null) return false; + + return true; } - public void printPosition(){ - System.out.format( "%c, %d\n", (posX + 'a'), (posY + 1) ); + @Override + public int hashCode() { + int result = posX; + result = 31 * result + posY; + result = 31 * result + (piece != null ? piece.hashCode() : 0); + return result; } } diff --git a/King.java b/King.java index da48aa4..155ef75 100644 --- a/King.java +++ b/King.java @@ -6,4 +6,9 @@ public class King extends ChessPiece { public King(int posX, int posY) { super(posX, posY); } + + @Override + public String toString() { + return "K"; + } } diff --git a/Knight.java b/Knight.java index ab642b4..8e0ba1a 100644 --- a/Knight.java +++ b/Knight.java @@ -6,4 +6,9 @@ public class Knight extends ChessPiece{ public Knight(int posX, int posY){ super(posX, posY); } + + @Override + public String toString() { + return "N"; + } } diff --git a/Pawn.java b/Pawn.java index e90b63c..4fdc5e3 100644 --- a/Pawn.java +++ b/Pawn.java @@ -11,15 +11,21 @@ public Pawn(int posX, int posY) { } public TreeSet validSquares(){ - if(clear) { - if (starting) { - posX + 2; - } - posX + 1; - } +// if(clear) { +// if (starting) { +// posX + 2; +// } +// posX + 1; +// } + return null; } - public capture(){ +// public capture(){ +// +// } + @Override + public String toString() { + return "P"; } } diff --git a/PositionTuple.java b/PositionTuple.java new file mode 100644 index 0000000..63fe024 --- /dev/null +++ b/PositionTuple.java @@ -0,0 +1,20 @@ +public class PositionTuple { + public final int x; + public final int y; + + public PositionTuple(int x, int y) { + this.x = x; + this.y = y; + } + public PositionTuple(final String destination) { + final char[] destinationArr = destination.toCharArray(); + final char c = destinationArr[0]; + this.x = c <= 'Z' ? c - 'A' : c - 'a'; + this.y = destinationArr[1] - '0'; + } + + @Override + public String toString() { + return "(" + this.x + ", " + this.y + ")"; + } +} diff --git a/Queen.java b/Queen.java index 98a7e01..ac8bcbf 100644 --- a/Queen.java +++ b/Queen.java @@ -6,4 +6,9 @@ public class Queen extends ChessPiece { public Queen(int posX, int posY) { super(posX, posY); } + + @Override + public String toString() { + return "Q"; + } } diff --git a/Rook.java b/Rook.java index bef101d..9792af4 100644 --- a/Rook.java +++ b/Rook.java @@ -7,5 +7,8 @@ public Rook(int posX, int posY) { super(posX, posY); } - + @Override + public String toString() { + return "R"; + } }