Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Assignment Tic-Tac-Toe For this assignment, you will be creating an interac

ID: 3821546 • Letter: J

Question

Java Assignment
Tic-Tac-Toe
For this assignment, you will be creating an interactive Tic-Tac-Toe game.
There are 2 parts to this assignment. In the first part, you are going to be given a problem and you will then need to create a structure, write algorithms and a flow chart to solve it. In the second part, you’ll be turning this into a java program.
So let’s get started!
Part 1: Your Tic-Tac-Toe game!
You love playing tic-tac-toe, but don’t always have someone to play the game with. So, you decide to create your own that you can play against the computer.
This will be a standard tic-tac-toe game with X’s and O’s where the players alternate taking turns placing their tokens (i.e., X or O, depending on which one they are). It will include:
· Standard 3 x 3 playing board (required: use 2D arrays)
· A winning game is either:
o 3 across
o 3 up and down
o 3 diagonally
· Tie games are possible if a winning game is not achieved.
· The game board with associated placement of the played X’s and O’s must be displayed after each turn.
Program Flow:
· First, ask the user for their name and welcome them.
· Then randomly decide who will go first, the user or the computer (and let the user know this)
· The user and computer will alternate turns placing their tokens.
o The user should be asked where they want to place their token
o The computer should be asked to randomly choose an empty spot
· The game ends when either a winning game occurs (see above) or all the spots are filled.
· At the end of the game, ask the user if they would like to play again.
Input Options:
There are numerous options for getting input from users to indicate where they want their token placed.
Option 1: Ask them to give you the column and row number of their desired spot.
Option 2: Pre-fill each of the spots with a number, allowing users to select one number to indicate their desired spot. For example:
1 | 2 | 3
__|___|__
4 | 5 | 6
__|___|__
7 | 8 | 9
| |

REMINDER: You must comment your code and include JavaDocs documentation as part of your assignment.

Fala:risort lise int heat 4: sh-la,- reazetiz irst estues rxoo oftledsnc

Explanation / Answer

1. TicTacToe.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TicTacToe {

private char[][] board = new char[3][3];
private String player1;
private String player2;
private int currentPlayer;
private char marker1;
private char marker2;
private int plays;
private BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));

protected void init() {
int counter = 0;
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
board[i][i1] = Character.forDigit(++counter, 10);
}
}
currentPlayer = 1;
plays = 0;
}

protected void switchPlayers() {
if (getCurrentPlayer() == 1) {
setCurrentPlayer(2);
} else {
setCurrentPlayer(1);
}
setPlays(getPlays() + 1);
}

protected boolean placeMarker(int play) {
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
if (board[i][i1] == Character.forDigit(play, 10)) {
board[i][i1] = (getCurrentPlayer() == 1) ? getMarker1() : getMarker2();
return true;
}
}
}
return false;
}

protected boolean winner() {
//Checking rows
char current = ' ';
for (int i = 0; i < 3; i++) {
int i1 = 0;
for (i1 = 0; i1 < 3; i1++) {
if (!Character.isLetter(board[i][i1])) {
break;
}
if (i1 == 0) {
current = board[i][i1];
} else if (current != board[i][i1]) {
break;
}
if (i1 == 2) {
//Found winner
return true;
}
}
}
//Checking columns
for (int i = 0; i < 3; i++) {
current = ' ';
int i1 = 0;
for (i1 = 0; i1 < 3; i1++) {
if (!Character.isLetter(board[i1][i])) {
break;
}
if (i1 == 0) {
current = board[i1][i];
} else if (current != board[i1][i]) {
break;
}
if (i1 == 2) {
//Found winner
return true;
}
}
}
//Checking diagonals
current = board[0][0];
if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current) {
return true;
}
current = board[2][0];
if (Character.isLetter(current) && board[1][1] == current && board[0][2] == current) {
return true;
}
return false;
}

protected String getRules() {
StringBuilder builder = new StringBuilder();
builder.append("Players take turns marking a square. Only squares ");
builder.append("not already marked can be picked. Once a player has ");
builder.append("marked three squares in a row, the player wins! If all squares ");
builder.append("are marked and no three squares are the same, a tie game is declared. ");
builder.append("Have Fun! ");
return builder.toString();
}

protected String getPrompt() {
String prompt = "";
try {
prompt = reader.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
return prompt;
}

protected String drawBoard() {
StringBuilder builder = new StringBuilder("Game board: ");
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
builder.append("[" + board[i][i1] + "]");
}
builder.append(" ");
}
return builder.toString();
}

public int getCurrentPlayer() {
return currentPlayer;
}

public void setCurrentPlayer(int currentPlayer) {
this.currentPlayer = currentPlayer;
}

public char getMarker1() {
return marker1;
}

public void setMarker1(char marker1) {
this.marker1 = marker1;
}

public char getMarker2() {
return marker2;
}

public void setMarker2(char marker2) {
this.marker2 = marker2;
}

public int getPlays() {
return plays;
}

public void setPlays(int plays) {
this.plays = plays;
}

public String getPlayer1() {
return player1;
}

public void setPlayer1(String player1) {
this.player1 = player1;
}

public String getPlayer2() {
return player2;
}

public void setPlayer2(String player2) {
this.player2 = player2;
}
}

2. TicTacToeDriver.java

public class TicTacToeDriver {

   public void play() {
       TicTacToe game = new TicTacToe();
       System.out.println("Welcome! Tic Tac Toe is a two player game.");
       System.out.print("Enter player one's name: ");
       game.setPlayer1(game.getPrompt());
       System.out.print("Enter player two's name: ");
       game.setPlayer2(game.getPrompt());
       boolean markerOk = false;
       while (!markerOk) {
           System.out.print("Select any letter as " + game.getPlayer1() + "'s marker: ");
           String marker = game.getPrompt();
           if (marker.length() == 1 && Character.isLetter(marker.toCharArray()[0])) {
               markerOk = true;
               game.setMarker1(marker.toCharArray()[0]);
           } else {
               System.out.println("Invalid marker, try again");
           }
       }
       markerOk = false;
       while (!markerOk) {
           System.out.print("Select any letter as " + game.getPlayer2() + "'s marker: ");
           String marker = game.getPrompt();
           if (marker.length() == 1 && Character.isLetter(marker.toCharArray()[0])) {
               markerOk = true;
               game.setMarker2(marker.toCharArray()[0]);
           } else {
               System.out.println("Invalid marker, try again");
           }
       }

       boolean continuePlaying = true;

       while (continuePlaying) {

           game.init();
           System.out.println();
           System.out.println(game.getRules());
           System.out.println();
           System.out.println(game.drawBoard());
           System.out.println();

           String player = null;
           while (!game.winner() && game.getPlays() < 9) {
               player = game.getCurrentPlayer() == 1 ? game.getPlayer1() : game.getPlayer2();
               boolean validPick = false;
               while (!validPick) {
                   System.out.print("It is " + player + "'s turn. Pick a square: ");
                   String square = game.getPrompt();
                   if (square.length() == 1 && Character.isDigit(square.toCharArray()[0])) {
                       int pick = 0;
                       try {
                           pick = Integer.parseInt(square);
                       } catch (NumberFormatException e) {
                           // Do nothing here, it'll evaluate as an invalid
                           // pick on the next row.
                       }
                       validPick = game.placeMarker(pick);
                   }
                   if (!validPick) {
                       System.out.println("Square can not be selected. Retry");
                   }
               }
               game.switchPlayers();
               System.out.println();
               System.out.println(game.drawBoard());
               System.out.println();
           }
           if (game.winner()) {
               System.out.println("Game Over - " + player + " WINS!!!");
           } else {
               System.out.println("Game Over - Draw");
           }
           System.out.println();
           System.out.print("Play again? (Y/N): ");
           String choice = game.getPrompt();
           if (!choice.equalsIgnoreCase("Y")) {
               continuePlaying = false;
           }
       }
   }

   public static void main(String[] args) {
       TicTacToeDriver main = new TicTacToeDriver();
       main.play();
   }
}

Steps to compile above java program.

$ javac TicTacToeDriver.java TicTacToe.java

Steps to execute above java program.

$ java TicTacToeDriver

Welcome! Tic Tac Toe is a two player game.
Enter player one's name: 1
Enter player two's name: 2
Select any letter as 1's marker: o
Select any letter as 2's marker: x

Players take turns marking a square. Only squares
not already marked can be picked. Once a player has
marked three squares in a row, the player wins! If all squares
are marked and no three squares are the same, a tie game is declared.
Have Fun!

Game board:
[1][2][3]
[4][5][6]
[7][8][9]


It is 1's turn. Pick a square: 5

Game board:
[1][2][3]
[4][o][6]
[7][8][9]


It is 2's turn. Pick a square: 1

Game board:
[x][2][3]
[4][o][6]
[7][8][9]


It is 1's turn. Pick a square: 2

Game board:
[x][o][3]
[4][o][6]
[7][8][9]


It is 2's turn. Pick a square: 4

Game board:
[x][o][3]
[x][o][6]
[7][8][9]


It is 1's turn. Pick a square: 8

Game board:
[x][o][3]
[x][o][6]
[7][o][9]


Game Over - 1 WINS!!!

Play again? (Y/N):

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote