the_rooster_17
06-24-2009, 11:04 PM
I am working on a program that plays Roshambo. I have the majority of it done and it compiles well and works properly. The thing that I am missing is a loop statement(s) and switch statement(s) or conditional expression(s).
I am confused on how to effectively integrate the loop and switch statements and was wondering if someone could give me some guidance. I was thinking of basically making the program loop until someone wins 3 out of 5 games and then declares the winner. For the switch I was thinking something basic like TRUE if the user wins and FALSE if the user loses or something. I know there is a better way and I was hoping someone could help me integrate these statements more effectively. Thanks for any help.
--------------------------------------------------------------------------
package Roshambo;
//Program uses Scanner and Random public class.
import java.util.Scanner;
import java.util.Random;
public class Roshambo {
public static void main(String[] args) {
//User selects "E", "H", or "A".
String userSelection;
//Computer selects "E", "H", or "A".
String computerSelection="";
//Random number used to determine computers selection.
int computerInt;
Random generator = new Random();
//Get player selection.
System.out.println ("Play ROSHAMBO!");
System.out.println ("E = Elephant");
System.out.println ("H = Human");
System.out.println ("A = Ant");
System.out.println ("Chose your fighter!");
Scanner userSelection = new (System.in);
//Make selections uppercase for ease of comparison.
userSelection = userSelection.toUpperCase();
System.out.println ("You chose a "+userSelection+" as your fighter!");
String [] options = {"Elephant", "Human", "Ant"};
//Generate the computer selection (0,1,2).
computerInt = generator.nextInt(options.length);
computerSelection = options[computerInt].substring(0, 1);
//Print the computer selection.
System.out.println ("The computer has chosen "+computerSelection+". Prepare to fight!");
//See who won
if (userSelection.equals(computerSelection))
System.out.println("It's a tie!");
else if (userSelection.equals("E") && computerSelection.equals("H"))
System.out.println("Elephant crushes human. You win!");
else if (userSelection.equals("A") && computerSelection.equals("E"))
System.out.println("Ant kills Elephant by entering its trunk. You win!");
else if (userSelection.equals("H") && computerSelection.equals("A"))
System.out.println("Human crushes Ant. You win!");
else if (userSelection.equals("H") && computerSelection.equals("E"))
System.out.println("Elephant crushes human. You lose!");
else if (userSelection.equals("E") && computerSelection.equals("A"))
System.out.println("Ant kills Elephant by entering its trunk. You Lose!");
else if (userSelection.equals("A") && computerSelection.equals("H"))
System.out.println("Human crushes Ant. You lose!");
}
}
--------------------------------------------------------------------------
I am confused on how to effectively integrate the loop and switch statements and was wondering if someone could give me some guidance. I was thinking of basically making the program loop until someone wins 3 out of 5 games and then declares the winner. For the switch I was thinking something basic like TRUE if the user wins and FALSE if the user loses or something. I know there is a better way and I was hoping someone could help me integrate these statements more effectively. Thanks for any help.
--------------------------------------------------------------------------
package Roshambo;
//Program uses Scanner and Random public class.
import java.util.Scanner;
import java.util.Random;
public class Roshambo {
public static void main(String[] args) {
//User selects "E", "H", or "A".
String userSelection;
//Computer selects "E", "H", or "A".
String computerSelection="";
//Random number used to determine computers selection.
int computerInt;
Random generator = new Random();
//Get player selection.
System.out.println ("Play ROSHAMBO!");
System.out.println ("E = Elephant");
System.out.println ("H = Human");
System.out.println ("A = Ant");
System.out.println ("Chose your fighter!");
Scanner userSelection = new (System.in);
//Make selections uppercase for ease of comparison.
userSelection = userSelection.toUpperCase();
System.out.println ("You chose a "+userSelection+" as your fighter!");
String [] options = {"Elephant", "Human", "Ant"};
//Generate the computer selection (0,1,2).
computerInt = generator.nextInt(options.length);
computerSelection = options[computerInt].substring(0, 1);
//Print the computer selection.
System.out.println ("The computer has chosen "+computerSelection+". Prepare to fight!");
//See who won
if (userSelection.equals(computerSelection))
System.out.println("It's a tie!");
else if (userSelection.equals("E") && computerSelection.equals("H"))
System.out.println("Elephant crushes human. You win!");
else if (userSelection.equals("A") && computerSelection.equals("E"))
System.out.println("Ant kills Elephant by entering its trunk. You win!");
else if (userSelection.equals("H") && computerSelection.equals("A"))
System.out.println("Human crushes Ant. You win!");
else if (userSelection.equals("H") && computerSelection.equals("E"))
System.out.println("Elephant crushes human. You lose!");
else if (userSelection.equals("E") && computerSelection.equals("A"))
System.out.println("Ant kills Elephant by entering its trunk. You Lose!");
else if (userSelection.equals("A") && computerSelection.equals("H"))
System.out.println("Human crushes Ant. You lose!");
}
}
--------------------------------------------------------------------------