Commit 4371d38f authored by Omid Sayfun's avatar Omid Sayfun

Final Update

parent 396290a7
package lab.game; package lab.game;
import javax.xml.stream.events.StartDocument;
import java.util.*; import java.util.*;
public class Bishop extends Piece{ public class Bishop extends Piece{
...@@ -22,20 +23,20 @@ public class Bishop extends Piece{ ...@@ -22,20 +23,20 @@ public class Bishop extends Piece{
return false; return false;
} }
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
int xShift = 0; if( p.x - x == 0 ){
int yShift = 0;
if( this.x - x != 0 ){
xShift = (x - this.x) / Math.abs(x - this.x); return false;
} }
if( this.y - y != 0 ){ if( p.y - y == 0 ){
yShift = (y - this.y) / Math.abs(y - this.y); return false;
} }
int xShift = (x - p.x) / Math.abs(x - p.x);
int yShift = (y - p.y) / Math.abs(y - p.y);
int i = 1; int i = 1;
while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){ while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){ if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
return false; return false;
} }
...@@ -44,12 +45,12 @@ public class Bishop extends Piece{ ...@@ -44,12 +45,12 @@ public class Bishop extends Piece{
return true; return true;
} }
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = (x - p.x) / Math.abs(x - p.x);; int xShift = (x - this.x) / Math.abs(x - this.x);
int yShift = (y - p.y) / Math.abs(y - p.y); int yShift = (y - this.y) / Math.abs(y - this.y);
int i = 1; int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){ while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){ if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){
return false; return false;
} }
......
...@@ -4,6 +4,8 @@ import java.util.*; ...@@ -4,6 +4,8 @@ import java.util.*;
public class Board{ public class Board{
private ArrayList<Piece> whitePieces; private ArrayList<Piece> whitePieces;
private ArrayList<Piece> blackPieces; private ArrayList<Piece> blackPieces;
public int whiteScore = 0;
public int blackScore = 0;
public Board(){ public Board(){
this.whitePieces = new ArrayList<Piece>(); this.whitePieces = new ArrayList<Piece>();
...@@ -139,10 +141,13 @@ public class Board{ ...@@ -139,10 +141,13 @@ public class Board{
System.out.println("WN: White Knight\t|\tBN: Black Knight"); System.out.println("WN: White Knight\t|\tBN: Black Knight");
System.out.println("WR: White Rook \t|\tBR: Black Rook"); System.out.println("WR: White Rook \t|\tBR: Black Rook");
System.out.println("WP: White Pawn \t|\tBP: Black Pawn"); System.out.println("WP: White Pawn \t|\tBP: Black Pawn");
System.out.println("Acceptable Command: '2D 4D'(Move From 2D to 4D)"); System.out.println("Acceptable Command: 'D2 D4'(Move From 2D to 4D)");
System.out.println("Press any key to start the game...");
} }
public void printBoard(){ public void printBoard(String player){
if( player.equals("W") ){
for(int j = 9; j > 0; j--){ for(int j = 9; j > 0; j--){
for(char i = 'A'; i < 'J'; i++){ for(char i = 'A'; i < 'J'; i++){
if( j == 9 ){ if( j == 9 ){
...@@ -168,6 +173,33 @@ public class Board{ ...@@ -168,6 +173,33 @@ public class Board{
} }
System.out.println(); System.out.println();
} }
}else{
for(int j = 0; j < 9; j++){
for(char i = 'I'; i >= 'A'; i--){
if( j == 0 ){
if( i == 'I' ){
System.out.print(" ");
}else{
System.out.print((char)(i) + " ");
}
}else{
if( i == 'I' ){
System.out.print(j + " ");
}else{// Main Clause
char mapedI = (char)(i);
if( isTaken(j, mapedI) ){
System.out.print(takenBy(j, mapedI).getName() + " ");
}else{
System.out.print("- ");
}
}
}
}
System.out.println();
}
}
} }
public boolean move(String from, String to, String color){// Name Format: 1D public boolean move(String from, String to, String color){// Name Format: 1D
...@@ -217,6 +249,10 @@ public class Board{ ...@@ -217,6 +249,10 @@ public class Board{
if( found.checkWay(all, toArray[1], toArray[0] - '0') ){ if( found.checkWay(all, toArray[1], toArray[0] - '0') ){
if( (found instanceof Pawn) && found.crossMove(toArray[1], toArray[0] - '0') && attack == null ){
return false;
}else{
if( attack != null ){ if( attack != null ){
enemy.remove(attack); enemy.remove(attack);
...@@ -224,6 +260,7 @@ public class Board{ ...@@ -224,6 +260,7 @@ public class Board{
found.setY(toArray[0] - '0'); found.setY(toArray[0] - '0');
found.setX(toArray[1]); found.setX(toArray[1]);
return true; return true;
}
}else{ }else{
return false; return false;
} }
...@@ -247,7 +284,7 @@ public class Board{ ...@@ -247,7 +284,7 @@ public class Board{
} }
} }
public boolean kingCheck(ArrayList<Piece> all, ArrayList<Piece> base, ArrayList<Piece> oponent){ public boolean kingCheck(ArrayList<Piece> all, ArrayList<Piece> base, ArrayList<Piece> enemy){
// find king // find king
int kingY = 0; int kingY = 0;
char kingX = 'A'; char kingX = 'A';
...@@ -258,15 +295,68 @@ public class Board{ ...@@ -258,15 +295,68 @@ public class Board{
kingX = p.x; kingX = p.x;
} }
} }
for(Piece p : oponent){ for(Piece p : enemy){
if( p.canMove(kingX, kingY) ){ // Check if move is valid if( p.canMove(kingX, kingY) ){ // Check if move is valid
if( p.checkWay(all, kingX, kingY) ){ if( p.checkWay(all, kingX, kingY) ){
System.out.println(p.getName());
return true; return true;
} }
} }
} }
return false; return false;
} }
private boolean kingMate(ArrayList<Piece> all, ArrayList<Piece> base, ArrayList<Piece> enemy){
int kingY = 0;
char kingX = 'A';
for(Piece p : base){
if( p instanceof King){
kingY = p.y;
kingX = p.x;
}
}
int[] X = {1, 1, 1, 0, -1, -1, -1, 0};
int[] Y = {1, 0, -1, -1, -1, 0, 1, 1};
for(int i = 0; i < 8; i++){
char tempX = (char)(kingX + X[i]);
int tempY = kingY + Y[i];
boolean flag = false;
for(Piece p : enemy){
if( p.canMove(tempX, tempY) ){
if( p.checkWay(all, tempX, tempY) ){
flag = true;
}
}
}
if( !flag ){
return false;
}
}
return true;
}
public String checkMate(){
ArrayList<Piece> all = new ArrayList<Piece>();
for(Piece p : this.blackPieces){
all.add(p);
}
for(Piece p : this.whitePieces){
all.add(p);
}
if( kingMate(all, this.whitePieces, this.blackPieces) ){
return "B";
}else if ( kingMate(all, this.whitePieces, this.blackPieces) ){
return "W";
}else{
return null;
}
}
} }
...@@ -40,6 +40,15 @@ public class Pawn extends Piece{ ...@@ -40,6 +40,15 @@ public class Pawn extends Piece{
return false; return false;
} }
@Override
public boolean crossMove(char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
return true;
}
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){ if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
......
...@@ -57,6 +57,10 @@ public abstract class Piece{ ...@@ -57,6 +57,10 @@ public abstract class Piece{
return false; return false;
} }
public boolean crossMove(char x, int y){
return false;
}
abstract boolean canMove(char x, int y); abstract boolean canMove(char x, int y);
abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y); abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y);
......
...@@ -15,7 +15,7 @@ public class Queen extends Piece{ ...@@ -15,7 +15,7 @@ public class Queen extends Piece{
} }
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){ public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
if( Rook.checkWay(this, pieces, x, y) && Bishop.checkWay(this, pieces, x, y) ){ if( Rook.checkWay(this, pieces, x, y) || Bishop.checkWay(this, pieces, x, y) ){
return true; return true;
} }
......
...@@ -56,13 +56,14 @@ public class Rook extends Piece{ ...@@ -56,13 +56,14 @@ public class Rook extends Piece{
yShift = (y - p.y) / Math.abs(y - p.y); yShift = (y - p.y) / Math.abs(y - p.y);
} }
int i = 1; int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){ while( x != (char)(p.x + i * xShift) || y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){ if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
return false; return false;
} }
i++; i++;
} }
System.out.println("Fuck1");
return true; return true;
} }
} }
...@@ -9,12 +9,16 @@ public class Observ{ ...@@ -9,12 +9,16 @@ public class Observ{
Scanner sc = new Scanner(System.in); Scanner sc = new Scanner(System.in);
Board mainBoard = new Board(); Board mainBoard = new Board();
mainBoard.initPieces(); mainBoard.initPieces();
// mainBoard.printHelp(); new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
mainBoard.printHelp();
sc.nextLine();
String player = "W"; String player = "W";
for(int i = 0; i < 500; i++){ String checkMate = null;
boolean play = true;
while(play){
while(true){ while(true){
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
mainBoard.printBoard(); mainBoard.printBoard(player);
if( player.equals("W") ){ if( player.equals("W") ){
System.out.print("White "); System.out.print("White ");
...@@ -23,11 +27,19 @@ public class Observ{ ...@@ -23,11 +27,19 @@ public class Observ{
} }
System.out.print("Player Move: "); System.out.print("Player Move: ");
String input = sc.nextLine(); String input = sc.nextLine();
// Check if input is valid if( input.split(" ").length == 2 ){
if( mainBoard.move(input.split(" ")[0], input.split(" ")[1], player) ){
checkMate = mainBoard.checkMate();
if( checkMate == null ){
if( mainBoard.move(new StringBuilder(input.split(" ")[0]).reverse().toString(), new StringBuilder(input.split(" ")[1]).reverse().toString(), player) ){
break; break;
} }
}else{
play = false;
}
}
} }
if( player.equals("W") ){// Change PLayer if( player.equals("W") ){// Change PLayer
...@@ -36,6 +48,15 @@ public class Observ{ ...@@ -36,6 +48,15 @@ public class Observ{
player = "W"; player = "W";
} }
} }
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
System.out.println("\t The Game is over");
if( checkMate.equals("W") ){
System.out.print("White ");
}else{
System.out.print("Black ");
}
System.out.println("Player Won");
sc.close(); sc.close();
} }
} }
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