Login Register






rock scissors paper filter_list
Author
Message
RE: rock scissors paper #21
@Deque thanks to you now i know why i should always use methods Smile
Code:
while ((!choice.equalsIgnoreCase("rock")) && (!choice.equalsIgnoreCase("paper")) && (!choice.equalsIgnoreCase("scissors")))
i'll work on making a method for this it won't be hard !!

i'll read about enum and the difference between Math.random() and Random.nextInt(n) thx for sharing the links !!
i'll work on all the points you told me about and ill post the code here again!!

ps:thank you very much
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #22
@Deque - Huh? :S

That's not my Java code, I only posted C code and referenced OP's java code. A reference to Java docs for using enums in my C code though won't help. I know an enum could've been used, but it was more of an example from any release code. I could improve it further with a little more bitwise trickery, but it would've complicated things more for OP since he doesn't know C or C++ and I'm not a Java programmer.
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 #23
I just wanted to show that you dont need to use strings for programming this game, and you also dont necessarily have to manually validate/check every outcome if you devise a unique formula.

Edit: I can post optimized code using an enum when I am off work though if anyone is curious. Smile I should've commented my code though since he doesnt understand C and it's a little more advanced for the game strategy.
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 #24
(11-04-2013, 06:18 PM)ArkPhaze Wrote: @Deque - Huh? :S

That's not my Java code, I only posted C code and referenced OP's java code. A reference to Java docs for using enums in my C code though won't help. I know an enum could've been used, but it was more of an example from any release code. I could improve it further with a little more bitwise trickery, but it would've complicated things more for OP since he doesn't know C or C++ and I'm not a Java programmer.

I don't know what you are talking about or why you are confused. I didn't say anything about your code.
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 #25
@ArkPhaze well i agree with @Deque she didn't say anything about ur code !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #26
@Deque can you pls give en exemple on how to us Random.nextInt(n) cz i tried for many times and i cant figure out how to make it work !!

never mind figured how to make it work had to import java.util.Random
(This post was last modified: 11-04-2013, 09:07 PM by LordPankake.)
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #27
@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 !!
[Image: blackeagle_zps6ad86521.gif]

Reply

RE: rock scissors paper #28
An enum is exactly what Frooxious said, named constants. I would make 3 different aliases for Rock Paper Scissors and then instead of comparing the integrals, you can compare them by named aliases. I'm sure Deque will respond about the random integer. If she wasnt talking about my code then its my mistake. Its hard reading posts on a phone; why I dont usually do it. I'm almost home from work though. What Deque said about enumsnis correct though, and regardless, it applied to my code too, so I was confused perhaps. I didnt want to convolute things more, and especially now since I know you havent worked with enums much.

@Deque - yeah sorry! I apologize, My phone with a smaller width put the mention for me on a newline, so I correlated my mention with the following paragraph:
Quote:You should really have a look into enums and see how you can improve your program with your newly gained knowledge.
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 #29
(11-04-2013, 02:05 PM)Deque Wrote: Using Math.random() is a bad idea in general if all you need is a choice of three, which is an integer range.

Definitely, I talked with him on the IRC as well and also suggested to use a switch with that (plus you can directly check for equality with integer, instead of using range comparison).

The way I do this kind of thing myself that I find neat (not in Java though since I don't use that much) is in combination with enums, where I put some "TERMINATOR" element at the end of the enum that stays the same (the name doesn't change) and when I need to select random element from the enum, initialize array based on it or run through a loop, I can do something like this:


Code:
enum Option { Option0, Option1, Option2, Option3, END }; // .... // Random.Range(int min, int max) generates between min (including) and max (excluding) Option randomOption = (Option)Random.Range(0, (int)Option.END);

That way I can keep altering, expanding and modifying the elements in the enum as needed and the code that works with it stays the same and takes all the new elements into account, so it saves a lot of work and keeps the code nicely organized and flexible.

@BlackEagle: I recommend looking at this:
http://docs.oracle.com/javase/tutorial/j.../enum.html

Essentially you'll do something like
Code:
public enum Choice { ROCK, PAPER, SCISSORS }

And then you use "Choice" as new "datatype" instead of "string" to store the choice.

For example modify the function to pick random choice like this:


Code:
public static Choice pcChoice() { int random = (int) Math.random()*3; if(random == 0) return Choice.ROCK; if(random == 1) return Choice.PAPER; if(random == 2) return Choice.SCISSORS; }
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 #30
(11-05-2013, 02:54 AM)Frooxius Wrote: @BlackEagle: I recommend looking at this:
http://docs.oracle.com/javase/tutorial/j.../enum.html

Essentially you'll do something like
Code:
public enum Choice { ROCK, PAPER, SCISSORS }

And then you use "Choice" as new "datatype" instead of "string" to store the choice.

For example modify the function to pick random choice like this:[/color]

Code:
public static Choice pcChoice() { int random = (int) Math.random()*3; if(random == 0) return Choice.ROCK; if(random == 1) return Choice.PAPER; if(random == 2) return Choice.SCISSORS; }

@Frooxius - Deque already mentioned to avoid Math.random() because it returns a floating point value. Instead of having to cast to an integer, why not start with a function that already returns the type you need? --> Random.nextInt()
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply