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

Hint : Create a Tic-Tac-Toe game for two players. The game will take in a locati

ID: 3700844 • Letter: H

Question

Hint :
Create a Tic-Tac-Toe game for two players. The game will take in a location on the game board (1-9) and place the player's mark ("X" or "O") onto the board on the requested location. It is suggested that you use a 2-dimensional String array to store your board. It should have 3 rows and 3 columns You should only need to use one board in your game. The game should print out current layout of the board before we request a location from the currently player. If neither player has selected a location, we should print out a number the player can use to select that location. If a player has selected a location, we should print out that player's mark. As we play the game, we should detcrmine whether or not the user has successfully won the game by checking to see if we have three of the same mark in a row in 8 different directions An example Java file has been provided to help with breaking up the problem into different methods Reference: https://en.wikipedia.org/wiki/Tic-tac-toe

Explanation / Answer

Code

import java.util.Arrays;

import java.util.InputMismatchException;

import java.util.Scanner;

public class tictactoe {

static Scanner in;

static String[] board;

static String turn;

public static void main(String[] args) {

in = new Scanner(System.in);

board = new String[9];

turn = "X";

String winner = null;

EmptyBoardPrint();

System.out.println("Welcome to 2 Player Tic Tac Toe.");

System.out.println("--------------------------------");

Boardprint();

System.out.println("X's will play first. Enter a location to place X in:");

while (winner == null) {

int numInput;

try {

numInput = in.nextInt();

if (!(numInput > 0 && numInput <= 9)) {

System.out.println("Invalid input; re-enter location:");

continue;

}

} catch (InputMismatchException e) {

System.out.println("Invalid input; re-enter location:");

continue;

}

if (board[numInput-1].equals(String.valueOf(numInput))) {

board[numInput-1] = turn;

if (turn.equals("X")) {

turn = "O";

} else {

turn = "X";

}

Boardprint();

winner = Winner();

} else {

System.out.println("Location already taken; please re-enter location:");

continue;

}

}

if (winner.equalsIgnoreCase("draw")) {

System.out.println("It's a draw! Thanks for playing.");

} else {

System.out.println("Congratulations! " + winner + "'s have won! Thanks for playing.");

}

}

static String Winner() {

for (int a = 0; a < 8; a++) {

String line = null;

switch (a) {

case 0:

line = board[0] + board[1] + board[2];

break;

case 1:

line = board[3] + board[4] + board[5];

break;

case 2:

line = board[6] + board[7] + board[8];

break;

case 3:

line = board[0] + board[3] + board[6];

break;

case 4:

line = board[1] + board[4] + board[7];

break;

case 5:

line = board[2] + board[5] + board[8];

break;

case 6:

line = board[0] + board[4] + board[8];

break;

case 7:

line = board[2] + board[4] + board[6];

break;

}

if (line.equals("XXX")) {

return "X";

} else if (line.equals("OOO")) {

return "O";

}

}

for (int a = 0; a < 9; a++) {

if (Arrays.asList(board).contains(String.valueOf(a+1))) {

break;

}

else if (a == 8) return "draw";

}

System.out.println(turn + "'s turn; enter a location " + turn + " in:");

return null;

}

static void Boardprint() {

System.out.println("| " + board[0] + " | " + board[1] + " | " + board[2] + " |");

System.out.println("|-----------|");

System.out.println("| " + board[3] + " | " + board[4] + " | " + board[5] + " |");

System.out.println("|-----------|");

System.out.println("| " + board[6] + " | " + board[7] + " | " + board[8] + " |");

}

static void EmptyBoardPrint() {

for (int a = 0; a < 9; a++) {

board[a] = String.valueOf(a+1);

}

}

}

I have previously answered this

Thank You

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