patternjavaMinor
Beginnings of a Chess game
Viewed 0 times
chessgamebeginnings
Problem
I'm pretty new to programming (haven't written much code myself, only read code for my computer science studies etc) and I keep getting confused by how to best structure my code.
Let me get a bit more specific by including the code of a Chess game I started to program today (want it to fully function text based, GUI maybe later):
```
/**
* Created by Max on 03/08/2016.
*/
public class Figures {
public String name;
private Field fieldObject = new Field('0', 0);
public Figures(String name) {
this.name = name;
};
public void move(Field src, Field dst) { // get name and the color of figures from src and dst + calls another
String figurename = StringfigureChecker(src); // move function to actually do it (so i can move the long switch
boolean colordst = colorfigureChecker(dst); // case to the bottom
boolean colorsrc = colorfigureChecker(src);
if (!src.checkEmpty()) {
moveFigure(colordst, colorsrc, figurename, src, dst);
}
else System.out.println("Can't move nothing, son");
}
public boolean colorfigureChecker (Field a) { //returns color of field a
return a.getColor(a);
}
public String StringfigureChecker (Field a) { // returns figure name of field a
return a.getFigure(a);
}
public void moveFigure(boolean colordst, boolean colorsrc, String figure, Field dst, Field src) {
switch (figure) {
case "Rookie":
//if (colorsrc) - implement later to differentiate between white and black movements (forward only)
if ((dst.getY() - src.getY()) > 2) {
System.out.println("Rookie can't move more than two steps, ever.");
} else if (dst.getX() - src.getX() > 1) {
System.out.println("Rookie can't move more than one step to the side, ever.");
} else if (dst.getX() - src.getX() == 1
Let me get a bit more specific by including the code of a Chess game I started to program today (want it to fully function text based, GUI maybe later):
```
/**
* Created by Max on 03/08/2016.
*/
public class Figures {
public String name;
private Field fieldObject = new Field('0', 0);
public Figures(String name) {
this.name = name;
};
public void move(Field src, Field dst) { // get name and the color of figures from src and dst + calls another
String figurename = StringfigureChecker(src); // move function to actually do it (so i can move the long switch
boolean colordst = colorfigureChecker(dst); // case to the bottom
boolean colorsrc = colorfigureChecker(src);
if (!src.checkEmpty()) {
moveFigure(colordst, colorsrc, figurename, src, dst);
}
else System.out.println("Can't move nothing, son");
}
public boolean colorfigureChecker (Field a) { //returns color of field a
return a.getColor(a);
}
public String StringfigureChecker (Field a) { // returns figure name of field a
return a.getFigure(a);
}
public void moveFigure(boolean colordst, boolean colorsrc, String figure, Field dst, Field src) {
switch (figure) {
case "Rookie":
//if (colorsrc) - implement later to differentiate between white and black movements (forward only)
if ((dst.getY() - src.getY()) > 2) {
System.out.println("Rookie can't move more than two steps, ever.");
} else if (dst.getX() - src.getX() > 1) {
System.out.println("Rookie can't move more than one step to the side, ever.");
} else if (dst.getX() - src.getX() == 1
Solution
Rather than a single class for
Then implement all your piece classes:
As for the main program, I would have a
By the time you are done, you will most definitely need more than four classes, and you should use interfaces to show how classes are related (and a class can implement multiple interfaces). An important note when developing a project like this--you should research unit testing and unit test everything.
Figures, you should create a class for each "figure" (typically known as "pieces"). This class should implement an interface of all the members that all the pieces should know about collectively (see this SO answer for an idea on how to implement Coordinate):interface IPiece {
boolean isValidMove(Coordinate current, Coordinate to);
Color getPieceColor();
}Then implement all your piece classes:
public class Rook implements IPiece {
public Rook(Color color) {
// set things up here
}
boolean IsValidMove(Coordinate current, Coordinate to) {
// check that the coordinates are not the same
// check that the move does not go off the board,
// whatever coordinate you are using for the edge of the board
// you may wish to expose this as a static value of your `Field` class
// check that the move is valid--for example,a rook can only move in straight lines
// this means that either the coordinate `X` or `Y` values will match
}
public Color getPieceColor() {
return _pieceColor;
}
}As for the main program, I would have a
Board (or Field) class to represent the state of the board, a set of enums for representing different things, such as piece color, and a main controller class to start the program and keep track of the state. Your controller class will keep an instance of the board, track who's turn it is, and so on.By the time you are done, you will most definitely need more than four classes, and you should use interfaces to show how classes are related (and a class can implement multiple interfaces). An important note when developing a project like this--you should research unit testing and unit test everything.
Code Snippets
interface IPiece {
boolean isValidMove(Coordinate current, Coordinate to);
Color getPieceColor();
}public class Rook implements IPiece {
public Rook(Color color) {
// set things up here
}
boolean IsValidMove(Coordinate current, Coordinate to) {
// check that the coordinates are not the same
// check that the move does not go off the board,
// whatever coordinate you are using for the edge of the board
// you may wish to expose this as a static value of your `Field` class
// check that the move is valid--for example,a rook can only move in straight lines
// this means that either the coordinate `X` or `Y` values will match
}
public Color getPieceColor() {
return _pieceColor;
}
}Context
StackExchange Code Review Q#136789, answer score: 4
Revisions (0)
No revisions yet.