Skip to content

Commit

Permalink
added better error catching
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWang18 committed Dec 22, 2020
1 parent c369a02 commit 406980d
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 86 deletions.
56 changes: 18 additions & 38 deletions src/main/java/domain/Logic/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,8 @@ private boolean isValidPromotion(Pawn pawn, Square start, Square end) {

private boolean promote(ColorType color, Square end, Pair readableXY) throws IOException {

String pChoice = UI.parsePieceChoice(readableXY.toString()); //idk abt this static method association and this implementation in general
String pieceChoice = UI.parsePieceChoice(readableXY.toString()); //idk abt this static method association and this implementation in general - g

//B B. B..... b bishop matches

String knightRegex = "^(A\\.*|k(night)?)$"; // '\\' need to use double backslash becasue \ is already used as an escape sequence in java
String bishopRegex = "^(B\\.*|b(ishop)?)$";
Expand All @@ -212,22 +211,14 @@ private boolean promote(ColorType color, Square end, Pair readableXY) throws IOE

//could use Switch on a boolean, but opted for ifs for readability

if (pChoice.matches(bishopRegex)) {

if (pieceChoice.matches(bishopRegex)) {
end.setPiece(new Bishop(color));

} else if (pChoice.matches(knightRegex)) {

} else if (pieceChoice.matches(knightRegex)) {
end.setPiece(new Knight(color));

} else if (pChoice.matches(rookRegex)) {

} else if (pieceChoice.matches(rookRegex)) {
end.setPiece(new Rook(color));

} else if (pChoice.matches(queenRegex)) {

} else if (pieceChoice.matches(queenRegex)) {
end.setPiece(new Queen(color));

} else {
return false;
}
Expand Down Expand Up @@ -594,22 +585,6 @@ public List<Move> getMoves() {
return this.previousMoves;
}

private void undoMove() {
Move previous = getPrevMove();

if (previous == null)
return;

if (previous.getStartingPair() != previous.getEndingPair()) {
addMove(previous.getEndingSquare(), previous.getStartingSquare(), previous.getPieceMoved(), null);
if (previous.isCapture()) {
board.setPiece(previous.getEndingSquare(), previous.getPieceKilled());
}
}
previousMoves.remove(getPrevMove());
switchCurrentPlayer();
}

public Move getPrevMove() {
if (previousMoves.isEmpty())
return null;
Expand All @@ -627,16 +602,21 @@ public ColorType getTurn() {
public Square[][] getBoard(){
return board.getBoard();
}

public class Tuple2<K,V>{

private final K e1;
private void undoMove() {
Move previous = getPrevMove();

private final V e2;

public Tuple2(K ele1, V ele2){
this.e1 = ele1;
this.e2 = ele2;
if (previous == null)
return;

if (previous.getStartingPair() != previous.getEndingPair()) {
addMove(previous.getEndingSquare(), previous.getStartingSquare(), previous.getPieceMoved(), null);
if (previous.isCapture()) {
board.setPiece(previous.getEndingSquare(), previous.getPieceKilled());
}
}
previousMoves.remove(getPrevMove());
switchCurrentPlayer();
}

}
124 changes: 88 additions & 36 deletions src/main/java/domain/Logic/Move.java
Original file line number Diff line number Diff line change
@@ -1,73 +1,125 @@
package domain.Logic;


import domain.Pieces.Piece;

//

public class Move {

import domain.Pieces.*;

/*
* Container class for every type of move so far... should i include validation in here> and do castling, promotion checking here...
* would be creating a lot of objs in memory that would be removed
*/
public class Move {

public enum MoveType{
STANDARD,
CASTLE,
EN_PASSANT,
PAWN_CAPTURE,
PROMOTION;
}

private Square start;
private Square end;

private Piece piecemoved;
private Piece piecekilled;

public Move(Square start, Square end, Piece piecemoved, Piece piecekilled) {
private Piece pieceMoved;
private Piece pieceKilled;

private MoveType type;


public Move(Square start, Square end) {
this.start = start;
this.end = end;
}

public Move(Square start, Square end, Piece pieceMoved, Piece pieceKilled){
this.start = start;
this.end = end;
this.pieceMoved = pieceMoved;
this.pieceKilled = pieceKilled;
move(0);
}

public Move(Square start, Square end, Pawn pPawn, Piece pieceKilled){
this.start = start;
this.end = end;
this.piecemoved = piecemoved;
this.piecekilled = piecekilled;
setPieces();
this.pieceMoved = pPawn;
this.pieceKilled = pieceKilled;
}

public void move(int code) {
if (code == 0) {
setStandardMove();
} else if (code == 1) {
setPawnCapture();
} else if (code == 2) {
setCastling();
} else if (code == 3){
setEnPassant();
} else if (code == 4) {
setPromotion();
}

public Move(Square start, Square end, Piece piecemoved){
this(start,end,piecemoved,null);
}

//refactor
public void move() {
setPieces();
private void setStandardMove() {
end.setPiece(this.pieceMoved);
start.setPiece(null);
}

private void setPawnCapture(){

}

private void setCastling(){

}

private void setEnPassant(){

}

private void setPieces(){
end.setPiece(piecemoved);
start.setPiece(null);
private void setPromotion(){

}

public boolean isCapture(){
return piecekilled != null;
public boolean isCapture() {
return pieceKilled != null;
}

public Piece getPieceMoved() {
return this.piecemoved;
}
return this.pieceMoved;
}

public Piece getPieceKilled(){
return this.piecekilled;
public Piece getPieceKilled() {
return this.pieceKilled;
}

public Pair getStartingPair(){
public Pair getStartingPair() {
return getStartingSquare().getCoord();
}

public Pair getEndingPair(){
public Pair getEndingPair() {
return getEndingSquare().getCoord();
}
public Square getStartingSquare(){

public Square getStartingSquare() {
return this.start;
}
public Square getEndingSquare(){

public Square getEndingSquare() {
return this.end;
}

@Override
public String toString(){
if(piecekilled == null)
return this.piecemoved.getColor()+" "+this.piecemoved.getReadablePiece() + ", " + this.getStartingPair() + " -> " + this.getEndingPair();
@Override
public String toString() {
if (pieceKilled == null)
return this.pieceMoved.getColor() + " " + this.pieceMoved.getReadablePiece() + ", " + this.getStartingPair()
+ " -> " + this.getEndingPair();

return this.piecemoved.getColor()+" "+this.piecemoved.getReadablePiece() + ", " + this.getStartingPair() + " -> " + this.getEndingPair() +", captures " +piecekilled.getColor() + " " + piecekilled.getReadablePiece();
return this.pieceMoved.getColor() + " " + this.pieceMoved.getReadablePiece() + ", " + this.getStartingPair()
+ " -> " + this.getEndingPair() + ", captures " + pieceKilled.getColor() + " "
+ pieceKilled.getReadablePiece();
}
}
3 changes: 1 addition & 2 deletions src/main/java/domain/Pieces/Bishop.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@



import java.util.Arrays;
import java.util.List;

import domain.Logic.Pair;
Expand Down Expand Up @@ -49,7 +48,7 @@ public List<Pair> getPiecePath(Pair start, Pair end) {
temparr[i - 1] = new Pair(startx + signX * i, starty + signY * i); //signY * i is direction times growing length
}
// System.out.println(Arrays.toString(temparr));
return Arrays.asList(temparr);
return java.util.Arrays.asList(temparr);
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/domain/Pieces/Visitor/Visitor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package domain.Pieces.Visitor;



import domain.Logic.Pair;
import domain.Logic.Square;
import domain.Pieces.*;

public interface Visitor<T> {
Expand Down
35 changes: 26 additions & 9 deletions src/main/java/domain/UserInterface/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,31 @@ private void askMode() throws IOException { // allow the user to choose between
String noRegex = "n(o)?";
String sillyRegex = "s(illy)?";

if (input.matches(classicRegex)) {
if (input.toLowerCase().matches(classicRegex)) {

game.setMode(new ClassicRules());
System.out.println("Okay! There are the standard rules in play! Lowercase letters is black, and uppercase is white. \nLet's begin");

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

game.setMode(new NoRules());
System.out.println("Okay! There are no moving rules! Lowercase letters is black, and uppercase is white. \nLet's begin");

} else if (input.matches(sillyRegex)){
} else if (input.toLowerCase().matches(sillyRegex)){
;
}
else{
Errors.inputError();
askMode();
}

}

// Prompting for user input until game is over
public void getMoves() throws IOException {
String input1 = "";
String input2 = "";

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

while (!game.isGameOver() && !userEndCase(input1, input2)) { // not working
try {
Expand All @@ -101,8 +102,10 @@ public void getMoves() throws IOException {
System.out.println("enter the square you wish to move from.");
input1 = br.readLine();

if (!isInputValid(input1))
if (!isInputValid(input1)) //go back to top of the loop if wrong input, swap the characters if reversed, could i put this in a method with a return to top of loop?
continue;
else if(peek(input1))
input1 = swap(input1);

Pair startingPair = getPair(input1.toLowerCase());

Expand All @@ -111,14 +114,14 @@ public void getMoves() throws IOException {
continue;
}

int startfile = startingPair.getX();
int startrank = startingPair.getY();

System.out.println("Enter the square you wish to move to.");
input2 = br.readLine();

if (!isInputValid(input2))
continue;
else if(peek(input2))
input2 = swap(input2);

Pair endingPair = getPair(input2.toLowerCase());

Expand All @@ -127,6 +130,8 @@ public void getMoves() throws IOException {
continue;
}

int startfile = startingPair.getX();
int startrank = startingPair.getY();
int endfile = endingPair.getX();
int endrank = endingPair.getY();

Expand All @@ -151,14 +156,26 @@ public void getMoves() throws IOException {

}

private boolean peek(String str){ //if they give reversed format

return str.matches("^\\d\\w$");

}

private String swap(String str){
StringBuilder sb = new StringBuilder(); //could have also done a char array traversal
sb.append(str.charAt(1));
sb.append(str.charAt(0));
return sb.toString();
}

private boolean isInputValid(String input) {

return !(input.isBlank() || (input.length() > 2));

}

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();

Expand Down
Binary file removed 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/Logic/Move.class
Binary file not shown.
Binary file modified target/classes/domain/Pieces/Bishop.class
Binary file not shown.
Binary file modified target/classes/domain/UserInterface/UI.class
Binary file not shown.

0 comments on commit 406980d

Please sign in to comment.