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);
}
}
// 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);
}
}