Implemented view code for AI turns
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@319 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
15558d7138
commit
b19a46b7b2
8 changed files with 82 additions and 15 deletions
|
@ -137,4 +137,10 @@ public class MockView implements IView {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enableThinkPanel(boolean b) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import java.util.Set;
|
||||||
|
|
||||||
import jrummikub.control.turn.ITurnControl;
|
import jrummikub.control.turn.ITurnControl;
|
||||||
import jrummikub.control.turn.TurnControlFactory;
|
import jrummikub.control.turn.TurnControlFactory;
|
||||||
|
import static jrummikub.control.turn.TurnControlFactory.Type.*;
|
||||||
import jrummikub.model.Hand;
|
import jrummikub.model.Hand;
|
||||||
import jrummikub.model.IHand;
|
import jrummikub.model.IHand;
|
||||||
import jrummikub.model.IPlayer;
|
import jrummikub.model.IPlayer;
|
||||||
|
@ -35,6 +36,7 @@ public class RoundControl {
|
||||||
private Event restartRoundEvent = new Event();
|
private Event restartRoundEvent = new Event();
|
||||||
private Event1<Score> endOfRoundEvent = new Event1<Score>();
|
private Event1<Score> endOfRoundEvent = new Event1<Score>();
|
||||||
private List<Connection> connections = new ArrayList<Connection>();
|
private List<Connection> connections = new ArrayList<Connection>();
|
||||||
|
private ITurnControl turnControl;
|
||||||
private boolean roundFinished;
|
private boolean roundFinished;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -76,26 +78,39 @@ public class RoundControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareTurn() {
|
private void prepareTurn() {
|
||||||
|
boolean isHuman = roundState.getActivePlayer().getPlayerSettings().getTurnControlType() == HUMAN;
|
||||||
clonedTable = (ITable) roundState.getTable().clone();
|
clonedTable = (ITable) roundState.getTable().clone();
|
||||||
view.enableStartTurnPanel(true);
|
|
||||||
|
if (isHuman) {
|
||||||
|
view.enableStartTurnPanel(true);
|
||||||
|
} else {
|
||||||
|
view.enableThinkPanel(true);
|
||||||
|
}
|
||||||
|
|
||||||
view.getTablePanel().setStoneSets(clonedTable);
|
view.getTablePanel().setStoneSets(clonedTable);
|
||||||
view.setCurrentPlayerName(roundState.getActivePlayer()
|
view.setCurrentPlayerName(roundState.getActivePlayer()
|
||||||
.getPlayerSettings().getName());
|
.getPlayerSettings().getName());
|
||||||
view.setCurrentPlayerColor(roundState.getActivePlayer()
|
view.setCurrentPlayerColor(roundState.getActivePlayer()
|
||||||
.getPlayerSettings().getColor());
|
.getPlayerSettings().getColor());
|
||||||
view.setHasLaidOut(roundState.getActivePlayer().getLaidOut());
|
view.setHasLaidOut(roundState.getActivePlayer().getLaidOut());
|
||||||
|
|
||||||
|
if (!isHuman)
|
||||||
|
startTurn();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startTurn() {
|
private void startTurn() {
|
||||||
|
if (turnControl != null)
|
||||||
|
return;
|
||||||
|
boolean isHuman = roundState.getActivePlayer().getPlayerSettings().getTurnControlType() == HUMAN;
|
||||||
boolean inspectOnly = roundState.getTurnNumber() < 1;
|
boolean inspectOnly = roundState.getTurnNumber() < 1;
|
||||||
boolean mayRedeal = inspectOnly
|
boolean mayRedeal = inspectOnly
|
||||||
&& roundState.getActivePlayer().getHand()
|
&& roundState.getActivePlayer().getHand()
|
||||||
.getIdenticalStoneCount() >= 3;
|
.getIdenticalStoneCount() >= 3;
|
||||||
|
|
||||||
view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal);
|
if (isHuman) {
|
||||||
|
view.getPlayerPanel().setEndTurnMode(inspectOnly, mayRedeal);
|
||||||
ITurnControl turnControl = TurnControlFactory.getFactory(roundState.getActivePlayer()
|
}
|
||||||
|
turnControl = TurnControlFactory.getFactory(roundState.getActivePlayer()
|
||||||
.getPlayerSettings().getTurnControlType()).create();
|
.getPlayerSettings().getTurnControlType()).create();
|
||||||
turnControl.setup(roundState.getActivePlayer(), clonedTable,
|
turnControl.setup(roundState.getActivePlayer(), clonedTable,
|
||||||
view, inspectOnly, mayRedeal);
|
view, inspectOnly, mayRedeal);
|
||||||
|
@ -142,6 +157,8 @@ public class RoundControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void endOfTurn() {
|
private void endOfTurn() {
|
||||||
|
view.enableThinkPanel(false);
|
||||||
|
turnControl = null;
|
||||||
if (roundState.getTurnNumber() >= 1) {
|
if (roundState.getTurnNumber() >= 1) {
|
||||||
checkTurn();
|
checkTurn();
|
||||||
}
|
}
|
||||||
|
@ -335,6 +352,7 @@ public class RoundControl {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void redeal() {
|
private void redeal() {
|
||||||
|
turnControl = null;
|
||||||
for (Connection c : new ArrayList<Connection>(connections)) {
|
for (Connection c : new ArrayList<Connection>(connections)) {
|
||||||
c.remove();
|
c.remove();
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,8 @@ public class BaseAIControl extends AbstractTurnControl {
|
||||||
|
|
||||||
private void emitEndOfTurn() {
|
private void emitEndOfTurn() {
|
||||||
long timeElapsed = System.currentTimeMillis() - startTime;
|
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) {
|
if (waitTime > 0) {
|
||||||
try {
|
try {
|
||||||
|
|
BIN
src/jrummikub/resource/dark_wood.png
Normal file
BIN
src/jrummikub/resource/dark_wood.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
|
@ -142,4 +142,6 @@ public interface IView {
|
||||||
* specifies if the interface is to be enabled or disabled
|
* specifies if the interface is to be enabled or disabled
|
||||||
*/
|
*/
|
||||||
void showInterface(boolean enable);
|
void showInterface(boolean enable);
|
||||||
|
|
||||||
|
public void enableThinkPanel(boolean b);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,16 +29,23 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
|
||||||
private final static int HEIGHT = 2;
|
private final static int HEIGHT = 2;
|
||||||
|
|
||||||
private final static BufferedImage BACKGROUND;
|
private final static BufferedImage BACKGROUND;
|
||||||
|
private final static BufferedImage DARK_BACKGROUND;
|
||||||
static {
|
static {
|
||||||
ImageIcon image = new ImageIcon(
|
ImageIcon image = new ImageIcon(
|
||||||
HandPanel.class.getResource("/jrummikub/resource/wood.png"));
|
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(),
|
BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
|
||||||
BufferedImage.TYPE_INT_RGB);
|
BufferedImage.TYPE_INT_RGB);
|
||||||
|
DARK_BACKGROUND = new BufferedImage(darkImage.getIconWidth(), darkImage.getIconHeight(),
|
||||||
|
BufferedImage.TYPE_INT_RGB);
|
||||||
|
|
||||||
image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
|
image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
|
||||||
|
darkImage.paintIcon(null, DARK_BACKGROUND.createGraphics(), 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private BufferedImage scaledBackground = BACKGROUND;
|
private BufferedImage scaledBackground = BACKGROUND;
|
||||||
|
private BufferedImage scaledDarkBackground = BACKGROUND;
|
||||||
|
|
||||||
private PlayerPanel playerPanel;
|
private PlayerPanel playerPanel;
|
||||||
|
|
||||||
|
@ -47,7 +54,6 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
|
||||||
|
|
||||||
private boolean repaintAll = true;
|
private boolean repaintAll = true;
|
||||||
private Collection<Stone> selectedStones = Collections.emptyList();
|
private Collection<Stone> selectedStones = Collections.emptyList();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Board instance
|
* 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 scaled = new BufferedImage(size, size,
|
||||||
BufferedImage.TYPE_INT_RGB);
|
BufferedImage.TYPE_INT_RGB);
|
||||||
Graphics2D g = scaled.createGraphics();
|
Graphics2D g = scaled.createGraphics();
|
||||||
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
|
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
|
||||||
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||||
|
|
||||||
g.drawImage(BACKGROUND, 0, 0, size, size, null);
|
g.drawImage(background, 0, 0, size, size, null);
|
||||||
|
|
||||||
return scaled;
|
return scaled;
|
||||||
}
|
}
|
||||||
|
@ -86,13 +92,22 @@ class HandPanel extends AbstractStonePanel implements IHandPanel {
|
||||||
|
|
||||||
int size = height / HEIGHT;
|
int size = height / HEIGHT;
|
||||||
|
|
||||||
|
|
||||||
|
BufferedImage background = isEnabled() ? scaledBackground : scaledDarkBackground ;
|
||||||
|
|
||||||
if (repaintAll) {
|
if (repaintAll) {
|
||||||
if (scaledBackground.getHeight() != size)
|
if (background.getHeight() != size) {
|
||||||
scaledBackground = getScaledBackground(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 i = 0; i < HEIGHT; ++i) {
|
||||||
for (int xpos = -size * i / 3; xpos < width; xpos += size) {
|
for (int xpos = -size * i / 3; xpos < width; xpos += size) {
|
||||||
g.drawImage(scaledBackground, xpos, size * i, null);
|
g.drawImage(background, xpos, size * i, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import java.awt.event.ComponentAdapter;
|
||||||
import java.awt.event.ComponentEvent;
|
import java.awt.event.ComponentEvent;
|
||||||
import java.awt.event.ComponentListener;
|
import java.awt.event.ComponentListener;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JLabel;
|
import javax.swing.JLabel;
|
||||||
|
@ -16,8 +17,11 @@ import javax.swing.JProgressBar;
|
||||||
import javax.swing.UIManager;
|
import javax.swing.UIManager;
|
||||||
import javax.swing.border.EmptyBorder;
|
import javax.swing.border.EmptyBorder;
|
||||||
|
|
||||||
|
import jrummikub.model.Position;
|
||||||
|
import jrummikub.model.Stone;
|
||||||
import jrummikub.util.Event;
|
import jrummikub.util.Event;
|
||||||
import jrummikub.util.IEvent;
|
import jrummikub.util.IEvent;
|
||||||
|
import jrummikub.util.Pair;
|
||||||
import jrummikub.view.IPlayerPanel;
|
import jrummikub.view.IPlayerPanel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -382,4 +386,20 @@ class PlayerPanel extends JPanel implements IPlayerPanel {
|
||||||
keepStonesButton.setVisible(false);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -97,8 +97,8 @@ public class View extends JFrame implements IView {
|
||||||
mainLayer.add(table);
|
mainLayer.add(table);
|
||||||
|
|
||||||
playerPanel = new PlayerPanel();
|
playerPanel = new PlayerPanel();
|
||||||
playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0, 0,
|
playerPanel.setBorder(new MatteBorder(PLAYER_PANEL_BORDER_WIDTH, 0, 0,
|
||||||
Color.BLACK));
|
0, Color.BLACK));
|
||||||
mainLayer.add(playerPanel);
|
mainLayer.add(playerPanel);
|
||||||
|
|
||||||
startTurnPanel = new StartTurnPanel();
|
startTurnPanel = new StartTurnPanel();
|
||||||
|
@ -250,4 +250,9 @@ public class View extends JFrame implements IView {
|
||||||
playerPanel.showButtons(false);
|
playerPanel.showButtons(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enableThinkPanel(boolean enable) {
|
||||||
|
playerPanel.enableButtons(!enable);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue