Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please fill in the code in Java. Thank you! SuperClass: import java.util.Scanner; import java.util.Random; public abstract class TwoPlayerBoardGame { protected static final Scanner console =
Please fill in the code in Java. Thank you!
SuperClass:
import java.util.Scanner;
import java.util.Random;
public abstract class TwoPlayerBoardGame
protected static final Scanner console new ScannerSystemin;
protected static final Random random new Random;
protected final char board;
private final int MAXMOVES;
protected Player current;
private Player other;
protected TwoPlayerBoardGamechar board, int MAXMOVES, Player p Player p
this.board board;
this.MAXMOVES MAXMOVES;
this.current p;
this.other p;
public final void play
for int i ; i MAXMOVES; i
System.out.printlnthis;
do
if currentisHuman
askForMove;
receiveMove;
else
generateMove;
while validMove;
applyMove;
if someoneWon
System.out.printlnthis;
celebrateMove;
return;
else
prepareForNextMove;
System.out.printlnthis;
System.out.printlnIts a draw.";
protected abstract void askForMove;
protected abstract void receiveMove;
protected abstract void generateMove;
protected abstract boolean validMove;
protected abstract void applyMove;
protected abstract boolean someoneWon;
protected abstract void celebrateMove;
protected void prepareForNextMove
Player tmp current;
current other;
other tmp;
SubClass:
public final class TicTacToe extends TwoPlayerBoardGame
private char XO X;
private int row;
private int col;
public TicTacToePlayer p Player p
initialize the board
MAXMOVES:
supernull p p;
for TicTacToe, the board is a xD char array
you may initliaze every element as
public String toString
print the board to the terminal,
its format should look like this:
return ;
protected void askForMove
print some text prompt to the terminal
eg
Student, it's your move and you're Xs
Please choose your move by typing row col where row is or and col is or
protected void receiveMove
row console.nextInt;
col console.nextInt;
protected void generateMove
row Math.absrandomnextInt;
col Math.absrandomnextInt;
protected boolean validMove
check if the position is outside of the board
check if the position has already been occupied by a previous move
return true;
protected void applyMove
simply modify the corresponding eleemnt in d array char
either X or O
protected boolean someoneWon
identity if a certain row, col or diagonal
has been all occupied by X or O
return false;
protected void celebrateMove
ask you to print some text promt to the terminal
eg
That was a winning move!
Student X wins!
protected void prepareForNextMove
make use of the super keyword to call prepareForNextMove from parent class
change the var XO
either from X to O or from O to X
Code to check Classes:
public class TestGames
public static void mainString args
TwoPlayerBoardGame game;
game new TicTacToePlayercreateHumanStudent Player.createComputer;
game.play;
game new TicTacToePlayercreateHumanRoy Player.createHumanMoss;
game.play;
game new ConnectFourPlayercreateHumanStudent Player.createComputer;
game.play;
game new ConnectFourPlayercreateHumanJen Player.createHumanMoss;
game.play;
Code from Player.java
public class TestGames
public static void mainString args
TwoPlayerBoardGame game;
game new TicTacToePlayercreateHumanStudent Player.createComputer;
game.play;
game new TicTacToePlayercreateHumanRoy Player.createHumanMoss;
game.play;
game new ConnectFourPlayercreateHumanStudent Player.createComputer;
game.play;
game new ConnectFourPlayercreateHumanJen Player.createHumanMoss;
game.play;
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