Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added default string representations for pieces #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.idea
*.iml
out

5 changes: 5 additions & 0 deletions Bishop.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ public class Bishop extends ChessPiece {
public Bishop(int posX, int posY) {
super(posX, posY);
}

@Override
public String toString() {
return "B";
}
}
36 changes: 28 additions & 8 deletions ChessBoard.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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];
}
}
12 changes: 11 additions & 1 deletion ChessGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
39 changes: 29 additions & 10 deletions ChessPiece.java
Original file line number Diff line number Diff line change
@@ -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;
}
51 changes: 41 additions & 10 deletions ChessSquare.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions King.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ public class King extends ChessPiece {
public King(int posX, int posY) {
super(posX, posY);
}

@Override
public String toString() {
return "K";
}
}
5 changes: 5 additions & 0 deletions Knight.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ public class Knight extends ChessPiece{
public Knight(int posX, int posY){
super(posX, posY);
}

@Override
public String toString() {
return "N";
}
}
20 changes: 13 additions & 7 deletions Pawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,21 @@ public Pawn(int posX, int posY) {
}

public TreeSet<ChessSquare> 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";
}
}
20 changes: 20 additions & 0 deletions PositionTuple.java
Original file line number Diff line number Diff line change
@@ -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 + ")";
}
}
5 changes: 5 additions & 0 deletions Queen.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ public class Queen extends ChessPiece {
public Queen(int posX, int posY) {
super(posX, posY);
}

@Override
public String toString() {
return "Q";
}
}
5 changes: 4 additions & 1 deletion Rook.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ public Rook(int posX, int posY) {
super(posX, posY);
}


@Override
public String toString() {
return "R";
}
}