Skip to content

Commit

Permalink
added concrete implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWang18 committed Dec 21, 2020
1 parent a1d815f commit c369a02
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/main/java/domain/Logic/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
CRUD CLI Chess game
could get rid of null checking and have a blank or empty piecetype
TODO refactor move checking into Move class, could have an abstract move class with promotion, castling, normal, capture pawn etc
*/

import java.util.ArrayList;
Expand Down Expand Up @@ -34,7 +35,7 @@ public final class Game { //prohibit inheritance

public boolean over = false;

private Visitor<Boolean> visitor;
private Visitor<Boolean> visitor; //Should i have encapsulated this? Could have alternatively passed it so we make sure not null

private Game() {
board = new Board();
Expand All @@ -60,8 +61,8 @@ public void setMode(Visitor<Boolean> pVisitor){
/*
* param: numerical coordinates indicating the location of a move on the Board's
* 2D array
* TODO Need param of visitor - Instead of accessing directly the subs implemented class, we just accept the passed visitor
*
* TODO Need param of visitor - Instead of accessing directly the subs implemented class, we just accept the passed visitor..
* I could put a lot of the logic inside concrete visitors like if the conditions are true invoke behav
*/
public boolean tryMove(int startx, int starty, int endx, int endy) throws IOException {

Expand Down Expand Up @@ -120,8 +121,8 @@ private boolean checkPawnMove(Square start, Square end, Piece pawn, Piece killed
executeMove(start, end, pawn, killedPiece);
return promote(pawn.getColor(), end, start.getCoord());
}
//visitor.visitPawn(pPawn, start.getCoord(), end.getCoord(), killedPiece);
boolean test = pPawn.validOrNah(start.getCoord(), end.getCoord(), killedPiece)

boolean test = visitor.invokeBehavior(pawn ,start.getCoord(), end.getCoord(), killedPiece)
&& checkPiecesPath(pawn.getPiecePath(start.getCoord(), end.getCoord()), start.getCoord(),
end.getCoord()).value;

Expand Down Expand Up @@ -244,8 +245,7 @@ private boolean atEndOfBoard(Piece pawn, Pair endXY) {
}

private boolean checkStandardMove(Square start, Square end, Piece startPiece, Piece killedPiece) {
boolean testValidMove = !isPawn(startPiece)
&& startPiece.validOrNah(start.getCoord(), end.getCoord());
boolean testValidMove = visitor.invokeBehavior(startPiece ,start.getCoord(), end.getCoord(), killedPiece);

//Message holds data about the legality of move
Message moveData = checkPiecesPath(startPiece.getPiecePath(start.getCoord(), end.getCoord()),
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/domain/Pieces/Pawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ public List<Pair> getPiecePath(Pair startXY, Pair endXY) {
}

@Override
public <T> T accept(Visitor<T> visitor, Pair startXY, Pair endXY) {
public <T> T accept(Visitor<T> visitor, Pair startXY, Pair endXY) { //Since pawns validity depends on the captured square
//we need to have an overloaded accept method with proper arguments
return accept(visitor, startXY, endXY, null);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/domain/Pieces/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Piece(PieceType type, ColorType color) {

public abstract List<Pair> getPiecePath(Pair startXY, Pair endXY);

public abstract boolean validOrNah(Pair startXY, Pair endXY); // checks if the movement coordinates are feasbile for that piece
public abstract boolean validOrNah(Pair startXY, Pair endXY); // should instead have the visitor and use the visitor.validOrNah(this, startXY, endXY) ?

public ColorType getColor() {
return this.color;
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/domain/Pieces/Visitor/ClassicRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ public class ClassicRules implements Visitor<Boolean> { // could make everyone q
* class so they can choose classic or nah
*
*/

public void changeBehavior(Piece piece, Pair startXY, Pair endXY) {
if (piece instanceof Pawn) {
;
@Override
public Boolean invokeBehavior(Piece piece, Pair startXY, Pair endXY, Piece killedPiece) {
if (piece instanceof Pawn) {
Pawn pawn = (Pawn) piece; //trust me im a pro
return pawn.accept(this, startXY, endXY, killedPiece);
}
piece.accept(this, startXY, endXY);
return piece.accept(this, startXY, endXY);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/domain/Pieces/Visitor/NoRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
//Every move is valid as long as they are possible
public class NoRules implements Visitor<Boolean> {

@Override
public Boolean invokeBehavior(Piece piece, Pair startXY, Pair endXY, Piece killedPiece) {
return piece.accept(this, startXY, endXY);
}

@Override
public Boolean visitBishop(Bishop bishop, Pair startXY, Pair endXY) {
return true;
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/domain/Pieces/Visitor/SillyRules.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package domain.Pieces.Visitor;

import domain.Logic.Pair;
import domain.Pieces.Bishop;
import domain.Pieces.King;
import domain.Pieces.Knight;
import domain.Pieces.Pawn;
import domain.Pieces.Piece;
import domain.Pieces.Queen;
import domain.Pieces.Rook;

public class SillyRules implements Visitor<Boolean> {

@Override
public Boolean visitBishop(Bishop bishop, Pair startXY, Pair endXY) {
// TODO Auto-generated method stub
return null;
}

@Override
public Boolean visitPawn(Pawn pawn, Pair startXY, Pair endXY, Piece killedPiece) {
// TODO Auto-generated method stub
return null;
}

@Override
public Boolean visitKnight(Knight knight, Pair startXY, Pair endXY) {
// TODO Auto-generated method stub
return null;
}

@Override
public Boolean visitKing(King king, Pair startXY, Pair endXY) {
// TODO Auto-generated method stub
return null;
}

@Override
public Boolean visitQueen(Queen queen, Pair startXY, Pair endXY) {
// TODO Auto-generated method stub
return null;
}

@Override
public Boolean visitRook(Rook rook, Pair startXY, Pair endXY) {
// TODO Auto-generated method stub
return null;
}

@Override
public Boolean invokeBehavior(Piece piece, Pair startXY, Pair endXY, Piece killedPiece) {
// TODO Auto-generated method stub
return null;
}

}
1 change: 1 addition & 0 deletions src/main/java/domain/Pieces/Visitor/Visitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface Visitor<T> {
// visitor interface used to add functionality to Piece subclasses easily, can
// return any generic type T concrete implementations are all gonna be Bool tho..
//should prob nest to reduce file clutter
public abstract T invokeBehavior(Piece piece, Pair startXY, Pair endXY, Piece killedPiece);

public T visitBishop(Bishop bishop, Pair startXY, Pair endXY);

Expand Down
43 changes: 31 additions & 12 deletions src/main/java/domain/UserInterface/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import domain.Logic.Square;
import domain.Pieces.Visitor.*;

import org.apache.commons.lang3.tuple.ImmutablePair;

public class UI implements Runnable {

Expand All @@ -34,22 +33,23 @@ public UI(Game g, Reader r) {

@Override
public void run() {
showGreeting();

if (this.read instanceof BufferedReader) { // testing if we are doing user input
br = (BufferedReader) read;
try {
showGreeting();
getMoves();
} catch (IOException e) {
e.printStackTrace();
}
}
} else
System.err.println("testing");
}

public void showGreeting() {
System.out.println("Welcome to CL Chess! \nLowercase letters is black, and uppercase is white. \nLet's begin");
game.printBoard();
public void showGreeting() throws IOException {
System.out.println("Welcome to CL Chess!");
askMode();
showBoard();
}

private void gameIsOver() {
Expand All @@ -58,15 +58,29 @@ private void gameIsOver() {
// if yes undo moves until gone
}

private void askMode() throws IOException { //add regex
private void askMode() throws IOException { // allow the user to choose between Normal rules, any sound moves i.e pawn can move unlimited diagnals, and Silly

System.out.println("What style of rules do you want to play?");
System.out.println("Classic, Silly, or None");

String input = br.readLine();
if (input.equals("Classic")) {

String classicRegex = "c(lassic)?";
String noRegex = "n(o)?";
String sillyRegex = "s(illy)?";

if (input.matches(classicRegex)) {

game.setMode(new ClassicRules());
} else if (input.equals("None")) {
//
}else{

} else if (input.matches(noRegex)) {

game.setMode(new NoRules());

} else if (input.matches(sillyRegex)){
;
}
else{
Errors.inputError();
askMode();
}
Expand All @@ -78,6 +92,8 @@ public void getMoves() throws IOException {
String input1 = "";
String input2 = "";

System.out.println("Proper format is character file then numerical rank.");

while (!game.isGameOver() && !userEndCase(input1, input2)) { // not working
try {
System.out.print(game.getTurn() + " to move, ");
Expand Down Expand Up @@ -141,8 +157,11 @@ private boolean isInputValid(String input) {

}

private void showBoard(){
private void showBoard(){ //display board in the UI
System.out.println("Lowercase letters is black, and uppercase is white. \nLet's begin");

Square[][] bd = game.getBoard();

for (int i = 0; i < bd.length; i++) {
System.out.println((i+1) +" " +Arrays.toString(bd[i]).replaceAll("\\[|]|,", "")); //replace the Array funky business with regex
}
Expand Down
Binary file modified target/classes/domain/Logic/Game$Tuple2.class
Binary file not shown.
Binary file modified target/classes/domain/Logic/Game.class
Binary file not shown.
Binary file modified target/classes/domain/Pieces/Pawn.class
Binary file not shown.
Binary file modified target/classes/domain/Pieces/Visitor/ClassicRules.class
Binary file not shown.
Binary file modified target/classes/domain/Pieces/Visitor/NoRules.class
Binary file not shown.
Binary file modified target/classes/domain/Pieces/Visitor/Visitor.class
Binary file not shown.
Binary file modified target/classes/domain/UserInterface/UI.class
Binary file not shown.
Binary file modified target/test-classes/domain/AppTest.class
Binary file not shown.

0 comments on commit c369a02

Please sign in to comment.