Commit 41971b18 authored by hosein's avatar hosein

playground -> show

parent 974a22f2
...@@ -13,9 +13,11 @@ public class PlayGround { ...@@ -13,9 +13,11 @@ public class PlayGround {
private Bishop[] wBishops, bBishops; private Bishop[] wBishops, bBishops;
private Queen wQueen, bQueen; private Queen wQueen, bQueen;
private King wKing, bKing; private King wKing, bKing;
public boolean check;
public PlayGround() { public PlayGround() {
nutsID = new HashMap<>(); nutsID = new HashMap<>();
check = false;
wPawns = new Pawn[8]; wPawns = new Pawn[8];
bPawns = new Pawn[8]; bPawns = new Pawn[8];
...@@ -88,8 +90,8 @@ public class PlayGround { ...@@ -88,8 +90,8 @@ public class PlayGround {
ground[0][0] = -10; ground[0][0] = -10;
ground[0][1] = -12; ground[0][1] = -12;
ground[0][2] = -14; ground[0][2] = -14;
ground[0][3] = -16; ground[0][3] = -15;
ground[0][4] = -15; ground[0][4] = -16;
ground[0][5] = -13; ground[0][5] = -13;
ground[0][6] = -11; ground[0][6] = -11;
ground[0][7] = -9; ground[0][7] = -9;
...@@ -117,23 +119,22 @@ public class PlayGround { ...@@ -117,23 +119,22 @@ public class PlayGround {
if ((Math.abs(id1) < 9 && string[0].charAt(0) == string[1].charAt(0) && id2 != 0) || (id1 * id2 > 0)) //2 check destination is empty or enemy nut if ((Math.abs(id1) < 9 && string[0].charAt(0) == string[1].charAt(0) && id2 != 0) || (id1 * id2 > 0)) //2 check destination is empty or enemy nut
return 2; return 2;
Nut j = null;
short check; //4 cant move because of check Nut j = null; //4 check
for (Nut i : nutsID.keySet()) { for (Nut i : nutsID.keySet()) {
j = i;
if (nutsID.get(i) == id1) { if (nutsID.get(i) == id1) {
i.move(getDestination(string[1].charAt(0), (short) (string[1].charAt(1)))); j = i;
i.move(getDestination(string[1].charAt(0), (short) (string[1].charAt(1))), ground);
break; break;
} }
} }
check = isCheck(); isCheck(turn);
j.move(getDestination(string[0].charAt(0), (short) (string[0].charAt(1)))); j.move(getDestination(string[0].charAt(0), (short) (string[0].charAt(1))), ground);
if (check != 0) return 4; if (getCheck()) return 4;
j = null;
// 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) {
if (!i.canMove(getDestination(string[1].charAt(0), (short) (string[1].charAt(1))))) return 5; if (!i.canMove(getDestination(string[1].charAt(0), (short) (string[1].charAt(1))))) return 5;
...@@ -154,26 +155,19 @@ public class PlayGround { ...@@ -154,26 +155,19 @@ public class PlayGround {
} }
} }
return 1; //1 can move return 1; //1 can move
} }
//todo first move pawn ke mitone 2 ta bere!
public void moveNut(String[] string) { public void moveNut(String[] string) {
int id2 = getNutID(string[1].charAt(0), (short) string[1].charAt(1)), id1 = getNutID(string[0].charAt(0), (short) string[0].charAt(1)); int id2 = getNutID(string[1].charAt(0), (short) string[1].charAt(1)), id1 = getNutID(string[0].charAt(0), (short) string[0].charAt(1));
if (id2 != 0) if (id2 != 0)
removeNut(id2); removeNut(id2);
for (Nut i : nutsID.keySet()) for (Nut i : nutsID.keySet())
if (nutsID.get(i) == getNutID(string[0].charAt(0), (short) (string[0].charAt(1)))) { if (nutsID.get(i) == id1) {
i.move(getDestination(string[1].charAt(0), (short) (string[1].charAt(1)))); i.move(getDestination(string[1].charAt(0), (short) (string[1].charAt(1))), ground);
break; break;
} }
ground[getDestination(string[0].charAt(0), (short) string[0].charAt(1))[0]][getDestination(string[0].charAt(0), (short) string[0].charAt(1))[1]] = 0;
ground[getDestination(string[1].charAt(0), (short) string[1].charAt(1))[0]][getDestination(string[1].charAt(0), (short) string[1].charAt(1))[1]] = id1;
} }
private void removeNut(int id) { private void removeNut(int id) {
...@@ -200,10 +194,131 @@ public class PlayGround { ...@@ -200,10 +194,131 @@ public class PlayGround {
return ground; return ground;
} }
public short isCheck() { public void isCheck(int turn) {
if (turn == 1) {
for (int i = 0; i < 8; i++)
if (bPawns[i].canCheck(wKing.getXY())) {
check = true;
return;
}
if ((bQueen.canMove(wKing.getXY()) && bQueen.canRoute(wKing.getXY(), ground)) ||
(bRooks[0].canMove(wKing.getXY()) && bRooks[0].canRoute(wKing.getXY(), ground)) ||
(bRooks[1].canMove(wKing.getXY()) && bRooks[1].canRoute(wKing.getXY(), ground)) ||
(bKnights[0].canMove(wKing.getXY()) && bKnights[0].canRoute(wKing.getXY(), ground)) ||
(bKnights[1].canMove(wKing.getXY()) && bKnights[1].canRoute(wKing.getXY(), ground)) ||
(bBishops[0].canMove(wKing.getXY()) && bBishops[0].canRoute(wKing.getXY(), ground)) ||
(bBishops[1].canMove(wKing.getXY()) && bBishops[1].canRoute(wKing.getXY(), ground))
) {
check = true;
return;
}
} else {
for (int i = 0; i < 8; i++)
if (wPawns[i].canCheck(bKing.getXY())) {
check = true;
return;
}
if ((wQueen.canMove(bKing.getXY()) && wQueen.canRoute(bKing.getXY(), ground)) ||
(wRooks[0].canMove(bKing.getXY()) && wRooks[0].canRoute(bKing.getXY(), ground)) ||
(wRooks[1].canMove(bKing.getXY()) && wRooks[1].canRoute(bKing.getXY(), ground)) ||
(wKnights[0].canMove(bKing.getXY()) && wKnights[0].canRoute(bKing.getXY(), ground)) ||
(wKnights[1].canMove(bKing.getXY()) && wKnights[1].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))
) {
check = true;
return;
}
}
check = false;
}
public boolean getCheck() {
return check;
}
public void show() {
System.out.print("\n\n a b c d e f g h\n _ _ _ _ _ _ _ _\n");
for (int i = 0, j; i < 8; i++) {
System.out.print((8 - i) + " ");
for (j = 0; j < 8; j++) {
switch (ground[i][j]) {
case 0:
System.out.print("| ");
break;
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
System.out.print("|P");
break;
case -1:
case -2:
case -3:
case -4:
case -5:
case -6:
case -7:
case -8:
System.out.print("|p");
break;
case 9:
case 10:
System.out.print("|R");
break;
case -9:
case -10:
System.out.print("|r");
break;
case 11:
case 12:
System.out.print("|K");
break;
case -11:
case -12:
System.out.print("|k");
break;
case 13:
case 14:
System.out.print("|B");
break;
case -13:
case -14:
System.out.print("|b");
break;
case 15:
System.out.print("|Q");
break;
case -15:
System.out.print("|q");
break;
case 16:
System.out.print("|X");
break;
case -16:
System.out.print("|x");
}
}
System.out.print("|\n");
}
System.out.println(" ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ");
return 0;
} }
} }
\ No newline at end of file
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