In this gambling example a bet of SFr. 5.- must be placed after each round. A none rigged coin is tossed until "tail" is shown for the first time. This ends the round. The win of the game is calculated according to the tossed coins. If the round ends after only one toss, the whole bet is lost. Two tosses win 1.-, three tosses 2.- and so on. The win is calculated with the following formula: 2 k − 1 SFr, the coin being tossed k-times. This game was originally played in a casino in St. Petersburg.
Playing several rounds, one does not fail to notice loosing a lot of money. The theoretically expected win tough is positive or even unlimited, meaning that the player thinks he can win a huge amount of money if only playing long enough.
Program code:
// Petersburg.java import ch.aplu.jgamegrid.*; import java.awt.*; import ch.aplu.util.*; public class Petersburg extends GameGrid { public int totalProfit; public int loss; public final int entry = 5; public int maxWin; private final GGBackground bg; String info = "This is a simulation of the St. Petersburg paradox. \n" + "Game rules: For every Game you pay SFr. 5.-.\n" + "A fair coin is tossed repeatedly until a number appears, ending the game.\n" + "Starting with SFr 1.- for every head you get twice paid out.\n" + "If only a number appears, you lose the use.\n" + "(http://en.wikipedia.org/wiki/St._Petersburg_paradox)"; public Petersburg() { super(15, 1, 82); setBgColor(Color.white); show(); bg = getBg(); bg.setPaintColor(Color.black); addStatusBar(30); ModelessOptionPane mop = new ModelessOptionPane(getPosition().x, getPosition().y + getAreaSize().height, info); mop.setTitle("Information"); reset(); } public void newGame() { setTitle("New game, you paid " + entry + " SFr entering price"); removeAllActors(); loss = loss - entry; int total = totalProfit + loss; String data = String.format("Top gain: %-20d You lost: %-20d " + " You won: %-20d Profit: %-20d", maxWin, loss, totalProfit, total); setStatusText(data); addActor(new Coin(0, 0, this), new Location(0, 0)); } public void reset() { bg.clear(); totalProfit = 0; maxWin = 0; loss = 0; setTitle("Intialized"); newGame(); } public static void main(String[] args) { new Petersburg(); } } // ------------- class Coin ------------------------- class Coin extends Actor { private int Xpos; private int currentWin; // currentWin = 2^Xpos; private Petersburg pb; private boolean lost; public Coin(int Xpos, int currentWin, Petersburg pb) { super(true, "sprites/coin.gif", 3); show(2); this.currentWin = currentWin; this.Xpos = Xpos; this.pb = pb; } public void act() { if (lost) { pb.getBg().clear(); // beseitigt Schrift auf Hintergrund pb.newGame(); } else { setDirection(Math.random() * 360); boolean head; if ((int)(Math.random() * 2) == 1) head = true; else head = false; if (head) { show(0); if (currentWin == 0) currentWin = 1; else currentWin = 2 * currentWin; gameGrid.addActor(new Coin(Xpos + 1, currentWin, pb), //next coin new Location(Xpos + 1, 0)); this.setActEnabled(false); } else { show(1); lost = true; // necessary to see the tail pb.totalProfit = pb.totalProfit + currentWin; gameGrid.setTitle("Round finished! You win: " + currentWin + ".- SFr "); if (currentWin > pb.maxWin) pb.maxWin = currentWin; } } } } |