Rock-paper-scissors: Difference between revisions

m
→‎{{header|Java}}: Shortened a bit, no need to total up the throws every round, just count as you go
(+Java)
m (→‎{{header|Java}}: Shortened a bit, no need to total up the throws every round, just count as you go)
Line 174:
public class RPS {
public enum Item{
ROCK, PAPER, SCISSORS; //add other throwable items here if you wish
}
//EnumMap uses a simple array under the hood
Line 183:
//add other rules here for additional types
}};
 
public static Map<Item, Item> losesTo = new EnumMap<Item, Item>(Item.class){{
put(Item.SCISSORS, Item.ROCK);
Line 190:
//add other rules here for additional types
}};
 
public static Map<Item, Integer> counts = new EnumMap<Item, Integer>(Item.class){{
put(Item.ROCK, 1);
Line 197:
//add other counts here for additional types
}};
 
private static int totalThrows = Item.values().length;
 
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Line 212 ⟶ 214:
}
counts.put(choice, counts.get(choice) + 1);
totalThrows += count+;
System.out.println("Computer chose: " + aiChoice);
if(aiChoice.equals(choice)){
Line 225 ⟶ 228:
 
private static Item getAIChoice() {
int totalThrows = 0;
for(Integer count:counts.values()){
totalThrows += count;
}
int rand = (int)(Math.random() * (totalThrows));
for(Item item:Item.values()){
Line 277 ⟶ 276:
You chose...wisely. You win!
...</pre>
 
=={{header|Python}}==
<lang python>#!/usr/bin/python
Anonymous user