This is the excellent project of Chess game for beginners. The basic purpose behind this project is that Java beginner can understand OOP concepts like inheritance, Polymorphism, Encapsulation, and dynamic binding.
Source Code
Start.Java
package riu.cms7667.chesslearningproject.chess; import java.util.Scanner; import riu.cms7667.chesslearningproject.chess.Bishop; import riu.cms7667.chesslearningproject.chess.ChessPiece; import riu.cms7667.chesslearningproject.chess.King; import riu.cms7667.chesslearningproject.chess.Knight; import riu.cms7667.chesslearningproject.chess.Pawn; import riu.cms7667.chesslearningproject.chess.Queen; import riu.cms7667.chesslearningproject.chess.Rook; public class Start { ChessPiece objChessPiece=new ChessPiece(); Pawn objPawn = new Pawn(); Queen objQueen = new Queen(); King objKing = new King(); Knight objKnight = new Knight(); Bishop objBishop = new Bishop(); Rook objRook = new Rook(); Scanner input = new Scanner(System.in); public static void display(int row, int column, String pShape) { String[][] pPos=new String[8][8]; for(int a=0;a<8;a++){ for(int b =0;b<8;b++){ pPos[a][b]=" "; }//End For b }//End For a row=row-1; column=column-1; pPos[row][column]=pShape; System.out.print("\t\t -------CHESS~BOARD--------\n"); System.out.print("\t\t |===========| \n"); System.out.print("\t\t ____________\\/_____________\n"); for (int d=0; d<8;d++){ System.out.println("\t\tRow-->"+(d+1)+" |"+pPos[d][0]+"_|"+pPos[d][1]+"_|"+pPos[d][2]+"_|"+pPos[d][3]+"_|"+pPos[d][4]+"_|"+pPos[d][5]+"_|"+pPos[d][6]+"_|"+pPos[d][7]+"_|"); } System.out.println("\t\t |1^|2^|3^|4^|5^|6^|7^|8^|"); System.out.print("\t\t COLUMN \n"); } public void movePiece(){ Scanner input = new Scanner(System.in); char yesOrNo = 'Y'; while (yesOrNo =='Y'){ System.out.print("You have these pieces\n"); viewPieces(); System.out.print("\nWhich Piece do you want to move\n"); System.out.println("You Select : "); int selection = selectPiece(); if(selection==6){ objChessPiece = objPawn ; objChessPiece.validMove(objChessPiece.pieceName); }else if(selection==5){ objChessPiece = objQueen; objChessPiece.validMove(objChessPiece.pieceName); }else if(selection==4){ objChessPiece = objKing; objChessPiece.validMove(objChessPiece.pieceName); }else if(selection==3){ objChessPiece = objKnight; objChessPiece.validMove(objChessPiece.pieceName); }else if(selection==2){ objChessPiece = objBishop; objChessPiece.validMove(objChessPiece.pieceName); }else if(selection==1){ objChessPiece = objRook; objChessPiece.validMove(objChessPiece.pieceName); } else{ error(); } System.out.println("\n\tDo u want to move another piece\n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); yesOrNo=(input.next()).charAt(0); if(Character.isLowerCase(yesOrNo)){ yesOrNo=Character.toUpperCase(yesOrNo); } } } public static void error() { System.out.print("\t\t **************************\n"); System.out.print("\t\t |--------> ERROR <--------| \n"); System.out.print("\t\t **************************\n"); System.out.print("\t\t You Select some thin wrong\n"); System.out.print("\t\t OR\n"); System.out.print("\t\t There may be some othere Problem\n"); System.out.print("\t\t It is better for you to try again...!\n"); System.out.print("\t\t **************************\n"); } private int selectPiece() { int c = input.nextInt(); return c; } private void viewPieces() { System.out.print("\t\tId :"+objRook.ID+"\t\tName :"+objRook.pieceName+"\n\n"); System.out.print("\t\tId :"+objBishop.ID+"\t\tName :"+objBishop.pieceName+"\n\n"); System.out.print("\t\tId :"+objKnight.ID+"\t\tName :"+objKnight.pieceName+"\n\n"); System.out.print("\t\tId :"+objKing.ID+"\t\tName :"+objKing.pieceName+"\n\n"); System.out.print("\t\tId :"+objQueen.ID+"\t\tName :"+objQueen.pieceName+"\n\n"); System.out.print("\t\tId :"+objPawn.ID+"\t\tName :"+objPawn.pieceName+"\n\n"); } public void StartGame(){ Scanner input = new Scanner(System.in); char mainYesOrNo = 'Y'; while (mainYesOrNo =='Y'){ System.out.print("\t\t ==========================\n"); System.out.print("\t\t Welcome To Chess Learning \n"); System.out.print("\t\t Software \n"); System.out.print("\t\t ==========================\n"); System.out.print("\tQ:What do you want to do?\n\n"); System.out.print("\t\t1 : View valid moves in Row/Columns\n\n"); System.out.print("\t\t2 : View possible move in Words\n\n"); System.out.println("\t\t\tYou Select : "); int switchChoice = input.nextInt(); switch (switchChoice) { case 1: { movePiece(); break; } case 2: { possibleMove(); break; } default: { System.out.println("Invalid Selection"); error(); break; } } System.out.println("\n\tDo u want to run your Program Again \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); mainYesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(mainYesOrNo )){ mainYesOrNo =Character.toUpperCase(mainYesOrNo ); }//End If }//End While } private void possibleMove() { Scanner input = new Scanner(System.in); char possibleYesOrNo = 'Y'; while (possibleYesOrNo =='Y'){ System.out.println("\tFor Which Piece do you want to see possible move \n"); viewPieces(); System.out.println("You Select"); int pieceNo = input.nextInt(); switch (pieceNo) { case 1: ChessPiece objPiece1 =new Rook(); objPiece1.move(); break; case 2: ChessPiece objPiece2 =new Bishop(); objPiece2.move(); break; case 3: ChessPiece objPiece3 =new Knight(); objPiece3.move(); break; case 4: ChessPiece objPiece4 =new King(); objPiece4.move(); break; case 5: ChessPiece objPiece5 =new Queen(); objPiece5.move(); break; case 6: ChessPiece objPiece6 =new Pawn(); objPiece6.move(); break; default: System.out.println("Invalid Selection"); error(); break; }//End Case System.out.println("\n\tDo u want to see move for another piece in words \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); possibleYesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(possibleYesOrNo )){ possibleYesOrNo =Character.toUpperCase(possibleYesOrNo ); }//End If }//End While }//End of possibleMove() }//End of Class Start
ChessPiece.Java
package riu.cms7667.chesslearningproject.chess; public class ChessPiece { protected int[][] initialPossition= new int[8][8]; protected int[][] finalPossition= new int[8][8]; public int ID=6; public String pieceName; protected int[] finalColumnPosition= new int[8]; protected int[] finalRowPosition= new int[8]; protected int[] initialRowPosition= new int[8]; protected int[] initialColumnPosition= new int[8]; public void validMove(String Name){ System.out.println("You want to move "+Name+" "); } public void move() { System.out.println("The piece can move "); } }
Rook.Java
package riu.cms7667.chesslearningproject.chess; import java.util.Scanner; import riu.cms7667.chesslearningproject.start.Start; public class Rook extends ChessPiece { int noOfRook; private Scanner input; public Rook() { super(); ID=1; pieceName="Rook"; noOfRook = 2; for(int i=0;i<noOfRook;i++){ if (i==0){ initialColumnPosition[i]=0; initialRowPosition[i]=7; }else if (i==1){ initialColumnPosition[i]=7; initialRowPosition[i]=7; } finalColumnPosition[i]=0; finalRowPosition[i]= 0; initialPossition[initialRowPosition[i]][initialColumnPosition[i]]=1; finalPossition[finalRowPosition[i]][finalColumnPosition[i]]=0; } }// End Bishop public void validMove(String Name) { int pieceSelection; Scanner input = new Scanner(System.in); System.out.println("\tYou have "+noOfRook+" "+pieceName+"s"); System.out.println("Which "+pieceName+" do you want to move"); viewRowColumn(); System.out.println("Your Selection : "); pieceSelection=input.nextInt(); char rookYesOrNo = 'Y'; while (rookYesOrNo =='Y'){ //method setRowColumn(pieceSelection); //for loop System.out.println("\n\tDo u want to run your Program Again \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); rookYesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(rookYesOrNo )){ rookYesOrNo =Character.toUpperCase(rookYesOrNo ); }//End If }//End While }//end validMove private void setRowColumn(int pieceSelection) { int[] getArray=new int[2]; int moveBishop; Scanner input = new Scanner(System.in); if((pieceSelection>0)&&(pieceSelection<3)){ System.out.println("Your selected "+pieceName+" no :"+pieceSelection+"\nIs in"+"\nRow :"+(initialRowPosition[pieceSelection-1]+1)+"\t\tColumn :"+(initialColumnPosition[pieceSelection-1]+1)); System.out.println("This "+pieceName+" can move in"); move(); System.out.println("How would you like to move {Press(1-->4)}"); System.out.println("Your Selection : "); moveBishop = input.nextInt(); switch(moveBishop){ case 1:{ getArray = moveForward(pieceSelection); break; } case 2:{ getArray=moveBackword(pieceSelection); break; } case 3:{ getArray= moveLeft(pieceSelection); break; } case 4:{ getArray= moveRight(pieceSelection); break; } default:{ getArray= moveLeft(pieceSelection); break; } }//end switch System.out.println("Your selected "+pieceName+"\n"); System.out.println("Now is in"+"\n-----\nRow :"+(initialRowPosition[pieceSelection-1]+1)+" AND\nColumn :"+(initialColumnPosition[pieceSelection-1]+1)+"\n-----\n"); Start.display(initialRowPosition[pieceSelection-1]+1, initialColumnPosition[pieceSelection-1]+1, "R"); } } private int[] moveRight(int pieceSelection) { int[] rightArray=new int[2]; System.out.println("You Select Left \n You have these choices"); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1; while(!(finalColumnPosition[pieceSelection-1]==1)){ finalColumnPosition[pieceSelection-1]--; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Column do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int Column = input.nextInt(); if ((Column <=8)&&(Column >-1)){ Column = Column-1; finalColumnPosition[pieceSelection-1]=Column ; finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialRowPosition[pieceSelection-1]=finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; // finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; }else{ System.out.println("Can note move"); initialRowPosition[pieceSelection-1]=finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; } return rightArray; } private int[] moveLeft(int pieceSelection) { int[] leftArray=new int[2]; System.out.println("You Select Left \n You have these choices"); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1; while(!(finalColumnPosition[pieceSelection-1]==8)){ finalColumnPosition[pieceSelection-1]++; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Column do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int Column = input.nextInt(); if ((Column <=8)&&(Column >-1)){ Column = Column-1; finalColumnPosition[pieceSelection-1]=Column ; finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialRowPosition[pieceSelection-1]=finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; // finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; }else{ System.out.println("Can note move"); initialRowPosition[pieceSelection-1]=finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; } leftArray[0]=initialRowPosition[pieceSelection-1]; leftArray[1]=initialRowPosition[pieceSelection-1]; return leftArray; } private int[] moveBackword(int pieceSelection) { System.out.println("You Select Backword \n You have these choices"); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1; while(!(finalRowPosition[pieceSelection-1]==8)){ finalRowPosition[pieceSelection-1]++; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); if(row>=initialRowPosition[pieceSelection-1]){ if ((row <=8)&&(row >-1)){ row = row-1; finalRowPosition[pieceSelection-1]=row ; finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialRowPosition[pieceSelection-1]=finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; // finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; }else{ System.out.println("Can note move"); initialRowPosition[pieceSelection-1]=finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; } }else{ System.out.println("Can note move"); initialRowPosition[pieceSelection-1]=finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; } int[] backwordArray=new int[2]; backwordArray[0]=initialRowPosition[pieceSelection-1]; backwordArray[1]=initialColumnPosition[pieceSelection-1]; return backwordArray; } private int[] moveForward(int pieceSelection) { System.out.println("You Select Forward \n You have these choices"); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1; while(!(finalRowPosition[pieceSelection-1]==1)){ finalRowPosition[pieceSelection-1]--; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); if ((row <=8)&&(row >-1)){ row = row-1; finalRowPosition[pieceSelection-1]=row ; finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialRowPosition[pieceSelection-1]=finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; // initialRowPosition[pieceSelection-1]=row; }else{ System.out.println("Can note move"); initialRowPosition[pieceSelection-1] = finalRowPosition[pieceSelection-1]; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; } int[] forwardArray=new int[2]; forwardArray[0]=initialRowPosition[pieceSelection-1]; forwardArray[1]=initialColumnPosition[pieceSelection-1]; return forwardArray; } private void viewRowColumn() { for(int i=0;i<noOfRook;i++){ int c=i+1; int b =initialRowPosition[i]+1; int a =initialColumnPosition[i]+1; System.out.println("Piece no :"+c+"\n\t\tIs in"+"\n\t\tRow :"+b); System.out.println("\t\tColumn :"+a); } } public void move() { super.move(); System.out.println("Four directions\n"); System.out.print("\t1 : Forward\n"); System.out.print("\t2 : Backward\n"); System.out.print("\t3 : Left\n"); System.out.print("\t4 : Right\n"); } }
Queen.Java
package riu.cms7667.chesslearningproject.chess; import java.util.Scanner; import riu.cms7667.chesslearningproject.start.Start; public class Queen extends ChessPiece{ int noOfQueen; Scanner input; public Queen() { super(); ID =5; pieceName ="Queen"; noOfQueen = 1; initialRowPosition[7]=7; initialColumnPosition[3]=3; finalRowPosition[7]=0; finalColumnPosition[3]=0; initialPossition [initialRowPosition[7]][initialColumnPosition[4]]=1; finalPossition[finalRowPosition[7]][finalColumnPosition[3]]=0; } public void validMove(String Name) { input = new Scanner(System.in); super.validMove(Name); System.out.println(pieceName+" can move through these 3 ways "); char queenYesOrNo = 'Y'; while (queenYesOrNo =='Y'){ // Implemented code setRowColumn(); //loop System.out.println("\n\tDo u want toContinue \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); queenYesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(queenYesOrNo )){ queenYesOrNo =Character.toUpperCase(queenYesOrNo ); }//End If }//End While } private void setRowColumn() { System.out.print("Which one would you like\n"); move(); int getInt = input.nextInt(); finalColumnPosition[3]=initialColumnPosition[3]+1; finalRowPosition[7]=initialRowPosition[7]+1; switch(getInt){ case 1:{ moveStraight(); break; } case 2:{ moveLeftDiagonal(); break; } case 3:{ if((initialColumnPosition[3]<7)&&((initialRowPosition[3]>=0)&&(initialRowPosition[3]<=7))){ moveRightDiagonal(); }else{ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; } break; } } finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; System.out.println("Your selected "+pieceName+"\n"); System.out.println("Now is in"+"\n-----\nRow :"+(finalRowPosition[7]+1)+" AND\nColumn :"+(finalColumnPosition[3]+1)+"\n-----\n"); Start.display(finalRowPosition[7]+1, finalColumnPosition[3]+1, "Q"); initialRowPosition[7] = finalRowPosition[7]; initialColumnPosition[3] = finalColumnPosition[3]; } public void moveRightDiagonal(){ System.out.println("Right Diagonal in"); finalColumnPosition[3] =initialColumnPosition[3]+1; finalRowPosition[7]=initialRowPosition[7]+1; while(((!(finalColumnPosition[3]==8))&&(!(finalRowPosition[7]==1)))){ finalColumnPosition[3]++; finalRowPosition[7]--; System.out.println("Row :"+(finalRowPosition[7])+"\t\tColumn :"+finalColumnPosition[3]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[3] =initialColumnPosition[3]; finalRowPosition[7]=initialRowPosition[7]; input = new Scanner(System.in); int row = input.nextInt(); if((row <8)&&(row >-1)){ row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[7]){ finalPossition [finalRowPosition[7]][finalColumnPosition[3]]=1; initialPossition [initialRowPosition[7]][initialColumnPosition[3]]=0; initialColumnPosition[3]=finalColumnPosition[3]; initialRowPosition[7]=row; } finalColumnPosition[3]++; finalRowPosition[7]--; } }else{ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; } } public void moveLeftDiagonal(){ System.out.println("You Select Left Diagonal \n You have these choices "); finalColumnPosition[3] =initialColumnPosition[3]+1; finalRowPosition[7]=initialRowPosition[7]+1; while((!(finalColumnPosition[3]==1))&&(!(finalRowPosition[7]==8))){ finalColumnPosition[3]--; finalRowPosition[7]--; System.out.println("Row :"+finalRowPosition[7]+"\t\tColumn :"+finalColumnPosition[3]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[3] =initialColumnPosition[3]; finalRowPosition[7]=initialRowPosition[7]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[7]){ finalPossition [finalRowPosition[7]][finalColumnPosition[3]]=1; initialPossition [initialRowPosition[7]][initialColumnPosition[3]]=0; initialColumnPosition[3]=finalColumnPosition[3]; initialRowPosition[7]=row; } finalColumnPosition[3]--; finalRowPosition[7]--; } } public void moveStraight(){ System.out.println("You Select Straight \n You have these choices"); while(!(finalRowPosition[7]==1)){ finalRowPosition[7]--; System.out.println("Row :"+finalRowPosition[7]+"\t\tColumn :"+finalColumnPosition[3]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[3] =initialColumnPosition[3]; finalRowPosition[7]=initialRowPosition[7]; input = new Scanner(System.in); int row = input.nextInt(); if ((row <8)&&(row >-1)){ row = row-1; initialColumnPosition[3]=finalColumnPosition[3]; initialRowPosition[7]=row; }else{ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; } } public void move() { super.move(); System.out.println("both the straights and the diagonals that is\n"); System.out.print("\t1 : Straight\n"); System.out.print("\t2 : Left Diagonal\n"); System.out.print("\t3 : Right Diagonal\n"); } }package riu.cms7667.chesslearningproject.chess; import java.util.Scanner; import riu.cms7667.chesslearningproject.start.Start; public class Queen extends ChessPiece{ int noOfQueen; Scanner input; public Queen() { super(); ID =5; pieceName ="Queen"; noOfQueen = 1; initialRowPosition[7]=7; initialColumnPosition[3]=3; finalRowPosition[7]=0; finalColumnPosition[3]=0; initialPossition [initialRowPosition[7]][initialColumnPosition[4]]=1; finalPossition[finalRowPosition[7]][finalColumnPosition[3]]=0; } public void validMove(String Name) { input = new Scanner(System.in); super.validMove(Name); System.out.println(pieceName+" can move through these 3 ways "); char queenYesOrNo = 'Y'; while (queenYesOrNo =='Y'){ // Implemented code setRowColumn(); //loop System.out.println("\n\tDo u want toContinue \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); queenYesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(queenYesOrNo )){ queenYesOrNo =Character.toUpperCase(queenYesOrNo ); }//End If }//End While } private void setRowColumn() { System.out.print("Which one would you like\n"); move(); int getInt = input.nextInt(); finalColumnPosition[3]=initialColumnPosition[3]+1; finalRowPosition[7]=initialRowPosition[7]+1; switch(getInt){ case 1:{ moveStraight(); break; } case 2:{ moveLeftDiagonal(); break; } case 3:{ if((initialColumnPosition[3]<7)&&((initialRowPosition[3]>=0)&&(initialRowPosition[3]<=7))){ moveRightDiagonal(); }else{ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; } break; } } finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; System.out.println("Your selected "+pieceName+"\n"); System.out.println("Now is in"+"\n-----\nRow :"+(finalRowPosition[7]+1)+" AND\nColumn :"+(finalColumnPosition[3]+1)+"\n-----\n"); Start.display(finalRowPosition[7]+1, finalColumnPosition[3]+1, "Q"); initialRowPosition[7] = finalRowPosition[7]; initialColumnPosition[3] = finalColumnPosition[3]; } public void moveRightDiagonal(){ System.out.println("Right Diagonal in"); finalColumnPosition[3] =initialColumnPosition[3]+1; finalRowPosition[7]=initialRowPosition[7]+1; while(((!(finalColumnPosition[3]==8))&&(!(finalRowPosition[7]==1)))){ finalColumnPosition[3]++; finalRowPosition[7]--; System.out.println("Row :"+(finalRowPosition[7])+"\t\tColumn :"+finalColumnPosition[3]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[3] =initialColumnPosition[3]; finalRowPosition[7]=initialRowPosition[7]; input = new Scanner(System.in); int row = input.nextInt(); if((row <8)&&(row >-1)){ row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[7]){ finalPossition [finalRowPosition[7]][finalColumnPosition[3]]=1; initialPossition [initialRowPosition[7]][initialColumnPosition[3]]=0; initialColumnPosition[3]=finalColumnPosition[3]; initialRowPosition[7]=row; } finalColumnPosition[3]++; finalRowPosition[7]--; } }else{ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; } } public void moveLeftDiagonal(){ System.out.println("You Select Left Diagonal \n You have these choices "); finalColumnPosition[3] =initialColumnPosition[3]+1; finalRowPosition[7]=initialRowPosition[7]+1; while((!(finalColumnPosition[3]==1))&&(!(finalRowPosition[7]==8))){ finalColumnPosition[3]--; finalRowPosition[7]--; System.out.println("Row :"+finalRowPosition[7]+"\t\tColumn :"+finalColumnPosition[3]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[3] =initialColumnPosition[3]; finalRowPosition[7]=initialRowPosition[7]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[7]){ finalPossition [finalRowPosition[7]][finalColumnPosition[3]]=1; initialPossition [initialRowPosition[7]][initialColumnPosition[3]]=0; initialColumnPosition[3]=finalColumnPosition[3]; initialRowPosition[7]=row; } finalColumnPosition[3]--; finalRowPosition[7]--; } } public void moveStraight(){ System.out.println("You Select Straight \n You have these choices"); while(!(finalRowPosition[7]==1)){ finalRowPosition[7]--; System.out.println("Row :"+finalRowPosition[7]+"\t\tColumn :"+finalColumnPosition[3]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[3] =initialColumnPosition[3]; finalRowPosition[7]=initialRowPosition[7]; input = new Scanner(System.in); int row = input.nextInt(); if ((row <8)&&(row >-1)){ row = row-1; initialColumnPosition[3]=finalColumnPosition[3]; initialRowPosition[7]=row; }else{ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; } } public void move() { super.move(); System.out.println("both the straights and the diagonals that is\n"); System.out.print("\t1 : Straight\n"); System.out.print("\t2 : Left Diagonal\n"); System.out.print("\t3 : Right Diagonal\n"); } }
Pawn.Java
package riu.cms7667.chesslearningproject.chess; import java.util.Scanner; import riu.cms7667.chesslearningproject.start.Start; public class Pawn extends ChessPiece { int noOfPawns; private Scanner input; public Pawn() { super(); ID =6; pieceName ="Pawn"; noOfPawns = 8; for(int i=0;i<noOfPawns;i++){ initialRowPosition[i]=6; initialColumnPosition[i]=i; finalColumnPosition[i]=0; finalRowPosition[i]= 0; initialPossition[initialRowPosition[i]][initialColumnPosition[i]]=1; finalPossition[finalRowPosition[i]][finalColumnPosition[i]]=0; } } public void validMove(String Name) { super.validMove(Name); System.out.println("\tYou have "+noOfPawns+" "+pieceName+"s"); System.out.println("Which "+pieceName+" do you want to move"); viewRowColumn(); input= new Scanner(System.in); int d =input.nextInt(); if((d>0)&&(d<=8)){ int e=initialRowPosition[d-1]+1;//increments one to display System.out.println("Your selected "+pieceName+" no :"+d+"\nIs in"+"\nRow :"+e+"\t\tColumn :"+d); int[] varChoice = new int[2]; varChoice = viewPossibility(d); int g= varChoice [1]+1;//[1] [0] for [Row] [column]in viewPossibility(); System.out.println("Your selected "+pieceName+" no :"+d+"\n"); System.out.println("Now is in"+"\n-----\nRow :"+g+" AND\nColumn :"+varChoice [0]+"\n-----\n"); Start.display(g, varChoice[0], "P"); finalPossition[varChoice [1]][varChoice [0]-1]=1; initialPossition[e-1][d-1]=0; }else{ Start.error(); } } private int[] viewPossibility(int index) { int b = 0; if (initialColumnPosition[index-1]==0){ initialRowPosition[index-1]=initialRowPosition[index-1]-1; System.out.println("This "+pieceName+" can move in"); for(int j=0;j<2;j++){ int a =initialColumnPosition[j]+1; b =initialRowPosition[index-1]+1; System.out.println("\nRow :"+b+"\t\tColumn :"+a); } }else if(initialColumnPosition[index-1]==7){ initialRowPosition[index-1]=initialRowPosition[index-1]-1; System.out.println("This "+pieceName+"e can move in"); for(int j=7;j<9;j++){ int a =initialColumnPosition[j-1]+1; b =initialRowPosition[index-1]+1; System.out.println("\nRow :"+b+"\t\tColumn :"+a); } }else if((initialColumnPosition[index-1]<=6)||(initialColumnPosition[index-1]>=1)){ System.out.println("This "+pieceName+" can move in"); initialRowPosition[index-1]=initialRowPosition[index-1]-1; int[] a =new int[3]; a[0]=initialColumnPosition[index-2]; a[1]=initialColumnPosition[index-1]; a[2]=initialColumnPosition[index]; for(int k=0;k<3;k++){ int c = a[k]+1; b =initialRowPosition[index-1]+1; System.out.println("\nRow :"+b+"\t\tColumn :"+c); }//end for }else{ Start.error(); }//end else System.out.println("In which column do you want to move\n Column : "); input= new Scanner(System.in); int var[] = new int[2]; var[0] = input.nextInt(); var[1]= b-1; return var; } private void viewRowColumn() { for(int i=0;i<noOfPawns;i++){ int c=i+1; int b =initialRowPosition[i]+1; System.out.println("Piece no :"+c+"\n\t\tIs in"+"\n\t\tRow :"+b); int a =initialColumnPosition[i]+1; System.out.println("\t\tColumn :"+a); } } public void move() { super.move(); System.out.println("1 square per turn"); } }
Knight.Java
package riu.cms7667.chesslearningproject.chess; import java.util.Scanner; import riu.cms7667.chesslearningproject.start.Start; public class Knight extends ChessPiece{ int noOfKnight; private Scanner input; public Knight() { super(); ID=3; pieceName="Knight"; noOfKnight = 2; for(int i=0;i<noOfKnight;i++){ if (i==0){ initialColumnPosition[i]=1; initialRowPosition[i]=7; }else if (i==1){ initialColumnPosition[i]=6; initialRowPosition[i]=7; } finalColumnPosition[i]=0; finalRowPosition[i]= 0; initialPossition[initialRowPosition[i]][initialColumnPosition[i]]=1; finalPossition[finalRowPosition[i]][finalColumnPosition[i]]=0; } } public void validMove(String Name) { input= new Scanner(System.in); int pieceSelection; System.out.println("\tYou have "+noOfKnight+" "+pieceName+"s"); System.out.println("Which "+pieceName+" do you want to move"); viewRowColumn(); pieceSelection =input.nextInt(); char yesOrNo = 'Y'; while (yesOrNo =='Y'){ // method setRowColumn(pieceSelection); // loop System.out.println("\n\tDo u want toContinue \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); yesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(yesOrNo )){ yesOrNo =Character.toUpperCase(yesOrNo ); }//End If }//End While } private void setRowColumn(int pieceSelection) { int moveKing; int[] getterRowColumn = new int[2]; if((pieceSelection>0)&&(pieceSelection<3)){ System.out.println("Your selected "+pieceName+" no :"+pieceSelection+"\nIs in"+"\nRow :"+(initialRowPosition[pieceSelection-1]+1)+"\t\tColumn :"+(initialColumnPosition[pieceSelection-1]+1)); System.out.println("This "+pieceName+" can move in"); move(); System.out.println("How would you like to move {Press(1-->8)}"); System.out.println("Your Selection : "); moveKing = input.nextInt(); getterRowColumn = seterRowColumn(moveKing,pieceSelection); if ((getterRowColumn[0]<8)&&(getterRowColumn[0]>-1)&&(getterRowColumn[1]<8)&&(getterRowColumn[1]>-1)){ finalPossition[getterRowColumn[0]][getterRowColumn[1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; System.out.println("Your selected "+pieceName+" no :"+pieceSelection+"\n"); System.out.println("Now is in"+"\n-----\nRow :"+(getterRowColumn[0]+1)+" AND\nColumn :"+(getterRowColumn[1]+1)+"\n-----\n"); Start.display(getterRowColumn[0]+1, getterRowColumn[1]+1, "N"); initialRowPosition[pieceSelection-1]=getterRowColumn[0]; initialColumnPosition[pieceSelection-1]=getterRowColumn[1]; }else{ System.out.println("Can note move"); finalRowPosition[pieceSelection-1]=0; finalColumnPosition[pieceSelection-1]=0; } }else{ Start.error(); } }//End setRowColumn private int[] seterRowColumn(int t,int k){ switch(t){ case 1:{ if(k==1){ finalRowPosition[k-1]=initialRowPosition[k-1]-2; finalColumnPosition[k-1]=initialColumnPosition[k-1]+1; } else if (k==2){ finalRowPosition[k-1]=initialRowPosition[k-1]-2; finalColumnPosition[k-1]=initialColumnPosition[k-1]+1; } break; } case 2:{ if(k==1){ finalRowPosition[k-1]=initialRowPosition[k-1]-2; finalColumnPosition[k-1]=initialColumnPosition[k-1]-1; } else if (k==2){ finalRowPosition[k-1]=initialRowPosition[k-1]-2; finalColumnPosition[k-1]=initialColumnPosition[k-1]-1; } break; } case 3:{ if(k==1){ finalRowPosition[k-1]=initialRowPosition[k-1]+2; finalColumnPosition[k-1]=initialColumnPosition[k-1]+1; } else if (k==2){ finalRowPosition[k-1]=initialRowPosition[k-1]+2; finalColumnPosition[k-1]=initialColumnPosition[k-1]+1; } break; } case 4:{ if(k==1){ finalRowPosition[k-1]=initialRowPosition[k-1]+2; finalColumnPosition[k-1]=initialColumnPosition[k-1]-1; } else if (k==2){ finalRowPosition[k-1]=initialRowPosition[k-1]+2; finalColumnPosition[k-1]=initialColumnPosition[k-1]-1; } break; } case 5:{ if(k==1){ finalRowPosition[k-1]=initialRowPosition[k-1]+1; finalColumnPosition[k-1]=initialColumnPosition[k-1]+2; } else if (k==2){ finalRowPosition[k-1]=initialRowPosition[k-1]+1; finalColumnPosition[k-1]=initialColumnPosition[k-1]+2; } break; } case 6:{ if(k==1){ finalRowPosition[k-1]=initialRowPosition[k-1]-1; finalColumnPosition[k-1]=initialColumnPosition[k-1]+2; } else if (k==2){ finalRowPosition[k-1]=initialRowPosition[k-1]-1; finalColumnPosition[k-1]=initialColumnPosition[k-1]+2; } break; }case 7:{ if(k==1){ finalRowPosition[k-1]=initialRowPosition[k-1]+1; finalColumnPosition[k-1]=initialColumnPosition[k-1]-2; } else if (k==2){ finalRowPosition[k-1]=initialRowPosition[k-1]+1; finalColumnPosition[k-1]=initialColumnPosition[k-1]-2; } break; } case 8:{ if(k==1){ finalRowPosition[k-1]=initialRowPosition[k-1]-1; finalColumnPosition[k-1]=initialColumnPosition[k-1]-2; } else if (k==2){ finalRowPosition[k-1]=initialRowPosition[k-1]-1; finalColumnPosition[k-1]=initialColumnPosition[k-1]-2; } break; } default :{ System.out.println("Invalid Selection"); } break; } int[] j=new int[2]; j[0]=finalRowPosition[k-1]; j[1]=finalColumnPosition[k-1]; return j; } private void viewRowColumn() { for(int i=0;i<noOfKnight;i++){ int c=i+1; int b =initialRowPosition[i]+1; int a =initialColumnPosition[i]+1; System.out.println("Piece no :"+c+"\n\t\tIs in"+"\n\t\tRow :"+b); System.out.println("\t\tColumn :"+a); } } public void move() { super.move(); System.out.println("in an L-shaped pattern"); System.out.println("1 : Forward Left"); System.out.println("2 : Forward Right"); System.out.println("3 : Backward Left"); System.out.println("4 : Backward Right"); System.out.println("5 : Left up"); System.out.println("6 : Left down"); System.out.println("7 : Right down"); System.out.println("8 : Right up"); } }
King.Java
package riu.cms7667.chesslearningproject.chess;
import java.util.Scanner; import riu.cms7667.chesslearningproject.start.Start; public class King extends ChessPiece { int noOfKing; public King() { super(); ID =4; pieceName ="King"; noOfKing = 1; initialRowPosition[7]=7; initialColumnPosition[4]=4; finalRowPosition[7]=0; finalColumnPosition[4]=0; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=1; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=0; } Scanner input = new Scanner(System.in); public void validMove(String Name) { super.validMove(Name); System.out.println(pieceName+" can move in any 1 neighbour direction "); System.out.print("\n Initial Row : "+ (initialRowPosition[7]+1)); System.out.print("\n Initial Column : "+ (initialColumnPosition[4]+1)+"\n"); char kingYesOrNo = 'Y'; while (kingYesOrNo =='Y'){ move(); // method setRowColum(); // loop System.out.println("\n\tDo u want to move again? \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); kingYesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(kingYesOrNo )){ kingYesOrNo =Character.toUpperCase(kingYesOrNo ); }//End If }//End While } private void setRowColum() { int mov; System.out.print("\nWhich one would you like to move\n"); System.out.print("\t You Select : "); mov = input.nextInt(); switch (mov) { case 1: if (initialRowPosition[7]==0){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]-1; finalColumnPosition[4]=initialColumnPosition[4]; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]= 1; } break; case 2: if (initialRowPosition[7]==7){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]+1; finalColumnPosition[4]=initialColumnPosition[4]; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 3: if (initialColumnPosition[4]==0){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]-1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 4: if (initialColumnPosition[4]==7){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]+1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 5: if (initialRowPosition[7]==0){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]-1; finalColumnPosition[4]=initialColumnPosition[4]-1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 6: if (initialRowPosition[7]==0){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]-1; finalColumnPosition[4]=initialColumnPosition[4]+1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 7: if (initialRowPosition[7]==7){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]+1; finalColumnPosition[4]=initialColumnPosition[4]-1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 8: if (initialRowPosition[7]==7){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]+1; finalColumnPosition[4]=initialColumnPosition[4]+1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; default: System.out.println("Invalid Selection"); break; } System.out.println("Your selected "+pieceName+"\n"); System.out.println("Now is in"+"\n-----\nRow :"+(finalRowPosition[7]+1)+" AND\nColumn :"+(finalColumnPosition[4]+1)+"\n-----\n"); Start.display(finalRowPosition[7]+1, finalColumnPosition[4]+1, "K"); initialRowPosition[7] = finalRowPosition[7]; initialColumnPosition[4] = finalColumnPosition[4]; } public void move() { super.move(); System.out.print("\n\t1 : Forward"); System.out.print("\n\t2 : Backword"); System.out.print("\n\t3 : Left"); System.out.print("\n\t4 : Right"); System.out.print("\n\t5 : Upper Left Diagonal"); System.out.print("\n\t6 : Upper Right Diagonal"); System.out.print("\n\t7 : Lower Left Diagonal"); System.out.print("\n\t8 : Lower Right Diagonal"); } }
Bishop.Java
package riu.cms7667.chesslearningproject.chess; import .util.Scanner; import riu.cms7667.chesslearningproject.start.Start; public class King extends ChessPiece { int noOfKing; public King() { super(); ID =4; pieceName ="King"; noOfKing = 1; initialRowPosition[7]=7; initialColumnPosition[4]=4; finalRowPosition[7]=0; finalColumnPosition[4]=0; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=1; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=0; } Scanner input = new Scanner(System.in); public void validMove(String Name) { super.validMove(Name); System.out.println(pieceName+" can move in any 1 neighbour direction "); System.out.print("\n Initial Row : "+ (initialRowPosition[7]+1)); System.out.print("\n Initial Column : "+ (initialColumnPosition[4]+1)+"\n"); char kingYesOrNo = 'Y'; while (kingYesOrNo =='Y'){ move(); // method setRowColum(); // loop System.out.println("\n\tDo u want to move again? \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); kingYesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(kingYesOrNo )){ kingYesOrNo =Character.toUpperCase(kingYesOrNo ); }//End If }//End While } private void setRowColum() { int mov; System.out.print("\nWhich one would you like to move\n"); System.out.print("\t You Select : "); mov = input.nextInt(); switch (mov) { case 1: if (initialRowPosition[7]==0){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]-1; finalColumnPosition[4]=initialColumnPosition[4]; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]= 1; } break; case 2: if (initialRowPosition[7]==7){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]+1; finalColumnPosition[4]=initialColumnPosition[4]; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 3: if (initialColumnPosition[4]==0){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]-1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 4: if (initialColumnPosition[4]==7){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]+1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 5: if (initialRowPosition[7]==0){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]-1; finalColumnPosition[4]=initialColumnPosition[4]-1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 6: if (initialRowPosition[7]==0){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]-1; finalColumnPosition[4]=initialColumnPosition[4]+1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 7: if (initialRowPosition[7]==7){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]+1; finalColumnPosition[4]=initialColumnPosition[4]-1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; case 8: if (initialRowPosition[7]==7){ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[4]=initialColumnPosition[4]; }else{ finalRowPosition[7]=initialRowPosition[7]+1; finalColumnPosition[4]=initialColumnPosition[4]+1; initialPossition[initialRowPosition[7]][initialColumnPosition[4]]=0; finalPossition[finalRowPosition[7]][finalColumnPosition[4]]=1; } break; default: System.out.println("Invalid Selection"); break; } System.out.println("Your selected "+pieceName+"\n"); System.out.println("Now is in"+"\n-----\nRow :"+(finalRowPosition[7]+1)+" AND\nColumn :"+(finalColumnPosition[4]+1)+"\n-----\n"); Start.display(finalRowPosition[7]+1, finalColumnPosition[4]+1, "K"); initialRowPosition[7] = finalRowPosition[7]; initialColumnPosition[4] = finalColumnPosition[4]; } public void move() { super.move(); System.out.print("\n\t1 : Forward"); System.out.print("\n\t2 : Backword"); System.out.print("\n\t3 : Left"); System.out.print("\n\t4 : Right"); System.out.print("\n\t5 : Upper Left Diagonal"); System.out.print("\n\t6 : Upper Right Diagonal"); System.out.print("\n\t7 : Lower Left Diagonal"); System.out.print("\n\t8 : Lower Right Diagonal"); } }package riu.cms7667.chesslearningproject.chess; import java.util.Scanner; import riu.cms7667.chesslearningproject.start.Start; public class Bishop extends ChessPiece { private int noOfBishop; private Scanner input; public Bishop() { super(); ID=2; pieceName="Bishop"; noOfBishop = 2; for(int i=0;i<noOfBishop;i++){ if (i==0){ initialColumnPosition[i]=2; initialRowPosition[i]=7; }else if (i==1){ initialColumnPosition[i]=5; initialRowPosition[i]=7; } finalColumnPosition[i]=0; finalRowPosition[i]= 0; initialPossition[initialRowPosition[i]][initialColumnPosition[i]]=1; finalPossition[finalRowPosition[i]][finalColumnPosition[i]]=0; } }// End Bishop() public void validMove(String Name) { int pieceSelection; input = new Scanner(System.in); System.out.println("\tYou have "+noOfBishop+" "+pieceName+"s"); System.out.println("Which "+pieceName+" do you want to move"); viewRowColumn(); System.out.println("Your Selection : "); pieceSelection=input.nextInt(); char bishopYesOrNo = 'Y'; while (bishopYesOrNo =='Y'){ // Implemented code setRowColumn(pieceSelection); // while loop System.out.println("\n\tDo u want toContinue \n\t\t\tY = yes\n\t\t\tN = No\n"); System.out.println("You Select : "); bishopYesOrNo =(input.next()).charAt(0); if(Character.isLowerCase(bishopYesOrNo )){ bishopYesOrNo =Character.toUpperCase(bishopYesOrNo ); }//End If }//End While }//End validMove private void setRowColumn(int pieceSelection) { if((pieceSelection>0)&&(pieceSelection<3)){ int moveBishop; int[] getterRowColumn=new int[2]; System.out.println("Your selected "+pieceName+" no :"+pieceSelection+"\nIs in"+"\nRow :"+(initialRowPosition[pieceSelection-1]+1)+"\t\tColumn :"+(initialColumnPosition[pieceSelection-1]+1)); System.out.println("This "+pieceName+" can move in"); move(); System.out.println("How would you like to move {Press(1-->4)}"); System.out.println("Your Selection : "); moveBishop = input.nextInt(); getterRowColumn = seterRowColumn(moveBishop,pieceSelection); if((getterRowColumn[1]>=0)&&(getterRowColumn[1]<=7)&&((getterRowColumn[0]>=0)&&(getterRowColumn[0]<=7))){ System.out.println("Your selected "+pieceName+"\n"); System.out.println("Now is in"+"\n-----\nRow :"+(getterRowColumn[0]+1)+" AND\nColumn :"+(getterRowColumn[1]+1)+"\n-----\n"); Start.display(getterRowColumn[0]+1, getterRowColumn[1]+1, "B"); }else{ System.out.println("Can note move"); finalRowPosition[7]=initialRowPosition[7]; finalColumnPosition[3]=initialColumnPosition[3]; } } } private int[] seterRowColumn(int moveBishop, int pieceSelection) { switch(moveBishop){ case 1:{ if(pieceSelection==1){ System.out.println("You Select Right Diagonal up \n You have these choices "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1;//a finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1;//b while((!(finalColumnPosition[pieceSelection-1]==1))||(finalRowPosition[pieceSelection-1]==1)){ finalColumnPosition[pieceSelection-1]--; finalRowPosition[pieceSelection-1]--; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1]=initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[pieceSelection-1]){ finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; initialRowPosition[pieceSelection-1]=row; } finalColumnPosition[pieceSelection-1]--; finalRowPosition[pieceSelection-1]--; } } else if (pieceSelection==2){ System.out.println("You Select Right Diagonal up \n You have these choices "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1;//a finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1;//b while(!(finalColumnPosition[pieceSelection-1]==1)){ finalColumnPosition[pieceSelection-1]--; finalRowPosition[pieceSelection-1]--; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[pieceSelection-1]){ finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; initialRowPosition[pieceSelection-1]=row; } finalColumnPosition[pieceSelection-1]--; finalRowPosition[pieceSelection-1]--; } } break; } case 2:{ if(pieceSelection==1){ System.out.println("You Select Left Diagonal down \n You have these choices "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1;//a finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1;//b while((!(finalColumnPosition[pieceSelection-1]==8))&&(!(finalRowPosition[pieceSelection-1]==8))){ finalColumnPosition[pieceSelection-1]++; finalRowPosition[pieceSelection-1]++; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[pieceSelection-1]){ finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; initialRowPosition[pieceSelection-1]=row; } finalColumnPosition[pieceSelection-1]++; finalRowPosition[pieceSelection-1]++; } } else if (pieceSelection==2){ System.out.println("You Select Left Diagonal down \n You have these choices "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1;//a finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1;//b while((!(finalColumnPosition[pieceSelection-1]==8))&&(!(finalRowPosition[pieceSelection-1]==8))){ finalColumnPosition[pieceSelection-1]++; finalRowPosition[pieceSelection-1]++; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[pieceSelection-1]){ finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; initialRowPosition[pieceSelection-1]=row; } finalColumnPosition[pieceSelection-1]++; finalRowPosition[pieceSelection-1]++; } } break; } case 3:{ if(pieceSelection==1){ System.out.println("You Select Right Diagonal down \n You have these choices "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1;//a finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1;//b while((!(finalColumnPosition[pieceSelection-1]==1))&&(!(finalRowPosition[pieceSelection-1]==8))){ finalColumnPosition[pieceSelection-1]--; finalRowPosition[pieceSelection-1]++; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[pieceSelection-1]){ finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; initialRowPosition[pieceSelection-1]=row; } finalColumnPosition[pieceSelection-1]--; finalRowPosition[pieceSelection-1]++; } } else if (pieceSelection==2){ System.out.println("You Select Right Diagonal down \n You have these choices "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1;//a finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1;//b while((!(finalColumnPosition[pieceSelection-1]==1))&&(!(finalRowPosition[pieceSelection-1]==8))){ finalColumnPosition[pieceSelection-1]--; finalRowPosition[pieceSelection-1]++; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[pieceSelection-1]){ finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; initialRowPosition[pieceSelection-1]=row; } finalColumnPosition[pieceSelection-1]--; finalRowPosition[pieceSelection-1]++; } } break; } case 4:{ if(pieceSelection==1){ System.out.println("You Select Left Diagonal up \n You have these choices "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1;//a finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1;//b while((!(finalColumnPosition[pieceSelection-1]==8))&&(!(finalRowPosition[pieceSelection-1]==1))){ finalColumnPosition[pieceSelection-1]++; finalRowPosition[pieceSelection-1]--; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[pieceSelection-1]){ finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; initialRowPosition[pieceSelection-1]=row; } finalColumnPosition[pieceSelection-1]++; finalRowPosition[pieceSelection-1]--; } } else if (pieceSelection==2){ System.out.println("You Select Left Diagonal up \n You have these choices "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]+1;//a finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]+1;//b while((!(finalColumnPosition[pieceSelection-1]==8))&&(!(finalRowPosition[pieceSelection-1]==1))){ finalColumnPosition[pieceSelection-1]++; finalRowPosition[pieceSelection-1]--; System.out.println("Row :"+finalRowPosition[pieceSelection-1]+"\t\tColumn :"+finalColumnPosition[pieceSelection-1]); } System.out.println("In which Row do you want to move\n Row: "); finalColumnPosition[pieceSelection-1] =initialColumnPosition[pieceSelection-1]; finalRowPosition[pieceSelection-1]=initialRowPosition[pieceSelection-1]; input = new Scanner(System.in); int row = input.nextInt(); row = row-1; for (int l=0;l<8;l++){ if(row == finalRowPosition[pieceSelection-1]){ finalPossition[finalRowPosition[pieceSelection-1]][finalColumnPosition[pieceSelection-1]]=1; initialPossition[initialRowPosition[pieceSelection-1]][initialColumnPosition[pieceSelection-1]]=0; initialColumnPosition[pieceSelection-1]=finalColumnPosition[pieceSelection-1]; initialRowPosition[pieceSelection-1]=row; } finalColumnPosition[pieceSelection-1]++; finalRowPosition[pieceSelection-1]--; } } break; } default :{ System.out.println("Invalid Selection"); } break; }//End Switch int[] returnArray=new int[2]; returnArray[0]=initialRowPosition[pieceSelection-1]; returnArray[1]=initialColumnPosition[pieceSelection-1]; return returnArray; } void viewRowColumn() { for(int i=0;i<noOfBishop;i++){ int c=i+1; int b =initialRowPosition[i]+1; int a =initialColumnPosition[i]+1; System.out.println("Piece no :"+c+"\n\t\tIs in"+"\n\t\tRow :"+b); System.out.println("\t\tColumn :"+a); } } public void move() { super.move(); System.out.println("in dignals"); System.out.println("1 : Right up diagonal "); System.out.println("2 : Left down diagonal "); System.out.println("3 : Right down diagonal "); System.out.println("4 : Left up diagonal "); } }
0 comments:
Post a Comment