/************************************************************************\ |* 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 HiddenDomino extends ExtensiveForm { int width, height; final static int HORISONTAL = 0, VERTICAL = 1; public String getName() { return "HD." + width + "." + height; } public HiddenDomino(String width, String height) { try { this.width = Integer.parseInt(width); this.height= Integer.parseInt(height); } catch(NumberFormatException e) { System.err.println("usage: HiddenDomino "); System.exit(1); } } public GameTree generateTree() { return new PlaceDomino(); } private class PlaceDomino extends PlayerNode { public PlaceDomino() { super(1); for (int x1 = 0 ; x1 < width ; x1++) { for (int y1 = 0 ; y1 < height ; y1++) { if(x1 + 1 < width) { addChild("(" + x1 + "+," + y1 + ")", "", new GuessDomino(x1, y1, HORISONTAL)); } if(y1 + 1 < height) { addChild("(" + x1 + "," + y1 + "+)", "", new GuessDomino(x1, y1, VERTICAL)); } } } } } private class GuessDomino extends PlayerNode { public GuessDomino(int x1, int y1, int dir) { super(2); for (int x2 = 0 ; x2 < width ; x2++) { for (int y2 = 0 ; y2 < height ; y2++) { addChild("", "(" + x2 + "," + y2 + ")", new GameLeaf(afterMath(x1, y1, dir, x2, y2))); } } } } private int afterMath(int x1, int y1, int dir, int x2, int y2) { switch (dir) { case HORISONTAL: if (y1 == y2 && (x1 == x2 || x1 + 1 == x2)) return -1; else return 1; case VERTICAL: if (x1 == x2 && (y1 == y2 || y1 + 1 == y2)) return -1; else return 1; } throw new RuntimeException("Ooops... Fell through."); } }