Commit 202c418a authored by hosein's avatar hosein

playground -> canMove added

parent d12b3f86
package pack;
public class Game {
}
package pack.Nut;
public class Soldier {
}
package pack.Nuts;
public class Bishop {
}
package pack.Nuts;
public class King {
}
package pack.Nuts;
public class Knight {
}
package pack.Nuts;
public abstract class Nut {
public abstract void move();
}
package pack.Nuts;
public class Pawn {
}
package pack.Nuts;
public class Queen {
}
package pack.Nuts;
public class Rook {
}
package pack;
public class Play {
}
package pack; package pack;
import pack.Nuts.*;
import java.util.HashMap;
public class PlayGround { public class PlayGround {
private int[][] ground;
private HashMap<Nut, Integer> nutsID;
private Pawn[] wPawns, bPawns;
private Rook[] wRooks, bRooks;
private Knight[] wKnights, bKnights;
private Bishop[] wBishops, bBishops;
private Queen wQueen, bQueen;
private King wKing, bKing;
public PlayGround() {
nutsID = new HashMap<>();
wPawns = new Pawn[8];
bPawns = new Pawn[8];
for (int i = 0; i < 8; i++) {
wPawns[i] = new Pawn(i + 1);
nutsID.put(wPawns[i], i + 1);
bPawns[i] = new Pawn((i + 1) * -1);
nutsID.put(bPawns[i], (i + 1) * -1);
}
wRooks = new Rook[2];
wRooks[0] = new Rook(9);
nutsID.put(wRooks[0], 9);
wRooks[1] = new Rook(10);
nutsID.put(wRooks[1], 10);
bRooks = new Rook[2];
bRooks[0] = new Rook(-9);
nutsID.put(bRooks[0], -9);
bRooks[1] = new Rook(-10);
nutsID.put(wRooks[1], -10);
wKnights = new Knight[2];
wKnights[0] = new Knight(11);
nutsID.put(wKnights[0], 11);
wKnights[1] = new Knight(12);
nutsID.put(wRooks[1], 12);
bKnights = new Knight[2];
bKnights[0] = new Knight(-11);
nutsID.put(bKnights[0], -11);
bKnights[1] = new Knight(-12);
nutsID.put(bKnights[1], -12);
wBishops = new Bishop[2];
wBishops[0] = new Bishop(13);
nutsID.put(wBishops[0], 13);
wBishops[1] = new Bishop(14);
nutsID.put(wBishops[1], 14);
bBishops = new Bishop[2];
bBishops[0] = new Bishop(-13);
nutsID.put(bBishops[1], -13);
bBishops[1] = new Bishop(-14);
nutsID.put(bBishops[1], -14);
wQueen = new Queen(15);
nutsID.put(wQueen, 15);
bQueen = new Queen(-15);
nutsID.put(bQueen, -15);
wKing = new King(16);
nutsID.put(wKing, 16);
bKing = new King(-16);
nutsID.put(bKing, -16);
ground = new int[8][8];
for (int i = 2, j; i < 6; i++)
for (j = 0; j < 8; j++)
ground[i][j] = 0;
for (int i = 0; i < 8; i++) // white pawns
ground[6][i] = i + 1;
for (int i = 7; i > -1; i--) // black pawns
ground[1][i] = i - 8;
ground[0][0] = -10;
ground[0][1] = -12;
ground[0][2] = -14;
ground[0][3] = -16;
ground[0][4] = -15;
ground[0][5] = -13;
ground[0][6] = -11;
ground[0][7] = -9;
ground[7][0] = 9;
ground[7][1] = 11;
ground[7][2] = 13;
ground[7][3] = 15;
ground[7][4] = 16;
ground[7][5] = 14;
ground[7][6] = 12;
ground[7][7] = 10;
}
// TODO 2 show mokhtalef yeki avale bazi ba 1-8 + a-h va yeki bedone ina
public void showPlayGround() {
System.out.print(" 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(" ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ");
}
public short canMove(String[] string, int turn) {
if (getNutID(string[0].charAt(0), (short) (string[0].charAt(1))) == 0) //6 mabda mohree nist
return 6;
if (getNutID(string[0].charAt(0), (short) (string[0].charAt(1))) * getNutID(string[1].charAt(0), (short) (string[1].charAt(1))) > 0) //2 destination is not empty
return 2;
// Todo shayad bekhad on mohre ro bezare jelosh
boolean check; //4 cant move because of check
if (turn == 1)
check = wKing.isCheck();
else
check = bKing.isCheck();
if (check) return 4;
Nut j = null; //5 if nut moves, king will be check
for (Nut i : nutsID.keySet()) {
j = i;
if (nutsID.get(i) == getNutID(string[0].charAt(0), (short) (string[0].charAt(1)))) {
i.move(getDestination(string[1].charAt(0), (short) (string[1].charAt(1))));
break;
}
}
if (turn == 1)
check = wKing.isCheck();
else
check = bKing.isCheck();
//todo reverse sarbaz mojaze
j.move(getDestination(string[0].charAt(0), (short) (string[0].charAt(1))));
if (check) return 5;
// on mohre on harekato nemitone bere
for (Nut i : nutsID.keySet()) {
if (nutsID.get(i) == getNutID(string[0].charAt(0), (short) (string[0].charAt(1)))) {
if (!i.canMove(getDestination(string[1].charAt(0), (short) (string[1].charAt(1))))) return 7;
else break;
}
}
// the route can not be traversed
for (Nut i : nutsID.keySet()) {
if (nutsID.get(i) == getNutID(string[0].charAt(0), (short) (string[0].charAt(1)))) {
if (!i.canRoute()) return 3;
else break;
}
}
return 1; //1 can move
}
public void moveNut(String[] string) {
for (Nut i : nutsID.keySet()) {
if (nutsID.get(i) == getNutID(string[0].charAt(0), (short) (string[0].charAt(1)))) {
i.move(getDestination(string[1].charAt(0), (short) (string[1].charAt(1))));
break;
}
}
}
public int getNutID(char c, short s) {
return ground[8 - s][(int) (c) - 97];
}
public int getNutID(int i, int j) {
return ground[i][j];
}
public int[] getDestination(char c, short s) {
int[] r = new int[2];
r[0] = 8 - s;
r[1] = (int) c - 97;
return r;
}
} }
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