Im getting an error that bluej is expecting a bracket to be at the beginning of a class, however all brackets are in place and accounted for....IM a bit confused?
Printable View
Im getting an error that bluej is expecting a bracket to be at the beginning of a class, however all brackets are in place and accounted for....IM a bit confused?
Heres the code I currently have laid out
Code:import java.util.Scanner;
import java.util.Random;
public class Tic Tac Toe {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
String[][] board= {{" "," "," "},{" "," "," "},{" "," "," "}};
int x=0;
int y=0;
int option=0;
String player1;
String player2;
System.out.println("Press 1 for 2 players, or press 7 to exit");
option=keyboard.nextInt();
if (option==1) {
System.out.println("Player 1 enter your name");
player1=keyboard.next();
System.out.println("Player 2 enter your name");
player2=keyboard.next();
boolean w=winner(board);
while (w==true||w==false) {
System.out.println(player1+"Enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X") {
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="Y"){
System.out.println("That space is already taken");
continue;
}
board[x][y]="X";
displayBoard(board);
do {
System.out.println(player2+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X") {
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="O") {
System.out.println("That space is already taken");
continue;
}
board[x][y]="O";
displayBoard(board);
break;
}
while (true);
}
}
}
public static void displayBoard (String z[][]) {
System.out.println(" "+z[0][0]+" | "+z[0][1]+" | "+z[0][2]);
System.out.println(" ---------");
System.out.println(" "+z[1][0]+" | "+z[1][1]+" | "+z[1][2]);
System.out.println(" ---------");
System.out.println(" "+z[2][0]+" | "+z[2][1]+" | "+z[2][2]);
}
public static boolean winner (String z[][])
{
if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" )
|| ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" )
|| ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" )
|| ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" )
|| ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" )
|| ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" )
|| ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) )
{
return true;
}
}
}
Your Program still has lots of Buggs. Like the game doesnt end. Try using Swing =)
The winner function doesnt have a return statement if the 'if' function is not satisfied. Remember to put default values if the 'if' condition is not satisfied. =)
Code:public static boolean winner (String z[][]){
if ( ( z[0][0].equalsIgnoreCase("X") && z[0][1].equalsIgnoreCase("X") && z[0][2].equalsIgnoreCase("X") )
|| ( z[1][0].equalsIgnoreCase("X") && z[1][1].equalsIgnoreCase("X") && z[1][2].equalsIgnoreCase("X") )
|| ( z[2][0].equalsIgnoreCase("X") && z[2][1].equalsIgnoreCase("X") && z[2][2].equalsIgnoreCase("X") )
|| ( z[0][0].equalsIgnoreCase("X")&& z[1][0].equalsIgnoreCase("X")&& z[2][0].equalsIgnoreCase("X") )
|| ( z[0][1].equalsIgnoreCase("X") && z[1][1].equalsIgnoreCase("X")&& z[2][1].equalsIgnoreCase("X") )
|| ( z[0][0].equalsIgnoreCase("X") && z[1][1].equalsIgnoreCase("X") && z[2][2].equalsIgnoreCase("X") )
|| ( z[0][2].equalsIgnoreCase("X") && z[1][1].equalsIgnoreCase("X") && z[2][0].equalsIgnoreCase("X") ) )
{return true;}
else {return false;}
}
Ive been taking different approaches and what not, I changed the code a bit so Im like a couple of steps ahead:)
Alright so I made the necessary corrections, but I still have the bracket errror
Code:public class Tic Tac Toe {
public static void main(String[] args) {
Scanner keyboard=new Scanner(System.in);
String[][] board= {{" "," "," "},{" "," "," "},{" "," "," "}};
int x=0;
int y=0;
int option=0;
String player1;
String player2;
System.out.println("Press 1 for 2 players, or press 7 to exit");
option=keyboard.nextInt();
if (option==1) {
System.out.println("Player 1 enter your name");
player1=keyboard.next();
System.out.println("Player 2 enter your name");
player2=keyboard.next();
boolean w=winner(board);
while (w==true||w==false) {
System.out.println(player1+"Enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X") {
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="Y"){
System.out.println("That space is already taken");
continue;
}
board[x][y]="X";
displayBoard(board);
do {
System.out.println(player2+" enter your choice");
x=keyboard.nextInt();
y=keyboard.nextInt();
if (board[x][y]=="X") {
System.out.println("That space is already taken");
continue;
}
if (board[x][y]=="O") {
System.out.println("That space is already taken");
continue;
}
board[x][y]="O";
displayBoard(board);
break;
}
while (true);
}
}
}
public static void displayBoard (String z[][]) {
System.out.println(" "+z[0][0]+" | "+z[0][1]+" | "+z[0][2]);
System.out.println(" ---------");
System.out.println(" "+z[1][0]+" | "+z[1][1]+" | "+z[1][2]);
System.out.println(" ---------");
System.out.println(" "+z[2][0]+" | "+z[2][1]+" | "+z[2][2]);
}
public static boolean winner (String z[][])
{
if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" )
|| ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" )
|| ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" )
|| ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" )
|| ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" )
|| ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" )
|| ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) )
{
return true; }
else {return false;}
}
}
Class definition is wrong. You can't leave space in class name. It should be like this,
Code:public class TicTacToe {
// body of the class
}