Question

Let b11, b12, b13, b21, b22, b23, b31, b32, b33 represent the 3 x 3 tic...

Let b11, b12, b13, b21, b22, b23, b31, b32, b33 represent the 3 x 3 tic tac toe board. Write a Java program that assigns each variable randomly with '_', 'O', 'X' and determines if O, X, both, or none wins. (Note that _ indicates a blank.) The program will have to display the board along with the right answer. For example, if b11='O', b12='_', b13='O', b21='X', b22='O', b23='X', b31='_', b32='X', b33='O' are generated, then it will display

0 0
Add a comment Improve this question Transcribed image text
Answer #1

steps:

1. Generate 0 to 2 and depending the random value assign 'O','X' or '_' to each cell.

2. There are 8 winning combinations

2.1-> 3 horizontal rows  

2.2-> 3 vertical cols

2.3-> 2 diagonal

3.if in any of the cases all symbols are equal ('O' or 'X'), then there is a winner('O' or 'X')

else

No winner

code:

//////////////////////////////////////////////////

import java.util.*;


public class HelloWorld {

static char[][] b=new char[3][3];
static int winner1=0; //flag for 'O' is winner
static int winner2=0; //flag for 'X' is winner


//this function traverse a row
//and checks all the elements are equal are not
//if all are equal it decides the winner alse returns
public static void check_row(int row_no){
char start=b[row_no][0];//this the starting element
if(start=='_')//if starting element has no symbol return
return;
for(int j=1;j<3;j++)
if(start!=b[row_no][j])//if there is a single mis match there will be no winner so return
return ;
  
//all elements are equal decide winner depending on start element
if(start=='O')
winner1=1;
else
winner2=1;

}
  
//this function traverse a Col
//and checks all the elements are equal are not
//if all are equal it decides the winner alse returns
public static void check_col(int col_no){
char start=b[0][col_no];//this the starting element
if(start=='_')//if starting element has no symbol return
return;
for(int i=1;i<3;i++)
if(start!=b[i][col_no])//if there is a single mismatch there will be no winner so return
return ;
//all elements are equal decide winner depending on start element
if(start=='O')
winner1=1;
else
winner2=1;

}
//this function traverse the first diagonal
//and checks all the elements are equal are not
//if all are equal it decides the winner alse returns
public static void check_digLR(){
char start=b[0][0];
if(start=='_')
return;
for(int i=1;i<3;i++)
if(start!=b[i][i])
return ;
if(start=='O')
winner1=1;
else
winner2=1;
  
}
//this function traverse the second diagonal
//and checks all the elements are equal are not
//if all are equal it decides the winner alse returns
public static void check_digRL(){
char start=b[0][2];
if(start=='_')
return;
for(int i=1;i<3;i++)
if(start!=b[i][2-i])
return ;
  
if(start=='O')
winner1=1;
else
winner2=1;
  
}
  
//print the board
public static void disp(){
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println();
}   
}
  
public static void main(String[] args) {

  
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
Random rand = new Random();
int r = rand.nextInt(3); //randomly generating the number from 0 to 2
//depending on value of r symbols are assigned to each cell
if(r==0)
b[i][j]='O';
else if(r==1)
b[i][j]='X';
else
b[i][j]='_';
}
disp();
//every possible winnig case is checked
for(int i=0;i<3;i++)
{
check_row(i);
  
check_col(i);
  
}
check_digRL();
check_digLR();
  
//Winner is displayed
if(winner1+winner2==0)
System.out.println("No winner");
else if(winner1+winner2==2)
System.out.println("Both winner");
else if(winner1==1)
System.out.println("O is Winner");
else
System.out.println("X is Winner");

}
}

///////////////////////////////////////////////////

output:

for any issues please comment down below.

Add a comment
Know the answer?
Add Answer to:
Let b11, b12, b13, b21, b22, b23, b31, b32, b33 represent the 3 x 3 tic...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • XTR-0.10 B12 X12-0.20 B21 V= 1-0 x,-0.30 X23-0.20 B13 X13-0.10 B22 Fig. 1. Single-machine infinit...

    XTR-0.10 B12 X12-0.20 B21 V= 1-0 x,-0.30 X23-0.20 B13 X13-0.10 B22 Fig. 1. Single-machine infinite bus system (Simple system-I) 1. The synchronous generator in Fig.1 delivers 0.8 p.u. real power at 0.85 power factor leading at voltage at infinite bus (1.0 р.u. voltage). Determine a) The real and reactive power output of the generator b) The generator internal voltage c) An equation for the electrical power delivered by the generator as a function of 8. d) For a three-phase-to ground...

  • 18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use di...

    18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...

  • Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board...

    Tic-Tac-Toe (arrays and multidimensional arrays) C++ Write an interactive program that plays tic-tac-toe. Represent the board as a 3 X 3 character array. Initialize the array to blanks and ask each player in turn to input a position. The first player's position is marked on the board with a O and the second player's position is marked with an X. Continue the process until a player wins or the game is a draw. To win, a player must have 3...

  • In Java create a program where a user plays Tic Tac Toe against the computer. Create...

    In Java create a program where a user plays Tic Tac Toe against the computer. Create a class TicTacToe that will enable you write a program to play Tic-Tac-Toe. The class contains a private 3x3 two-dimensional array. Use an enum type to represent the value in each cell of the array. The enum’s constants should be named X, O, and EMPTY (for a position that does not contain an X or O). The constructor should initialize the board elements to...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program...

    I need screenshots for this solution done in Flowgorithm. Thank you. Tic-Tac-Toe Game Design a program that allows two players to play a game of tic-tac-toe. Use a two- dimensional String array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: a. Displays the contents of the board array. b. Allows player 1 to select a location...

  • Java project In a game of tic-tac-toe, two players take turns marking an available cell in...

    Java project In a game of tic-tac-toe, two players take turns marking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Create...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates...

    Write a C++ program, tictac.cpp, that repeatedly reads in tic-tac-toe boards and for each one indicates if there is a winner and who won. The board will be read in as 3x3 array of characters (x, o or . for a blank spot). You are required to write the following functions to help solve the problem: // input prompts the user for a board and inputs it // into the given array void input (char board [3][3]); // print prints...

  • Objective: To write a program to allow a game of Tic Tac Toe to be played,...

    Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner. Details: The TicTacToe board is stored in a 3x3 multidimensional list of characters containing spaces at the start of the game...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT