Commit 4bb8935f authored by hosein's avatar hosein

pack completed

parent 1d44fbd4
...@@ -125,11 +125,21 @@ public class Game { ...@@ -125,11 +125,21 @@ public class Game {
i.nextLine(); i.nextLine();
} }
public int getTurn(){return turn;} public int getTurn() {
return turn;
}
public PlayGround getPlayGround() { public PlayGround getPlayGround() {
return playGround; return playGround;
} }
public boolean isEnd() {
if (!playGround.getCheck())
return false;
return
!playGround.canKingScape(turn);
}
} }
...@@ -31,6 +31,8 @@ public class Bishop extends Nut { ...@@ -31,6 +31,8 @@ public class Bishop extends Nut {
@Override @Override
public boolean canMove(int[] destination) { public boolean canMove(int[] destination) {
if (!isAlive) return false;
if (x == destination[0] && y == destination[1]) return false;
if (x == destination[0]) return false; if (x == destination[0]) return false;
else return Math.abs((float) (y - destination[1]) / (float) (x - destination[0])) == 1.0; else return Math.abs((float) (y - destination[1]) / (float) (x - destination[0])) == 1.0;
} }
...@@ -45,10 +47,22 @@ public class Bishop extends Nut { ...@@ -45,10 +47,22 @@ public class Bishop extends Nut {
if (destination[1] < y) Y = -1; if (destination[1] < y) Y = -1;
else Y = 1; else Y = 1;
for (int i = x + X, j = y + Y; i > -1 && i != destination[0] && j > -1 && j != destination[1]; i += X, j += Y) for (int i = x + X, j = y + Y; i > -1 && i < 8 && i != destination[0] && j > -1 && j < 8 && j != destination[1]; i += X, j += Y)
if (ground[i][j] != 0) if (ground[i][j] != 0)
return false; return false;
return true; return true;
} }
public int[][] getMoves(int[][] ground) {
int[][] moves = new int[1][2];
moves[0][0] = -1;
return moves;
}
} }
...@@ -24,17 +24,41 @@ public class King extends Nut { ...@@ -24,17 +24,41 @@ public class King extends Nut {
return Math.pow(x - destination[0], 2) + Math.pow(y - destination[1], 2) <= 2; return Math.pow(x - destination[0], 2) + Math.pow(y - destination[1], 2) <= 2;
} }
public int[] getXY(){ public int[] getXY() {
int[] r = new int[2]; int[] r = new int[2];
r[0] = x; r[0] = x;
r[1] = y; r[1] = y;
return r; return r;
} }
@Override @Override
public boolean canRoute(int[] destination, int[][] ground) { public boolean canRoute(int[] destination, int[][] ground) {
return true; return true;
} }
public int[][] getMoves(int[][] ground) {
int[][] moves = new int[8][2];
int i, I, j, J;
i = I = j = J = 0;
if (x > 0) i = -1;
if (y > 0) j = -1;
if (x < 7) I = 2;
if (y < 7) J = 2;
int k = 0;
for (int q = x + i, w; q < x + I; q++)
for (w = y + j; w < y + J; w++)
if (ground[q][w] == 0 && (q != x || w != y)) {
moves[k][0] = q;
moves[k][1] = w;
k++;
}
moves[k][0] = -1;
return moves;
}
} }
...@@ -34,6 +34,7 @@ public class Knight extends Nut { ...@@ -34,6 +34,7 @@ public class Knight extends Nut {
@Override @Override
public boolean canMove(int[] destination) { public boolean canMove(int[] destination) {
if (!isAlive) return false;
return Math.pow(destination[0] - x, 2) + Math.pow(destination[1] - y, 2) == 5; return Math.pow(destination[0] - x, 2) + Math.pow(destination[1] - y, 2) == 5;
} }
...@@ -42,5 +43,12 @@ public class Knight extends Nut { ...@@ -42,5 +43,12 @@ public class Knight extends Nut {
return true; return true;
} }
public int[][] getMoves(int[][] ground) {
int[][] moves = new int[1][2];
moves[0][0] = -1;
return moves;
}
} }
...@@ -19,5 +19,6 @@ public abstract class Nut { ...@@ -19,5 +19,6 @@ public abstract class Nut {
isAlive = false; isAlive = false;
} }
public abstract int[][] getMoves(int[][] ground);
} }
...@@ -22,6 +22,7 @@ public class Pawn extends Nut { ...@@ -22,6 +22,7 @@ public class Pawn extends Nut {
@Override @Override
public boolean canMove(int[] destination) { public boolean canMove(int[] destination) {
if (!isAlive) return false;
if (id > 0) { if (id > 0) {
if (x == 6 && destination[0] - x == -2 && destination[1] == y) return true; if (x == 6 && destination[0] - x == -2 && destination[1] == y) return true;
return destination[0] < x && destination[0] >= x - 1 && destination[1] <= y + 1 && destination[1] >= y - 1; return destination[0] < x && destination[0] >= x - 1 && destination[1] <= y + 1 && destination[1] >= y - 1;
...@@ -44,6 +45,16 @@ public class Pawn extends Nut { ...@@ -44,6 +45,16 @@ public class Pawn extends Nut {
return (x + 1 == k[0] && y - 1 == k[1]) || (x + 1 == k[0] && y + 1 == k[1]); return (x + 1 == k[0] && y - 1 == k[1]) || (x + 1 == k[0] && y + 1 == k[1]);
} }
@Override
public int[][] getMoves(int[][] ground) {
int[][] moves = new int[1][2];
moves[0][0] = -1;
return moves;
}
} }
...@@ -10,7 +10,7 @@ public class Queen extends Nut { ...@@ -10,7 +10,7 @@ public class Queen extends Nut {
y = 3; y = 3;
} else { } else {
x = 0; x = 0;
y = 4; y = 3;
} }
} }
...@@ -21,13 +21,10 @@ public class Queen extends Nut { ...@@ -21,13 +21,10 @@ public class Queen extends Nut {
@Override @Override
public boolean canMove(int[] destination) { public boolean canMove(int[] destination) {
if (x == destination[0] || y == destination[1]) return true; if (!isAlive) return false;
else { if (x == destination[0] && y == destination[1]) return false;
if (x == destination[0]) else if (x == destination[0] || y == destination[1]) return true;
return false; else return Math.abs((float)(y - destination[1]) / (float) (x - destination[0])) == 1;
else
return Math.abs((y - destination[1]) / (x - destination[0])) == 1;
}
} }
@Override @Override
...@@ -36,18 +33,19 @@ public class Queen extends Nut { ...@@ -36,18 +33,19 @@ public class Queen extends Nut {
int t; int t;
if (destination[1] - y > 0) t = 1; if (destination[1] - y > 0) t = 1;
else t = -1; else t = -1;
for (int i = y; i != destination[1]; i += t) for (int i = y + t; i > -1 && i < 8 && i != destination[1]; i += t)
if (ground[x][i] != 0) if (ground[x][i] != 0)
return false; return false;
return true;
} else if (destination[1] == y) { } else if (destination[1] == y) {
int t; int t;
if (destination[0] - x > 0) t = 1; if (destination[0]> x) t = 1;
else t = -1; else t = -1;
for (int i = x; i != destination[0]; i += t) for (int i = x + t; i > -1 && i < 8 && i != destination[0]; i += t)
if (ground[i][y] != 0) if (ground[i][y] != 0)
return false; return false;
return true;
} }
int X, Y; int X, Y;
if (destination[0] < x) X = -1; if (destination[0] < x) X = -1;
...@@ -56,7 +54,7 @@ public class Queen extends Nut { ...@@ -56,7 +54,7 @@ public class Queen extends Nut {
if (destination[1] < y) Y = -1; if (destination[1] < y) Y = -1;
else Y = 1; else Y = 1;
for (int i = x + X, j = y + Y; i > -1 && i != destination[0] && j > -1 && j != destination[1]; i += X, j += Y) for (int i = x + X, j = y + Y; i > -1 && i < 8 && i != destination[0] && j > -1 && j < 8 && j != destination[1]; i += X, j += Y)
if (ground[i][j] != 0) if (ground[i][j] != 0)
return false; return false;
...@@ -65,8 +63,11 @@ public class Queen extends Nut { ...@@ -65,8 +63,11 @@ public class Queen extends Nut {
} }
public int[][] getMoves(int[][] ground) {
int[][] moves = new int[1][2];
moves[0][0] = -1;
return moves;
}
......
...@@ -32,6 +32,8 @@ public class Rook extends Nut { ...@@ -32,6 +32,8 @@ public class Rook extends Nut {
@Override @Override
public boolean canMove(int[] destination) { public boolean canMove(int[] destination) {
if (!isAlive) return false;
if (x == destination[0] && y == destination[1]) return false;
return x == destination[0] || y == destination[1]; return x == destination[0] || y == destination[1];
} }
...@@ -41,20 +43,27 @@ public class Rook extends Nut { ...@@ -41,20 +43,27 @@ public class Rook extends Nut {
int t; int t;
if (destination[1] - y > 0) t = 1; if (destination[1] - y > 0) t = 1;
else t = -1; else t = -1;
for (int i = y ; i != destination[1]; i += t) for (int i = y + t; i > -1 && i < 8 && i != destination[1]; i += t)
if (ground[x][i] != 0) if (ground[x][i] != 0)
return false; return false;
} else { } else {
int t; int t;
if (destination[0] - x > 0) t = 1; if (destination[0] - x > 0) t = 1;
else t = -1; else t = -1;
for (int i = x ; i != destination[0]; i += t) for (int i = x + t; i > -1 && i < 8 && i != destination[0]; i += t)
if (ground[i][y] != 0) if (ground[i][y] != 0)
return false; return false;
} }
return true; return true;
} }
public int[][] getMoves(int[][] ground) {
int[][] moves = new int[1][2];
moves[0][0] = -1;
return moves;
} }
}
...@@ -5,10 +5,17 @@ public class Play { ...@@ -5,10 +5,17 @@ public class Play {
Game game = new Game(); Game game = new Game();
game.startGame(); game.startGame();
boolean continue_ = true; boolean continue_;
do { do {
continue_ = game.play(game.getInput()); continue_ = game.play(game.getInput());
game.showPlayGround(); game.getPlayGround().show();
if (game.getPlayGround().getCheck()) {
if (game.getTurn() == 1)
System.out.println("CHECKed!\nbe careful WHITEl!\n");
else
System.out.println("CHECKed!\nbe careful BLACK!\n");
}
continue_ = !game.isEnd();
}while (continue_); }while (continue_);
} }
......
...@@ -39,13 +39,13 @@ public class PlayGround { ...@@ -39,13 +39,13 @@ public class PlayGround {
bRooks[0] = new Rook(-9); bRooks[0] = new Rook(-9);
nutsID.put(bRooks[0], -9); nutsID.put(bRooks[0], -9);
bRooks[1] = new Rook(-10); bRooks[1] = new Rook(-10);
nutsID.put(wRooks[1], -10); nutsID.put(bRooks[1], -10);
wKnights = new Knight[2]; wKnights = new Knight[2];
wKnights[0] = new Knight(11); wKnights[0] = new Knight(11);
nutsID.put(wKnights[0], 11); nutsID.put(wKnights[0], 11);
wKnights[1] = new Knight(12); wKnights[1] = new Knight(12);
nutsID.put(wRooks[1], 12); nutsID.put(wKnights[1], 12);
bKnights = new Knight[2]; bKnights = new Knight[2];
bKnights[0] = new Knight(-11); bKnights[0] = new Knight(-11);
...@@ -61,7 +61,7 @@ public class PlayGround { ...@@ -61,7 +61,7 @@ public class PlayGround {
bBishops = new Bishop[2]; bBishops = new Bishop[2];
bBishops[0] = new Bishop(-13); bBishops[0] = new Bishop(-13);
nutsID.put(bBishops[1], -13); nutsID.put(bBishops[0], -13);
bBishops[1] = new Bishop(-14); bBishops[1] = new Bishop(-14);
nutsID.put(bBishops[1], -14); nutsID.put(bBishops[1], -14);
...@@ -96,7 +96,6 @@ public class PlayGround { ...@@ -96,7 +96,6 @@ public class PlayGround {
ground[0][6] = -11; ground[0][6] = -11;
ground[0][7] = -9; ground[0][7] = -9;
ground[7][0] = 9; ground[7][0] = 9;
ground[7][1] = 11; ground[7][1] = 11;
ground[7][2] = 13; ground[7][2] = 13;
...@@ -120,11 +119,10 @@ public class PlayGround { ...@@ -120,11 +119,10 @@ public class PlayGround {
return 2; return 2;
//4 check //4 check
ifMoveWillCheck(getDestination((string[0].charAt(0)) , (short) (string[0].charAt(1))), getDestination(string[1].charAt(0), (short) (string[1].charAt(1))), turn); ifMoveWillCheck(getDestination((string[0].charAt(0)), (short) (string[0].charAt(1))), getDestination(string[1].charAt(0), (short) (string[1].charAt(1))), turn);
if (getCheck()) return 4; if (getCheck()) return 4;
//5 on mohre on harekato nemitone bere //5 on mohre on harekato nemitone bere
for (Nut i : nutsID.keySet()) { for (Nut i : nutsID.keySet()) {
if (nutsID.get(i) == id1) { if (nutsID.get(i) == id1) {
...@@ -153,8 +151,19 @@ public class PlayGround { ...@@ -153,8 +151,19 @@ public class PlayGround {
int id1 = ground[origin[0]][origin[1]], id2 = ground[destination[0]][destination[1]]; int id1 = ground[origin[0]][origin[1]], id2 = ground[destination[0]][destination[1]];
ground[origin[0]][origin[1]] = 0; ground[origin[0]][origin[1]] = 0;
ground[destination[0]][destination[1]] = id1; ground[destination[0]][destination[1]] = id1;
if (id1 == 16)
wKing.move(destination, ground);
else if (id1 == -16)
bKing.move(destination, ground);
isCheck(turn); isCheck(turn);
getCheck();
if (id1 == 16)
wKing.move(origin, ground);
else if (id1 == -16)
bKing.move(origin, ground);
ground[origin[0]][origin[1]] = id1; ground[origin[0]][origin[1]] = id1;
ground[destination[0]][destination[1]] = id2; ground[destination[0]][destination[1]] = id2;
} }
...@@ -224,12 +233,12 @@ public class PlayGround { ...@@ -224,12 +233,12 @@ public class PlayGround {
(wKnights[0].canMove(bKing.getXY())) || (wKnights[0].canMove(bKing.getXY())) ||
(wKnights[1].canMove(bKing.getXY())) || (wKnights[1].canMove(bKing.getXY())) ||
(wBishops[0].canMove(bKing.getXY()) && wBishops[0].canRoute(bKing.getXY(), ground)) || (wBishops[0].canMove(bKing.getXY()) && wBishops[0].canRoute(bKing.getXY(), ground)) ||
(wBishops[1].canMove(bKing.getXY()) && (wBishops[1].canRoute(bKing.getXY(), ground)))) { (wBishops[1].canMove(bKing.getXY()) && wBishops[1].canRoute(bKing.getXY(), ground))) {
check = true; check = true;
return; return;
} }
check = false;
} }
check = false;
} }
public boolean getCheck() { public boolean getCheck() {
...@@ -321,4 +330,59 @@ public class PlayGround { ...@@ -321,4 +330,59 @@ public class PlayGround {
} }
} public boolean canKingScape(int turn) {
\ No newline at end of file boolean previousCheck = getCheck();
if (turn > 0) {
int[][] moves = wKing.getMoves(ground);
for (int i = 0; moves[i][0] != -1; i++) {
ifMoveWillCheck(wKing.getXY(), moves[i], turn);
if (!getCheck()) {
check = previousCheck;
return true;
}
}
for (Nut n : nutsID.keySet())
if (nutsID.get(n) > 0) {
moves = n.getMoves(ground);
for (int i = 0; moves[i][0] != -1; i++) {
ifMoveWillCheck(wKing.getXY(), moves[i], turn);
if (!getCheck()) {
check = previousCheck;
return true;
}
}
}
} else {
int[][] moves = bKing.getMoves(ground);
for (int i = 0; moves[i][0] != -1; i++) {
ifMoveWillCheck(bKing.getXY(), moves[i], turn);
if (!getCheck()) {
check = previousCheck;
return true;
}
}
for (Nut n : nutsID.keySet())
if (nutsID.get(n) > 0) {
moves = n.getMoves(ground);
for (int i = 0; moves[i][0] != -1; i++) {
ifMoveWillCheck(bKing.getXY(), moves[i], turn);
if (!getCheck()) {
check = previousCheck;
return true;
}
}
}
}
check = previousCheck;
return false;
}
}
Markdown is supported
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