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 && dst.getX() - src.getX() == 1 && (colordst != colorsrc) && !fieldObject.checkEmpty(dst)) { //fieldObject or dst.checkempty?
fieldObject.conquer(src, dst);
System.out.printf("Successfully beaten %s and moved to %c%d", StringfigureChecker(dst), dst.getX(), dst.getY());
} else if (((dst.getX() - src.getX()) == 0) && ((dst.getY() - src.getY()) == 1)) { //for the sake of testing, rework later for proper +1, +2 movements
fieldObject.conquer(src, dst);
System.out.printf("Successfully moved to %c%d", dst.getX(), dst.getY());
} else System.out.println("notpossibleblablatest"); //just to check if basic movements work, complete latere
break;
default:
System.out.println("defaultcase");
break;
}
}
}
Then here's my Field
class (left out some getters and setters to make it less long):
public class Field {
private char X;
private int Y;
private String figure;
private boolean color; // true = white
private boolean empty; // true = field is empty
public Field (char X, int Y) {
this.X = X;
this.Y = Y;
}
public String getFigure(Field a) {
return a.figure;
}
public boolean getColor(Field a) {
if (a.color) return true;
else return false;
}
public boolean checkEmpty(Field a) {
if (a.empty) return true;
else return false;
}
public void makeEmpty (Field a) {
a.figure = null;
a.empty = true;
}
public void conquer(Field src, Field dst) {
dst.figure = src.figure;
dst.color = src.color;
}
}
Main
(only building an array of Field
s so far to create the playfield):
public class Main {
public static void main(String[] args) {
char fieldChar = 'a';
int fieldNumber = 0;
Field[][] playfield = new Field[8][8]; //creating 8x8 playfield
playfield[0][0] = new Field('a', 0);
int j = 0;
for (char i = 0; i < 8; i++){
fieldChar = (char) (i + 65);
for (j = 0; j < 8; j++) {
playfield[i][j] = new Field(fieldChar, fieldNumber + j +1);
}
}
}
I'm not looking as much for very technical optimizations that I might not understand yet, but would appreciate advice on how to improve the structure and how to be smarter about the coding. I'd like to take every possible hint I can to learn as much I can from my mistakes or too long winded attempts of coding certain things.
I'd also like to know if it would be at all useful to use an interface or to split things into more classes to program this Chess game that I try to build.
I also realise I don't really know when and how to best use the constructor. I think it's completely unneeded for me to use my constructor to initialize my Fields. Could it not have just been done in the main loop?
Future Plans
As of now it's only these 3 classes and I plan on making a big switch case for the different figures in Chess to:
- check if the requested movement is possible + allowed
- maybe add a 4th class to check the game state after every move
- see if there's checkmate, check or castling is possible
- maybe add a few algorithms that save the current game states, so that in the next states not everything has to be recalculated (but most of that after I have a completely functioning program already)