Advertisement
SuperJedi224

Liar's Dice Controller

Jun 5th, 2015
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 KB | None | 0 0
  1. import java.io.FileNotFoundException;
  2. import java.io.PrintStream;
  3. import java.util.*;
  4.  
  5. public class Controller {
  6.     protected static Map<Class<?>,Long> w=new HashMap<Class<?>,Long>();
  7.     public static long getScore(Player p){
  8.         return w.get(p.getClass());
  9.     }
  10.     private static void addPoints(Player p,int n){
  11.         Class<?> c=p.getClass();
  12.         w.put(c, w.get(c)+n);
  13.     }
  14.     private Controller(){}
  15.     private static List<List<Integer>> dice=new ArrayList<List<Integer>>();
  16.     private static List<Integer> roll(int n){
  17.         List<Integer> l=new ArrayList<Integer>();
  18.         Random r=new Random((long)(Math.random()*Long.MAX_VALUE));
  19.         for(int i=0;i<n;i++){
  20.             l.add(1+r.nextInt(6));
  21.         }
  22.         return l;
  23.     }
  24.     public static int diceInPlay(){
  25.         int n=0;
  26.         for(List<Integer> l:dice)n+=l.size();
  27.         return n;
  28.     }
  29.     private static List<Player> p;
  30.     private static int i;
  31.     private static List<Player> p(){
  32.         List<Player> p=new ArrayList<Player>();
  33.         p.add(new Nobody());
  34.         p.add(new MostlyHonestAbe());
  35.         p.add(new Pirate());
  36.         p.add(new Fidelio());
  37.         p.add(new AbsurdBot());
  38.         p.add(new Statistician());
  39.         p.add(new DrHouse());
  40.         p.add(new StraightShooter());
  41.         return p;
  42.     }
  43.     private static List<Player> getSubset(List<Player> p,int i){
  44.         List<Player> q=new ArrayList<Player>();
  45.         while(i*p.size()>0){
  46.             int j=(int)(Math.random()*p.size());
  47.             q.add(p.remove(j));
  48.             i--;
  49.         }
  50.         return q;
  51.     }
  52.     private static Player currentPlayer(){
  53.         return p.get(i);
  54.     }
  55.     public static void main(String[] a) throws FileNotFoundException{
  56.         for(Player k:p()){
  57.             w.put(k.getClass(),0L);
  58.         }
  59.         for(int i=1;i<=1000;i++){System.out.print(i+":");main();}
  60.         p=p();
  61.         PrintStream o=new PrintStream("res.txt");
  62.         for(Player pl:p){
  63.             o.println(pl+":"+getScore(pl));
  64.         }
  65.         o.close();
  66.     }
  67.     private static void main(){
  68.         dice.clear();
  69.         int t=0;
  70.         p=getSubset(p(),3+(int)(3*Math.random()));
  71.         System.out.print(p+" -");
  72.         List<String> bids=new ArrayList<String>();
  73.         for(int i=0;i<p.size();i++){
  74.             dice.add(roll(5));
  75.         }
  76.         i=0;
  77.         while(p.size()>1){
  78.             t++;
  79.             i%=p.size();
  80.             addPoints(p.get(i),1);
  81.             int[] d=new int[p.size()];
  82.             for(int j=0;j<d.length;j++){
  83.                 d[j]=dice.get(j).size();
  84.             }
  85.             int[] md=new int[dice.get(i).size()];
  86.             for(int j=0;j<md.length;j++){
  87.                 md[j]=dice.get(i).get(j);
  88.             }
  89.             String z=currentPlayer().bid(i, d, md, bids.toArray(new String[0]));
  90.             if(z.equals("Liar!")){
  91.                 if(bids.size()==0){
  92.                     dice.remove(i);
  93.                     p.remove(i);
  94.                     bids.clear();
  95.                     i--;
  96.                     if(i<0)i=p.size()-1;
  97.                     for(int i=0;i<dice.size();i++){
  98.                         dice.set(i, roll(dice.get(i).size()));
  99.                     }
  100.                     continue;
  101.                 }
  102.                 boolean bo=testBid(bids.get(bids.size()-1));
  103.                 bids.clear();
  104.                 if(bo){
  105.                     dice.get(i).remove(dice.get(i).size()-1);
  106.                     i--;
  107.                     if(i<0)i=p.size()-1;
  108.                     dice.get(i).add(1);
  109.                     addPoints(p.get(i),10);
  110.                 }else{
  111.                     i--;
  112.                     if(i<0)i=p.size()-1;
  113.                     dice.get(i).remove(dice.get(i).size()-1);
  114.                     i++;i%=p.size();
  115.                     dice.get(i).add(1);
  116.                     addPoints(p.get(i),10);
  117.                 }
  118.                 for(int i=0;i<dice.size();i++){
  119.                     dice.set(i, roll(dice.get(i).size()));
  120.                 }
  121.                 while(dice.indexOf(new ArrayList<Integer>())!=-1){
  122.                     int i=dice.indexOf(new ArrayList<Integer>());
  123.                     dice.remove(i);
  124.                     p.remove(i);
  125.                 }
  126.                 continue;
  127.             }else{
  128.                 boolean bo=isLegal(z)&&(bids.size()==0||isGreaterThan(z,bids.get(bids.size()-1)));
  129.                 if(bo){
  130.                     bids.add(z);
  131.                 }else{
  132.                     dice.remove(i);
  133.                     p.remove(i);
  134.                     bids.clear();
  135.                     i--;
  136.                     if(i<0)i=p.size()-1;
  137.                     for(int i=0;i<dice.size();i++){
  138.                         dice.set(i, roll(dice.get(i).size()));
  139.                     }
  140.                     continue;
  141.                 }
  142.             }
  143.             i++;
  144.             i%=p.size();
  145.             if(t>=5000){
  146.                 System.out.println("Round timed out.");
  147.                 return ;
  148.             }
  149.         }
  150.         System.out.println(p.get(0)+" wins in "+t+" turns.");
  151.         addPoints(p.get(0),1000);
  152.     }
  153.     private static boolean testBid(String string) {
  154.         Scanner r=new Scanner(string);
  155.         int x=r.nextInt(),y=r.nextInt();
  156.         r.close();
  157.         for(List<Integer> l:dice)for(int i:l)if(i==y||i==1)x--;
  158.         return x<=0;
  159.     }
  160.     private static boolean isLegal(String string) {
  161.         try{Scanner r=new Scanner(string);
  162.         int x=r.nextInt(),y=r.nextInt();
  163.         r.close();
  164.         return 0<y&&0<x&&y<7;}catch(Exception e){return false;}
  165.     }
  166.     public static String[] playerNames(){
  167.         String[] s=new String[p.size()];
  168.         for(int i=0;i<s.length;i++){
  169.             s[i]=p.get(i).toString();
  170.         }
  171.         return s;
  172.     }
  173.     public static int valueOf(String i) {
  174.         String[] j=i.split(" ");
  175.         int y=Integer.parseInt(j[0]);
  176.         int z=Integer.parseInt(j[1]);
  177.         return y*6+z;
  178.     }
  179.     public static boolean isGreaterThan(String string1,String string2) {
  180.         return valueOf(string1)>valueOf(string2);
  181.     }
  182.     public static int numPlayers(){
  183.         return p.size();
  184.     }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement