diff options
author | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-05-30 01:31:36 +0200 |
---|---|---|
committer | Jannis Harder <harder@informatik.uni-luebeck.de> | 2011-05-30 01:31:36 +0200 |
commit | b19a46b7b259f3a6e189ba594446be3ad54801f5 (patch) | |
tree | 405d28e31fd04009d49749ed0d8d87365b0bb6b4 /src | |
parent | 15558d7138efbf16a52d49484a6923b03514f453 (diff) | |
download | JRummikub-b19a46b7b259f3a6e189ba594446be3ad54801f5.tar JRummikub-b19a46b7b259f3a6e189ba594446be3ad54801f5.zip |
Implemented view code for AI turns
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@319 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src')
-rw-r--r-- | src/jrummikub/control/RoundControl.java | 28 | ||||
-rw-r--r-- | src/jrummikub/control/turn/BaseAIControl.java | 3 | ||||
-rw-r--r-- | src/jrummikub/resource/dark_wood.png | bin | 0 -> 19252 bytes | |||
-rw-r--r-- | src/jrummikub/view/IView.java | 2 | ||||
-rw-r--r-- | src/jrummikub/view/impl/HandPanel.java | 29 | ||||
-rw-r--r-- | src/jrummikub/view/impl/PlayerPanel.java | 20 | ||||
-rw-r--r-- | src/jrummikub/view/impl/View.java | 9 |
7 files changed, 76 insertions, 15 deletions
diff --git a/src/jrummikub/control/RoundControl.java b/src/jrummikub/control/RoundControl.java index 5f46fd5..e1d2e97 100644 --- a/src/jrummikub/control/RoundControl.java +++ b/src/jrummikub/control/RoundControl.java @@ -7,6 +7,7 @@ import java.util.Set; import jrummikub.control.turn.ITurnControl; import jrummikub.control.turn.TurnControlFactory; +import static jrummikub.control.turn.TurnControlFactory.Type.*; import jrummikub.model.Hand; import jrummikub.model.IHand; import jrummikub.model.IPlayer; @@ -35,6 +36,7 @@ public class RoundControl { private Event restartRoundEvent = new Event(); private Event1<Score> endOfRoundEvent = new Event1<Score>(); private List<Connection> connections = new ArrayList<Connection>(); + private ITurnControl turnControl; private boolean roundFinished; /** @@ -76,26 +78,39 @@ public class RoundControl { } private void prepareTurn() { + boolean isHuman = roundState.getActivePlayer().getPlayerSettings().getTurnControlType() == HUMAN; clonedTable = (ITable) roundState.getTable().clone(); - view.enableStartTurnPanel(true); + + if (isHuman) { + view.enableStartTurnPanel(true); + } else { + view.enableThinkPanel(true); + } + view.getTablePanel().setStoneSets(clonedTable); view.setCurrentPlayerName(roundState.getActivePlayer() .getPlayerSettings().getName()); view.setCurrentPlayerColor(roundState.getActivePlayer() .getPlayerSettings().getColor()); view.setHasLaidOut(roundState.getActivePlayer().getLaidOut()); - + + if (!isHuman) + startTurn(); } private void startTurn() { + if (turnControl != null) + return; + boolean isHuman = roundState.getActivePlayer().getPlayerSettings().getTurnControlType() == HUMAN; boolean inspectOnly = roundState.getTurnNumber() < 1; boolean mayRedeal = inspectOnly && roundState.getActivePlayer().getHand() .getIdenticalStoneCount() >= 3; - view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal); - - ITurnControl turnControl = TurnControlFactory.getFactory(roundState.getActivePlayer() + if (isHuman) { + view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal); + } + turnControl = TurnControlFactory.getFactory(roundState.getActivePlayer() .getPlayerSettings().getTurnControlType()).create(); turnControl.setup(roundState.getActivePlayer(), clonedTable, view, inspectOnly, mayRedeal); @@ -142,6 +157,8 @@ public class RoundControl { } private void endOfTurn() { + view.enableThinkPanel(false); + turnControl = null; if (roundState.getTurnNumber() >= 1) { checkTurn(); } @@ -335,6 +352,7 @@ public class RoundControl { } private void redeal() { + turnControl = null; for (Connection c : new ArrayList<Connection>(connections)) { c.remove(); } diff --git a/src/jrummikub/control/turn/BaseAIControl.java b/src/jrummikub/control/turn/BaseAIControl.java index 2ed4c6e..d143dc1 100644 --- a/src/jrummikub/control/turn/BaseAIControl.java +++ b/src/jrummikub/control/turn/BaseAIControl.java @@ -51,7 +51,8 @@ public class BaseAIControl extends AbstractTurnControl { private void emitEndOfTurn() { long timeElapsed = System.currentTimeMillis() - startTime; - long waitTime = 2000 - timeElapsed; + long timeNeeded = Math.min((long)(1000 + Math.random() * hand.getSize() * 100), 50000); + long waitTime = timeNeeded - timeElapsed; if (waitTime > 0) { try { diff --git a/src/jrummikub/resource/dark_wood.png b/src/jrummikub/resource/dark_wood.png Binary files differnew file mode 100644 index 0000000..d3dc7fb --- /dev/null +++ b/src/jrummikub/resource/dark_wood.png diff --git a/src/jrummikub/view/IView.java b/src/jrummikub/view/IView.java index c2afcf8..ade6769 100644 --- a/src/jrummikub/view/IView.java +++ b/src/jrummikub/view/IView.java @@ -142,4 +142,6 @@ public interface IView { * specifies if the interface is to be enabled or disabled */ void showInterface(boolean enable); + + public void enableThinkPanel(boolean b); } diff --git a/src/jrummikub/view/impl/HandPanel.java b/src/jrummikub/view/impl/HandPanel.java index bede319..1851ed9 100644 --- a/src/jrummikub/view/impl/HandPanel.java +++ b/src/jrummikub/view/impl/HandPanel.java @@ -29,16 +29,23 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { private final static int HEIGHT = 2; private final static BufferedImage BACKGROUND; + private final static BufferedImage DARK_BACKGROUND; static { ImageIcon image = new ImageIcon( HandPanel.class.getResource("/jrummikub/resource/wood.png")); + ImageIcon darkImage = new ImageIcon( + HandPanel.class.getResource("/jrummikub/resource/dark_wood.png")); BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(), BufferedImage.TYPE_INT_RGB); + DARK_BACKGROUND = new BufferedImage(darkImage.getIconWidth(), darkImage.getIconHeight(), + BufferedImage.TYPE_INT_RGB); image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0); + darkImage.paintIcon(null, DARK_BACKGROUND.createGraphics(), 0, 0); } private BufferedImage scaledBackground = BACKGROUND; + private BufferedImage scaledDarkBackground = BACKGROUND; private PlayerPanel playerPanel; @@ -47,7 +54,6 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { private boolean repaintAll = true; private Collection<Stone> selectedStones = Collections.emptyList(); - /** * Creates a new Board instance */ @@ -65,14 +71,14 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { }); } - private BufferedImage getScaledBackground(int size) { + private BufferedImage getScaledBackground(int size, BufferedImage background) { BufferedImage scaled = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); Graphics2D g = scaled.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); - g.drawImage(BACKGROUND, 0, 0, size, size, null); + g.drawImage(background, 0, 0, size, size, null); return scaled; } @@ -86,13 +92,22 @@ class HandPanel extends AbstractStonePanel implements IHandPanel { int size = height / HEIGHT; + + BufferedImage background = isEnabled() ? scaledBackground : scaledDarkBackground ; + if (repaintAll) { - if (scaledBackground.getHeight() != size) - scaledBackground = getScaledBackground(size); - + if (background.getHeight() != size) { + if (!isEnabled()) { + scaledDarkBackground = getScaledBackground(size, DARK_BACKGROUND); + } else { + scaledBackground = getScaledBackground(size, BACKGROUND); + } + background = isEnabled() ? scaledBackground : scaledDarkBackground; + } + for (int i = 0; i < HEIGHT; ++i) { for (int xpos = -size * i / 3; xpos < width; xpos += size) { - g.drawImage(scaledBackground, xpos, size * i, null); + g.drawImage(background, xpos, size * i, null); } } } diff --git a/src/jrummikub/view/impl/PlayerPanel.java b/src/jrummikub/view/impl/PlayerPanel.java index 5eba07a..ba0deb4 100644 --- a/src/jrummikub/view/impl/PlayerPanel.java +++ b/src/jrummikub/view/impl/PlayerPanel.java @@ -8,6 +8,7 @@ import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.ComponentListener; import java.text.DecimalFormat; +import java.util.Collections; import javax.swing.JButton; import javax.swing.JLabel; @@ -16,8 +17,11 @@ import javax.swing.JProgressBar; import javax.swing.UIManager; import javax.swing.border.EmptyBorder; +import jrummikub.model.Position; +import jrummikub.model.Stone; import jrummikub.util.Event; import jrummikub.util.IEvent; +import jrummikub.util.Pair; import jrummikub.view.IPlayerPanel; /** @@ -382,4 +386,20 @@ class PlayerPanel extends JPanel implements IPlayerPanel { keepStonesButton.setVisible(false); } } + + void enableButtons(boolean enable) { + sortByGroupsButton.setEnabled(enable); + sortByRunsButton.setEnabled(enable); + if (!enable) { + setEndTurnMode(false, false); + endTurnButton.setText("<html><center>Computer denkt nach"); + hand.setStones(Collections.<Pair<Stone,Position>>emptyList()); + handRowDownButton.setEnabled(false); + handRowDownButton.setEnabled(false); + } + endTurnButton.setEnabled(enable); + redealButton.setEnabled(enable); + keepStonesButton.setEnabled(enable); + hand.setEnabled(enable); + } } diff --git a/src/jrummikub/view/impl/View.java b/src/jrummikub/view/impl/View.java index e374f8c..fceaeeb 100644 --- a/src/jrummikub/view/impl/View.java +++ b/src/jrummikub/view/impl/View.java @@ -97,8 +97,8 @@ public class View extends JFrame implements IView { mainLayer.add(table); playerPanel = new PlayerPanel(); - playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0, - Color.BLACK)); + playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, + 0, Color.BLACK)); mainLayer.add(playerPanel); startTurnPanel = new StartTurnPanel(); @@ -250,4 +250,9 @@ public class View extends JFrame implements IView { playerPanel.showButtons(false); } } + + @Override + public void enableThinkPanel(boolean enable) { + playerPanel.enableButtons(!enable); + } } |