Drewyboyo Posted February 16, 2018 Posted February 16, 2018 We're supposed to simulate a game of clue in our AP Comp Sci class using arrays/lists. What would be an efficient way of assigning/generating values to randomly select? We use the Math class and usually something like math.random but need more help and guidance on this thanks
Drewyboyo Posted February 16, 2018 Author Posted February 16, 2018 3 minutes ago, FrostBug said: What even is a 'game of clue' ? https://en.wikipedia.org/wiki/Cluedo
FrostBug Posted February 16, 2018 Posted February 16, 2018 3 minutes ago, Drewyboyo said: https://en.wikipedia.org/wiki/Cluedo CBA to read all of that sorry. If you could give a summary of what you're supposed to make with this, that'd probably help
Drewyboyo Posted February 16, 2018 Author Posted February 16, 2018 1 minute ago, FrostBug said: CBA to read all of that sorry. If you could give a summary of what you're supposed to make with this, that'd probably help 4 people in an array/list, 4 locations in an array/list, randomly select a person , and a place and then I just simply print out the result. Initial idea was to assign everyone a value, generate a value, and print whatever the value was assigned to
FrostBug Posted February 16, 2018 Posted February 16, 2018 2 minutes ago, Drewyboyo said: 4 people in an array/list, 4 locations in an array/list, randomly select a person , and a place and then I just simply print out the result. Initial idea was to assign everyone a value, generate a value, and print whatever the value was assigned to public class Clue { private final static String[] PEOPLE = {"Tom", "FrostBug", "NormieNurd", "Moldy" }; private final static String[] PLACES = {"Library", "House", "Place", "OtherPlace" }; public static void main(String[] args) { String person = PEOPLE[(int)(Math.random() * PEOPLE.length)]; String place = PLACES[(int)(Math.random() * PLACES.length)]; System.out.println("Murder at " + place + " by " + person); } } Like that? 1
dreameo Posted February 16, 2018 Posted February 16, 2018 lol randomly select an index from 'people' and 'location', remove the objects from the list, print out result, continue till empty, profit
Drewyboyo Posted February 16, 2018 Author Posted February 16, 2018 4 minutes ago, FrostBug said: public class Clue { private final static String[] PEOPLE = {"Tom", "FrostBug", "NormieNurd", "Moldy" }; private final static String[] PLACES = {"Library", "House", "Place", "OtherPlace" }; public static void main(String[] args) { String person = PEOPLE[(int)(Math.random() * PEOPLE.length)]; String place = PLACES[(int)(Math.random() * PLACES.length)]; System.out.println("Murder at " + place + " by " + person); } } Like that? yea, i just added.. String person1 = PEOPLE[(int)(Math.random() * PEOPLE.length)]; and added in the print to make a murdered, and not just a murderer, thanks : D
battleguard Posted February 17, 2018 Posted February 17, 2018 https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int-
liverare Posted February 17, 2018 Posted February 17, 2018 If you haven't figured out arrays work in Java, then I know you haven't a clue what private final static means. You should probably tweak your code, unless you want to be that kid who hands in an A+ work, but who can't explain any of it.
Drewyboyo Posted February 19, 2018 Author Posted February 19, 2018 On 2/17/2018 at 1:58 PM, liverare said: If you haven't figured out arrays work in Java, then I know you haven't a clue what private final static means. You should probably tweak your code, unless you want to be that kid who hands in an A+ work, but who can't explain any of it. i know final when regarding declaring variables, any different there? if so, then I could learn. Didn't even spot that part of the code till you pointed it out.