Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

Commit

Permalink
Logic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Procio committed May 23, 2019
1 parent 9a6213c commit c0a0455
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 58 deletions.
9 changes: 6 additions & 3 deletions src/main/java/logic/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public Board(Integer[][] state){
this.state = state;
}
public PosXY[] getComputerMove(PlayerColor color){
System.out.println("[Board] state:\n" + this.toString());
return color.equals(PlayerColor.WHITE) ?
this.whitePlayer.getComputerMove(this.state):
this.blackPlayer.getComputerMove(this.state);
}
/*TODO change pawn on last square */
/*TODO check if position is attacked in board for King*/
/*TODO en passant */
/*TODO castling */
Expand Down Expand Up @@ -63,9 +63,12 @@ public boolean isOver() {
return this.isOver;
}
public void move(PlayerColor color, PosXY[] move){
if(color.equals(PlayerColor.WHITE))
if(color.equals(PlayerColor.WHITE)) {
this.state = whitePlayer.move(move[0], move[1], this.state);
else
this.blackPlayer.enemyMove(move[1]);
}else {
this.state = blackPlayer.move(move[0], move[1], this.state);
this.whitePlayer.enemyMove(move[1]);
}
}
}
23 changes: 13 additions & 10 deletions src/main/java/logic/Piece.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ public PosXY[] getRandomMove(Integer[][] state) {
return null;
}

/**
* @return true if square contains piece of same color as this
*/
public Boolean squareOccupied(PosXY to, Integer[][] state){
if(this.getColor().equals(PlayerColor.WHITE)){
if(state[to.getX()][to.getY()] > 0)
return true;
}else{
if(state[to.getX()][to.getY()] < 0)
return true;
}
return false;
}
public PlayerColor getColor() {
return color;
}
Expand All @@ -43,19 +56,9 @@ public Integer getX(){
public Integer getY(){
return this.posXY.getY();
}
public void setPosXY(PosXY posXY) {
this.posXY = posXY;
}
public PosXY getPosXY() {
return posXY;
}
public void setX(Integer x){
this.posXY.setX(x);
}
public void setY(Integer y){
this.posXY.setY(y);
}

@Override
public String toString() {
return this.posXY.toString() + " " + this.color.toString();
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/logic/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ public PosXY[] getComputerMove(Integer state[][]){
Random rand = new Random();
ArrayList<Piece> tempPieces = new ArrayList<Piece>(this.pieces);
while(ret == null && !tempPieces.isEmpty()) {
System.out.println("[Player] tempPieces.size() = " + tempPieces.size());
Piece p = tempPieces.remove(rand.nextInt(tempPieces.size()));
System.out.println("[Player] random piece:\n" + p.toString());
ret = p.getRandomMove(state);
if (ret != null)
System.out.println("[Player] random move:\n" + ret[0].toString() + " " + ret[1].toString());
}
return ret;
}
Expand All @@ -45,7 +43,12 @@ public Integer[][] move(PosXY from, PosXY to, Integer state[][]) {
public Integer[][] move(Piece p, PosXY to, Integer[][] state) {
return p.move(to, state);
}

public void enemyMove(PosXY to){
Piece p = getPiece(to);
if(p != null){
this.pieces.remove(p);
}
}
public Player(PlayerColor color, Integer[][] state){
this.color = color;
this.pieces = new ArrayList<Piece>();
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/logic/pieces/Bishop.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public Integer[][] move(PosXY to, Integer[][] state) {
public Boolean moveValid(PosXY to, Integer[][] state) {
if(!this.getAllDestinations().contains(to))
return false;
if(state[to.getX()][to.getY()] > 0 && this.getColor().equals(PlayerColor.WHITE))
return false;
else if(state[to.getX()][to.getY()] < 0 && this.getColor().equals(PlayerColor.BLACK))
if(this.squareOccupied(to, state))
return false;
int x = this.getX();
int y = this.getY();
Expand All @@ -36,7 +34,7 @@ else if(state[to.getX()][to.getY()] < 0 && this.getColor().equals(PlayerColor.BL
y--;
else
y++;
if(this.getPosXY().equals(to))
if(to.getX() == x && to.getY() == y)
return true;
if(!state[x][y].equals(0))
return false;
Expand All @@ -58,6 +56,7 @@ public ArrayList<PosXY> getAllDestinations() {
if(x+i <= 7 && y+i <= 7)
ret.add(new PosXY(x+i, y+i));
}
System.out.println(ret.toString());
return ret;
}

Expand Down
37 changes: 24 additions & 13 deletions src/main/java/logic/pieces/Pawn.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,45 @@ public Integer[][] move(PosXY to, Integer[][] state) {

public Boolean moveValid(PosXY to, Integer[][] state) {
if(this.getY().equals(to.getY())){
/* straight move */
if(state[to.getX()][to.getY()] != 0)
return false;
if(this.getColor().equals(PlayerColor.WHITE)) {
if (this.getX().equals(to.getX()+1)){
//basic move
return true;
}else if(this.getX().equals(to.getX()+2) && this.getX().equals(6)){
}else if(this.getX().equals(6) && to.getX().equals(4)){
//double move
return true;
if(state[5][to.getY()].equals(0))
return true;
}
}else{
if(this.getX().equals(to.getX()-1)){
//basic black move
return true;
}else if(this.getX().equals(to.getX()-2) && this.getX().equals(1)){
}else if(this.getX().equals(1) && to.getX().equals(3)){
//double black move
return true;
if(state[2][to.getY()].equals(0))
return true;
}
}
}else{
/* take move */
if (this.getColor().equals(PlayerColor.WHITE)) {
if(this.getX().equals(to.getX()+1) &&
(this.getY().equals(to.getY()-1) ||
this.getY().equals(to.getY()+1))) {
if (state[to.getX()][to.getY()] < 0)
return true;
}
} else {
if(this.getX().equals(to.getX()-1) &&
(this.getY().equals(to.getY()-1) ||
this.getY().equals(to.getY()+1))) {
if (state[to.getX()][to.getY()] > 0)
return true;
}
}
}else if(this.getX().equals(to.getX()-1)
&& (this.getY().equals(to.getY()+1)
|| this.getY().equals(to.getY()-1))){
if(state[to.getX()][to.getY()] >= 0 && this.getColor().equals(PlayerColor.WHITE))
return false;
if(state[to.getX()][to.getY()] <= 0 && this.getColor().equals(PlayerColor.BLACK))
return false;
//take
return true;
}
return false;
}
Expand Down
41 changes: 22 additions & 19 deletions src/main/java/logic/pieces/Queen.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ public Integer[][] move(PosXY to, Integer[][] state) {
public Boolean moveValid(PosXY to, Integer[][] state) {
if(!this.getAllDestinations().contains(to))
return false;
if(state[to.getX()][to.getY()] > 0 && this.getColor().equals(PlayerColor.WHITE))
return false;
else if(state[to.getX()][to.getY()] < 0 && this.getColor().equals(PlayerColor.BLACK))
if(this.squareOccupied(to, state))
return false;
int x = this.getX();
int y = this.getY();
if(x == to.getX()){
while(true){
/* straight move along X axis */
while(y != to.getY()){
if(y < to.getY())
++y;
else
Expand All @@ -38,7 +37,8 @@ else if(state[to.getX()][to.getY()] < 0 && this.getColor().equals(PlayerColor.BL
return false;
}
}else if(y == to.getY()){
while(true){
/* straight move along Y axis */
while(x != to.getX()){
if(x < to.getX())
++x;
else
Expand All @@ -48,20 +48,23 @@ else if(state[to.getX()][to.getY()] < 0 && this.getColor().equals(PlayerColor.BL
if(!state[x][y].equals(0))
return false;
}
}
for (int i = 1; i < 7; i++) {
if(to.getX() < x)
x--;
else
x++;
if(to.getY() < y)
y--;
else
y++;
if(this.getPosXY().equals(to))
return true;
if(!state[x][y].equals(0))
return false;
return false;
}else {
/* diagonal move */
for (int i = 1; i < 7; i++) {
if (to.getX() < x)
x--;
else
x++;
if (to.getY() < y)
y--;
else
y++;
if (to.getX() == x && to.getY() == y)
return true;
if (!state[x][y].equals(0))
return false;
}
}
return false;
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/logic/pieces/Rook.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ public Integer[][] move(PosXY to, Integer[][] state) {
public Boolean moveValid(PosXY to, Integer[][] state) {
if(!this.getAllDestinations().contains(to))
return false;
if(state[to.getX()][to.getY()] > 0 && this.getColor().equals(PlayerColor.WHITE))
return false;
else if(state[to.getX()][to.getY()] < 0 && this.getColor().equals(PlayerColor.BLACK))
if(this.squareOccupied(to, state))
return false;
int x = this.getX();
int y = this.getY();
if(x == to.getX()){
while(true){
while(y != to.getY()){
if(y < to.getY())
++y;
else
Expand All @@ -38,7 +36,7 @@ else if(state[to.getX()][to.getY()] < 0 && this.getColor().equals(PlayerColor.BL
return false;
}
}else if(y == to.getY()){
while(true){
while(x != to.getX()){
if(x < to.getX())
++x;
else
Expand Down
Binary file modified target/classes/logic/Board.class
Binary file not shown.
Binary file modified target/classes/logic/Piece.class
Binary file not shown.
Binary file modified target/classes/logic/Player.class
Binary file not shown.
Binary file modified target/classes/logic/pieces/Bishop.class
Binary file not shown.
Binary file modified target/classes/logic/pieces/Pawn.class
Binary file not shown.
Binary file modified target/classes/logic/pieces/Queen.class
Binary file not shown.
Binary file modified target/classes/logic/pieces/Rook.class
Binary file not shown.

0 comments on commit c0a0455

Please sign in to comment.