Answered step by step
Verified Expert Solution
Link Copied!

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 Scanner(System.in);
protected static final Random random = new Random();
protected final char[][] board;
private final int MAX_MOVES;
protected Player current;
private Player other;
protected TwoPlayerBoardGame(char[][] board, int MAX_MOVES, Player p1, Player p2){
this.board = board;
this.MAX_MOVES = MAX_MOVES;
this.current = p1;
this.other = p2;
}
public final void play(){
for (int i =0; i < MAX_MOVES; ++i){
System.out.println(this);
do {
if (current.isHuman()){
askForMove();
receiveMove();
}
else {
generateMove();
}
}
while (!validMove());
applyMove();
if (someoneWon()){
System.out.println(this);
celebrateMove();
return;
}
else {
prepareForNextMove();
}
}
System.out.println(this);
System.out.println("It's 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 TicTacToe(Player p1, Player p2){
// initialize the board
// MAX_MOVES: 9
super(null,0, p1, p2);
// for TicTacToe, the board is a 3x32D 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
// e.g.
// Student, it's your move and you're Xs.
// Please choose your move by typing row col where row is 0,1, or 2 and col is 0,1, or 2.
}
protected void receiveMove(){
row = console.nextInt();
col = console.nextInt();
}
protected void generateMove(){
row = Math.abs(random.nextInt())%3;
col = Math.abs(random.nextInt())%3;
}
protected boolean validMove(){
//1. check if the position is outside of the board
//2. check if the position has already been occupied by a previous move
return true;
}
protected void applyMove(){
// simply modify the corresponding eleemnt in 2d 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
// e.g.
// 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 main(String[] args){
TwoPlayerBoardGame game;
game = new TicTacToe(Player.createHuman("Student"), Player.createComputer());
game.play();
game = new TicTacToe(Player.createHuman("Roy"), Player.createHuman("Moss"));
game.play();
game = new ConnectFour(Player.createHuman("Student"), Player.createComputer());
game.play();
game = new ConnectFour(Player.createHuman("Jen"), Player.createHuman("Moss"));
game.play();
}
}
Code from Player.java
public class TestGames {
public static void main(String[] args){
TwoPlayerBoardGame game;
game = new TicTacToe(Player.createHuman("Student"), Player.createComputer());
game.play();
game = new TicTacToe(Player.createHuman("Roy"), Player.createHuman("Moss"));
game.play();
game = new ConnectFour(Player.createHuman("Student"), Player.createComputer());
game.play();
game = new ConnectFour(Player.createHuman("Jen"), Player.createHuman("Moss"));
game.play();
}
}

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions

Question

2. List the advantages of listening well

Answered: 1 week ago