Answered Essay: My project is to create a tic tac toe game that has a player(the user) and then the computer. Whoever is 'X' al

My project is to create a tic tac toe game that has a player(the user) and then the computer. Whoever is ‘X’ always goes first, and the human player gets to choose which marker they want (X or O). I think that my methods are all correct and maybe there is just a problem in the implementation portion in the main method, but I can’t seem to get the board displaying properly each time, and I don’t think that the game is going back and forth with the turns like it’s supposed to. I just need some guidance on where I’m going wrong.

This is my main method:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoe;

import java.util.Scanner;

/**
*
* @author brikr
*/
public class TicTacToe {

/**
* @param args the command line arguments
*/
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());
}
}
}

This is my abstract Player class:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoe;

/**
*
* @author brikr
*/
public abstract class Player {

private char marker;

public Player(char marker){
this.marker = marker;
}

public char getMarker() {
return marker;
}

abstract int move(Board theBoard);

}

This is my HumanPlayer class:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoe;

import java.util.Scanner;

/**
*
* @author brikr
*/
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;
}

}

This is my ComputerPlayer class:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoe;

/**
*
* @author brikr
*/
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;
}

}

This is my Board class:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tictactoe;

/**
*
* @author brikr
*/
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’;
}

}

Expert 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;
}

}

——————————————————————————————————————————-
This is output I got after runnig this program. Where exactly you want to make improvement?

What is your name?
Bob
Would you like to be X or O?
x
What would you like the computer’s difficulty level to be? 0 or 1?
0
It’s your turn first!
1 | 2 | 3 |
———–
4 | 5 | 6 |
———–
7 | 8 | 9 |
You can move to any number 1-9. What do you choose?
7
It’s your turn first!
1 | 2 | 3 |
———–
4 | 5 | 6 |
———–
7 | 8 | 9 |
You can move to any number 1-9. What do you choose?
7
1 | 2 | 3 |
———–
4 | 5 | 6 |
———–
X | 8 | 9 |
N
1
O | 2 | 3 |
———–
4 | 5 | 6 |
———–
X | 8 | 9 |
N
It’s your turn first!
1 | 2 | 3 |
———–
4 | 5 | 6 |
———–
7 | 8 | 9 |
You can move to any number 1-9. What do you choose?

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.