This site requires JavaScript, please enable it in your browser!
Greenfoot back
LearningProgramming
LearningProgramming wrote ...

2013/3/25

Rock, Paper, Scissor, Lizard, Spock

1
2
JetLennit JetLennit

2013/3/27

#
are you familiar with joptionpane?
danpost danpost

2013/3/27

#
Please refer to Swing Components in the Java tutorials.
JetLennit JetLennit

2013/3/27

#
I am familiar with it, i was seeing if he was, it would help with his program
danpost danpost

2013/3/27

#
@JetLennit, I might be best for him to get a better grasp of the Java language before branching off to things like that.
JetLennit JetLennit

2013/3/27

#
LearningProgramming wrote...
without graphics and also creating the best of series...
The easiest way to do it without graphics is with joptionpane
danpost danpost

2013/3/27

#
@JetLennit, I am not totally sure if that is really what LearningProgrammer wants. You are the one who mentioned 'without graphics' first and I believe he replied settling for at least that much, maybe hoping to complete the graphics part later. @LearningProgrammer, please be more specific as to what you want with your scenario (how you want it to basically work between the user and the game); this includes what you want displayed as far as player options and controls, how the user is to pick an option, what you want displayed for the computer options and choice, etc. I understand the 'best of' part of it, so you do not need to get into that, just what you want when the 'best of' is reached (after the series is won).
JetLennit JetLennit

2013/3/27

#
i apologize i hadn't understood that
danpost danpost

2013/3/27

#
@JetLennit, you do not have to apologize. We do not know for sure what he wants, as he has not been very clear on the matter. Let us wait for a response by him and see what comes of it.
Hey Guys thanks a lot for the responds, i just wanted something basic using an eclipse in java. Something I could enter a weapon like "Rock" and maybe the computer chose "Scissors" and the user wins because "Rock" crushes "Scissors" or I chose "Paper" and the computer chose "Scissors" and the computer wins because "Scissors" cuts "Paper".
danpost danpost

2013/3/30

#
I am not sure what limitations are imposed by eclipse, but if you need a class code for choices -- maybe something like:
import java.util.Random;

public class Choice
{
    private static final String[] PHRASE = { 
        "rock mirrors rock", "rock breaks scissors", "rock crushes lizard",
        "Spock mirrors Spock", "Spock vaporizes rock", "Spock smashes (or melts) scissors",
        "paper mirrors paper", "paper disproves Spock", "paper covers rock",
        "lizard mirrors lizard", "lizard eats paper", "lizard poisons Spock",
        "scissors mirrors scissors", "scissors decapitate lizard", "scissors cut paper" };
    private static final int ROCK = 0, SPOCK = 1, PAPER = 2, LIZARD = 3, SCISSORS = 4;
    private static final int COMPUTER = -1, TIE = 0, PLAYER = 1;
    private static Random rand = new Random(System.currentTimeMillis());
    private int playerChoice, computerChoice;
    private int winner;
    
    public Choice(int playerOption)
    {
        playerChoice = playerOption;
        computerChoice = rand.nextInt(5);
        if (playerChoice == computerChoice)
        {
            winner = TIE;
            return;
        }
        if ((playerChoice - computerChoice + 5) % 5 < 3) winner = PLAYER; else winner = COMPUTER;
    }

    public int getWinner()
    {
        return winner;
    }

    public int getPlayerChoice()
    {
        return playerChoice;
    }

    public int getComputerChoice()
    {
        return computerChoice;
    }

    public String getPhrase()
    {
        int winOpt = 0, loseOpt = 0;
        if (winner == TIE) winOpt = loseOpt = playerChoice;
        else if (winner == PLAYER)
        {
            winOpt = playerChoice;
            loseOpt = computerChoice;
        }
        else
        {
            winOpt = computerChoice;
            loseOpt = playerChoice;
        }
        return PHRASE[winOpt * 3 + (winOpt - loseOpt + 5) % 5];
    }
}
When the player has chosen an option, do the following:
// int playerOption = [some value that the player chooses]
Choice choice = new Choice(playerOption);
int computerOption = choice.getComputerChoice();
int winner = choice.getWinner();
String message = choice.getPhrase();
You can then compare these values like this
if (winner == Choice.COMPUTER)
// or
if (computerChoice == Choice.ROCK)
// or you could use a switch
switch(winner)
{
    case Choice.COMPUTER:  // some code
        break;
    case Choice.TIE: // some code
        break;
    case Choice PLAYER: // some code
        break;
}
You need to login to post a reply.
1
2