Login Register






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


rock scissors paper filter_list
Author
Message
RE: rock scissors paper #11
@ArkPhaze well it's hard for me to understand what u did it's another language i guess its c++ am i right ?
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #12
(11-04-2013, 01:26 AM)blackeagle Wrote: @ArkPhaze well it's hard for me to understand what u did it's another language i guess its c++ am i right ?

I wrote that in C actually. But what parts do you not understand? I can explain them.
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 #13
@ArkPhaze i didnt understand what u meant by you don't have to compare values individually like that for every outcome. You can use math to your advantage !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #14
(11-04-2013, 01:40 AM)blackeagle Wrote: @ArkPhaze i didnt understand what u meant by you don't have to compare values individually like that for every outcome. You can use math to your advantage !!

Well, look at my piece of code lol. Do you see me comparing 1 with 2, 1 with 0, and so on? Smile The way my code works, I calculate the output as probability that the CPU had already won. Depending on if the CPU lost, won, or had a draw with your user input, depends on what I calculate after the fact, for the CPU's choice.

If I say that you chose ROCK, and the CPU won, then obviously the CPU had chosen Paper. Reversely, in that same scenario, if the CPU lost, then obviously the CPU chose scissors. A draw is self explanatory. I'm doing things in reverse and using booleans to my advantage in this way.

Out of all possible values, 0, 1, 2 -- both 1 and 2 actually evaluate to true when put into an if statement. These indicate a loss or a win, because the last one (0) is reserved for a draw, and it evaluates false within an if statement expression. It works out perfectly.

Now that my variable 'cpu' was used to calculate the odds of a win/loss over a draw, I can change that variable's purpose to hold what the CPU chose. It's an effective use of 1 variable for 2 reasons. Now a win or loss out of the first expression returning true in the case that 'cpu' is equal to either 1 or 2, I can decide whether that is a win or loss depending on if 'cpu' is odd or even, which again, works out to be perfect because out of 1 and 2, it's a 50/50 split for odd/even. To check that, I avoided the modulo operator and did some bitwise magic instead.
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 #15
@ArkPhaze now i understood Smilebut i guess this is a little hard for me and i guess it's not that bad my way Tongue lol i'm very tired for now i might work on ur idea 2mw thx for the help Biggrin
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #16
Yours doesn't even need to be as advanced. All I'm saying, as what another member has said above, is to use integrals for the checking, not strings.
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 #17
(11-04-2013, 12:52 AM)Frooxius Wrote: Just a quick suggestion: Try multiplying the random change by 3, so you don't have to compare it against fractional numbers. Like this:

Code:
public static String pcChoice() {
        double random = Math.random()*3;
        String choicePc = null;
        if (random <= 1) {
            choicePc = "rock";
        }
        if ((random > 1) && (random <= 2)) {
            choicePc = "paper";
        }
        if (random > 3) {
            choicePc = "scissors";
        }
        return choicePc;
    }

Using Math.random() is a bad idea in general if all you need is a choice of three, which is an integer range.
Math.random() returns a double.
Random.nextInt(n) returns an int and should be prefered as it is less biased. Before I repeat any explanation I refer to this discussion about the difference: http://stackoverflow.com/questions/73862...nextintint



@"blackeagle"

That's exactly what I wanted with the methods.
I also see you have put a game loop into it too.

Now let's go down to the details. Some of these where already mentioned by @Frooxius and @"ArkPhaze".

You should really have a look into enums and see how you can improve your program with your newly gained knowledge.
Look here for more: http://howtodoinjava.com/2012/12/07/guid...m-in-java/
http://docs.oracle.com/javase/tutorial/j.../enum.html

About the random choice for the PC. Java has a Random class that provides several methods to get random values. See at the documentation here: http://docs.oracle.com/javase/7/docs/api...andom.html
Try to get used to reading the documentation if you have any questions regarding how and when to use certain classes or methods.
If you have problems understanding it right now, don't worry. You will get used to it. Just look a bit for methods that seem to suit your needs. (I actually wanted to go there by letting you choose the right one, but I already mentioned it to Frooxius above, so, well, let's go on) So find the nextInt(n) method, read its explanation and try to use it for your program.
If you have any problems, come back to me. It is very important that you learn to help yourself, which is why I prefer that you try first and possibly come back with questions.
We will look at the pc choice method afterwards to improve it further.



Code:
while ((!choice.equalsIgnoreCase("rock")) && (!choice.equalsIgnoreCase("paper")) && (!choice.equalsIgnoreCase("scissors")))

If you have a relatively complicated conditional expression like this, prefer to extract it as a method too.
In this case you can name it i.e. isValidChoice and pass the user's choice into it.
The outline:

Code:
private boolean isValidChoice(String choice) {
    //your code
}

And you would use it like this:

Code:
while(isValidChoice(choice)) {

The biggest advantage here is the better readability.



chooseWinner is a relatively repetitive method implementation we can shorten a lot once you read about enums. Wink



Code:
} while (playAgain.equalsIgnoreCase("y"));
        if (playAgain.equals("n")) {
            System.out.println("thx for playing");
        }

You are using equals and equalsIgnoreCase incosistently. One time the user is allowed to press Y and the other time he isn't.
Instead of doing this all the time and creating possible bugs, make playAgain a boolean. Check once if the userinput equalsIgnoreCase("y") and then set the correct boolean value to playAgain. This way you also avoid the need of consistency in the use of equals vs equalsIgnoreCase.

The last if statement will be superfluous then (after your corrections). After exiting the loop the if condition should always yield true, so just leave the if statement away and print your thanks string.
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 #18
Quote:and finally can you tell me why do i need to make methods if i'm using them only once !!

I overlooked this question at the first read.
Reasons to create methods:

1. Readability because you have more abstract sight to it.

Example: Imagine someone tells you how to create a meal by telling you which limbs you have to move. Like: "Move your left arm up and move your finger like this" You won't understand a thing that's going on if you just read this, will you?
But if someone tells you an instruction like "pour milk over it" you will.

Now look at your code in main before and after. If the names of the methods are well chosen you won't have any problem to understand what this does:

userChoice()
chooseWinner()

Why are there chapters in books? Because they make it easier to find stuff. They put structure into it, which you can also access easily by looking into the index of the book. Same for your code. If you structure the code into classes and methods, you can find them more easily and your IDE may even make an "index" for you where you have fast access by one click.

The advantages of readability shouldn't be underestimated. It will be easier to spot any problems and to expand your program. It will be easier for others to read your code and it will be less likely that you produce any bugs.

But readability is not the only advantage.

2. Reusability

Although you said you won't reuse any of the functions, you might want to reuse them in the future. If that's in other programs or not doesn't matter.

3. Easier to test
It easier to write a test for a function, because you don't have to copy&paste anything. You just use it as is.
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 #19
@Deque
I didn't quite understand why you mentioned me
since i have no posts in this thread.


Reply

RE: rock scissors paper #20
(11-04-2013, 02:33 PM)Slarek Wrote: @Deque
I didn't quite understand why you mentioned me
since i have no posts in this thread.

By bad. Corrected. (I wrongly remembered you being one of the posters here)
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







Users browsing this thread: 1 Guest(s)