Jump to content

User:Dsamarin

From Wikipedia, the free encyclopedia

Don't gamble

[edit]
import java.util.Random;

public class Gambler {
	
	static int bet; /* Amount of current bet on the table */
	static int money; /* Amount of money in pocket */
	static int initial; /* Amount of money I started out with */
	static Random generator;
	static boolean isGambling; /* Whether or not I am gambling */
	
	public Gambler(int m) {
		
		initial = money = m;
		generator = new Random();
		isGambling = true;
		
		say("I have $"+money+" and I'm ready to gamble.");
		
		Random();
	}
	
	public void Smart() {
	/* Know when to quit */
		double percentbet = 1.0/8.0; // Percentage of spendable money to bet

		int store = money/2; // Amount to not gamble with
		while(isGambling) {
			bet = (int) (((double) (money-store))*percentbet); if( bet == 0 ) bet++;
			say("\tSaving $"+store+"");
			if( money <= store ) {
				quit();
				break;
			}
			gamble();
			
			store = Math.max(store, money/2); // Store can only go up
			if( money >= initial+(money-store)) store = Math.max(store, initial);
				// Hurrah
		}
	}
	
	public void ReallySmart() {
		quit();
	}
	
	public void Half() {
		while(isGambling) {
			bet = money/2; if(bet == 0) { quit(); break; }
			gamble();
		}
	}
	
	public void HalfWithStore() {
		double collection = 9.0/10.0;
		int store = (int) (((double) money)*collection);
		while(isGambling) {
			bet = (int) (((double) (money-store))*(1.0-collection));
			if( money <= store ) { quit(); break; }
			gamble();
			if(money >= initial*2)
				store = (int) (((double) money)*collection);
		}
	}
	
	public void Random() {
		while(isGambling) {
			bet = generator.nextInt(money);
			gamble();
			if( money <= 1 ) { quit(); }
		}
	}
	
	public void Martingale() {
	/* Double bet after loss; Half after win */
		bet = 1;
		boolean win;
		while(isGambling) {
			win = gamble();
			if( win ) {
				bet = bet/2;
				if( bet == 0 ) bet = 1;
			}
			else bet = bet*2;
		}
	}
	
	public void MartingaleInverse() {
	/* Double bet after win; Half after loss */
		bet = 1;
		boolean win;
		while(isGambling) {
			win = !gamble();
			if( win ) {
				bet = bet/2;
				if( bet == 0 ) bet = 1;
			}
			else bet = bet*2;
		}
	}
	
	public boolean gamble() {
		boolean win = generator.nextBoolean();
		if( win ) {
			money = money+bet;
			say("I won $"+bet+"! ($"+money+")");
		} else {
			money = money-bet;
			say("I lost $"+bet+"! ($"+money+")");
			if( money < 0 ) { goBankrupt(); }
		}
		return win;
	}

	public void quit() {
		int diff = money-initial;
		say("I am done gambling! I leave with $"+money+"!");
		if( diff > 0 ) {
			say("In the end, I gained $"+diff+"!");
		} else if ( diff < 0 ) {
			say("In the end, I lost $"+Math.abs(diff)+"!");
		} else {
			say("In the end, I left with what I came with.");
		}
		isGambling = false;
	}
	
	public void goBankrupt() {
		say("I am bankrupt!");
		quit();
	}

	public void say(String msg) {
		System.out.println(msg);
	}

}