P12.5 Write a tic-tac-toe game that allows a human player to play against the co
ID: 3739610 • Letter: P
Question
P12.5 Write a tic-tac-toe game that allows a human player to play against the computer. Your program will play many turns against a human opponent, and it will learn When it is the computer's turn, the computer randomly selects an empty field, except that it won't ever choose a losing combination. For that purpose, your pro- gram must keep an array of losing combinations. Whenever the human wins, the immediately preceding combination i s stored as losing. For example, suppose that computer and O = human. Suppose the current combination is Now it is the human's turn, who will of course choose 0 The computer should then remember the preceding combination as a losing combination. As a result, the computer will never again choose that combination from or 0 Discover classes and supply a UML diagram before you begin to programExplanation / Answer
TicTacToe.java
-----------------------------------------------
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
char player = ' ';
char computerMarker = ' ';
int humanCount = 0;
int computerCount = 0;
Board theBoard = new Board();
char [][] board = new char[3][3];
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("Would you like to be X or O?");
char humanMark = input.next().charAt(0);
char humanMarker = Character.toUpperCase(humanMark);
while(humanMark != 'x' && humanMark != 'o'){
System.out.println("Please pick either 'X' or 'O'");
humanMark = input.next().charAt(0);
humanMarker = Character.toUpperCase(humanMark);
}
if (humanMarker == 'X') {
computerMarker = 'O';
} else {
computerMarker = 'X';
}
System.out.println("What would you like the computer's difficulty level to be? 0 or 1?");
int difficultyLevel = input.nextInt();
while (difficultyLevel != 0 && difficultyLevel != 1) {
System.out.println("Please enter a value that is 0 or 1");
difficultyLevel = input.nextInt();
}
Player human = new HumanPlayer(name, humanMarker);
Player computer = new ComputerPlayer(difficultyLevel, computerMarker);
char turn = 'X';
while (theBoard.gameStatus() == 'N') {
if (human.getMarker() == turn) {
human.move(theBoard);
theBoard.setTheBoard(human.move(theBoard), human.getMarker());
theBoard.displayTheBoard();
}
else {
System.out.println(computer.move(theBoard));
theBoard.setTheBoard(computer.move(theBoard), computer.getMarker());
theBoard.displayTheBoard();
}
turn = turn == 'O' ? 'X' : 'O';
System.out.println(theBoard.gameStatus());
}
}
}
------------------------------------------------------------------------------
Player.java
-------------------------------------
public abstract class Player {
private char marker;
public Player(char marker){
this.marker = marker;
}
public char getMarker() {
return marker;
}
abstract int move(Board theBoard);
}
---------------------------------------------------------------------------
Board.java
-------------------------------
public class Board {
private final char[][] theBoard = new char[3][3];
public Board() {
this.theBoard[0][0] = '1';
this.theBoard[0][1] = '2';
this.theBoard[0][2] = '3';
this.theBoard[1][0] = '4';
this.theBoard[1][1] = '5';
this.theBoard[1][2] = '6';
this.theBoard[2][0] = '7';
this.theBoard[2][1] = '8';
this.theBoard[2][2] = '9';
}
public void setTheBoard(int move, char character){
if(move == 1){
theBoard[0][0] = character;
}
if(move == 2){
theBoard[0][1] = character;
}
if(move == 3){
theBoard[0][2] = character;
}
if(move == 4){
theBoard[1][0] = character;
}
if(move == 5){
theBoard[1][1] = character;
}
if(move == 6){
theBoard[1][2] = character;
}
if(move == 7){
theBoard[2][0] = character;
}
if(move == 8){
theBoard[2][1] = character;
}
if(move == 9){
theBoard[2][2] = character;
}
}
boolean isValidMove(int attemptedMove) {
if (attemptedMove == 1) {
if (theBoard[0][0] == 'X' || theBoard[0][0] == 'O') {
return false;
}
}
if (attemptedMove == 2) {
if (theBoard[0][1] == 'X' || theBoard[0][1] == 'O') {
return false;
}
}
if (attemptedMove == 3) {
if (theBoard[0][2] == 'X' || theBoard[0][2] == 'O') {
return false;
}
}
if (attemptedMove == 4) {
if (theBoard[1][0] == 'X' || theBoard[1][0] == 'O') {
return false;
}
}
if (attemptedMove == 5) {
if (theBoard[1][1] == 'X' || theBoard[1][1] == 'O') {
return false;
}
}
if (attemptedMove == 6) {
if (theBoard[1][2] == 'X' || theBoard[1][2] == 'O') {
return false;
}
}
if (attemptedMove == 7) {
if (theBoard[2][0] == 'X' || theBoard[2][0] == 'O') {
return false;
}
}
if (attemptedMove == 8) {
if (theBoard[2][1] == 'X' || theBoard[2][1] == 'O') {
return false;
}
}
if (attemptedMove == 9) {
if (theBoard[2][2] == 'X' || theBoard[2][2] == 'O') {
return false;
}
}
if (attemptedMove > 9) {
return false;
} else if (attemptedMove < 1) {
return false;
}
return true;
}
public char newTurn(char player) {
return player == 'O' ? 'X' : 'O';
}
public void displayTheBoard() {
System.out.println(theBoard[0][0] + " | " + theBoard[0][1] + " | " + theBoard[0][2] + " | ");
System.out.println(("-----------"));
System.out.println(theBoard[1][0] + " | " + theBoard[1][1] + " | " + theBoard[1][2] + " | ");
System.out.println(("-----------"));
System.out.println(theBoard[2][0] + " | " + theBoard[2][1] + " | " + theBoard[2][2] + " | ");
}
public char gameStatus() {
if ((theBoard[0][0] == theBoard[0][1]) && (theBoard[0][1] == theBoard[0][2])) {
return theBoard[0][0];
} else if ((theBoard[1][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[1][2])) {
return theBoard[1][0];
} else if ((theBoard[2][0] == theBoard[2][1]) && (theBoard[2][1] == theBoard[2][2])) {
return theBoard[2][0];
} else if ((theBoard[0][0] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][2])) {
return theBoard[0][0];
} else if ((theBoard[0][2] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][0])) {
return theBoard[1][2];
} else if ((theBoard[0][0] == theBoard[1][0]) && (theBoard[1][0] == theBoard[2][0])) {
return theBoard[0][0];
} else if ((theBoard[0][1] == theBoard[1][1]) && (theBoard[1][1] == theBoard[2][1])) {
return theBoard[0][1];
} else if ((theBoard[0][2] == theBoard[1][2]) && (theBoard[1][2] == theBoard[2][2])) {
return theBoard[0][2];
}
for (int row = 0; row < 3; row++) {
for (int column = 0; column < 3; column++) {
if (theBoard[row][column] == ' ') {
return 'T';
}
}
}
return 'N';
}
}
------------------------------------------------------------------------------------------------
ComputerPlayer.java
-----------------------------------------------
public class ComputerPlayer extends Player {
private int difficultyLevel;
public ComputerPlayer(int difficultyLevel, char marker){
super(marker);
this.difficultyLevel = difficultyLevel;
}
Board b = new Board();
@Override
int move(Board theBoard) {
int move = (int)(Math.random()*9);
b.isValidMove(move);
while(b.isValidMove(move) == false){
move = (int)(Math.random() * 9);
}
if(b.isValidMove(move) == true){
//b.displayTheBoard();
}
return move;
}
}
------------------------------------------------------------------------------------------------
HumanPlayer.java
---------------------------------------------
import java.util.Scanner;
public class HumanPlayer extends Player{
Scanner input = new Scanner(System.in);
private String name;
public HumanPlayer(String name, char marker){
super(marker);
this.name = name;
}
Board b = new Board();
@Override
int move(Board theBoard) {
int move = 0;
if(super.getMarker() == 'X'){
System.out.println("It's your turn first!");
b.displayTheBoard();
System.out.println("You can move to any number 1-9. What do you choose?");
move = input.nextInt();
b.isValidMove(move);
while(b.isValidMove(move) == false){
System.out.println("That move is not valid. Please try again.");
move = input.nextInt();
}
}
else if(super.getMarker() == 'O'){
System.out.println("It's the computer's turn first!");
}
return move;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.