Results 1 to 6 of 6
Thread: Extra bracket
- 05-16-2008, 12:42 AM #1
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
- 05-16-2008, 01:28 AM #2
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
Heres the code I currently have laid out
Java 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; } } }
- 05-16-2008, 03:29 AM #3
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
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. =)
Java 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;} }Mind only knows what lies near the heart, it alone sees the depth of the soul.
- 05-16-2008, 03:30 AM #4
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
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, 05:31 AM #5
Member
- Join Date
- May 2008
- Posts
- 9
- Rep Power
- 0
Alright so I made the necessary corrections, but I still have the bracket errror
Java 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;} } }
- 05-16-2008, 06:12 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Class definition is wrong. You can't leave space in class name. It should be like this,
Java Code:public class TicTacToe { // body of the class }


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks