mardi 1 mars 2016

Java - Rock, Paper, Scissors, Lizard, Spock - Buttons instead of text?

We had to make a rock paper scissors type game for our class. The way we set it up was to have the user input their choice via a inputDialog box.

However I would like for their to just be some buttons that the user can click on to make their choice and not have to type anything in.

Is there a simple way to do this?

Here's my code:

import javax.swing.JOptionPane;
import java.util.Random;

public class RPSLS
{

   public static void main(String[] args)
   {

    final int ROCK=0, PAPER=1, SCISSORS=2, LIZARD=3, SPOCK=4;
    Random rng = new Random();
    String uChoice;
    String message = "";

    int cChoice = rng.nextInt(5);
    uChoice = JOptionPane.showInputDialog("ROCK, PAPER, SCISSORS, LIZARD, SPOCK!", "ENTER YOUR CHOICE!");
    uChoice = uChoice.toUpperCase();
    uChoice = uChoice.substring(0,2);

   switch ( cChoice )
   {
     case ROCK:
         switch ( uChoice )
         {
            case "RO":
            message = "You Chose Rock \rComputer Chose Rock \rResult: Tie!";
            break;

            case "PA":
            message = "You Chose Paper \rComputer Chose Rock \rResult: You Win!";
            break;

            case "SC":
            message = "You Chose Scissors \rComputer Chose Rock \rResult: You Lose!";
            break;

        case "LI":
           message = "You Chose Lizard \rComputer Chose Rock \rResult: You Lose!";
           break;

        case "SP":
           message = "You Chose Spock \rComputer Chose Rock \rResult: You Win!";
           break;            
     }
     break;

  case PAPER:
     switch ( uChoice )
     {
        case "RO":
           message = "You Chose Rock \rComputer Chose Paper \rResult: You Lose!";
           break;

        case "PA":
           message = "You Chose Paper \rComputer Chose Paper \rResult: Tie!";
           break;

        case "SC":
           message = "You Chose Scissors \rComputer Chose Paper \rResult: You Win!";
           break;

        case "LI":
           message = "You Chose Lizard \rComputer Chose Paper \rResult: You Win!";
           break;

        case "SP":
           message = "You Chose Spock \rComputer Chose Paper \rResult: You Lose!";
           break;            
     }
     break;

  case SCISSORS:
     switch ( uChoice )
     {
        case "RO":
           message = "You Chose Rock \rComputer Chose Scissors \rResult: You Win!";
           break;

        case "PA":
           message = "You Chose Paper \rComputer Chose Scissors \rResult: You Lose!";
           break;

        case "SC":
           message = "You Chose Scissors \rComputer Chose Scissors \rResult: Tie!";
           break;

        case "LI":
           message = "You Chose Lizzard \rComputer Chose Scissors \rResult: You Lose!";
           break;

        case "SP":
           message = "You Chose Spock \rComputer Chose Scissors \rResult: You Win!";
           break;            
     }      
     break;

  case LIZARD:
     switch ( uChoice )
     {
        case "RO":
           message = "You Chose Rock \rComputer Chose Lizard \rResult: You Win!";
           break;

        case "PA":
           message = "You Chose Paper \rComputer Chose Lizard \rResult: You Lose!";
           break;

        case "SC":
           message = "You Chose Scissors \rComputer Chose Lizard \rResult: You Win!";
           break;

        case "LI":
           message = "You Chose Lizard \rComputer Chose Lizard \rResult: Tie!";
           break;

        case "SP":
           message = "You Chose Spock \rComputer Chose Lizard \rResult: You Lose!";
           break;            
     }      
     break;

  case SPOCK:
     switch ( uChoice )
     {
        case "RO":
           message = "You Chose Rock \rComputer Chose Spock \rResult: You Lose!";
           break;

        case "PA":
           message = "You Chose Paper \rComputer Chose Spock \rResult: You Win!";
           break;

        case "SC":
           message = "You Chose Scissors \rComputer Chose Spock \rResult: You Lose!";
           break;

        case "LI":
           message = "You Chose Lizard \rComputer Chose Spock \rResult: You Win!";
           break;

        case "SP":
           message = "You Chose Spock \rComputer Chose Spock \rResult: Tie!";
           break;            
     }      
     break;            
   }

System.out.println( message );

   }

} // end of class




Aucun commentaire:

Enregistrer un commentaire