Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*Please read the whole tic tac toe game code in java and answer the questions which i have attached. This is our group project, and

*Please read the whole tic tac toe game code in java and answer the questions which i have attached. This is our group project, and the questions i have attached below are the questions which have to be answered in presentation. Answers should be in points only, no long paragraphs* Course name: data structure & algorithm
____________
package main;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char[][] board = new char[3][3];
for (int i = 0; i
for (int j = 0; j
board[i][j] = '-';
}
}
Scanner scan = new Scanner(System.in);
System.out.println("Let's start the game TIC TAC TOE.");
System.out.print("Enter the name of Player1: ");
String play1 = scan.nextLine();
System.out.print("Enter the name of Player2: ");
String play2 = scan.nextLine();
boolean player1 = true;
boolean gameEnded = false;
while (!gameEnded) {
drawBoard(board);
if (player1) {
System.out.println(play1 + "'s Turn (X): ");
} else {
System.out.println(play2 + "'s Turn (O): ");
}
char c = '-';
if (player1) {
c = 'X';
} else {
c = 'O';
}
int row = 0;
int col = 0;
while (true) {
System.out.print("Enter a row number (0, 1, or 2): ");
row = scan.nextInt();
System.out.print("Enter a column number (0, 1, or 2): ");
col = scan.nextInt();
if (row 2 || col > 2) {
System.out.println("This position is off the "+
"bounds of the board! Try again.");
} else if (board[row][col] != '-') {
System.out.println("Someone has already made a "+
"move at this position! Try again.");
} else {
break;
}
}
board[row][col] = c;
if (playerHasWon(board) == 'X') {
System.out.println(play1 + " has won!");
gameEnded = true;
} else if (playerHasWon(board) == 'O') {
System.out.println(play2 + " has won!");
gameEnded = true;
} else {
if (boardIsFull(board)) {
System.out.println("It's a tie!");
gameEnded = true;
} else {
player1 = !player1;
}
}
}
drawBoard(board);
scan.close();
}
public static void drawBoard(char[][] board) {
System.out.println("Board:");
for (int i = 0; i
for (int j = 0; j
System.out.print(board[i][j]);
}
System.out.println();
}
}
public static char playerHasWon(char[][] board) {
for (int i = 0; i
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != '-') {
return board[i][0];
}
}
for (int j = 0; j
if (board[0][j] == board[1][j] &&
board[1][j] == board[2][j] &&
board[0][j] != '-') {
return board[0][j];
}
}
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != '-') {
return board[0][0];
}
if (board[2][0] == board[1][1] &&
board[1][1] == board[0][2] &&
board[2][0] != '-') {
return board[2][0];
}
return ' ';
}
public static boolean boardIsFull(char[][] board) {
for (int i = 0; i
for (int j = 0; j
if (board[i][j] == '-') {
return false;
}
}
}
return true;
}
}
image text in transcribed
Background: What is your project? What data structures / algorithms did you use

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

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions

Question

What are the attributes of a technical decision?

Answered: 1 week ago

Question

How do the two components of this theory work together?

Answered: 1 week ago