Commit f9c7099c authored by hosein's avatar hosein

game -> updated

parent 5eca5a8c
package pack; package pack;
import java.util.Scanner;
public class Game { public class Game {
private PlayGround playGround;
private short turn;
private String[] move;
Game() {
playGround = new PlayGround();
turn = 1;
move = new String[2];
}
public boolean play(String[] move) {
if (move != null) playGround.moveNut(move);
else {
if ((turn != 1))
System.out.println("\nCongratulation!\nWINNER IS _WHITE_ :)");
else
System.out.println("\nCongratulation!\nWINNER IS _BLACK_ :)");
return false;
}
System.out.println(" playGround.getCheck() == "+playGround.getCheck());
playGround.isCheck(turn);
System.out.println(" playGround.getCheck() == "+playGround.getCheck());
if (playGround.getCheck()) {
if (turn == 1)
System.out.println("White has been CHECKed!\nbe careful!");
else
System.out.println("Black has been CHECKed!\nbe careful!");
}
changeTurn();
return true;
}
private void changeTurn() {
turn *= -1;
}
public String[] getInput() {
if (turn == 1)
System.out.println("White turn:");
else
System.out.println("Black turn:");
String[] strings = new String[2];
Scanner input = new Scanner(System.in);
String userInput;
boolean break_ = false, continue_;
do {
if (turn == 1)
System.out.print("WHITE>");
else
System.out.print("BLACK>");
userInput = input.nextLine();
userInput = userInput.trim().toLowerCase();
continue_ = false;
if (userInput.equals("help")) {
System.out.println("First Enter nut location and then enter destination.\nfor example: 'e2 b5' for move the nut in e2 to b5\ncapitals characters are white and others are black\np = pawn, r = rook, k = knight\nb = bishop, q = queen, x = king");
} else if (userInput.equals("resign")) {
return null;
} else {
if (userInput.length() < 5) System.out.println("Unexpected input!\ntry again");
else {
char[] ch = new char[2];
ch[0] = userInput.charAt(0);
ch[1] = userInput.charAt(1);
int i = 2;
while (userInput.charAt(i) == 32) {
i++;
}
if (i == 2) System.out.println("Unexpected input!\ntry again");
else {
strings[0] = "" + ch[0] + ch[1];
ch[0] = userInput.charAt(i++);
ch[1] = userInput.charAt(i);
if (i + 1 != userInput.length()) System.out.println("Unexpected input!\ntry again");
else {
strings[1] = "" + ch[0] + ch[1];
continue_ = true;
}
}
}
}
if (continue_) {
if (!(strings[0].charAt(0) > 96 && strings[0].charAt(0) < 105) || !(strings[1].charAt(0) > 96 && strings[1].charAt(0) < 105))
System.out.println("Unexpected input!\nplease use a-h characters!");
else if (strings[0].charAt(1) > 56 || strings[0].charAt(1) < 49 || strings[1].charAt(1) > 56 || strings[1].charAt(1) < 49)
System.out.println("Unexpected input!\nplease use 1-8 numbers!");
else
switch (playGround.canMove(strings, turn)) {
case 1: // can move
break_ = true;
break;
case 2: // destination is not empty
System.out.println("Destination is not empty!\nchoose another destination");
break;
case 3: // the route can not be traversed
System.out.println("The route can not be traversed!\nchoose another destination");
break;
case 4: // cant move because of check or if nut moves, king will be check
System.out.println("You have been CHECKed or You will be CHECK if move that nut!\ntry another move to save your king");
break;
case 6: // if origin is empty
System.out.println("There isn’t any nut in Origin!\ntry another move");
break;
case 5: // if the nut cant move like that
System.out.println("That nut cant move like that!\ntry another move");
break;
case 7:
System.out.println("It’s " + ((turn == 1) ? "white" : "black") + " move!\ntry again");
break;
}
}
} while (!break_);
return strings;
}
public void startGame() {
System.out.println("WELCOME!\nFor Move: Enter nut location and then enter destination.\nfor example: 'a1 h8' for move the nut in a1 to h8\nFor Get Help: Enter 'help'\nand if a player wants to resign enter 'resign'");
showPlayGround();
System.out.print("Ready?\nPress Enter to Start!");
Scanner i = new Scanner(System.in);
i.nextLine();
}
public void showPlayGround() {
System.out.print(" a b c d e f g h\n _ _ _ _ _ _ _ _\n");
int[][] g = PlayGround.getGround();
for (int i = 0, j; i < 8; i++) {
System.out.print((8 - i) + " ");
for (j = 0; j < 8; j++) {
switch (g[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(" ¯ ¯ ¯ ¯ ¯ ¯ ¯ ¯ ");
}
} }
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