PDA

View Full Version : Why wont this work?



Raztat
02-19-2008, 06:02 PM
im making a paper rock scissors game in java. I got 2 versions. The first one you type in a number representing rock paper or scissors and it works fine. the second one you have to type in rock paper or scissors. This one is giving me trouble. Im trying to make it where once you type in what you want it converts it to a number. ill post the code below

// Brandon Conway
// Feb 14, 2008
// Rock Paper Scissors
import javax.swing.JOptionPane;
import java.util.*;
public class PRS2
{
public static void main(String[] args)
{

// User choosing rock paper or scissors
String choiceString = "";
String chosenItem;
String rock = "rock";
String paper = "paper";
String scissors= "scissors";

int itemType;
final String R = "Rock";
final String P = "Paper";
final String S = "Scissors";

int w = 0;
int l = 0;
int t = 0;

for (int rnd=1; rnd<=10; rnd++) {

choiceString = JOptionPane.showInputDialog(null,
"What do you choose?" +
"\nType it in");

if (choiceString.equalsIgnoreCase(rock));
{
choiceString = 1;
itemType = Integer.parseInt(choiceString);
}
else if (choiceString.equalsIgnoreCase(paper));
{
choiceString = 2;
itemType = Integer.parseInt(choiceString);
}
else if (choiceString.equalsIgnoreCase(scissors));
{
choiceString = 3;
itemType = Integer.parseInt(choiceString);
}
else
JOptionPane.showMessageDialog(null,
"Try again");


if(itemType == 1)
chosenItem = R;
else if (itemType == 2)
chosenItem = P;
else
chosenItem = S;

JOptionPane.showMessageDialog(null,
"You chose " + chosenItem + ".");
// End of user choosing item

// Computer choosing item
Random r = new Random();
int randint = r.nextInt(3);

if(randint == 0)
JOptionPane.showMessageDialog(null,"The computer chooses Rock.");
else if (randint == 1)
JOptionPane.showMessageDialog(null,"The computer chooses Paper.");
else
JOptionPane.showMessageDialog(null,"The computer chooses Scissors.");

// End computer choosing

// Determine Winner if computer chooses rock
if(randint == 0 && itemType == 1)
{
t = t + 1;
JOptionPane.showMessageDialog(null,"Its a tie.");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}

else if (randint == 0 && itemType == 2)
{
w = w + 1;
JOptionPane.showMessageDialog(null,"You Win! Paper beats Rock!");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}

else if(randint == 0 && itemType == 3)
{
l = l + 1;
JOptionPane.showMessageDialog(null,"You Lose. Rock beats Scissors.");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}

// Determine Winner if computer chooses paper
else if(randint == 1 && itemType == 1)
{
l = l + 1;
JOptionPane.showMessageDialog(null,"You Lose. Paper beats Rock.");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}

else if(randint == 1 && itemType == 2)
{
t = t + 1;
JOptionPane.showMessageDialog(null,"Its a tie.");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}

else if(randint == 1 && itemType == 3)
{
w = w + 1;
JOptionPane.showMessageDialog(null,"You Win! Scissors beats Paper!");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}

// Determine Winner if computer chooses scissors
else if(randint == 2 && itemType == 1)
{
w = w + 1;
JOptionPane.showMessageDialog(null,"You Win! Rock Beats Scissors!");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}

else if(randint == 2 && itemType == 2)
{
l = l + 1;
JOptionPane.showMessageDialog(null,"You Lose. Scissors Beats Paper.");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}

else if(randint == 2 && itemType == 3)
{
t = t + 1;
JOptionPane.showMessageDialog(null,"Its a tie.");
JOptionPane.showMessageDialog(null,"Won = " + w + " Lost = " + l + " Tie = " + t);
}
}

System.exit(0);
}
}

Raztat
03-02-2008, 04:49 PM
500 views and not 1 response?

boogyman
03-03-2008, 02:14 PM
I am guessing they all believed you mixed up the name and really meant Javascript

I am not 100% familiar with java, but you are trying to type cast a string to an integer. What I would suggest is to create integer constants 1/2/3 corresponding with rock/paper/scissors as you have declared them and just use the constant to assign the itemType.

Raztat
03-04-2008, 07:34 PM
i really mean java, its for java class, what i was trying to do was convert the string rock, paper, or scissors, to its corresponding number. But it doesnt seem to want to, thanks for the help Ill go try it out.

boogyman
03-04-2008, 08:10 PM
yes I know you mean real Java, I was saying why people looked but didn't respond.

if you would prefer not to use the constant method like I mentioned, use this type casting method



(int) choiceString = __#__;