tic-tac-toe!

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2007
Posts: 1
Reputation: en1gmat1c is an unknown quantity at this point 
Solved Threads: 0
en1gmat1c en1gmat1c is offline Offline
Newbie Poster

tic-tac-toe!

 
0
  #1
Nov 2nd, 2007
ok so im a new programmer and im making a tic tac toe program and i got this error
"operator && cannot be applied to boolean,java.lang.String" in the crazy long line in the winner method and am not sure how to fix it

import java.util.Scanner;
import java.util.Random;
public class tictactoe
{
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("Enter 1 for 2 player, 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])
{
return true;
}

}
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: tic-tac-toe!

 
0
  #2
Nov 3rd, 2007
Hi,

You are "==" operator which checks either sides of references points to the same object .
That is checks the memory location not the contents . So use equals() function to check the contents( "X" in your case)of the String object .

Try to check the value("X") using looping statements rather than a cumbersome lengthy if statement

Hope this helps
The important thing is not to stop questioning. Curiosity has its own reason for existing.
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: tic-tac-toe!

 
0
  #3
Nov 3rd, 2007
the crazy long line in the winner method
Start by splitting that line into fragments you can understand.
As the error says, you can't perform boolean operations on something that's not a boolean.
You're (it says, and I trust that it's right) comparing Strings with booleans there, which is not allowed.
In no small part that's no doubt because you wrote that entire crazy long line without giving thought to operator precedence rules, causing comparisons to happen that you are not aware of (and likely never intended).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 27
Reputation: schoolsoluction is an unknown quantity at this point 
Solved Threads: 3
schoolsoluction schoolsoluction is offline Offline
Light Poster

Re: tic-tac-toe!

 
-1
  #4
Nov 3rd, 2007
Hi, I see some 'common' mistakes newbie programmers often make.

Have a look at this page to help you out:
http://java-assignment.com/hints.php

The page does not tell you how to solve your assignment, but it does tell you how to write cleaner code. (like splitting up your code in small parts, naming conventions like starting a classname with capital letter, etc)

If someone has comments on the page, please let me know.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 55
Reputation: mickinator is an unknown quantity at this point 
Solved Threads: 5
mickinator mickinator is offline Offline
Junior Poster in Training

Re: tic-tac-toe!

 
0
  #5
Nov 5th, 2007
  1. public static boolean winner (String z[][])
  2. {
  3. if ( ( z[0][0]=="X" && z[0][1]=="X" && z[0][2]=="X" )
  4. || ( z[1][0]=="X" && z[1][1]=="X" && z[1][2]=="X" )
  5. || ( z[2][0]=="X" && z[2][1]=="X" && z[2][2]=="X" )
  6. || ( z[0][0]=="X" && z[1][0]=="X" && z[2][0]=="X" )
  7. || ( z[0][1]=="X" && z[1][1]=="X" && z[2][1]=="X" )
  8. || ( z[0][0]=="X" && z[1][1]=="X" && z[2][2]=="X" )
  9. || ( z[0][2]=="X" && z[1][1]=="X" && z[2][0]=="X" ) )
  10. return true;
  11. }

Ok the last term in that crazy long last line of yours didn't have anything to be compared to, "&&z[2][0])" which was wrong! I presumed it's like the others, and fixed it. Also I think that you would want each comparison to be separate, or else you would get the wrong answer a lot of the time. And finally, if you have a crazy long line like that, it might be better to sort it, like I've done above, makes it a lot easier to follow if you ask me.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: orienthis is an unknown quantity at this point 
Solved Threads: 1
orienthis orienthis is offline Offline
Newbie Poster

Re: tic-tac-toe!

 
0
  #6
Jan 16th, 2009
Hey, im not sure if you have resolved your problem yet, but when you use the == to compare String objects it compares pointers in memory. I know this was mentioned before, but i also want to mention that Strings are immutable and this means they cannot be changed. because of this, in java, when you have a currently made string containing "X" and you make another containing the same string, java makes them point to the same string in memory. this is why your code may work this time, but for future reference use the .equals() method because it compares the actual contents of the strings character by character and you always get the correct result.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1
Reputation: ◊ThePurpleBunny is an unknown quantity at this point 
Solved Threads: 0
◊ThePurpleBunny ◊ThePurpleBunny is offline Offline
Newbie Poster

Re: tic-tac-toe!

 
0
  #7
Jan 24th, 2009
If you are trying to compare string variables you have to use the .equals("string") operator.

For example:
String input = "The Purple Bunny";
String character9 = input.substring(8, 9);
if (character9.equals("l"))
System.out.println("Character 9 is an L");
else
System.out.println("Character 9 is not an L");


variable.equals("string") == true is REDUNDANT because the default evaluation for .equals("") is true. To check if it is false, however, simply use variable.equals("string") == false


Hope this helps.

◊The Purple Bunny
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: theStruggler is an unknown quantity at this point 
Solved Threads: 0
theStruggler theStruggler is offline Offline
Newbie Poster
 
0
  #8
Nov 1st, 2009
If you want to check whether a string is equal to another, use the .equals method. For example:
  1. ...
  2. String one = "X";
  3. String two = "X";
  4. if (one.equals(two))
  5. { System.out.println(one + " = " + two); }
  6. ...
The output should be
  1. X = X
Try using it with your strings. I would be quite a long code segment
but it would work if you do it correctly.
Hope it helped.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 7715 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC