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

Final Update

parent 396290a7
package lab.game;
import javax.xml.stream.events.StartDocument;
import java.util.*;
public class Bishop extends Piece{
......@@ -22,20 +23,20 @@ public class Bishop extends Piece{
return false;
}
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = 0;
int yShift = 0;
if( this.x - x != 0 ){
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
if( p.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;
while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.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) ){
return false;
}
......@@ -44,12 +45,12 @@ public class Bishop extends Piece{
return true;
}
public static boolean checkWay(Piece p, ArrayList<Piece> pieces, char x, int y){
int xShift = (x - p.x) / Math.abs(x - p.x);;
int yShift = (y - p.y) / Math.abs(y - p.y);
public boolean checkWay(ArrayList<Piece> pieces, char x, int y){
int xShift = (x - this.x) / Math.abs(x - this.x);
int yShift = (y - this.y) / Math.abs(y - this.y);
int i = 1;
while( x != (char)(p.x + i * xShift) && y != p.y + i * yShift ){
if( checkTaken(pieces, (char)(p.x + i * xShift), p.y + i * yShift) ){
while( x != (char)(this.x + i * xShift) && y != this.y + i * yShift ){
if( checkTaken(pieces, (char)(this.x + i * xShift), this.y + i * yShift) ){
return false;
}
......
......@@ -4,6 +4,8 @@ import java.util.*;
public class Board{
private ArrayList<Piece> whitePieces;
private ArrayList<Piece> blackPieces;
public int whiteScore = 0;
public int blackScore = 0;
public Board(){
this.whitePieces = new ArrayList<Piece>();
......@@ -139,34 +141,64 @@ public class Board{
System.out.println("WN: White Knight\t|\tBN: Black Knight");
System.out.println("WR: White Rook \t|\tBR: Black Rook");
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(){
for(int j = 9; j > 0; j--){
for(char i = 'A'; i < 'J'; i++){
if( j == 9 ){
if( i - 'A' > 0 ){
public void printBoard(String player){
if( player.equals("W") ){
System.out.print((char)(i - 1) + " ");
for(int j = 9; j > 0; j--){
for(char i = 'A'; i < 'J'; i++){
if( j == 9 ){
if( i - 'A' > 0 ){
System.out.print((char)(i - 1) + " ");
}else{
System.out.print(" ");
}
}else{
System.out.print(" ");
if( i == 'A' ){
System.out.print(j + " ");
}else{// Main Clause
char mapedI = (char)(i - 1);
if( isTaken(j, mapedI) ){
System.out.print(takenBy(j, mapedI).getName() + " ");
}else{
System.out.print("- ");
}
}
}
}else{
if( i == 'A' ){
System.out.print(j + " ");
}else{// Main Clause
char mapedI = (char)(i - 1);
if( isTaken(j, mapedI) ){
}
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(takenBy(j, mapedI).getName() + " ");
System.out.print(" ");
}else{
System.out.print("- ");
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();
}
System.out.println();
}
}
......@@ -217,13 +249,18 @@ public class Board{
if( found.checkWay(all, toArray[1], toArray[0] - '0') ){
if( attack != null ){
if( (found instanceof Pawn) && found.crossMove(toArray[1], toArray[0] - '0') && attack == null ){
return false;
}else{
if( attack != null ){
enemy.remove(attack);
enemy.remove(attack);
}
found.setY(toArray[0] - '0');
found.setX(toArray[1]);
return true;
}
found.setY(toArray[0] - '0');
found.setX(toArray[1]);
return true;
}else{
return false;
}
......@@ -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
int kingY = 0;
char kingX = 'A';
......@@ -258,15 +295,68 @@ public class Board{
kingX = p.x;
}
}
for(Piece p : oponent){
for(Piece p : enemy){
if( p.canMove(kingX, kingY) ){ // Check if move is valid
if( p.checkWay(all, kingX, kingY) ){
System.out.println(p.getName());
return true;
}
}
}
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{
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){
if( Math.abs(this.x - x) == 1 && Math.abs(this.y - y) == 1 ){
......
......@@ -57,6 +57,10 @@ public abstract class Piece{
return false;
}
public boolean crossMove(char x, int y){
return false;
}
abstract boolean canMove(char x, int y);
abstract boolean checkWay(ArrayList<Piece> pieces, char x, int y);
......
......@@ -15,7 +15,7 @@ public class Queen extends Piece{
}
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;
}
......
......@@ -56,13 +56,14 @@ public class Rook extends Piece{
yShift = (y - p.y) / Math.abs(y - p.y);
}
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) ){
return false;
}
i++;
}
System.out.println("Fuck1");
return true;
}
}
......@@ -9,12 +9,16 @@ public class Observ{
Scanner sc = new Scanner(System.in);
Board mainBoard = new Board();
mainBoard.initPieces();
// mainBoard.printHelp();
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
mainBoard.printHelp();
sc.nextLine();
String player = "W";
for(int i = 0; i < 500; i++){
String checkMate = null;
boolean play = true;
while(play){
while(true){
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
mainBoard.printBoard();
mainBoard.printBoard(player);
if( player.equals("W") ){
System.out.print("White ");
......@@ -23,10 +27,18 @@ public class Observ{
}
System.out.print("Player Move: ");
String input = sc.nextLine();
// Check if input is valid
if( mainBoard.move(input.split(" ")[0], input.split(" ")[1], player) ){
if( input.split(" ").length == 2 ){
break;
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;
}
}else{
play = false;
}
}
}
if( player.equals("W") ){// Change PLayer
......@@ -36,6 +48,15 @@ public class Observ{
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();
}
}
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