Rock-paper-scissors: Difference between revisions
Content added Content deleted
m (→{{header|Tcl}}: More comments) |
(→{{header|Java}}: Added fixes for expandability, added rules for RPScLSp in comments) |
||
Line 223: | Line 223: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
This could probably be made simpler, but some more complexity is necessary so that other items besides rock, paper, and scissors can be added (as school children like to do). The method <code>getAIChoice()</code> borrows from [[#Ada|the Ada example]] in spirit, but is more generic to additional items. |
This could probably be made simpler, but some more complexity is necessary so that other items besides rock, paper, and scissors can be added (as school children and nerds like to do [setup for rock-paper-scissors-lizard-spock is in multi-line comments]). The method <code>getAIChoice()</code> borrows from [[#Ada|the Ada example]] in spirit, but is more generic to additional items. |
||
<lang java5>import java.util. |
<lang java5>import java.util.Arrays; |
||
import java.util.Collections; |
|||
import java.util.EnumMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
import java.util.Map; |
||
import java.util.Scanner; |
import java.util.Scanner; |
||
Line 230: | Line 233: | ||
public class RPS { |
public class RPS { |
||
public enum Item{ |
public enum Item{ |
||
ROCK, PAPER, SCISSORS |
ROCK, PAPER, SCISSORS, /*LIZARD, SPOCK*/; |
||
} |
} |
||
//EnumMap uses a simple array under the hood |
//EnumMap uses a simple array under the hood |
||
public static Map<Item, Item> beats = new EnumMap<Item, Item>(Item.class){{ |
public static Map<Item, List<Item>> beats = new EnumMap<Item, List<Item>>(Item.class){{ |
||
put(Item.ROCK, Item.SCISSORS); |
put(Item.ROCK, Arrays.asList(Item.SCISSORS/*, Item.LIZARD*/)); |
||
put(Item.PAPER, Item.ROCK); |
put(Item.PAPER, Arrays.asList(Item.ROCK/*, Item.SPOCK*/)); |
||
put(Item.SCISSORS, Item.PAPER); |
put(Item.SCISSORS, Arrays.asList(Item.PAPER/*, Item.LIZARD*/)); |
||
/* |
|||
//add other rules here for additional types |
|||
put(Item.SPOCK, Arrays.asList(Item.ROCK, Item.SCISSORS)); |
|||
put(Item.LIZARD, Arrays.asList(Item.SPOCK, Item.PAPER)); |
|||
*/ |
|||
}}; |
}}; |
||
public static Map<Item, Item> losesTo = new EnumMap<Item, Item>(Item.class){{ |
public static Map<Item, List<Item>> losesTo = new EnumMap<Item, List<Item>>(Item.class){{ |
||
put(Item.SCISSORS, Item.ROCK); |
put(Item.SCISSORS, Arrays.asList(Item.ROCK/*, Item.SPOCK*/)); |
||
put(Item.ROCK, Item.PAPER); |
put(Item.ROCK, Arrays.asList(Item.PAPER/*, Item.SPOCK*/)); |
||
put(Item.PAPER, Item.SCISSORS); |
put(Item.PAPER, Arrays.asList(Item.SCISSORS/*, Item.LIZARD*/)); |
||
/* |
|||
//add other rules here for additional types |
|||
put(Item.SPOCK, Arrays.asList(Item.PAPER, Item.LIZARD)); |
|||
put(Item.LIZARD, Arrays.asList(Item.SCISSORS, Item.ROCK)); |
|||
*/ |
|||
}}; |
}}; |
||
public static Map<Item, Integer> counts = new EnumMap<Item, Integer>(Item.class){{ |
public static Map<Item, Integer> counts = new EnumMap<Item, Integer>(Item.class){{ |
||
put(Item.ROCK, 1); |
put(Item.ROCK, 1); |
||
Line 252: | Line 261: | ||
put(Item.SCISSORS, 1); |
put(Item.SCISSORS, 1); |
||
//add other counts here for additional types |
//add other counts here for additional types |
||
/* |
|||
put(Item.SPOCK, 1); |
|||
put(Item.LIZARD, 1); |
|||
*/ |
|||
}}; |
}}; |
||
private static int totalThrows = Item.values().length; |
private static int totalThrows = Item.values().length; |
||
public static void main(String[] args){ |
public static void main(String[] args){ |
||
Scanner in = new Scanner(System.in); |
Scanner in = new Scanner(System.in); |
||
Line 274: | Line 287: | ||
if(aiChoice.equals(choice)){ |
if(aiChoice.equals(choice)){ |
||
System.out.println("Tie!"); |
System.out.println("Tie!"); |
||
}else if(beats.get(choice). |
}else if(beats.get(choice).contains(aiChoice)){ |
||
System.out.println("You chose...wisely. You win!"); |
System.out.println("You chose...wisely. You win!"); |
||
}else{ |
}else{ |
||
Line 287: | Line 300: | ||
for(Item item:Item.values()){ |
for(Item item:Item.values()){ |
||
if(rand < counts.get(item)){ |
if(rand < counts.get(item)){ |
||
Collections.shuffle(losesTo.get(item)); |
|||
return losesTo.get(item).get(0); |
|||
} |
} |
||
rand -= counts.get(item); |
rand -= counts.get(item); |