Advertisement
SuperJedi224

Liar's Dice Testing Controller

Jun 11th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Controller{
  4.     protected static Map<Class<?>,Long> w=new HashMap<Class<?>,Long>();
  5.     public static long getScore(Player p){return 0;}
  6.     private Controller(){}
  7.     private static List<List<Integer>> dice=new ArrayList<List<Integer>>();
  8.     private static List<Integer> roll(int n){
  9.         List<Integer> l=new ArrayList<Integer>();
  10.         Random r=new Random((long)(Math.random()*Long.MAX_VALUE));
  11.         for(int i=0;i<n;i++){
  12.             l.add(1+r.nextInt(6));
  13.         }
  14.         return l;
  15.     }
  16.     public static int diceInPlay(){
  17.         int n=0;
  18.         for(List<Integer> l:dice)n+=l.size();
  19.         return n;
  20.     }
  21.     private static List<Player> p;
  22.     private static int i;
  23.     private static List<Player> p(){
  24.         List<Player> p=new ArrayList<Player>();
  25.         p.add(new Pirate());
  26.         p.add(new Nobody());
  27.         return p;
  28.     }
  29.     private static Player currentPlayer(){
  30.         return p.get(i);
  31.     }
  32.     public static void main(String[]a){
  33.         dice.clear();
  34.         int t=0;
  35.         p=p();
  36.         List<String> bids=new ArrayList<String>();
  37.         for(int i=0;i<p.size();i++){
  38.             dice.add(roll(5));
  39.         }
  40.         while(p.size()>1){
  41.             t++;
  42.             i%=p.size();
  43.             int[] d=new int[p.size()];
  44.             for(int j=0;j<d.length;j++){
  45.                 d[j]=dice.get(j).size();
  46.             }
  47.             int[] md=new int[dice.get(i).size()];
  48.             for(int j=0;j<md.length;j++){
  49.                 md[j]=dice.get(i).get(j);
  50.             }
  51.             String z=currentPlayer().bid(i, d, md, bids.toArray(new String[0]));
  52.             System.out.println(currentPlayer()+" "+z);
  53.             if(z.equals("Liar!")){
  54.                 boolean bo=testBid(bids.get(bids.size()-1));
  55.                 bids.clear();
  56.                 if(bo){
  57.                     dice.get(i).remove(dice.get(i).size()-1);
  58.                     i--;
  59.                     if(i<0)i=p.size()-1;
  60.                     dice.get(i).add(1);
  61.                 }else{
  62.                     i--;
  63.                     if(i<0)i=p.size()-1;
  64.                     dice.get(i).remove(dice.get(i).size()-1);
  65.                     i++;i%=p.size();
  66.                     dice.get(i).add(1);
  67.                 }
  68.                 for(int i=0;i<dice.size();i++){
  69.                     dice.set(i, roll(dice.get(i).size()));
  70.                 }
  71.                 while(dice.indexOf(new ArrayList<Integer>())!=-1){
  72.                     int i=dice.indexOf(new ArrayList<Integer>());
  73.                     dice.remove(i);
  74.                     p.remove(i);
  75.                 }
  76.                 continue;
  77.             }else{
  78.                 boolean bo=isLegal(z)&&(bids.size()==0||isGreaterThan(z,bids.get(bids.size()-1)));
  79.                 if(bo){
  80.                     bids.add(z);
  81.                 }else{
  82.                     dice.remove(i);
  83.                     p.remove(i);
  84.                     bids.clear();
  85.                     i--;
  86.                     for(int i=0;i<dice.size();i++){
  87.                         dice.set(i, roll(dice.get(i).size()));
  88.                     }
  89.                     continue;
  90.                 }
  91.             }
  92.             i++;
  93.             i%=p.size();
  94.             if(t>=10000)return ;
  95.         }
  96.         System.out.println(p.get(0)+" wins in "+t+" turns.");
  97.     }
  98.     private static boolean testBid(String string) {
  99.         Scanner r=new Scanner(string);
  100.         int x=r.nextInt(),y=r.nextInt();
  101.         r.close();
  102.         for(List<Integer> l:dice)for(int i:l)if(i==y||i==1)x--;
  103.         return x<=0;
  104.     }
  105.     private static boolean isLegal(String string) {
  106.         try{Scanner r=new Scanner(string);
  107.         int x=r.nextInt(),y=r.nextInt();
  108.         r.close();
  109.         return 0<y&&0<x&&y<7;}catch(Exception e){return false;}
  110.     }
  111.     public static String[] playerNames(){
  112.         String[] s=new String[p.size()];
  113.         for(int i=0;i<s.length;i++){
  114.             s[i]=p.get(i).toString();
  115.         }
  116.         return s;
  117.     }
  118.     public static int valueOf(String i) {
  119.         String[] j=i.split(" ");
  120.         int y=Integer.parseInt(j[0]);
  121.         int z=Integer.parseInt(j[1]);
  122.         return y*6+z;
  123.     }
  124.     public static boolean isGreaterThan(String string1,String string2) {
  125.         return valueOf(string1)>valueOf(string2);
  126.     }
  127.     public static int numPlayers(){
  128.         return p.size();
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement