Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


rock scissors paper filter_list
Author
Message
RE: rock scissors paper #31
@ArkPhaze: Yes, if it returns integers you can use that one as well and it's better suited for the situation, although both will work.

It might be also good idea for exercise to try to implement own extensions of the random function, where you pass the range within which you want to generate random number and it'll return it for you - make variants for both integers and doubles.

Also quite useful might be to implement function which accept some collection and return random element from it.
I love creativity and creating, I love science and rational thought, I am an open atheist and avid self-learner.

Reply

RE: rock scissors paper #32
(11-04-2013, 10:30 PM)blackeagle Wrote: @Deque i'm sorry to bother you again but i have readed alot about enum and wasn't able to understand how could i use it in my favor in my game can you pls give me an exemple in my game to see if i could understand why to use it when to use it and how to use it i'm totally lost !!

See @"Frooxius", he gave you an example for your code.

Strings have a problem, they can't be checked by the compiler for spelling errors, they are computational extensive to compare with each other and you can use strings that aren't allowed (like "house" instead of "rock", "paper" and "scissors")

If you use an Enum type, you ensure that:
No spelling errors happen (the compiler will tell you otherwise).
You only use options that are allowed (which are the Enums you defined) and there is no confusion about what kind of input is expected.

You can use them everytime you have a predefined set of options.
In your game it would be the choice of the player or the computer.
And you can as well create an Enum type for the Game result (WIN, TIE, LOOSE).
Look out in your code for string or number comparisons that are actually options or status codes (like "0" means "off", "1" means "on", "6" means "scratch my back"). These are likely candidates for an enum.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: rock scissors paper #33
i was able to make this so far
Code:
public enum Choice{
    rock,
    paper,
    scissors;
}

Code:
public static Choice pcChoice() {
        Scanner in = new Scanner(System.in);
        int rand = (new Random()).nextInt(3);
            Choice choicePc;
        if (rand == 0) {
           choicePc = Choice.rock;
        } else if (rand == 1) {
            choicePc = Choice.paper;
        } else {
            choicePc = Choice.scissors;
        }
        return choicePc;
    }

is this good ?
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #34
Why not just cast the random int to a Choice and avoid all the if logic?
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: rock scissors paper #35
(11-06-2013, 02:10 AM)blackeagle Wrote: i was able to make this so far
Code:
public enum Choice{
    rock,
    paper,
    scissors;
}

Code:
public static Choice pcChoice() {
        Scanner in = new Scanner(System.in);
        int rand = (new Random()).nextInt(3);
            Choice choicePc;
        if (rand == 0) {
           choicePc = Choice.rock;
        } else if (rand == 1) {
            choicePc = Choice.paper;
        } else {
            choicePc = Choice.scissors;
        }
        return choicePc;
    }

is this good ?

You don't need the scanner in your method.
Enum constants should be written uppercase only, so rename them to ROCK, PAPER, SCISSORS.

You can also shorten your code a lot by doing this:

Code:
Choice[] choices = Choice.values();
int rand = new Random().nextInt(choices.length());
return choices[rand];

values() returns an array with all enum constants. You just access this array with your random number.

@ArkPhaze:

You can't cast from Integer to Enum in Java.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: rock scissors paper #36
@Deque am sry fro the scanner i used it to try something and forgot to remove it !!
about the table of choces to shorten my code i totally agree with you i feel like i'm an idiot i didn't think about it !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #37
i was able to do this aswell
Code:
public class RockPaperScissors {
public enum Choice{
    ROCK,
    PAPER,
    SCISSORS;
}
    
    public static Choice playerChoice() {
       System.out.println("choose 0 for rock");
       System.out.println("choose 1 for paper");
       System.out.println("choose 2 for scissors");
        Scanner in = new Scanner(System.in);
             int numberChosen;Choice choicePlayer=Choice.ROCK;
        numberChosen = in.nextInt();
        
        switch(numberChosen){
        case 0: choicePlayer=Choice.ROCK;
         break;  
        case 1: choicePlayer=Choice.PAPER;
            break;
        case 2: choicePlayer=Choice.SCISSORS;
break;          
        default:System.out.println("enter 0 1 or 2") ;
    }
        return choicePlayer;

    }

   public static Choice pcChoice() {
         Choice []choicePc=Choice.values();
      int rand = (new Random()).nextInt((choicePc.length));
         return choicePc[rand];
        }

pls let me know if i'm doing anything wrong
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #38
Since you get integers from the user you can as well use Choice.values()[numberChosen] to return the Choice of the user (same principle as in pcChoice()).

Alternatively there is a method to get the enum constant for a given string.
Code:
Choice.valueOf("rock".toUpperCase())
will return the enum constant ROCK.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: rock scissors paper #39
ok so now i fixed this and thx for guiding me again !! now fo comparing for the tie its easy just made if (choise1==choice2)
but how two compare two that are difference for exemple in my case i want that if choice 1 is rock and choice 2 is scissors (choice 1 wish is the player will win)
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #40
i managed to find a way to do it but im not sure if there's a better way so lets see about this

this is the fixed playerChoice
Code:
public static Choice playerChoice() {
        System.out.println("choose 0 for rock");
        System.out.println("choose 1 for paper");
        System.out.println("choose 2 for scissors");
        Scanner in = new Scanner(System.in);
        int numberChosen;
        Choice choicePlayer[];
        numberChosen = in.nextInt();

        choicePlayer = Choice.values();

        return choicePlayer[numberChosen];

    }

and this is the new chooseWinner

Code:
public static void chooseWinner(Choice choice1, Choice choice2) {

        if (choice1 == choice2) {
            System.out.println("its a tie !");
        }
        //if player chose rock


        if ((choice1.ordinal() == 0) && (choice2.ordinal() == 2)) {
            System.out.println("player win");
        }
        if ((choice1.ordinal() == 0) && (choice2.ordinal() == 1)) {
            System.out.println("pc win");
        }

        //if player chose scissors
        if ((choice1.ordinal() == 2) && (choice2.ordinal() == 1)) {
            System.out.println("player win");
        }
        if ((choice1.ordinal() == 2) && (choice2.ordinal() == 0)) {
            System.out.println("pc won");
        }
        //if player chose paper
        if ((choice1.ordinal() == 1) && (choice2.ordinal() == 0)) {
            System.out.println("player win");
        }
        if ((choice1.ordinal() == 1) && (choice2.ordinal() == 2)) {
            System.out.println("pc won");
        }

    }
[Image: blackeagle_zps6ad86521.gif]

Reply







Users browsing this thread: 2 Guest(s)