diff options
Diffstat (limited to 'src/com/itmill/toolkit/demo/gogame/Board.java')
-rw-r--r-- | src/com/itmill/toolkit/demo/gogame/Board.java | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/com/itmill/toolkit/demo/gogame/Board.java b/src/com/itmill/toolkit/demo/gogame/Board.java new file mode 100644 index 0000000000..db37d1262b --- /dev/null +++ b/src/com/itmill/toolkit/demo/gogame/Board.java @@ -0,0 +1,107 @@ +package com.itmill.toolkit.demo.gogame; + +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.StringTokenizer; + +import com.itmill.toolkit.terminal.PaintException; +import com.itmill.toolkit.terminal.PaintTarget; +import com.itmill.toolkit.ui.AbstractComponent; + +public class Board extends AbstractComponent implements Game.Listener { + + // The game. + private Game game; + + // Players color. + private boolean playerPlaysBlack; + + // Last played coordinates. + private int lastX = -1; + private int lastY = -1; + + /** Creates board for the specified game, assuming the player plays + * the specified color. + */ + public Board(Game game, boolean playerPlaysBlack) { + this.game = game; + this.playerPlaysBlack = playerPlaysBlack; + game.addGameListener(this); + } + + /** Called on variable change. If the 'move' variable is found, a stone + * is placed for the player accordingly. + */ + public void changeVariables(Object source, Map variables) { + if (variables.containsKey("move")) { + StringTokenizer st = + new StringTokenizer((String) variables.get("move"), ","); + try { + int i = Integer.valueOf(st.nextToken()).intValue(); + int j = Integer.valueOf(st.nextToken()).intValue(); + game.addStone(i, j, playerPlaysBlack); + } catch (NumberFormatException ignore) { + } catch (NoSuchElementException ignore) { + } + } + } + + /** Tag for XML output. + */ + public String getTag() { + return "goboard"; + } + + + /** Paint the board to XML. + */ + public void paintContent(PaintTarget target) throws PaintException { + target.addAttribute("xmlns", "GO Sample Namespace"); + target.addAttribute("whitename", game.getWhitePlayer()); + target.addAttribute("blackname", game.getBlackPlayer()); + target.addAttribute("moves", game.getMoves()); + target.addAttribute("whitescaptured", game.getCapturedWhiteStones()); + target.addAttribute("blackscaptured", game.getCapturedBlackStones()); + int[][] state = game.getState(); + for (int j = 0; j < state.length; j++) { + target.startTag("row"); + for (int i = 0; i < state.length; i++) { + target.startTag("col"); + if (state[i][j] == Game.EMPTY) + target.addAttribute("move", "" + i + "," + j); + else { + if (i == lastX && j == lastY) + target.addAttribute( + "stone", + state[i][j] == Game.BLACK + ? "black-last" + : "white-last"); + else + target.addAttribute( + "stone", + state[i][j] == Game.BLACK ? "black" : "white"); + } + target.endTag("col"); + } + target.endTag("row"); + } + + target.addVariable(this, "move", ""); + } + + /** Destructor. + */ + protected void finalize() throws Throwable { + super.finalize(); + if (game != null) + game.removeGameListener(this); + } + + /** Implementing the Game.Listener interface, called when a stone is added. + */ + public void stoneIsAdded(Game game, int i, int j, boolean isBlack) { + lastX = i; + lastY = j; + requestRepaint(); + } +} |