Greed Game Sim

<< Prisoner's Delemma | OtherProjectsTrailIndex | GreedGameContest >>

The Greed Game is something I was told was invented by Rex Bogs. It is a fun party game that is based on "pressing your luck" on five rounds of throwing a die. The points accumulate until the number 2 is cast. Before the die is cast, players have an option of banking the total. If they think the next roll is not a 2, they keep standing. But if it is a 2, they get zero points for that round. To get a idea, you can try the game here

Since the chances of rolling a 2 is 1/6, then why not modify the game that so two die are cast, and the "loosing" roll would be doubles. Rolling doubles occurs 6/36 times, the same probability as rolling a single die and casting a 2. Of course, for a game, the fun is based on the variety of outcomes, not the average outcome.

It takes a while to play a game, so why not have the computer simulate the game many many times? We can then compare the original game to the modified game and see if the variation is about the same, or if one games seems different.

Die.java

public class Die {
    private int value, sides;
	public Die(int sides){
		this.sides=sides;
		this.value=1+(int)(sides*Math.random());
	}
	public int getValue(){
		return value;
	}

}

Player.java


public class Player {

	private int score;
	private boolean standing;
	public Player() {
		setScore(0);
		setStanding(true);
	}
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public boolean isStanding() {
		return standing;
	}
	public boolean setStanding(boolean standing) {
		boolean changed=standing!=isStanding();
		this.standing = standing;
		return changed;
	}
	public void decide(int points){
		if (standing && Math.random()<.33){
			standing=false;
			score+=points;
		}
	}

}

GameStats.java

import java.util.Arrays;


public class GameStats {

	private int[] scores ;
	private int[] zap;

	public GameStats(Player[] p, int[] zap) {
		scores=new int[p.length];
		for(int i=0; i<p.length;i++)
			scores[i]=p[i].getScore();
		this.zap=zap;
	}
	public double mean(int[] a){
		double sum=0;
		for (int i=0;i<a.length;i++){
			sum+=a[i];
		}
		return sum/a.length;
	}
	public double sd(int[] a){
		double mean=mean(a);
		double sum=0;
		int n=a.length;
		for(int i=0;i<n;i++){
			sum+=Math.pow(a[i]-mean, 2);
		}
		return Math.sqrt(sum/(n-1.0));
	}
	public double median(int [] a){
		Arrays.sort(a);
		int n=a.length;
		if (n%2==0){//even
			return (a[n/2]+a[1+n/2])/2.0;
		}
		return (double)a[n/2];
	}
	public int max(int[] a){
		Arrays.sort(a);
		return a[a.length-1];
	}
	public double meanScore(){
		return mean(scores);
	}
	public double meanZapped(){
		return mean(zap);
	}
	public double sdScore(){
		return sd(scores);
	}
	public double sdZapped(){
		return sd(zap);
	}
	public double medianScore(){
		return median(scores);
	}
	public double medianZapped(){
		return median(zap);
	}
	public String toString(){
		return "Winners score:"+max(scores)+" scores mean:"+mean(scores)+" med:"+median(scores)+" sd:"+sd(scores)+
				"\nMax zapped:"+max(zap)+" zapped mean:"+mean(zap)+" med:"+median(zap)+" sd:"+sd(zap)+"\n";
	}
}

GreedGameSim.java

import static java.lang.System.out;
public class GreedGameSim {

	private static Player[] players;

	public static void main(String[] args) {


		boolean verbose=false;
		int numberOfPlayers=100;
		out.println("\n Die on 2\n");
		for(int i=0;i<10;i++)
			out.println( doGreedGameDeathOn2(verbose, numberOfPlayers));
		out.println("\n Die on Doubles\n");
		for(int i=0;i<10;i++)
			out.println( doGreedGameDeathOnDoubles(false, numberOfPlayers));
	}
	public static boolean stillStanding(){
		for (int i=0; i<players.length; i++){
			if (players[i].isStanding())
				return true;
		}
		return false;
	}
	public static void resetPlayers(){
		for (Player p:players){
			p.setStanding(true);
		}

	}
	public static GameStats doGreedGameDeathOn2(boolean verbose, int n){
		players=new Player[n];
		int[] zapped = new int[5];
		for (int i=0; i<5;i++)
			zapped[i]=0;
		for (int i=0;i<players.length;i++)
			players[i]=new Player();
		Die d1=new Die(6);
		Die d2=new Die(6);
		for(int round=0;round<5;round++){
    		int roundTotal=d1.getValue()+d2.getValue();
        if (verbose){
        		out.println("\nDie: "+d1.getValue()+" "+d2.getValue()+"- starting with "+roundTotal+" for round "+(round+1));
        }
        	int zappedCount=0;
        	boolean firstRoll=true;
        	while (stillStanding()){
        		if(!firstRoll){
        			d1= new Die(6);
	        		if(verbose){
	        			out.println("\nrolled a "+d1.getValue());
	        		}
        		}
        		if (!firstRoll && d1.getValue()==2){
        			for (Player p:players){
        				boolean changed=p.setStanding(false);
        				if (changed)
        					zappedCount++;
        			}
        		}else{//not a 2 or first roll of round
        			int s=0;
        			if(!firstRoll)
        				roundTotal+=d1.getValue();
        			for (Player p:players){
        				p.decide(roundTotal);
        				if (p.isStanding())
							s++;

        			}
        			if (verbose) {

    					out.print(roundTotal+" pts so far - "+s+" still standing ");
    				}
        		}

        		firstRoll=false;

        	}//end of while standing
        	roundTotal=0;
        	if(verbose){
        		out.println(zappedCount+" got zero points round "+(round+1));
        	}
        	zapped[round]=zappedCount;
        	resetPlayers();
    }//end of round loop
		return new GameStats(players, zapped);
	}

	public static GameStats doGreedGameDeathOnDoubles(boolean verbose, int n){
		players=new Player[n];
		int[] zapped = new int[5];
		for (int i=0; i<5;i++)
			zapped[i]=0;
		for (int i=0;i<players.length;i++)
			players[i]=new Player();
		Die d1=new Die(6);
		Die d2=new Die(6);
		for(int round=0;round<5;round++){
    		int roundTotal=d1.getValue()+d2.getValue();
        if (verbose){
        		out.println("\nDie: "+d1.getValue()+" "+d2.getValue()+"- starting with "+roundTotal+" for round "+(round+1));
        }
        	int zappedCount=0;
        	boolean firstRoll=true;
        	while (stillStanding()){
        		if(!firstRoll){
        			d1= new Die(6);
        			d2= new Die(6);
	        		if(verbose){
	        			out.println("\nrolled a "+d1.getValue()+" "+d2.getValue());
	        		}
        		}
        		if (!firstRoll && d1.getValue()==d2.getValue()){
        			for (Player p:players){
        				boolean changed=p.setStanding(false);
        				if (changed)
        					zappedCount++;
        			}
        		}else{//not doubles or first roll of round
        			int s=0;
        			if(!firstRoll)
        				roundTotal+=d1.getValue()+d2.getValue();
        			for (Player p:players){
        				p.decide(roundTotal);
        				if (p.isStanding())
							s++;

        			}
        			if (verbose) {

    					out.print(roundTotal+" pts so far - "+s+" still standing ");
    				}
        		}

        		firstRoll=false;

        	}//end of while standing
        	roundTotal=0;
        	if(verbose){
        		out.println(zappedCount+" got zero points round "+(round+1));
        	}
        	zapped[round]=zappedCount;
        	resetPlayers();
    }//end of round loop
		return new GameStats(players, zapped);
	}


}

Things to Try

  1. Make a graphic display of the game results
  2. Is there a winning strategy for this game? Make different types of players that have different strategies.