Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

**I have the first half of the code for CheckersLogic, however I need the other half which would be CheckersTextConsole which should include the main

**I have the first half of the code for CheckersLogic, however I need the other half which would be CheckersTextConsole which should include the main method and call the methods.**

Checkers Game: Checkers is a strategy board game for two players which involve diagonal moves of uniform game pieces and mandatory captures by jumping over opponent pieces. It is played by two opponents, on opposite sides of the 8x8 checkered gameboard. One player has the dark pieces (or x tokens); the other has the light pieces (or o tokens). Each player has 12 pieces. Players alternate turns. A player may not move an opponent's piece. A move consists of moving a piece diagonally to an adjacent unoccupied square. If the adjacent square contains an opponent's piece, and the square immediately beyond it is vacant, the piece may be captured (and removed from the game) by jumping over it. Only the dark squares of the checkered board are used. A piece may move only diagonally into an unoccupied square. The player without pieces remaining, or who cannot move due to being blocked, loses the game. Pieces move one step diagonally forwards, and capture an opponent's piece by moving two consecutive steps in the same line, jumping over the piece on the first step. Multiple enemy pieces can be captured in a single turn provided this is done by successive jumps made by a single piece; the jumps do not need to be in the same line and may "zigzag" (change diagonal direction). Pieces can move/jump only in forward direction. Aim of the game: is to capture all the opponents pieces or render them unable to move. How the game ends: The first player to lose all of his or her pieces loses the game. If a player is put in a position where they cannot move, they lose.Reference: https://simple.wikipedia.org/wiki/Checkers ---------------------------------------------------------------------------------------------------- Program Requirements: Implement a Java-based Checkers game to be hosted in the ARENA Game system in the future. In this first version build it as a simple console-based game played by 2 players Player X and Player O. Ensure that following: Make use of good Object-Oriented design Provide documentation using Javadoc and appropriate comments in your code. Generate HTML documentation using Javadoc tool Create 2 packages core and ui. Create a separate class for the game logic called CheckersLogic.java and place it in core package Create a separate class for the text-based UI called CheckersTextConsole.java and place it in ui package Create a simple console-based UI as shown in the figures below. When the game starts, indicate that it is Player Xs turn. Ask the player to choose a piece to move by indicating the cell# followed by the new position, e,g, 3a-4b. Check if the move is valid. If valid move, then show the state of the grid by placing the piece in the correct position (after capturing opponent piece, where applicable). Next check for WIN/LOSE state (i.e., if one of the players has no pieces left, or if one of the players is unable to make any legal move). Continue the game if valid moves are possible. Next, indicate that it is Player Os turn. Ask the player to choose a piece to move by indicating the cell# followed by the new position, e.g., 3a-4b. A player wins the game when the opponent cannot make a move. This could be the case because all of the opponent's pieces have been captured or because all of opponents pieces are blocked in. Only allow moving forward. You dont have to implement Kinging or Crowning or Double piece features. 8 | _ | o | _ | o | _ | o | _ | o | 7 | o | _ | o | _ | o | _ | o | _ | 6 | _ | o | _ | o | _ | o | _ | o | 5 | _ | _ | _ | _ | _ | _ | _ | _ | 4 | _ | _ | _ | _ | _ | _ | _ | _ | 3 | x | _ | x | _ | x | _ | x | _ | 2 | _ | x | _ | x | _ | x | _ | x | 1 | x | _ | x | _ | x | _ | x | _ | a b c d e f g h

Code:

public class CheckersLogic { //properties private char[][] board; //the playing board private int playerX; //player X (1) private int playerO; //player O (2) //constructor public CheckersLogic(){ board = new char[8][8]; playerX = 1; playerO = 2; //set up the board for(int i=0; i<8; i++){ for(int j=0; j<8; j++){ if(i==0 || i==2){ if(j%2 == 0) board[i][j] = 'x'; else board[i][j] = '_'; } else if(i==1){ if(j%2 == 0) board[i][j] = '_'; else board[i][j] = 'x'; } else if(i==7 || i==5){ if(j%2 == 0) board[i][j] = 'o'; else board[i][j] = '_'; } else if(i==6){ if(j%2 == 0) board[i][j] = '_'; else board[i][j] = 'o'; } else{ board[i][j] = '_'; } } } } //methods public void printBoard(){ for(int i=0; i<8; i++){ System.out.print(8-i + " | "); for(int j=0; j<8; j++){ System.out.print(board[7-i][j] + " | "); } System.out.println(); } System.out.println(" a b c d e f g h"); } public boolean isValidMove(int player, int x1, int y1, int x2, int y2){ //check if the move is within the board if(x1<0 || x1>7 || x2<0 || x2>7 || y1<0 || y1>7 || y2<0 || y2>7) return false; //check if the move is valid if(!validMove(player, x1, y1, x2, y2)) return false; //check if the square is occupied if(board[x2][y2] != '_') return false; return true; } public boolean validMove(int player, int x1, int y1, int x2, int y2){ //player X moves forward if(player == playerX){ if(x2 == x1+1 && (y2 == y1-1 || y2 == y1+1)) return true; else return false; } //player O moves backward else if(player == playerO){ if(x2 == x1-1 && (y2 == y1-1 || y2 == y1+1)) return true; else return false; } else return false; } public void movePiece(int player, int x1, int y1, int x2, int y2){ //check if piece belongs to player if(player == playerX && board[x1][y1] == 'x' || player == playerO && board[x1][y1] == 'o'){ board[x2][y2] = board[x1][y1]; board[x1][y1] = '_'; //check for captures if(player == playerX){ if(x2 == x1+2 && y2 == y1-2 && board[x2-1][y2+1] == 'o'){ board[x2-1][y2+1] = '_'; } else if(x2 == x1+2 && y2 == y1+2 && board[x2-1][y2-1] == 'o'){ board[x2-1][y2-1] = '_'; } } else if(player == playerO){ if(x2 == x1-2 && y2 == y1-2 && board[x2+1][y2+1] == 'x'){ board[x2+1][y2+1] = '_'; } else if(x2 == x1-2 && y2 == y1+2 && board[x2+1][y2-1] == 'x'){ board[x2+1][y2-1] = '_'; } } System.out.println("Move successful!"); } else System.out.println("Invalid move!"); } public int checkWin(){ int countX=0, countO=0; //check if there are still pieces left for(int i=0; i<8; i++){ for(int j=0; j<8; j++){ if(board[i][j] == 'x') countX++; else if(board[i][j] == 'o') countO++; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems Uropean Conference Adbis 2020 Lyon France August 25 27 2020 Proceedings Lncs 12245

Authors: Jerome Darmont ,Boris Novikov ,Robert Wrembel

1st Edition

3030548317, 978-3030548315

More Books

Students also viewed these Databases questions

Question

Movie ticketing system can you want do it task analysis using HTA ?

Answered: 1 week ago

Question

Explain the nature of human resource management.

Answered: 1 week ago

Question

Write a note on Quality circles.

Answered: 1 week ago

Question

Describe how to measure the quality of work life.

Answered: 1 week ago

Question

4 How can you create a better online image for yourself?

Answered: 1 week ago