235k views
0 votes
Can someone who is skilled at coding create me a Java chess game. Please don't copy from online source codes. Make it original thanks! :D

User Rontron
by
4.2k points

1 Answer

1 vote

copy this and fill in the blanks so you can change it up a bit



--------------------------------------------------------------------------------------------------------------

public class Game {

private Board board = new Board();

private Player white;

private Player black;

public Game() {

super();

}

public void setColorWhite(Player player) {

this.white = player;

}

public void setColorBlack(Player player) {

this.black = player;

}

public Board getBoard() {

return board;

}

public void setBoard(Board board) {

this.board = board;

}

public Player getWhite() {

return white;

}

public void setWhite(Player white) {

this.white = white;

}

public Player getBlack() {

return black;

}

public void setBlack(Player black) {

this.black = black;

}

public boolean initializeBoardGivenPlayers() {

if(this.black == null || this.white == null)

return false;

this.board = new Board();

for(int i=0; i<black.getPieces().size(); i++){

board.getSpot(black.getPieces().get(i).getX(), black.getPieces().get(i).getY()).occupySpot(black.getPieces().get(i));

}

return true;

}

}

Player.java

public class Player {

public final int PAWNS = 8;

public final int BISHOPS = 2;

public final int ROOKS = 2;

public boolean white;

private List<Piece> pieces = new ArrayList<>();

public Player(boolean white) {

super();

this.white = white;

}

public List<Piece> getPieces() {

return pieces;

}

public void initializePieces(){

if(this.white == true){

for(int i=0; i<PAWNS; i++){ // draw pawns

pieces.add(new Pawn(true,i,2));

}

pieces.add(new Rook(true, 0, 0));

pieces.add(new Rook(true, 7, 0));

pieces.add(new Bishop(true, 2, 0));

pieces.add(new Bishop(true, 5, 0));

pieces.add(new Knight(true, 1, 0));

pieces.add(new Knight(true, 6, 0));

pieces.add(new Queen(true, 3, 0));

pieces.add(new King(true, 4, 0));

}

else{

for(int i=0; i<PAWNS; i++){ // draw pawns

pieces.add(new Pawn(true,i,6));

}

pieces.add(new Rook(true, 0, 7));

pieces.add(new Rook(true, 7, 7));

pieces.add(new Bishop(true, 2, 7));

pieces.add(new Bishop(true, 5, 7));

pieces.add(new Knight(true, 1, 7));

pieces.add(new Knight(true, 6, 7));

pieces.add(new Queen(true, 3, 7));

pieces.add(new King(true, 4, 7));

}

}

}

Board.java

public class Board {

private Spot[][] spots = new Spot[8][8];

public Board() {

super();

for(int i=0; i<spots.length; i++){

for(int j=0; j<spots.length; j++){

this.spots[i][j] = new Spot(i, j);

}

}

}

public Spot getSpot(int x, int y) {

return spots[x][y];

}

}

User Avepr
by
4.8k points