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
1 change: 1 addition & 0 deletions Hospital Simulator
Submodule Hospital Simulator added at 236bec
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Hello this is my personal java repo
# java-training

# 1 basics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public String fizzBuzz(int i) {
new NumberWordCorrelation(3, "Fizz"),
new NumberWordCorrelation(5, "Buzz"),
};

boolean isDivisible = false;

for (NumberWordCorrelation correlation : correlations) {
if (i % correlation.getNumber() == 0) {
isDivisible = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,63 @@
package clean.code.chess.requirements;

import java.util.HashMap;
import java.util.Map;

public class ChessBoard {

public static int MAX_BOARD_WIDTH = 7;
public static int MAX_BOARD_HEIGHT = 7;

private Map<PawnPosition, Pawn> mapOfPositionAndPawn;
private Map<Pawn, PawnPosition> mapOfPawnAndPosition;



private Pawn[][] pieces;

public ChessBoard() {
pieces = new Pawn[MAX_BOARD_WIDTH][MAX_BOARD_HEIGHT];

this.mapOfPawnAndPosition = new HashMap<>(MAX_BOARD_WIDTH * MAX_BOARD_HEIGHT);
this.mapOfPositionAndPawn = new HashMap<>(MAX_BOARD_WIDTH * MAX_BOARD_HEIGHT);
}

public void Add(Pawn pawn, int xCoordinate, int yCoordinate, PieceColor pieceColor) {
throw new UnsupportedOperationException("Need to implement ChessBoard.add()");
boolean isValid = false;
if (pieceColor == PieceColor.WHITE) {
isValid = ((xCoordinate == 0 || xCoordinate == 1) && (getPawn(new PawnPosition(xCoordinate, yCoordinate)) == null));
} else if (pieceColor == PieceColor.BLACK) {
isValid = ((xCoordinate == MAX_BOARD_HEIGHT - 1 || xCoordinate == MAX_BOARD_HEIGHT) && (getPawn(new PawnPosition(xCoordinate, yCoordinate)) == null));
}

if (isValid) {
pawn.addPawnOnChessboard(this, xCoordinate, yCoordinate);
}
}

public Pawn getPawn(PawnPosition position) {
return this.mapOfPositionAndPawn.get(position);
}

public void updatePawnPositionOnChessBoard(Pawn pawn, int xCoordinate, int yCoordinate) {
PawnPosition newPosition;
if (IsLegalBoardPosition(xCoordinate, yCoordinate)) {
newPosition = new PawnPosition(xCoordinate, yCoordinate);
pawn.setXCoordinate(xCoordinate);
pawn.setYCoordinate(yCoordinate);
}
else {
newPosition = new PawnPosition(-1, -1);
pawn.setXCoordinate(-1);
pawn.setYCoordinate(-1);
}

this.mapOfPawnAndPosition.put(pawn, newPosition);
this.mapOfPositionAndPawn.put(newPosition, pawn);
}


public boolean IsLegalBoardPosition(int xCoordinate, int yCoordinate) {
throw new UnsupportedOperationException("Need to implement ChessBoard.IsLegalBoardPosition()");
return (getPawn(new PawnPosition(xCoordinate, yCoordinate)) == null) &&
((xCoordinate >= 0 && xCoordinate < MAX_BOARD_WIDTH) && (yCoordinate >= 0 && yCoordinate < MAX_BOARD_HEIGHT));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Pawn(PieceColor pieceColor) {
this.pieceColor = pieceColor;
}

public ChessBoard getChesssBoard() {
public ChessBoard getChessBoard() {
return chessBoard;
}

Expand All @@ -35,6 +35,17 @@ public void setYCoordinate(int value) {
this.yCoordinate = value;
}

public PieceColor getColor() {
return this.pieceColor;
}

private int getMovingDirection() {
if (getColor() == PieceColor.BLACK) {
return -1;
}
return +1;
}

public PieceColor getPieceColor() {
return this.pieceColor;
}
Expand All @@ -44,7 +55,17 @@ private void setPieceColor(PieceColor value) {
}

public void Move(MovementType movementType, int newX, int newY) {
throw new UnsupportedOperationException("Need to implement Pawn.Move()");
// check if x coordinate is valid + is y coordinate is valid
if ((newX == getXCoordinate() ||
newX == getXCoordinate() + getMovingDirection()) &&
(newY == getYCoordinate() - 1 || newY == getYCoordinate() ||
newY == getYCoordinate() + 1))
this.chessBoard.updatePawnPositionOnChessBoard(this, newX, newY);
}

public void addPawnOnChessboard(ChessBoard chessBoard, int xCoordinate, int yCoordinate) {
this.chessBoard = chessBoard;
this.chessBoard.updatePawnPositionOnChessBoard(this, xCoordinate, yCoordinate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package clean.code.chess.requirements;

import java.util.Objects;

public class PawnPosition {
private final int x;
private final int y;

public PawnPosition(int x, int y) {
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PawnPosition that = (PawnPosition) o;
return x == that.x &&
y == that.y;
}

@Override
public int hashCode() {
return Objects.hash(x, y);
}
}
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