User:Dsamarin

Don't gamble

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);
	}

}

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.