001: package org.millstone.examples.gogame;
002:
003: import java.util.Map;
004: import java.util.NoSuchElementException;
005: import java.util.StringTokenizer;
006: import org.millstone.base.terminal.PaintException;
007: import org.millstone.base.terminal.PaintTarget;
008: import org.millstone.base.ui.AbstractComponent;
009:
010: public class Board extends AbstractComponent implements Game.Listener {
011:
012: // The game.
013: private Game game;
014:
015: // Players color.
016: private boolean playerPlaysBlack;
017:
018: // Last played coordinates.
019: private int lastX = -1;
020: private int lastY = -1;
021:
022: /** Creates board for the specified game, assuming the player plays
023: * the specified color.
024: */
025: public Board(Game game, boolean playerPlaysBlack) {
026: this .game = game;
027: this .playerPlaysBlack = playerPlaysBlack;
028: game.addGameListener(this );
029: }
030:
031: /** Called on variable change. If the 'move' variable is found, a stone
032: * is placed for the player accordingly.
033: */
034: public void changeVariables(Object source, Map variables) {
035: if (variables.containsKey("move")) {
036: StringTokenizer st = new StringTokenizer((String) variables
037: .get("move"), ",");
038: try {
039: int i = Integer.valueOf(st.nextToken()).intValue();
040: int j = Integer.valueOf(st.nextToken()).intValue();
041: game.addStone(i, j, playerPlaysBlack);
042: } catch (NumberFormatException ignore) {
043: } catch (NoSuchElementException ignore) {
044: }
045: }
046: }
047:
048: /** Tag for XML output.
049: */
050: public String getTag() {
051: return "goboard";
052: }
053:
054: /** Paint the board to XML.
055: */
056: public void paintContent(PaintTarget target) throws PaintException {
057: target.addAttribute("xmlns", "GO Sample Namespace");
058: target.addAttribute("whitename", game.getWhitePlayer());
059: target.addAttribute("blackname", game.getBlackPlayer());
060: target.addAttribute("moves", game.getMoves());
061: target.addAttribute("whitescaptured", game
062: .getCapturedWhiteStones());
063: target.addAttribute("blackscaptured", game
064: .getCapturedBlackStones());
065: int[][] state = game.getState();
066: for (int j = 0; j < state.length; j++) {
067: target.startTag("row");
068: for (int i = 0; i < state.length; i++) {
069: target.startTag("col");
070: if (state[i][j] == Game.EMPTY)
071: target.addAttribute("move", "" + i + "," + j);
072: else {
073: if (i == lastX && j == lastY)
074: target
075: .addAttribute(
076: "stone",
077: state[i][j] == Game.BLACK ? "black-last"
078: : "white-last");
079: else
080: target.addAttribute("stone",
081: state[i][j] == Game.BLACK ? "black"
082: : "white");
083: }
084: target.endTag("col");
085: }
086: target.endTag("row");
087: }
088:
089: target.addVariable(this , "move", "");
090: }
091:
092: /** Destructor.
093: */
094: protected void finalize() throws Throwable {
095: super .finalize();
096: if (game != null)
097: game.removeGameListener(this );
098: }
099:
100: /** Implementing the Game.Listener interface, called when a stone is added.
101: */
102: public void stoneIsAdded(Game game, int i, int j, boolean isBlack) {
103: lastX = i;
104: lastY = j;
105: requestRepaint();
106: }
107: }
108:
109: /* This Millstone sample code is public domain. *
110: * For more information see www.millstone.org. */
|