Question
I've written all the classes but the final class. Game. I've provided attached the project, and the skeleton class. You'll create the Game class and
I've written all the classes but the final class. Game. I've provided attached the project, and the skeleton class.
You'll create the Game class and use the Start method to play a full game of checkers.
You'll ask the user for
1. A piece name
2. X coordinate
3. Y coordinate.
4. Ensure proper eror handling and exception catching (make sure the user is forced to answer the question correctly).
5. Move the piece.
6. Switch turns
7. Continue until the game is over.
All of this functionality is written into the classes, you just need to wire it together.
Below i will list the code provided. I seperated by 7 classes please fill in main. Thank you
Main.java/*fill in*/
public class Main { public static void main(String[] args) { Game myGame = new Game();
myGame.Start();
} }
Board.java
import java.io.Serializable; import java.util.ArrayList; import java.util.Objects; import java.util.concurrent.TimeUnit;
public class Board {
Square[][] squares = new Square[8][8];
public Square[][] getSquares() { return squares; }
public void setSquares(Square[][] squares) { this.squares = squares; }
private void setBoard() { for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { squares[x][y] = new Square(); } switch (x) { case 0: setOddCols(x, "R"); break; case 1: setEvenCols(x, "R"); break; case 2: setOddCols(x, "R"); break; case 3: setAllBlank(x); break; case 4: setAllBlank(x); break; case 5: setEvenCols(x, "W"); break; case 6: setOddCols(x, "W"); break; case 7: setEvenCols(x, "W"); break; } } }
public String seralizeBoard(){ String line = "board:"; for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { line = line + squares[x][y].getPiece().getPieceName(); } line = line + " "; } return line ;
}
private void setEvenCols(int row, String color){ int forward = 0; int jump =0; if (color.equals("R")){ forward = 1; jump = 2; }else if (color.equals("W")){ forward = -1; jump = -2; } for( int y = 0;y<8;y++){ if(y%2==0){ Piece thisPiece = new RegularPiece(color,row,y,forward,jump); setPieceOnSpace(thisPiece,row,y);; thisPiece.setX(row); thisPiece.setY(y); }else{ RegularPiece emptyPiece = new RegularPiece("", row, y, 0, 0); setPieceOnSpace(emptyPiece, row, y); } } }
private void setOddCols(int row, String color){ int forward = 0; int jump =0; if (color.equals("R")){ forward = 1; jump = 2; }else if (color.equals("W")){ forward = -1; jump = -2; } for( int y = 0;y<8;y++){ if(y%2!=0){ Piece thisPiece = new RegularPiece(color,row,y,forward,jump); setPieceOnSpace(thisPiece,row,y);; thisPiece.setX(row); thisPiece.setY(y); }else{ RegularPiece emptyPiece = new RegularPiece("", row, y, 0, 0); setPieceOnSpace(emptyPiece, row, y); } } }
private void setAllBlank(int row){ for( int y = 0;y<8;y++){ RegularPiece emptyPiece = new RegularPiece("", row, y, 0, 0); setPieceOnSpace(emptyPiece, row, y); } }
public String serializeBoard(){ String line = "board:";
for (int x = 0; x < 8; x++) { for (int y = 0; y < 8; y++) { line = line + squares[x][y].getPiece().getDisplayName(); } line = line + " "; } line = line + ""; return line; }
//print the board public ArrayList
public Piece getPieceByName(String pieceName, Player player)throws InterruptedException{ ArrayList
for(Piece p : listOPieces){ if(Objects.isNull(p.getPieceName())){ returnPiece = null; System.out.println("The piece doesn't exist"); TimeUnit.SECONDS.sleep(1); } if(p.getPieceName().equals(pieceName) && player.getColor().equals(p.getColor())){ returnPiece = p; return returnPiece; }else{ returnPiece = null; } } String col = player.getColor(); return returnPiece; } public Piece getPieceByCoord(int x, int y){ return this.squares[x][y].getPiece(); }
public boolean isSquareBlank(int x, int y){ //System.out.println(squares[y][y].getPiece().getPieceName()); if(squares[x][y].getPiece().getPieceName().equals("")){ return true; }else{ return false; } }
public void setPieceOnSpace(Piece piece, int x, int y) { squares[x][y].setPiece(piece); }
public void removePieceOnSpace(int x, int y) { RegularPiece emptyPiece = new RegularPiece("", x, y, 0, 0); setPieceOnSpace(emptyPiece, x, y); }
public Board() { setBoard(); }
private class Square implements Serializable { private Piece piece; private int x; private int y;
public Piece getPiece() { return piece; }
public void setPiece(Piece piece) { this.piece = piece; }
public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
public Square() {
}
public Square(Piece piece, int x, int y) { this.piece = piece; this.x = x; this.y = y; } } }
Game.java
import java.util.ArrayList; import java.util.Scanner;
public class Game { public Board gameBoard; private Player RedPlayer; private Player WhitePlater; private Player current_player; private Player next_player;
public Board getGameBoard() { return gameBoard; }
public void setGameBoard(Board gameBoard) { this.gameBoard = gameBoard; }
public Player getRedPlayer() { return RedPlayer; }
public void setRedPlayer(Player redPlayer) { RedPlayer = redPlayer; }
public Player getWhitePlater() { return WhitePlater; }
public void setWhitePlater(Player whitePlater) { WhitePlater = whitePlater; }
public Player getCurrent_player() { return current_player; }
public void setCurrent_player(Player current_player) { this.current_player = current_player; }
public void Start(){ boolean gameOver = false; boolean validMove = false; ArrayList
this.gameBoard.removePieceOnSpace(6,1); this.gameBoard.removePieceOnSpace(7,2); this.gameBoard.removePieceOnSpace(5,4); Piece newPiece = new RegularPiece("R",6,1,1,2);
this.gameBoard.setPieceOnSpace(newPiece,6,1);
Scanner myScanner = new Scanner(System.in); this.next_player = Player.get_player_not_turn(this.RedPlayer,this.WhitePlater);
while(!gameOver){ //write code to move pieces and process input } this.gameBoard.ShowBoard(); if(this.RedPlayer.isWon()){ System.out.println("Red won!"); }else if(this.WhitePlater.isWon()){ System.out.println("White won!"); }else{ System.out.println("Game is running"); }
}
public Game(){ this.gameBoard = new Board(); this.RedPlayer = new Player("R"); this.WhitePlater = new Player("W"); this.RedPlayer.setTurn(true); this.WhitePlater.setTurn(false); } }
KingPiece.Java
public class KingPiece extends RegularPiece implements Piece{
public KingPiece(String color, int x, int y) { super(); this.setColor(color); this.setX(x); this.setY(y); this.setDisplayName("_K" + color + x + "-" + y + "_"); this.setPieceName("K" + color + x + "-" + y);
}
public boolean Move(Board b, int x, int y) {
boolean ret_val = false;
Piece jumpPiece = b.getPieceByCoord((this.getX() + (this.getFoward())), (this.getJumpY())); String jumpPieceColor = jumpPiece.getColor();
int jump_x = getJumpDirectX(x);
if (this.getY() < y) { this.setJumpY(Math.abs(y - 1)); } else { this.setJumpY(Math.abs(y + 1)); }
boolean retVal = false; if (Math.abs(y - this.getY()) == 1 && (Math.abs(x - this.getX()) == 1)) { b.removePieceOnSpace(this.getX(), this.getY()); b.setPieceOnSpace(this, x, y); this.setX(x); this.setY(y); ret_val = true; } else if (Math.abs(y - this.getY()) == 2 && (Math.abs(x - this.getX()) == 2) && !jumpPieceColor.equals(this.getColor())) { System.out.println("jump boy"); //remove jumped piece b.removePieceOnSpace(jump_x, this.getJumpY()); //remove the piece we moved b.removePieceOnSpace(this.getX(), this.getY()); //set the piece down we moved b.setPieceOnSpace(this, x, y); this.setX(x); this.setY(y); ret_val = true; }
return ret_val;
}
public int getJumpDirectX(int end_x_posit){ int jmpX = 0; if(this.getX() < end_x_posit){ jmpX = end_x_posit - 1; }else{ jmpX = end_x_posit + 1; } return jmpX; } }
Piece.java
public interface Piece {
public String getPieceName(); public int getX(); public void setX(int xCord); public void setY(int yCord); public int getY(); public String getColor(); public int getFoward(); public int getFoward_jump(); public void setPieceName(String fullName); public String getDisplayName(); public boolean Move(Board b, int x, int y);
}
Player.java
import java.util.ArrayList;
public class Player { public String color; public boolean turn;
public boolean won;
public boolean isWon() { return won; }
public void setWon(boolean won) { this.won = won; }
public boolean isTurn() { return turn; }
public static void flipTurn(ArrayList
public void flipTurn(Player player_not_turn){ this.setTurn(false); player_not_turn.setTurn(true); }
public static Player get_player_turn(Player p1, Player p2){ if(p1.isTurn()){ return p1; }else{ return p2; } }
public static Player get_player_not_turn(Player p1, Player p2){ if(!p1.isTurn()){ return p1; }else{ return p2; } }
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public void setTurn(boolean turn) { this.turn = turn; }
public Player (String color){ this.color=color; }
public Player(){
} }
RegularPiece.java
public class RegularPiece implements Piece { private String pieceName = ""; private String displayName = ""; private String color = ""; private int x = 0; private int y = 0; private int foward = 0; private int foward_jump = 0; private int jumpY = 0; private int king_row = 0;
public String getPieceName() { return pieceName; }
public void setPieceName(String pieceName) { this.pieceName = pieceName; }
public String getDisplayName() { return displayName; }
public void setDisplayName(String displayName) { this.displayName = displayName; }
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public int getX() { return x; }
public void setX(int x) { this.x = x; }
public int getY() { return y; }
public void setY(int y) { this.y = y; }
public int getFoward() { return foward; }
public void setFoward(int foward) { this.foward = foward; }
public int getFoward_jump() { return foward_jump; }
public void setFoward_jump(int foward_jump) { this.foward_jump = foward_jump; }
public int getJumpY() { return jumpY; }
public void setJumpY(int jumpY) { this.jumpY = jumpY; }
public int getKing_row() { return king_row; }
public void setKing_row(int king_row) { this.king_row = king_row; }
public boolean Move(Board b, int x, int y) { boolean ret_val = false;
if (this.y < y) { jumpY = Math.abs(y - 1); } else { jumpY = Math.abs(y + 1); }
Piece jumpedPiece = b.getPieceByCoord(this.x + (this.foward), jumpY);
//basic movement if ((Math.abs(y - this.y) == 1) && (x - this.x == this.foward) && b.isSquareBlank(x, y)) { b.removePieceOnSpace(this.x, this.y); b.setPieceOnSpace(this, x, y); this.x = x; this.y = y; if (this.x == this.king_row) { upgradeToKing(b); } ret_val = true; } else if(Math.abs(y - this.y) == 2 && (x - this.x == this.foward_jump) && !jumpedPiece.equals(this.color)) { //remove jumped piece b.removePieceOnSpace((this.x + (this.foward)), jumpY); //remove the piece we moved b.removePieceOnSpace(this.x, this.y); //set the piece down we moved b.setPieceOnSpace(this, x, y); this.x = x; this.y = y; if (this.x == this.king_row) { upgradeToKing(b); } //is the current x coordinate a king row? ret_val = true; } return ret_val;
}
private void upgradeToKing(Board b) { b.removePieceOnSpace(this.x, this.y); Piece king_piece = new KingPiece(this.color, this.x, this.y); b.setPieceOnSpace(king_piece,this.x,this.y); }
public RegularPiece() {
}
public RegularPiece(String color, int x, int y, int fwrd, int jmp) { this.color = color; this.x = x; this.y = y; this.foward = fwrd; this.foward_jump = jmp;
if (color.equals("R")) { this.king_row = 7; } else if (color.equals("W")) { this.king_row = 0; }
// if (this.color.equals("")) { this.displayName = "__" + x + "-" + y + "__"; this.pieceName = ""; } else { this.displayName = "_" + color + x + "-" + y + "__"; this.pieceName = color + x + "-" + y; }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started