import java.util.Scanner;
import java.util.Random;
public class TicTacToe
{
//instance variables
private char[][] board;
private Scanner scanner;
private char CurrentPlayer;
public TicTacToe()
{
//constructor
char[][] board = new char[3][3];
for (int i=0; i<board.length; i++)
{
for (int x=0; x<board[0].length;x++)
{
System.out.print("*");
}
System.out.println("");
}
double z = Math.random();
if (z<0.5)
{
CurrentPlayer='X';
}
else
{
CurrentPlayer='O';
}
}
}
#Help me complete this tic tac toe code
5 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @thick topaz! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Here is the assignment
Create a program that plays tic tac toe in the terminal
Use a 2D char array.
-
fill the array with '*' to show an empty board
-
randomly pick whether X or 0 goes first
-
instance variables:
--- 2D array of char
--- Scanner object
--- char to hold current player
--- others you feel necessary -
default constructor
--- sets the size of the game to 3 x 3
--- fills the board with the * char to show empty space
--- randomly picks X or O as starting player -
have a method to display the board
--- display in grid format -
have a method for a turn of the game
--- display current player
--- prompt to enter row / column (1-3)
--- read input
--- if space entered is already taken, ask user for new input
--- set the position in the board
--- switch to other player for next turn -
have a method to check for winner
-
have a play method that runs until there's a winner or the board is full