Commit 89e9d0ab authored by Jan Rossmeisl's avatar Jan Rossmeisl
Browse files

final complete working movesets / chess, without detecting check mate

parent afb08304
Pawn,blackPawn1,1,0,black
Pawn,blackPawn2,1,1,black
Pawn,blackPawn3,1,2,black
Pawn,blackPawn4,1,3,black
Pawn,blackPawn5,1,4,black
Pawn,blackPawn4,3,3,black
Pawn,blackPawn5,3,3,black
Pawn,blackPawn6,1,5,black
Pawn,blackPawn7,1,6,black
Pawn,blackPawn8,1,7,black
Pawn,whitePawn1,6,0,white
Pawn,whitePawn2,6,1,white
Pawn,whitePawn3,6,2,white
Pawn,whitePawn4,5,3,white
Pawn,whitePawn5,6,4,white
Pawn,whitePawn4,4,3,white
Pawn,whitePawn5,2,3,white
Pawn,whitePawn6,6,5,white
Pawn,whitePawn7,6,6,white
Pawn,whitePawn8,6,7,white
......@@ -21,7 +21,7 @@ Rook,whiteRook2,7,7,white
Knight,blackKnight1,0,1,black
Knight,blackKnight2,0,6,black
Knight,whiteKnight1,7,1,white
Knight,whiteKnight2,7,6,white
Knight,whiteKnight2,6,3,white
Bishop,blackBishop1,0,2,black
Bishop,blackBishop2,0,5,black
Bishop,whiteBishop1,7,2,white
......
......@@ -53,6 +53,9 @@ public class ActionHandler {
fileHandler.saveMove(coordX, coordY, chessPiece.getColor());
}
int diff = 5 - 7;
System.out.println(String.valueOf(diff));
}
public void selectPiece(ActionEvent e) {
......
......@@ -35,26 +35,80 @@ public class Bishop extends ChessPiece {
return "Bishop," + this.getName() + "," + this.getCoordX() + "," + this.getCoordY() + "," + this.getColor();
}
private boolean checkIfBlocked(Field field, int destX, int destY) {
int moveValueX = destX - this.getCoordX();
int moveValueY = destY - this.getCoordY();
if (moveValueX < 0) {
if (moveValueY < 0) {
for (int i = -1; i > moveValueX; i--) {
if (field.recs[this.getCoordX() + i][this.getCoordY() + i] instanceof ChessPiece) {
return false;
}
}
}
if (moveValueY > 0) {
for (int i = -1; i > moveValueX; i--) {
if (field.recs[this.getCoordX() + i][this.getCoordY() - i] instanceof ChessPiece) {
return false;
}
}
}
} else if (moveValueX > 0) {
if (moveValueY < 0) {
for (int i = 1; i < moveValueX; i++) {
if (field.recs[this.getCoordX() + i][this.getCoordY() - i] instanceof ChessPiece) {
return false;
}
}
}
if (moveValueY > 0) {
for (int i = 1; i < moveValueX; i++) {
if (field.recs[this.getCoordX() + i][this.getCoordY() + i] instanceof ChessPiece) {
return false;
}
}
}
}
return true;
}
@Override
public boolean checkMoveset(Field field, int destX, int destY) {
for (int i = 1; i < 8; i++) {
// if (field.recs[this.getCoordX() - i][this.getCoordY() + i] instanceof ChessPiece ||
// field.recs[this.getCoordX() + i][this.getCoordY() - i] instanceof ChessPiece ||
// field.recs[this.getCoordX() + i][this.getCoordY() + i] instanceof ChessPiece ||
// field.recs[this.getCoordX() - i][this.getCoordY() - i] instanceof ChessPiece) {
// return false;
// } else
if ((destX == (this.getCoordX() - i) && destY == (this.getCoordY() + i))) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() + i) && destY == (this.getCoordY() - i))) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() + i) && destY == (this.getCoordY() + i))) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() - i) && destY == (this.getCoordY() - i))) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
}
}
......
......@@ -39,29 +39,72 @@ public class Pawn extends ChessPiece {
public boolean checkMoveset(Field field, int destX, int destY) {
if (this.getColor().equals("white")) {
if (this.getCoordX() == 6) {
if (this.getCoordX() == 6) { // start move
if ((destX == (this.getCoordX() - 1) && destY == this.getCoordY())
|| ((destX == (this.getCoordX() - 2) && destY == this.getCoordY()))) {
if (field.recs[destX][destY] instanceof ChessPiece) { // no chess piece in front
return false;
}
return true;
} else if (field.recs[destX][destY] instanceof ChessPiece) { // kick enemy
if (destX == (this.getCoordX() - 1) && destY == this.getCoordY() - 1) {
return true;
} else if (destX == (this.getCoordX() - 1) && destY == this.getCoordY() + 1) {
return true;
}
}
} else {
if ((destX == (this.getCoordX() - 1) && destY == this.getCoordY())) {
} else if ((destX == (this.getCoordX() - 1) && destY == this.getCoordY())) { // normal move
if (field.recs[destX][destY] instanceof ChessPiece) { // no chess piece in front
return false;
}
return true;
} else if (field.recs[destX][destY] instanceof ChessPiece) { // kick enemy
if (destX == (this.getCoordX() - 1) && destY == this.getCoordY() - 1) {
return true;
} else if (destX == (this.getCoordX() - 1) && destY == this.getCoordY() + 1) {
return true;
}
}
} else if (this.getColor().equals("black")) {
if (this.getCoordX() == 1) {
if (this.getCoordX() == 1) { // start move
if ((destX == (this.getCoordX() + 1) && destY == this.getCoordY())
|| ((destX == (this.getCoordX() + 2) && destY == this.getCoordY()))) {
if (field.recs[destX][destY] instanceof ChessPiece) { // no chess piece in front
return false;
}
return true;
} else if (field.recs[destX][destY] instanceof ChessPiece) { // kick enemy
if (destX == (this.getCoordX() + 1) && (destY == this.getCoordY() - 1)) {
return true;
} else if (destX == (this.getCoordX() + 1) && (destY == this.getCoordY() + 1)) {
return true;
}
}
} else {
if ((destX == (this.getCoordX() + 1) && destY == this.getCoordY())) {
} else // normal move
if ((destX == (this.getCoordX() + 1) && destY == this.getCoordY())) {
if (field.recs[destX][destY] instanceof ChessPiece) { // no chess piece in front
return false;
}
return true;
} else if (field.recs[destX][destY] instanceof ChessPiece) { // kick enemy
if (destX == (this.getCoordX() + 1) && (destY == this.getCoordY() - 1)) {
return true;
} else if (destX == (this.getCoordX() + 1) && (destY == this.getCoordY() + 1)) {
return true;
}
}
}
}
return false;
}
......
......@@ -35,27 +35,138 @@ public class Queen extends ChessPiece{
return "Queen," + this.getName() + "," + this.getCoordX() + "," + this.getCoordY() + "," + this.getColor();
}
private boolean checkIfBlocked(Field field, int destX, int destY) {
int moveValueX = destX - this.getCoordX();
int moveValueY = destY - this.getCoordY();
if (moveValueX == 0) {
if (moveValueY < 0) {
for (int i = 0; i > moveValueY; i--) {
if (field.recs[this.getCoordX()][this.getCoordY() + i] instanceof ChessPiece &&
field.recs[this.getCoordX()][this.getCoordY() + i] != this) {
return false;
}
}
}
if (moveValueY > 0) {
for (int i = 0; i < moveValueY; i++) {
if (field.recs[this.getCoordX()][this.getCoordY() - i] instanceof ChessPiece &&
field.recs[this.getCoordX()][this.getCoordY() - i] != this) {
return false;
}
}
}
} else if (moveValueY == 0) {
if (moveValueX < 0) {
for (int i = 0; i > moveValueX; i--) {
if (field.recs[this.getCoordX() + i][this.getCoordY()] instanceof ChessPiece &&
field.recs[this.getCoordX() + i][this.getCoordY()] != this) {
return false;
}
}
}
if (moveValueX > 0) {
for (int i = 0; i < moveValueX; i++) {
if (field.recs[this.getCoordX() + i][this.getCoordY()] instanceof ChessPiece &&
field.recs[this.getCoordX() + i][this.getCoordY()] != this) {
return false;
}
}
}
}
if (moveValueX < 0) {
if (moveValueY < 0) {
for (int i = -1; i > moveValueX; i--) {
if (field.recs[this.getCoordX() + i][this.getCoordY() + i] instanceof ChessPiece) {
return false;
}
}
}
if (moveValueY > 0) {
for (int i = -1; i > moveValueX; i--) {
if (field.recs[this.getCoordX() + i][this.getCoordY() - i] instanceof ChessPiece) {
return false;
}
}
}
} else if (moveValueX > 0) {
if (moveValueY < 0) {
for (int i = 1; i < moveValueX; i++) {
if (field.recs[this.getCoordX() + i][this.getCoordY() - i] instanceof ChessPiece) {
return false;
}
}
}
if (moveValueY > 0) {
for (int i = 1; i < moveValueX; i++) {
if (field.recs[this.getCoordX() + i][this.getCoordY() + i] instanceof ChessPiece) {
return false;
}
}
}
}
return true;
}
@Override
public boolean checkMoveset(Field field, int destX, int destY) {
for (int i = 1; i < 8; i++) {
if ((destX == (this.getCoordX() - i) && destY == (this.getCoordY() + i))) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() + i) && destY == (this.getCoordY() - i))) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() + i) && destY == (this.getCoordY() + i))) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() - i) && destY == (this.getCoordY() - i))) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() + i) && destY == this.getCoordY())) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() - i) && destY == this.getCoordY())) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destY == (this.getCoordY() + i) && destX == this.getCoordX())) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destY == (this.getCoordY() - i) && destX == this.getCoordX())) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
}
}
......
......@@ -35,19 +35,81 @@ public class Rook extends ChessPiece {
return "Rook," + this.getName() + "," + this.getCoordX() + "," + this.getCoordY() + "," + this.getColor();
}
private boolean checkIfBlocked(Field field, int destX, int destY) {
int moveValueX = destX - this.getCoordX();
int moveValueY = destY - this.getCoordY();
if (moveValueX == 0) {
if (moveValueY < 0) {
for (int i = 0; i > moveValueY; i--) {
if (field.recs[this.getCoordX()][this.getCoordY() + i] instanceof ChessPiece &&
field.recs[this.getCoordX()][this.getCoordY() + i] != this) {
return false;
}
}
}
if (moveValueY > 0) {
for (int i = 0; i < moveValueY; i++) {
if (field.recs[this.getCoordX()][this.getCoordY() - i] instanceof ChessPiece &&
field.recs[this.getCoordX()][this.getCoordY() - i] != this) {
return false;
}
}
}
} else if (moveValueY == 0) {
if (moveValueX < 0) {
for (int i = 0; i > moveValueX; i--) {
if (field.recs[this.getCoordX() + i][this.getCoordY()] instanceof ChessPiece &&
field.recs[this.getCoordX() + i][this.getCoordY()] != this) {
return false;
}
}
}
if (moveValueX > 0) {
for (int i = 0; i < moveValueX; i++) {
if (field.recs[this.getCoordX() + i][this.getCoordY()] instanceof ChessPiece &&
field.recs[this.getCoordX() + i][this.getCoordY()] != this) {
return false;
}
}
}
}
return true;
}
@Override
public boolean checkMoveset(Field field, int destX, int destY) {
for (int i = 1; i < 8; i++) {
if ((destX == (this.getCoordX() + i) && destY == this.getCoordY())) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destX == (this.getCoordX() - i) && destY == this.getCoordY())) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destY == (this.getCoordY() + i) && destX == this.getCoordX())) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
} else if ((destY == (this.getCoordY() - i) && destX == this.getCoordX())) {
return true;
if (checkIfBlocked(field, destX, destY)) {
return true;
} else
return false;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment