Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implement an option for playing again or exiting the game: below is my board class: import java.util.ArrayList; public class Board { private ArrayList movementList =
Implement an option for playing again or exiting the game:
below is my board class:
import java.util.ArrayList;
public class Board
private ArrayList movementList new ArrayList;
public State currentState new State;
private boolean computerFirst;
public Boardboolean computerFirst
this.computerFirst computerFirst;
Turns the board into a string
private String boardString
StringBuilder result new StringBuilder;
This is for the top row, where we see the column names
result.append;
This covers the spacing between the columns
for int i ; i ; i
result.appendi ;
Newline to begin the Rows
result.append
;
Cheeky solution to print the letters.
We take the ascii values and increment up the list as we go on
int letter ;
for int i ; i ; i
result.appendchar letter ;
Spaces between each board column space
for int j ; j ; j
char marker whoMovedcurrentStateboardij;
result.appendmarker ;
Newline for the next row and letter
result.append
;
letter;
result.append
;
return result.toString;
@Override
public String toString
return stringAdjacentboardString moveListcomputerFirst; combining the board representation and the move list
public void print
System.out.printlntoString;
Add a move to the list of movements
public void addMoveMove move
movementList.addmove;
Takes movement data and will display a symbol on the board according to whether the player moved or computer moved
private static char whoMovedint mover
char result;
No Moves
if mover
result ;
return result;
Computer Moves
if mover
result X;
return result;
Player Moves
if mover
result O;
return result;
Incase there's an error
else
result ;
return result;
The literal list of moves taken
private String moveListboolean computerFirst
StringBuilder result new StringBuilder;
Selects our title
if computerFirst
result.appendPlayer vs Computer";
else
result.appendComputer vs Player";
for int i ; i movementList.size; i
String listOfMoves movementList.getitranslator;
Lists out each move. Checks for whether it's even or odd.
if i
result.append
i listOfMoves;
else
result.append listOfMoves;
return result.toString;
the board and move list need to be printed side by side, so this method splits and prints them integrated together
private String stringAdjacentString string String string
Will be used to break up the strings for the board and the movelist
String splitter
;
Dictates the placement of the player vs computer logo
String inserter ;
split the data into individual parts
String stringSplit stringsplitsplitter;
String stringSplit stringsplitsplitter;
Gets which string is the longest
int maxLength ;
for String s : stringSplit
if maxLength slength
maxLength slength;
Goes through and rebuilds the string with strings and combined.
StringBuilder b new StringBuilder;
int largerLength Math.maxstringSplit.length, stringSplit.length;
for int i ; i largerLength; i
Works on adding back string
if i stringSplit.length
bappendstringSpliti;
Works on adding back string
if i stringSplit.length
if i stringSplit.length
Adds in a buffer so that the move list is still in line with the table when the table ends
bappend;
bappendinserterappendstringSpliti;
bappend
;
return btoString;
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