Commit 41a183a5 authored by Omid Sayfun's avatar Omid Sayfun

Print and Move Added

parent 39136163
public class Bishop extends Piece{
public Bishop(char x, int y, String name){
super(x, y, name);
}
}
import java.util.*;
public class Board{
private ArrayList<Piece> whitePieces;
private ArrayList<Piece> blackPieces;
public Board(){
this.whitePieces = new ArrayList<Piece>();
this.blackPieces = new ArrayList<Piece>();
}
public void initPieces(){
// White Players
// Pawns
for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 2, "WP");
this.whitePieces.add(newPawn);
}
// Rook
for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 1, "WR");
this.whitePieces.add(newRook);
}
// Knight
for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 1, "WN");
this.whitePieces.add(newKnight);
}
// Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 1, "WB");
this.whitePieces.add(newBishop);
}
// Queen
Queen whiteQueen = new Queen('E', 1, "WQ");
this.whitePieces.add(whiteQueen);
// King
King whiteKing = new King('D', 1, "WK");
this.whitePieces.add(whiteKing);
// Black Players
// Pawns
for(char ch = 'A'; ch < 'I'; ch++){
Pawn newPawn = new Pawn(ch, 7, "BP");
this.blackPieces.add(newPawn);
}
// Rook
for(char ch = 'A'; ch < 'I'; ch += 7){
Rook newRook = new Rook(ch, 8, "BR");
this.blackPieces.add(newRook);
}
// Knight
for(char ch = 'B'; ch < 'H'; ch += 5){
Knight newKnight = new Knight(ch, 8, "BN");
this.blackPieces.add(newKnight);
}
// Bishop
for(char ch = 'C'; ch < 'G'; ch += 3){
Bishop newBishop = new Bishop(ch, 8, "BB");
this.blackPieces.add(newBishop);
}
// Queen
Queen blackQueen = new Queen('E', 8, "BQ");
this.blackPieces.add(blackQueen);
// King
King blackKing = new King('D', 8, "BK");
this.blackPieces.add(blackKing);
}
public boolean isTakenByWhite(int y, char x){
boolean flag = false;
for( Piece p : whitePieces ){
if( p.getY() == y && p.getX() == x ){
flag = true;
break;
}
}
return flag;
}
public boolean isTakenByBlack(int y, char x){
boolean flag = false;
for( Piece p : blackPieces ){
if( p.getY() == y && p.getX() == x ){
flag = true;
break;
}
}
return flag;
}
public boolean isTaken(int y, char x){
boolean flag = false;
flag = isTakenByWhite(y, x);
if( !flag )
flag = isTakenByBlack(y, x);
return flag;
}
public Piece takenByWhite(int y, char x){
Piece newPiece = null;
for( Piece p : whitePieces ){
if( p.getY() == y && p.getX() == x ){
newPiece = p;
break;
}
}
return newPiece;
}
public Piece takenByBlack(int y, char x){
Piece newPiece = null;
for( Piece p : blackPieces ){
if( p.getY() == y && p.getX() == x ){
newPiece = p;
break;
}
}
return newPiece;
}
public Piece takenBy(int y, char x){
Piece newPiece = null;
newPiece = takenByWhite(y, x);
if( newPiece == null )
newPiece = takenByBlack(y, x);
return newPiece;
}
public void printHelp(){
System.out.println("***** Board Help *****");
System.out.println("WK: White King \t|\tBK: Black King");
......@@ -8,4 +139,79 @@ public class Board{
System.out.println("WR: White Rook \t|\tBR: Black Rook");
System.out.println("WP: White Pawn \t|\tBP: Black Pawn");
}
public void printBoard(){
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{
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("- ");
}
}
}
}
System.out.println();
}
}
public boolean move(String from, String to, String color){// Name Format: 1D
char[] fromArray = from.toCharArray();
char[] toArray = to.toCharArray();
if( fromArray.length == 2 && toArray.length == 2 ){// Name is consisted of 2 chars
if( fromArray[0] >= '1' && fromArray[0] <= '8' && fromArray[1] >= 'A' && fromArray[1] <= 'H' ){// Check from bound
if( toArray[0] >= '1' && toArray[0] <= '8' && toArray[1] >= 'A' && toArray[1] <= 'H' ){// Check to bound
ArrayList<Piece> selector = null;
boolean found = false;
boolean toBusy = false;
if( color.equals("W") ){
selector = this.whitePieces;
found = isTakenByWhite(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByWhite(toArray[0] - '0', toArray[1]);
}else{
selector = this.blackPieces;
found = isTakenByBlack(fromArray[0] - '0', fromArray[1]);
toBusy = isTakenByBlack(toArray[0] - '0', toArray[1]);
}
if( found && !toBusy ){// Found and to is not busy
for(Piece p : selector){
if( p.getY() == fromArray[0] - '0' && p.getX() == fromArray[1] ){
p.setY(toArray[0] - '0');
p.setX(toArray[1]);
return true;
}
}
return false;
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
}else{
return false;
}
}
}
public class King extends Piece{
public King(char x, int y, String name){
super(x, y, name);
}
}
public class Knight extends Piece{
public Knight(char x, int y, String name){
super(x, y, name);
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Board mainBoard = new Board();
mainBoard.printHelp();
mainBoard.initPieces();
// mainBoard.printHelp();
mainBoard.move("8D", "3A", "B");
mainBoard.printBoard();
sc.close();
}
}
public class Pawn extends Piece{
private boolean firstMove;
public Pawn(char x, int y, String name){
super(x, y, name);
this.firstMove = true;
}
public void setFirstMove(boolean flag){
this.firstMove = flag;
}
public boolean getFirstMove(){
return this.firstMove;
}
}
public abstract class Piece{
private int y;
private char x;
private String name;
public Piece(char x, int y, String name){
setX(x);
setY(y);
setName(name);
}
public void setY(int y){
this.y = y;
}
public void setX(char x){
this.x = x;
}
public void setName(String name){
this.name = name;
}
public int getY(){
return this.y;
}
public char getX(){
return this.x;
}
public String getName(){
return this.name;
}
}
public class Queen extends Piece{
public Queen(char x, int y, String name){
super(x, y, name);
}
}
public class Rook extends Piece{
public Rook(char x, int y, String name){
super(x, y, name);
}
}
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