-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameStateMachine.java
171 lines (145 loc) · 3.77 KB
/
GameStateMachine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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;
public class GameStateMachine extends StateMachine
{
public static final String NEW = "new";
public static final String TURN = "turn";
public static final String WIN = "win";
public static final String DRAW = "draw";
public static final String TIMEOUT = "timeout";
public GameStateMachine(final GameController game, final GameView gameView)
{
super();
NewGame newGame = new NewGame(game);
DrawState drawState = new DrawState(game);
Player1Turn player1Turn = new Player1Turn(game);
Player2Turn player2Turn = new Player2Turn(game);
Player1Win player1Win = new Player1Win(game);
Player2Win player2Win = new Player2Win(game);
this.addState(newGame);
this.addState(drawState);
this.addState(player1Turn);
this.addState(player2Turn);
this.addState(player1Win);
this.addState(player2Win);
newGame.onSignal(TURN, player1Turn);
player1Turn.onSignal(WIN, player1Win);
player1Turn.onSignal(DRAW, drawState);
player1Turn.onSignal(TURN, player2Turn);
player1Turn.onSignal(TIMEOUT, player2Win);
player2Turn.onSignal(WIN, player2Win);
player2Turn.onSignal(DRAW, drawState);
player2Turn.onSignal(TURN, player1Turn);
player2Turn.onSignal(TIMEOUT, player1Win);
drawState.onSignal(NEW, newGame);
player1Win.onSignal(NEW, newGame);
player2Win.onSignal(NEW, newGame);
this.setInitialState(newGame);
}
private class NewGame extends State
{
public NewGame(final GameController game)
{
super("NewGame");
this.game = game;
}
public void enter()
{
this.game.reset();
this.game.pulse(new SignalEvent(GameStateMachine.TURN));
this.game.repaint();
}
public void destroy()
{
App.log("NewGame::destroy");
super.destroy();
this.game = null;
App.log("NewGame::destroy done");
}
private GameController game;
}
private class TurnState extends GameState
{
public TurnState(final GameController game, int player, final String name)
{
super(game, player, name);
}
public void enter()
{
this.status = GameEvent.CONTINUE;
this.keyAdapter = this.getPlayer().getKeyAdapter();
super.enter();
if (!App.gamer.isRestoring())
{
this.getPlayer().think(App.gamer.getContext());
App.gamer.startTimer();
}
}
public void leave()
{
super.leave();
App.gamer.stopTimer();
}
}
private class Player1Turn extends TurnState
{
public Player1Turn(final GameController game)
{
super(game, 0, "Player1Turn");
}
}
private class Player2Turn extends TurnState
{
public Player2Turn(final GameController game)
{
super(game, 1, "Player2Turn");
}
}
private class WinState extends GameState
{
public WinState(final GameController game, int player, final String name)
{
super(game, player, name);
this.keyAdapter = this.endGameKeyAdapter;
}
public void enter()
{
this.status = GameEvent.WIN;
super.enter();
App.gamer.stop();
}
}
private class Player1Win extends WinState
{
public Player1Win(final GameController game)
{
super(game, 0, "Player1Win");
}
}
private class Player2Win extends WinState
{
public Player2Win(final GameController game)
{
super(game, 1, "Player2Win");
}
}
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 = GameEvent.DRAW;
this.keyAdapter = this.endGameKeyAdapter;
}
public void enter()
{
super.enter();
App.gamer.stop();
}
}
}