From df3ac4eac37396bc617eec41c46a6f17b2ea9919 Mon Sep 17 00:00:00 2001 From: loony-bean Date: Mon, 12 Sep 2011 13:47:03 +0300 Subject: [PATCH] Player names depend on skin --- .../example/kindle/boardgame/GameEvent.java | 1 - src/com/example/kindle/boardgame/IPlayer.java | 7 ---- .../winningfour/boardgame/ComputerPlayer.java | 4 +-- .../winningfour/boardgame/GameController.java | 4 +-- .../winningfour/boardgame/GameState.java | 10 +++--- .../boardgame/GameStateMachine.java | 9 +++--- .../winningfour/boardgame/GameView.java | 32 +++++++++++++++++-- .../winningfour/boardgame/HumanPlayer.java | 4 +-- .../kindle/winningfour/boardgame/Player.java | 11 ++----- .../winningfour/options/OptionsFactory.java | 8 ++--- 10 files changed, 51 insertions(+), 39 deletions(-) diff --git a/src/com/example/kindle/boardgame/GameEvent.java b/src/com/example/kindle/boardgame/GameEvent.java index d566ecc..1c7558d 100644 --- a/src/com/example/kindle/boardgame/GameEvent.java +++ b/src/com/example/kindle/boardgame/GameEvent.java @@ -4,6 +4,5 @@ public class GameEvent { public static final int CONTINUE = 0; public static final int WIN = 1; - public static final int LOOSE = 2; public static final int DRAW = 3; } diff --git a/src/com/example/kindle/boardgame/IPlayer.java b/src/com/example/kindle/boardgame/IPlayer.java index 76770c4..c4969da 100644 --- a/src/com/example/kindle/boardgame/IPlayer.java +++ b/src/com/example/kindle/boardgame/IPlayer.java @@ -18,13 +18,6 @@ public interface IPlayer */ public Color getColor(); - /** - * Returns name of the player. - * - * @return Player name. - */ - public String getName(); - /** * This method should be called when it is time for player * to think on game and make it's move. Player is provided diff --git a/src/com/example/kindle/winningfour/boardgame/ComputerPlayer.java b/src/com/example/kindle/winningfour/boardgame/ComputerPlayer.java index b885fc6..a5574c1 100644 --- a/src/com/example/kindle/winningfour/boardgame/ComputerPlayer.java +++ b/src/com/example/kindle/winningfour/boardgame/ComputerPlayer.java @@ -17,9 +17,9 @@ public class ComputerPlayer extends Player public final static int MAXVAL = 100; public final static int DEPTH = 6; - public ComputerPlayer(final Color color, final String name) + public ComputerPlayer(final Color color) { - super(color, name); + super(color); App.log("ComputerPlayer::create"); diff --git a/src/com/example/kindle/winningfour/boardgame/GameController.java b/src/com/example/kindle/winningfour/boardgame/GameController.java index 3d85931..99eae63 100644 --- a/src/com/example/kindle/winningfour/boardgame/GameController.java +++ b/src/com/example/kindle/winningfour/boardgame/GameController.java @@ -265,9 +265,9 @@ public GameView getView() return this.gameView; } - public void setStatusText(final String status) + public void setStatus(int currentPlayerId, int status) { - this.gameView.setStatusText(status); + this.gameView.setStatus(currentPlayerId, status); } public void repaint() diff --git a/src/com/example/kindle/winningfour/boardgame/GameState.java b/src/com/example/kindle/winningfour/boardgame/GameState.java index c27f2b4..e835784 100644 --- a/src/com/example/kindle/winningfour/boardgame/GameState.java +++ b/src/com/example/kindle/winningfour/boardgame/GameState.java @@ -15,7 +15,7 @@ public class GameState extends State public GameState(final GameController game, int player, final String name) { super(name); - this.player = player; + this.playerId = player; this.game = game; this.endGameKeyAdapter = new KeyAdapter() @@ -39,7 +39,7 @@ public void enter() this.game.getView().addKeyListener(this.keyAdapter); } - this.game.setStatusText(this.status); + this.game.setStatus(this.playerId, this.status); this.game.repaint(); } @@ -53,7 +53,7 @@ public void leave() public IPlayer getPlayer() { - return this.game.getContext().getPlayers()[this.player]; + return this.game.getContext().getPlayers()[this.playerId]; } public void pulse(final SignalEvent signal) @@ -74,10 +74,10 @@ public void destroy() App.log("GameState::destroy done"); } - protected String status; + protected int status; protected GameState state; protected KeyAdapter keyAdapter; protected KeyAdapter endGameKeyAdapter; - protected int player; + protected int playerId; private GameController game; } diff --git a/src/com/example/kindle/winningfour/boardgame/GameStateMachine.java b/src/com/example/kindle/winningfour/boardgame/GameStateMachine.java index 3434677..c82544c 100644 --- a/src/com/example/kindle/winningfour/boardgame/GameStateMachine.java +++ b/src/com/example/kindle/winningfour/boardgame/GameStateMachine.java @@ -1,10 +1,10 @@ package com.example.kindle.winningfour.boardgame; +import com.example.kindle.boardgame.GameEvent; import com.example.kindle.sm.SignalEvent; import com.example.kindle.sm.State; import com.example.kindle.sm.StateMachine; import com.example.kindle.winningfour.App; -import com.example.kindle.winningfour.AppResources; public class GameStateMachine extends StateMachine { @@ -85,7 +85,7 @@ public TurnState(final GameController game, int player, final String name) public void enter() { - this.status = "" + this.getPlayer().getName() + App.bundle.getString(AppResources.KEY_GAME_TURN); + this.status = GameEvent.CONTINUE; this.keyAdapter = this.getPlayer().getKeyAdapter(); super.enter(); @@ -130,7 +130,7 @@ public WinState(final GameController game, int player, final String name) public void enter() { - this.status = "" + this.getPlayer().getName() + App.bundle.getString(AppResources.KEY_GAME_WIN); + this.status = GameEvent.WIN; super.enter(); App.gamer.stop(); } @@ -157,7 +157,8 @@ private class DrawState extends GameState public DrawState(final GameController game) { super(game, 0, "DrawState"); - this.status = App.bundle.getString(AppResources.KEY_GAME_DRAW); + //this.status = App.bundle.getString(AppResources.KEY_GAME_DRAW); + this.status = GameEvent.DRAW; this.keyAdapter = this.endGameKeyAdapter; } diff --git a/src/com/example/kindle/winningfour/boardgame/GameView.java b/src/com/example/kindle/winningfour/boardgame/GameView.java index 7f1f7a6..c43ba1b 100644 --- a/src/com/example/kindle/winningfour/boardgame/GameView.java +++ b/src/com/example/kindle/winningfour/boardgame/GameView.java @@ -9,11 +9,13 @@ import org.kwt.ui.KWTProgressBar; import com.amazon.kindle.kindlet.ui.KLabel; +import com.example.kindle.boardgame.GameEvent; import com.example.kindle.utils.GameClock; import com.example.kindle.winningfour.App; import com.example.kindle.winningfour.AppResources; import com.example.kindle.winningfour.boardgame.BoardItem; import com.example.kindle.winningfour.gui.GameImage; +import com.example.kindle.winningfour.options.OptionsFactory; import com.example.kindle.winningfour.skins.BoardLayout; public class GameView extends Container @@ -90,6 +92,8 @@ public void paint(Graphics g) this.selectedRow*(this.layout.pieceSizeX + this.layout.pieceGapX), this.layout.selectorY); + this.statusLabel.setText(this.getStatusString()); + super.paint(g); AppResources.getSkin().paintBoard(g, this); this.paintItems(this.items, g); @@ -125,9 +129,30 @@ public void setSelectedRow(int row) this.gameSelector.repaint(); } - public void setStatusText(final String text) + public void setStatus(final int currentPlayerId, int status) { - this.statusLabel.setText(text); + this.status = status; + this.currentPlayerId = currentPlayerId; + this.statusLabel.repaint(); + } + + private String getStatusString() + { + String result = ""; + String actor = (new OptionsFactory()).createSkin().getPlayerNames()[this.currentPlayerId]; + if (this.status == GameEvent.WIN) + { + result = actor + App.bundle.getString(AppResources.KEY_GAME_WIN); + } + else if (this.status == GameEvent.DRAW) + { + result = App.bundle.getString(AppResources.KEY_GAME_DRAW); + } + else if (this.status == GameEvent.CONTINUE) + { + result = actor + App.bundle.getString(AppResources.KEY_GAME_TURN); + } + return result; } public void setProgressTicks(int percents) @@ -167,6 +192,9 @@ public void destroy() private ArrayList items; private int selectedRow; private KWTProgressBar progressBar; + + private int status; + private int currentPlayerId; private BoardLayout layout; } diff --git a/src/com/example/kindle/winningfour/boardgame/HumanPlayer.java b/src/com/example/kindle/winningfour/boardgame/HumanPlayer.java index 364054a..4a30dc4 100644 --- a/src/com/example/kindle/winningfour/boardgame/HumanPlayer.java +++ b/src/com/example/kindle/winningfour/boardgame/HumanPlayer.java @@ -11,9 +11,9 @@ public class HumanPlayer extends Player { - public HumanPlayer(final Color color, final String name) + public HumanPlayer(final Color color) { - super(color, name); + super(color); } protected void onKeyboard(final KeyEvent event) diff --git a/src/com/example/kindle/winningfour/boardgame/Player.java b/src/com/example/kindle/winningfour/boardgame/Player.java index fcce6d4..014a383 100644 --- a/src/com/example/kindle/winningfour/boardgame/Player.java +++ b/src/com/example/kindle/winningfour/boardgame/Player.java @@ -10,10 +10,9 @@ public class Player implements IPlayer { - public Player(final Color color, final String name) + public Player(final Color color) { this.color = color; - this.name = name; this.keyAdapter = new KeyAdapter() { @@ -29,11 +28,6 @@ public Color getColor() return this.color; } - public String getName() - { - return this.name; - } - public KeyAdapter getKeyAdapter() { return this.keyAdapter; @@ -61,7 +55,7 @@ public boolean equals(Object other) { IPlayer lhs = (IPlayer) this; IPlayer rhs = (IPlayer) other; - return rhs.getName().equals(lhs.getName()); + return rhs.getColor().equals(lhs.getColor()); } return false; @@ -78,5 +72,4 @@ public void destroy() private KeyAdapter keyAdapter; private final Color color; - private final String name; } diff --git a/src/com/example/kindle/winningfour/options/OptionsFactory.java b/src/com/example/kindle/winningfour/options/OptionsFactory.java index 1f3a0b5..3eb7605 100644 --- a/src/com/example/kindle/winningfour/options/OptionsFactory.java +++ b/src/com/example/kindle/winningfour/options/OptionsFactory.java @@ -61,18 +61,16 @@ public IPlayer[] createPlayers() String firstTurn = (String) App.opts.get(AppOptions.OP_T_FIRST_TURN); String opponent = (String) App.opts.get(AppOptions.OP_T_OPPONENT); - String[] playerNames = this.createSkin().getPlayerNames(); - - IPlayer you = new HumanPlayer(Color.white, playerNames[0]); + IPlayer you = new HumanPlayer(Color.white); IPlayer opp = null; if (opponent.equals(AppOptions.OP_V_COMPUTER)) { - opp = new ComputerPlayer(Color.black, playerNames[1]); + opp = new ComputerPlayer(Color.black); } else if (opponent.equals(AppOptions.OP_V_HUMAN)) { - opp = new HumanPlayer(Color.black, playerNames[1]); + opp = new HumanPlayer(Color.black); } if (firstTurn.equals(AppOptions.OP_V_YOU))