|
|
|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-16-2008, 02:42 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
|
Extra bracket
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?
|
|

05-16-2008, 03:28 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
Heres the code I currently have laid out
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;
}
}
}
|
|

05-16-2008, 05:29 AM
|
|
Senior Member
|
|
Join Date: May 2008
Location: Makati, Philippines
Posts: 227
|
|
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. =)
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;}
}
__________________
Mind only knows what lies near the heart, it alone sees the depth of the soul.
|
|

05-16-2008, 05:30 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
Ive been taking different approaches and what not, I changed the code a bit so Im like a couple of steps ahead 
|
|

05-16-2008, 07:31 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 9
|
|
Alright so I made the necessary corrections, but I still have the bracket errror
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;}
}
}
|
|

05-16-2008, 08:12 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,376
|
|
Class definition is wrong. You can't leave space in class name. It should be like this,
public class TicTacToe {
// body of the class
}
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
All times are GMT +3. The time now is 03:29 PM.
|
|
VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org