Commit 755a7ca0 authored by Omid Sayfun's avatar Omid Sayfun

Added Move Validaity

parent 41a183a5
public class Bishop extends Piece{ public class Bishop extends Piece{
public Bishop(char x, int y, String name){ public Bishop(char x, int y, boolean color, String name){
super(x, y, name); super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) / Math.abs(this.y - y) == 1 ){
return true;
}
return false;
}
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( Math.abs(p.x - x) / Math.abs(p.y - y) == 1 ){
return true;
}
return false;
} }
} }
...@@ -13,56 +13,56 @@ public class Board{ ...@@ -13,56 +13,56 @@ public class Board{
// White Players // White Players
// Pawns // Pawns
for(char ch = 'A'; ch < 'I'; ch++){ for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 2, "WP"); Pawn newPawn = new Pawn(ch, 2, true, "WP");
this.whitePieces.add(newPawn); this.whitePieces.add(newPawn);
} }
// Rook // Rook
for(char ch = 'A'; ch < 'I'; ch += 7){ for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 1, "WR"); Rook newRook = new Rook(ch, 1, true, "WR");
this.whitePieces.add(newRook); this.whitePieces.add(newRook);
} }
// Knight // Knight
for(char ch = 'B'; ch < 'H'; ch += 5){ for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 1, "WN"); Knight newKnight = new Knight(ch, 1, true, "WN");
this.whitePieces.add(newKnight); this.whitePieces.add(newKnight);
} }
// Bishop // Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){ for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 1, "WB"); Bishop newBishop = new Bishop(ch, 1, true, "WB");
this.whitePieces.add(newBishop); this.whitePieces.add(newBishop);
} }
// Queen // Queen
Queen whiteQueen = new Queen('E', 1, "WQ"); Queen whiteQueen = new Queen('E', 1, true, "WQ");
this.whitePieces.add(whiteQueen); this.whitePieces.add(whiteQueen);
// King // King
King whiteKing = new King('D', 1, "WK"); King whiteKing = new King('D', 1, true, "WK");
this.whitePieces.add(whiteKing); this.whitePieces.add(whiteKing);
// Black Players // Black Players
// Pawns // Pawns
for(char ch = 'A'; ch < 'I'; ch++){ for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 7, "BP"); Pawn newPawn = new Pawn(ch, 7, false, "BP");
this.blackPieces.add(newPawn); this.blackPieces.add(newPawn);
} }
// Rook // Rook
for(char ch = 'A'; ch < 'I'; ch += 7){ for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 8, "BR"); Rook newRook = new Rook(ch, 8, false, "BR");
this.blackPieces.add(newRook); this.blackPieces.add(newRook);
} }
// Knight // Knight
for(char ch = 'B'; ch < 'H'; ch += 5){ for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 8, "BN"); Knight newKnight = new Knight(ch, 8, false, "BN");
this.blackPieces.add(newKnight); this.blackPieces.add(newKnight);
} }
// Bishop // Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){ for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 8, "BB"); Bishop newBishop = new Bishop(ch, 8, false, "BB");
this.blackPieces.add(newBishop); this.blackPieces.add(newBishop);
} }
// Queen // Queen
Queen blackQueen = new Queen('E', 8, "BQ"); Queen blackQueen = new Queen('E', 8, false, "BQ");
this.blackPieces.add(blackQueen); this.blackPieces.add(blackQueen);
// King // King
King blackKing = new King('D', 8, "BK"); King blackKing = new King('D', 8, false, "BK");
this.blackPieces.add(blackKing); this.blackPieces.add(blackKing);
} }
...@@ -138,6 +138,7 @@ public class Board{ ...@@ -138,6 +138,7 @@ 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)");
} }
public void printBoard(){ public void printBoard(){
...@@ -193,11 +194,18 @@ public class Board{ ...@@ -193,11 +194,18 @@ public class Board{
if( found && !toBusy ){// Found and to is not busy if( found && !toBusy ){// Found and to is not busy
for(Piece p : selector){ for(Piece p : selector){
if( p.getY() == fromArray[0] - '0' && p.getX() == fromArray[1] ){ if( p.getY() == fromArray[0] - '0' && p.getX() == fromArray[1] ){ // Find Piece in select
// To Do: Check if pathway is busy
// To Do: Check if king is checked and have to move king
if( p.canMove(toArray[1], toArray[0] - '0') ){ // Check if move is valid
p.setY(toArray[0] - '0'); p.setY(toArray[0] - '0');
p.setX(toArray[1]); p.setX(toArray[1]);
return true; return true;
}else{
return false;
}
} }
} }
return false; return false;
......
public class King extends Piece{ public class King extends Piece{
public King(char x, int y, String name){ public King(char x, int y, boolean color, String name){
super(x, y, name); super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Math.abs(this.x - x) < 2 && Math.abs(this.y - y) < 2 ){
return true;
}
return false;
} }
} }
public class Knight extends Piece{ public class Knight extends Piece{
public Knight(char x, int y, String name){ public Knight(char x, int y, boolean color, String name){
super(x, y, name); super(x, y, color, name);
}
public boolean canMove(char x, int y){
int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int Y[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
for (int i = 0; i < 8; i++) {
if( y == this.y + Y[i] && x == (char)(this.x + X[i]) ){
return true;
}
}
return false;
} }
} }
import java.util.*; import java.util.*;
import java.util.concurrent.*;
import java.io.IOException;
public class Main{ public class Main{
public static void main(String[] args){ public static void main(String[] args) throws IOException, InterruptedException{
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(); // mainBoard.printHelp();
mainBoard.move("8D", "3A", "B"); String player = "W";
for(int i = 0; i < 500; i++){
while(true){
new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
mainBoard.printBoard(); mainBoard.printBoard();
if( player.equals("W") ){
System.out.print("White ");
}else{
System.out.print("Black ");
}
System.out.print("Player Move: ");
String input = sc.nextLine();
if( mainBoard.move(input.split(" ")[0], input.split(" ")[1], player) ){
break;
}
}
if( player.equals("W") ){// Change PLayer
player = "B";
}else{
player = "W";
}
}
sc.close(); sc.close();
} }
} }
public class Pawn extends Piece{ public class Pawn extends Piece{
private boolean firstMove; private boolean firstMove;
public Pawn(char x, int y, String name){ public Pawn(char x, int y, boolean color, String name){
super(x, y, name); super(x, y, color, name);
this.firstMove = true; this.firstMove = true;
} }
...@@ -12,4 +12,26 @@ public class Pawn extends Piece{ ...@@ -12,4 +12,26 @@ public class Pawn extends Piece{
public boolean getFirstMove(){ public boolean getFirstMove(){
return this.firstMove; return this.firstMove;
} }
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 ){
if( Math.abs(this.y - y) == 1 || (this.firstMove && Math.abs(this.y - y) == 2) ){
if( (this.color && y - this.y > 0) || (!this.color && this.y - y > 0) ){ // Moving Backward
if( this.firstMove ){
this.firstMove = false;
}
return true;
}else{
return false;
}
}else{
return false;
}
}
return false;
}
} }
public abstract class Piece{ public abstract class Piece{
private int y; protected int y;
private char x; protected char x;
protected boolean color;
private String name; private String name;
public Piece(char x, int y, String name){ public Piece(char x, int y, boolean color, String name){
setX(x); setX(x);
setY(y); setY(y);
setColor(color);
setName(name); setName(name);
} }
...@@ -21,6 +23,10 @@ public abstract class Piece{ ...@@ -21,6 +23,10 @@ public abstract class Piece{
this.name = name; this.name = name;
} }
public void setColor(boolean color){
this.color = color;
}
public int getY(){ public int getY(){
return this.y; return this.y;
} }
...@@ -32,4 +38,10 @@ public abstract class Piece{ ...@@ -32,4 +38,10 @@ public abstract class Piece{
public String getName(){ public String getName(){
return this.name; return this.name;
} }
public boolean getColor(){
return this.color;
}
abstract boolean canMove(char x, int y);
} }
public class Queen extends Piece{ public class Queen extends Piece{
public Queen(char x, int y, String name){ public Queen(char x, int y, boolean color, String name){
super(x, y, name); super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( Rook.canMove(this, x, y) || Bishop.canMove(this, x, y) ){
return true;
}
return false;
} }
} }
public class Rook extends Piece{ public class Rook extends Piece{
public Rook(char x, int y, String name){ public Rook(char x, int y, boolean color, String name){
super(x, y, name); super(x, y, color, name);
}
public boolean canMove(char x, int y){ // Ignore the presence of other pieces
if( this.x - x == 0 || this.y - y == 0 ){
return true;
}
return false;
}
public static boolean canMove(Piece p, char x, int y){ // Ignore the presence of other pieces
if( p.x - x == 0 || p.y - y == 0 ){
return true;
}
return false;
} }
} }
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