/************************************************************************\ |* gtf is a framework for analyzing two-player zero-sum games *| |* Copyright (C) 2005 Troels Bjerre Sorensen *| |* *| |* This program is free software; you can redistribute it and/or modify *| |* it under the terms of the GNU General Public License as published by *| |* the Free Software Foundation; either version 2 of the License, or *| |* (at your option) any later version. *| |* *| |* This program is distributed in the hope that it will be useful, but *| |* WITHOUT ANY WARRANTY; without even the implied warranty of *| |* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *| |* General Public License for more details. *| |* *| |* You should have received a copy of the GNU General Public License *| |* along with this program; if not, write to the *| |* Free Software Foundation, Inc., 59 Temple Place - Suite 330, *| |* Boston, MA 02111-1307, USA. *| \************************************************************************/ package gtf.examples; import gtf.game.*; public class SingleCardPoker extends ExtensiveForm { int decksize, maxbet, ante; public String getName() { return "SCP."+decksize+"."+maxbet+"."+ante; } public SingleCardPoker(String decksize, String maxbet, String ante) { try { this.decksize = Integer.parseInt(decksize); this.maxbet = Integer.parseInt(maxbet); this.ante = Integer.parseInt(ante); } catch(NumberFormatException e) { System.err.println("usage: SingleCardPoker "); System.exit(1); } } public GameTree generateTree() { return new SingleCardDeal(); } private class SingleCardDeal extends RandomNode { public SingleCardDeal() { for(int p1card = 1; p1card <= decksize; p1card++) { for(int p2card = 1; p2card <= decksize; p2card++) { if(p1card != p2card) { addChild("card=" + p1card, "card=" + p2card, 1, new SingleCardAction(p1card, p2card, 1, 0)); } } } } } private class SingleCardAction extends PlayerNode { public SingleCardAction(int p1card, int p2card, int player, int pot) { super(player); switch(player) { case 1: if(pot == 0) { addChild("check", "p1 checks", new SingleCardAction(p1card, p2card, 2, pot)); addChild("bet", "p1 bets", new SingleCardAction(p1card, p2card, 2, pot + 1)); } else { addChild("fold", "p1 folds", new GameLeaf(1 - (pot + ante))); addChild("call", "p1 calls", new GameLeaf(p1card > p2card ? pot + ante : -(pot + ante))); if (pot < maxbet) { addChild("raise", "p1 raises", new SingleCardAction(p1card, p2card, 2, pot + 1)); } } break; case 2: if(pot == 0) { addChild("p2 checks", "check", new GameLeaf(p1card > p2card ? pot + ante : -(pot + ante))); addChild("p2 bets", "bet", new SingleCardAction(p1card, p2card, 1, pot + 1)); } else { addChild("p2 folds", "fold", new GameLeaf(pot + ante - 1)); addChild("p2 calls", "call", new GameLeaf(p1card > p2card ? pot + ante : -(pot + ante))); if (pot < maxbet) { addChild("p2 raises", "raise", new SingleCardAction(p1card, p2card, 1, pot + 1)); } } break; default: System.err.println("SingleCardPoker: invalid playernumber"); System.exit(1); break; } } } }