Commit 633367f9 authored by kimia's avatar kimia

who's turn?

parent 9dfb84e8
...@@ -6,193 +6,210 @@ public class Main{ ...@@ -6,193 +6,210 @@ public class Main{
*@version 1.0 *@version 1.0
*@since 2019-5-7 *@since 2019-5-7
*/ */
private static char boardChar[][] = new char [8][8] ; private static char boardChar[][] = new char [8][8] ;
private static void printBoard() { private static void printBoard() {
for (int i=1 ; i<9 ; ++i) { for (int i=1 ; i<9 ; ++i) {
System.out.print(" " + i) ; System.out.print(" " + i) ;
} }
System.out.print("\n ") ; System.out.print("\n ") ;
for (int i=0 ; i<8 ; ++i) for (int i=0 ; i<8 ; ++i)
System.out.print("+---") ; System.out.print("+---") ;
System.out.print("+\n") ; System.out.print("+\n") ;
for (int i=0 ; i<8 ; ++i) { for (int i=0 ; i<8 ; ++i) {
System.out.print( (char)(i+'a') ) ; System.out.print( (char)(i+'a') ) ;
for (int j=0 ; j<8 ; ++j) { for (int j=0 ; j<8 ; ++j) {
System.out.print("| " + boardChar[i][j] + " ") ; System.out.print("| " + boardChar[i][j] + " ") ;
} }
System.out.print("|\n ") ; System.out.print("|\n ") ;
for (int i2=0 ; i2<8 ; ++i2) for (int i2=0 ; i2<8 ; ++i2)
System.out.print("+---") ; System.out.print("+---") ;
System.out.print("+\n") ; System.out.print("+\n") ;
} }
} }
private static void updateBoard(Piece pw[], Piece pb[]) { private static void updateBoard(Piece pw[], Piece pb[]) {
for (int i=0 ; i<8 ; ++i) { for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) { for (int j=0 ; j<8 ; ++j) {
boardChar[i][j] = ' ' ; boardChar[i][j] = ' ' ;
} }
} }
for (Piece p:pw) { for (Piece p:pw) {
if (p.isDeleted()) continue ; if (p.isDeleted()) continue ;
if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ; if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ;
if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ; if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ;
if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ; if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ;
if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ; if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ;
if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ; if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ;
if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ; if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ;
} }
for (Piece p:pb) { for (Piece p:pb) {
if (p.isDeleted()) continue ; if (p.isDeleted()) continue ;
if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ; if (p instanceof Pawn) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'P' ;
if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ; if (p instanceof Bishop) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'B' ;
if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ; if (p instanceof Queen) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'Q' ;
if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ; if (p instanceof King) boardChar[p.getCell().getRow()][p.getCell().getCol()] = '$' ;
if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ; if (p instanceof Rook) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'R' ;
if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ; if (p instanceof Knight) boardChar[p.getCell().getRow()][p.getCell().getCol()] = 'K' ;
} }
} }
private static void checkCondition(Cell board[][], Piece pw[], Piece pb[]) { private static void checkCondition(Cell board[][], Piece pw[], Piece pb[]) {
for (Piece p:pw) { for (Piece p:pw) {
if (p instanceof King) { if (p instanceof King) {
int sw1=0 ; int sw1=0 ;
for (Piece p1:pb) { for (Piece p1:pb) {
if (p1.isValidMove(p.getCell(), board, pw, pb)) { if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ; sw1=1 ;
} }
} }
if(sw1==1) System.out.println("Check condition for White player!!") ; if(sw1==1) System.out.println("Check condition for White player!!") ;
} }
} }
for (Piece p:pb) { for (Piece p:pb) {
if (p instanceof King) { if (p instanceof King) {
int sw1=0 ; int sw1=0 ;
for (Piece p1:pw) { for (Piece p1:pw) {
if (p1.isValidMove(p.getCell(), board, pw, pb)) { if (p1.isValidMove(p.getCell(), board, pw, pb)) {
sw1=1 ; sw1=1 ;
} }
} }
if(sw1==1) System.out.println("Check condition for Balck player!!") ; if(sw1==1) System.out.println("Check condition for Balck player!!") ;
} }
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
Scanner in = new Scanner(System.in) ; Scanner in = new Scanner(System.in) ;
Player white = new Player(Color.WHITE) ; Player white = new Player(Color.WHITE) ;
Player black = new Player(Color.BLACK) ; Player black = new Player(Color.BLACK) ;
Piece pw[] = white.getPieces() ; Piece pw[] = white.getPieces() ;
Piece pb[] = black.getPieces() ; Piece pb[] = black.getPieces() ;
Cell board[][] = new Cell[8][8] ; Cell board[][] = new Cell[8][8] ;
for (int i=0 ; i<8 ; ++i) { for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) { for (int j=0 ; j<8 ; ++j) {
board[i][j] = new Cell (i,j) ; board[i][j] = new Cell (i,j) ;
} }
} }
for (Piece p:pw) { for (Piece p:pw) {
board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ; board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ;
} }
for (Piece p:pb) { for (Piece p:pb) {
board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ; board[p.getCell().getRow()][p.getCell().getCol()] = p.getCell() ;
} }
int wORb = 0 ; int wORb = 0 ;
updateBoard(pw, pb) ; updateBoard(pw, pb) ;
printBoard() ; printBoard() ;
while (true) { while (true) {
wORb++ ; if (wORb%2==0) System.out.println("White player turn") ;
String s = in.nextLine() ; else System.out.println("Black player turn") ;
if (s.length()==2) { String s = in.nextLine() ;
for (Piece p:pw) { if (s.length()==2) {
if (p.getCell().getRow()==s.charAt(0)-'a' && for (Piece p:pw) {
p.getCell().getCol()+1==s.charAt(1)-'0') { if (p.getCell().getRow()==s.charAt(0)-'a' &&
for (int i=0 ; i<8 ; ++i) { p.getCell().getCol()+1==s.charAt(1)-'0') {
for (int j=0 ; j<8 ; ++j) { for (int i=0 ; i<8 ; ++i) {
if (p.isValidMove(board[i][j], board, pw, pb)) { for (int j=0 ; j<8 ; ++j) {
System.out.print( (char)((int)'a'+i) ) ; if (p.isValidMove(board[i][j], board, pw, pb)) {
System.out.println(j+1) ; System.out.print( (char)((int)'a'+i) ) ;
} System.out.println(j+1) ;
} }
} }
}
} }
} }
for (Piece p:pb) { for (Piece p:pb) {
if (p.getCell().getRow()==s.charAt(0)-'a' && if (p.getCell().getRow()==s.charAt(0)-'a' &&
p.getCell().getCol()+1==s.charAt(1)-'0') { p.getCell().getCol()+1==s.charAt(1)-'0') {
for (int i=0 ; i<8 ; ++i) { for (int i=0 ; i<8 ; ++i) {
for (int j=0 ; j<8 ; ++j) { for (int j=0 ; j<8 ; ++j) {
if (p.isValidMove(board[i][j], board, pw, pb)) { if (p.isValidMove(board[i][j], board, pw, pb)) {
System.out.print( (char)((int)'a'+i) ) ; System.out.print( (char)((int)'a'+i) ) ;
System.out.println(j+1) ; System.out.println(j+1) ;
} }
} }
} }
} }
} }
} }
else if (s.length()==5) { else if (s.length()==5) {
for (Piece p:pw) { if (wORb%2==0) {
if (p.getCell().getRow()==s.charAt(0)-'a' && for (Piece p:pw) {
p.getCell().getCol()+1==s.charAt(1)-'0') { if (p.getCell().getRow()==s.charAt(0)-'a' &&
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb)) { p.getCell().getCol()+1==s.charAt(1)-'0') {
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ; if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb)) {
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; wORb++ ;
p.setCell(tmp) ; Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ; board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
for (Piece p1:pb) { p.setCell(tmp) ;
if (p1.getCell().getRow()==tmp.getRow() && p1.getCell().getCol()==tmp.getCol()) { board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
p1.setDeleted(true) ; for (Piece p1:pb) {
} if (p1.getCell().getRow()==tmp.getRow() && p1.getCell().getCol()==tmp.getCol()) {
} p1.setDeleted(true) ;
if (p instanceof Pawn) { }
Pawn myPawn = (Pawn) p ; }
myPawn.setOnce(false) ; if (p instanceof Pawn) {
} Pawn myPawn = (Pawn) p ;
for (Piece blackPiece:pb) { myPawn.setOnce(false) ;
if (blackPiece.getCell().getRow()==tmp.getRow() }
&& blackPiece.getCell().getCol()==tmp.getCol()) for (Piece blackPiece:pb) {
blackPiece.setDeleted(true) ; if (blackPiece.getCell().getRow()==tmp.getRow()
} && blackPiece.getCell().getCol()==tmp.getCol())
} blackPiece.setDeleted(true) ;
} }
} }
for (Piece p:pb) { }
if (p.getCell().getRow()==s.charAt(0)-'a' && }
p.getCell().getCol()+1==s.charAt(1)-'0') if (wORb%2==0) System.out.println("please enter a valid move!") ;
if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb) ) { else {
Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ; checkCondition(board, pw, pb) ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ; updateBoard(pw, pb) ;
p.setCell(tmp) ; printBoard() ;
board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ; }
for (Piece p1:pw) { }
if (p1.getCell().getRow()==tmp.getRow() && p1.getCell().getCol()==tmp.getCol()) { else {
p1.setDeleted(true) ; for (Piece p:pb) {
} if (p.getCell().getRow()==s.charAt(0)-'a' &&
} p.getCell().getCol()+1==s.charAt(1)-'0')
if (p instanceof Pawn) { if (p.isValidMove(new Cell(s.charAt(3)-'a', s.charAt(4)-'1'), board, pw, pb) ) {
Pawn myPawn = (Pawn) p; wORb++ ;
myPawn.setOnce(false) ; Cell tmp = new Cell(s.charAt(3)-'a', s.charAt(4)-'1') ;
} board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(true) ;
for (Piece whitePiece:pw) { p.setCell(tmp) ;
if (whitePiece.getCell().getRow()==tmp.getRow() board[p.getCell().getRow()][p.getCell().getCol()].setEmpty(false) ;
&& whitePiece.getCell().getCol()==tmp.getCol()) for (Piece p1:pw) {
whitePiece.setDeleted(true) ; if (p1.getCell().getRow()==tmp.getRow() && p1.getCell().getCol()==tmp.getCol()) {
} p1.setDeleted(true) ;
} }
} }
checkCondition(board, pw, pb) ; if (p instanceof Pawn) {
updateBoard(pw, pb) ; Pawn myPawn = (Pawn) p;
printBoard() ; myPawn.setOnce(false) ;
} }
else { for (Piece whitePiece:pw) {
System.out.println("Please enter a valid input!") ; if (whitePiece.getCell().getRow()==tmp.getRow()
} && whitePiece.getCell().getCol()==tmp.getCol())
} whitePiece.setDeleted(true) ;
}
}
}
if (wORb%2==1) System.out.println("please enter a valid move!") ;
else {
checkCondition(board, pw, pb) ;
updateBoard(pw, pb) ;
printBoard() ;
}
}
}
else {
System.out.println("Please enter a valid input!") ;
}
}
} }
} }
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