Rock-paper-scissors: Difference between revisions

(Go solution)
Line 433:
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.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.CollectionsRandom;
 
public class RPS {
public enum Item{
ROCK, PAPER, SCISSORS, /*LIZARD, SPOCK*/;
public List<Item> losesToList;
public boolean losesTo(Item other) {
return losesToList.contains(other);
/*}
static {
put(Item SCISSORS.PAPER,losesToList = Arrays.asList(Item.ROCK/*, Item.SPOCK*/));
put(Item ROCK.ROCK,losesToList = Arrays.asList(Item.PAPER/*, Item.SPOCK*/));
put(Item PAPER.ROCK,losesToList = Arrays.asList(Item.SCISSORS/*, Item.LIZARD*/));
/*
put(Item SPOCK.SPOCK,losesToList = Arrays.asList(Item.PAPER, Item.LIZARD));
put(Item LIZARD.LIZARD,losesToList = Arrays.asList(Item.SCISSORS, Item.ROCK));
*/
}
}
//EnumMap uses a simple array under the hood
public staticfinal Map<Item, List<Item>Integer> beatscounts = new EnumMap<Item, List<Item>Integer>(Item.class){{
for(Item item:Item.values()){
put(Item.ROCK, Arrays.asList(Item.SCISSORS/*, Item.LIZARD*/));
put(Item.ROCKitem, 1);
put(Item.PAPER, Arrays.asList(Item.ROCK/*, Item.SPOCK*/));
put(Item.SCISSORS, Arrays.asList(Item.PAPER/*, Item.LIZARD*/));
/*
put(Item.SPOCK, Arrays.asList(Item.ROCK, Item.SCISSORS));
put(Item.LIZARD, Arrays.asList(Item.SPOCK, Item.PAPER));
*/
}};
 
private static int totalThrows = Item.values().length;
public static Map<Item, List<Item>> losesTo = new EnumMap<Item, List<Item>>(Item.class){{
 
put(Item.SCISSORS, Arrays.asList(Item.ROCK/*, Item.SPOCK*/));
put(Item.ROCK, Arrays.asList(Item.PAPER/*, Item.SPOCK*/));
put(Item.PAPER, Arrays.asList(Item.SCISSORS/*, Item.LIZARD*/));
/*
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){{
put(Item.ROCK, 1);
put(Item.PAPER, 1);
put(Item.SCISSORS, 1);
//add other counts here for additional types
/*
put(Item.SPOCK, 1);
put(Item.LIZARD, 1);
*/
}};
private static int totalThrows = Item.values().length;
public static void main(String[] args){
RPS rps = new RPS();
rps.run();
}
 
public void run() {
Scanner in = new Scanner(System.in);
System.out.print("Make your choice: ");
Line 493 ⟶ 485:
totalThrows++;
System.out.println("Computer chose: " + aiChoice);
if(aiChoice.equals( == choice)){
System.out.println("Tie!");
}else if(beatsaiChoice.getlosesTo(choice).contains(aiChoice)){
System.out.println("You chose...wisely. You win!");
}else{
Line 504 ⟶ 496:
}
 
private static Itemfinal getAIChoiceRandom rng = new Random() {;
private Item getAIChoice() {
int rand = (int)(Mathrng.random() * nextInt(totalThrows));
for(Item item:Item.values()){
iffor(rand Map.Entry<Item, Integer> entry:counts.getentrySet(item)){
Item item = entry.getKey();
Collections.shuffle(losesTo.get(item));
int count = entry.getValue();
return losesTo.get(item).get(0);
if(rand < count){
List<Item> losesTo = item.losesToList;
return losesTo.get(item)rng.getnextInt(0losesTo.size()));
}
rand -= counts.get(item)count;
}
return null;
Anonymous user