![]() |
|
rock scissors paper - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Java, JVM, & JRE (https://sinister.li/Forum-Java-JVM-JRE) +--- Thread: rock scissors paper (/Thread-rock-scissors-paper) |
RE: rock scissors paper - blackeagle - 11-04-2013 @Deque thanks to you now i know why i should always use methods ![]() Code: while ((!choice.equalsIgnoreCase("rock")) && (!choice.equalsIgnoreCase("paper")) && (!choice.equalsIgnoreCase("scissors")))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 RE: rock scissors paper - ArkPhaze - 11-04-2013 @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. RE: rock scissors paper - ArkPhaze - 11-04-2013 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. I should've commented my code though since he doesnt understand C and it's a little more advanced for the game strategy.
RE: rock scissors paper - Deque - 11-04-2013 (11-04-2013, 06:18 PM)ArkPhaze Wrote: @Deque - Huh? :S I don't know what you are talking about or why you are confused. I didn't say anything about your code. RE: rock scissors paper - blackeagle - 11-04-2013 @ArkPhaze well i agree with @Deque she didn't say anything about ur code !! RE: rock scissors paper - blackeagle - 11-04-2013 @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 RE: rock scissors paper - blackeagle - 11-04-2013 @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 !! RE: rock scissors paper - ArkPhaze - 11-05-2013 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. RE: rock scissors paper - Frooxius - 11-05-2013 (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/java/javaOO/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;
}RE: rock scissors paper - ArkPhaze - 11-05-2013 (11-05-2013, 02:54 AM)Frooxius Wrote: @BlackEagle: I recommend looking at this: @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() |