I'm currently just working on a Chess game project to improve my object-oriented skills. It is currently only at the starting stages and there is quite a bit missing (such as error checking) but I thought before I go any further I should get some advice on if I am using correct practices and if there's anything I could be doing better.
Game.java
public class Game {
public static void main(String [] args)
{
//Creates players and board
Player p1 = new Player();
Player p2 = new Player();
Board board = new Board();
board.displayBoard();
//Return players command coordinates (current and target)
Coord[] command = p1.getCommand();
//Gets piece from supplied coordinates
Piece piece = board.getPiece(command[0]);
if (piece != null && piece.getColor() == p1.getColor())
{
board.movePiece(piece, command[1]);
}
board.displayBoard();
}
}
Player.java
import java.util.Scanner;
public class Player {
private boolean white;
private boolean turn;
static boolean whiteChosen = false;
public Player()
{
if (whiteChosen)
white = turn = false;
else
white = turn = whiteChosen = true;
}
public Coord[] getCommand()
{
Scanner input = new Scanner(System.in);
char[] command;
do
{
System.out.print("Move: ");
command=input.nextLine().toCharArray();
System.out.println();
} while (!validFormat(command));
input.close();
int x1, x2, y1, y2;
Coord pos1, pos2;
x1 = (int) command[0] - 97;
x2 = (int) command[3] - 97;
y1 = (int) (command[1] - '0' - 8) * -1;
y2 = (int) (command[4] - '0' - 8) * -1;
pos1 = new Coord(x1, y1);
pos2 = new Coord(x2, y2);
return new Coord[] {pos1, pos2};
}
boolean validFormat(char[] command)
{
return true;
}
public boolean getColor()
{
return this.white;
}
}
Board.java
public class Board {
private Piece[][] pieces;
public final int DIM = 8;
private final String[] backline = {"ROOK", "KNIGHT", "BISHOP", "QUEEN", "KING", "BISHOP", "BISHOP", "ROOK"};
//Sets up the board
public Board()
{
pieces = new Piece[8][8];
for (int y = 0; y < DIM; y++)
{
for (int x = 0; x < DIM; x++)
{
if (y == 0 || y == 7)
pieces[x][y] = new Piece(backline[x], new int[] {x, y}, true);
else if (y == 1 || y == 6)
pieces[x][y] = new Piece("PAWN", new int[] {x, y}, true);
}
}
}
//Returns piece at given coordinates
public Piece getPiece(Coord pos)
{
return pieces[pos.getX()][pos.getY()];
}
//Moves piece to given coordinates
public void movePiece(Piece piece, Coord pos)
{
this.pieces[piece.getPosition().getX()][piece.getPosition().getY()] = null;
this.pieces[pos.getX()][pos.getY()] = piece.setPosition(pos);
}
public void displayBoard()
{
for (int y = 0; y < DIM; y++)
{
for (int x = 0; x < DIM; x++)
{
if (pieces[x][y] != null)
System.out.print(pieces[x][y].getType() + " ");
else
System.out.print("NULL ");
}
System.out.println();
}
}
}
Piece.java
public class Piece {
private String type;
private Coord position;
private boolean white;
private boolean unmoved;
//Initializes a piece.
public Piece(String type, int[] position, boolean white)
{
this.type = type;
this.position = new Coord(position[0], position[1]);
this.white = white;
this.unmoved = true;
}
public Piece setPosition(Coord pos)
{
this.position = pos;
return this;
}
public String getType()
{
return this.type;
}
public Coord getPosition()
{
return this.position;
}
public boolean getColor()
{
return this.white;
}
}
Coord.java
public class Coord {
private int x;
private int y;
public Coord(int x, int y)
{
setPosition(x, y);
}
public void setPosition(int x, int y)
{
this.x = x;
this.y = y;
}
public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
}