0

I am designing a chess game using OO principles;. Suggestions on its improvement; like add of an interface class or other method

//board.java public abstract class board { public abstract void move(int x, int y); } //Game.java import java.util.Scanner; public class Game { public static void main(String[] args) { rook R = new rook(); System.out.println(" Rook your position is 4x4, Whats your next move?"); Scanner s = new Scanner(System.in); System.out.println("Enter Row"); int row =s.nextInt(); System.out.println("Enter column"); int column = s.nextInt(); R.move(row, column); pawn P = new pawn(); System.out.println(" Pawn your position is 5x5, Whats your next move?"); Scanner n = new Scanner(System.in); System.out.println("Enter Row"); row =n.nextInt(); System.out.println("Enter column"); column = n.nextInt(); P.move(row, column); bishop B = new bishop(); System.out.println(" Bishop your position is 3x2, Whats your next move?"); Scanner a = new Scanner(System.in); System.out.println("Enter Row"); row =a.nextInt(); System.out.println("Enter column"); column = a.nextInt(); B.move(row, column); knight K = new knight(); System.out.println(" Knight your position is 3x2, Whats your next move?"); Scanner b = new Scanner(System.in); System.out.println("Enter Row"); row =b.nextInt(); System.out.println("Enter column"); column = b.nextInt(); K.move(row, column); queen Q = new queen(); System.out.println(" Queen your position is 3x2, Whats your next move?"); Scanner c = new Scanner(System.in); System.out.println("Enter Row"); row =c.nextInt(); System.out.println("Enter column"); column = c.nextInt(); Q.move(row, column); king G = new king(); System.out.println("King your position is 3x2, Whats your next move?"); Scanner d = new Scanner(System.in); System.out.println("Enter Row"); row =d.nextInt(); System.out.println("Enter column"); column = d.nextInt(); G.move(row, column); } } //pawn.java public class pawn extends board { public void move(in

19th Jun 2018, 6:59 PM
Don Loic
5 Réponses
+ 1
First thing I'd say is why does Pawn extend Board? Personally I'd have an interface "Piece" to represent a chess piece. What can a Piece do? Move, capture, be captured, enact special moves (en passant, castling which interacts with other pieces). Pieces also need to record their current position (in class variables) You've repeated a lot of code, so this is a good sign we can extract it somewhere to be reused. The repeated block states the position, and where you want to move so move that to a method or utility class. This can use the piece's position to output the text. Not sure if board should be abstract, but a class of its own given what I've said above. But that's just me and I might not be seeing the same design as you are in your head. Do you want the board controlling the pieces or the pieces handling themselves? In the latter case the code for ensuring valid moves belongs to each Piece class which is a really nice way of keeping the logic separate and tidy. Interested to see where you go :)
19th Jun 2018, 7:01 PM
Dan Walker
Dan Walker - avatar
+ 1
It got segmented. The Board class was extended by the chess pieces which,I now realise, should extend the Piece class, it makes more sens. What about the interface, what should it contain?
20th Jun 2018, 1:13 PM
Don Loic
+ 1
//pawn.java public class pawn extends board { public void move(int x, int y) { int p=5, o = 5; if((x==p+1||x==p+2)&& y==o) System.out.println("Right move"); else System.out.println("wrong move"); } } //bishop.java public class bishop extends board { public void move(int x, int y) { int sum=3+2,diff=3-2; if(x+y==sum || x-y==diff) System.out.println("Right move"); else System.out.println("wrong move"); } } // knight.java public class knight extends board { public void move(int x, int y) { int a = 3,b = 2; if(x==a+2&& y==b-1||x==a+2 && y == b+1||x==a-2 && y ==b-1||x==a-2 &&y==b+1 || x==a+1 && y==b-2||x==a+1 && y==b+2||x==a-1 && y==b-2||x==a-1 && y==b+2) System.out.println("Right move"); else System.out.println("wrong move"); } } //rook.java public class rook extends board { public void move(int x, int y) { int i = 4; if(x == i|| y == i) System.out.println("Right move"); else System.out.println("wrong move"); } } //queen.java public class queen { public void move(int x, int y) { int a = 5, sum=3+2,diff=3-2; if(x == a|| y == a||x+y==sum || x-y==diff) System.out.println("Right move"); else System.out.println("wrong move"); } } //king.java public class king { public void move(int x, int y) { int a = 5,b=6; if(x==a+1&& y==b-1||x==a+1 && y == b+1||x==a-1 && y ==b-1||x==a-1 &&y==b+1 || x==a && y==b-1||x==a && y==b+1||x==a+1 && y==b||x==a-1 && y==b) System.out.println("Right move"); else System.out.println("wrong move"); } }
20th Jun 2018, 1:14 PM
Don Loic
0
As I mentioned, think about what a piece can do. Move is the obvious one so the interface should have that method. You might add capture(), which I imagine could take another Piece as a parameter, which would call a remove() method of the other piece, so that's another method. So far we have interface Piece { void move(int x, int y); //could be distance or actual coordinate void capture(Piece otherPiece); void remove(); }
22nd Jun 2018, 7:59 PM
Dan Walker
Dan Walker - avatar
0
Cool Thanks !!
24th Jun 2018, 8:19 AM
Don Loic