Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package clean.code.chess.requirements;

import java.awt.*;

public class ChessBoard {

public static int MAX_BOARD_WIDTH = 7;
Expand All @@ -13,10 +15,57 @@ public ChessBoard() {
}

public void Add(Pawn pawn, int xCoordinate, int yCoordinate, PieceColor pieceColor) {
throw new UnsupportedOperationException("Need to implement ChessBoard.add()");
if(IsLegalBoardPosition(xCoordinate, yCoordinate) && IsValidPieceColor(xCoordinate, Color))
{ pawn.setXCoordinate(xCoordinate);
pawn.setYCoordinate(yCoordinate);
pawn.setPieceColor(pieceColor);
this.pieces= new Pawn[xCoordinate][yCoordinate];

}
else pieces[MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH] = pawn;
}


public boolean IsLegalBoardPosition(int xCoordinate, int yCoordinate) {
throw new UnsupportedOperationException("Need to implement ChessBoard.IsLegalBoardPosition()");
if(xCoordinate<0 && yCoordinate<0) {
return false;
} else
if(xCoordinate>7 && yCoordinate>7)
return false;
else if(xCoordinate>7 && yCoordinate<7)
return false;
else if(xCoordinate<7 && yCoordinate>7)
return false;
else if (xCoordinate>0 && yCoordinate<0)
return false;
else if(xCoordinate<0 && yCoordinate>0)
return false;
else return true;
}


public void AvoidDuplicates(Pawn pawn,int xNewCoordination, int yNewCoordination) {
if(IsLegalBoardPosition(pawn.getXCoordinate(), pawn.getYCoordinate())){
xNewCoordination = pawn.getXCoordinate();
yNewCoordination = pawn.getYCoordinate();

pawn.setXCoordinate(xNewCoordination);
pawn.setYCoordinate(yNewCoordination);

this.pieces[xNewCoordination][yNewCoordination] = pawn;

}
else this.pieces[xNewCoordination][yNewCoordination] = null;
}
}


protected boolean IsValidPieceColor(int xCoordinate, Color color) {
if (color == Color.WHITE) {
return xCoordinate == 0 || xCoordinate == 1;
}
return xCoordinate == MAX_BOARD_HEIGHT - 1 || xCoordinate == MAX_BOARD_HEIGHT;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public int getXCoordinate() {

public void setXCoordinate(int value) {
this.xCoordinate = value;

}

public int getYCoordinate() {
Expand All @@ -39,7 +40,7 @@ public PieceColor getPieceColor() {
return this.pieceColor;
}

private void setPieceColor(PieceColor value) {
public void setPieceColor(PieceColor value) {
pieceColor = value;
}

Expand All @@ -56,4 +57,6 @@ protected String CurrentPositionAsString() {
String eol = System.lineSeparator();
return String.format("Current X: {1}{0}Current Y: {2}{0}Piece Color: {3}", eol, xCoordinate, yCoordinate, pieceColor);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testIsLegalBoardPosition_True_X_equals_5_Y_equals_5() {
@Test
public void testIsLegalBoardPosition_False_X_equals_11_Y_equals_5() {
boolean isValidPosition = testSubject.IsLegalBoardPosition(11, 5);
assertTrue(isValidPosition);
assertFalse(isValidPosition);
}

@Test
Expand Down