-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleController.java
More file actions
93 lines (73 loc) · 3.04 KB
/
SimpleController.java
File metadata and controls
93 lines (73 loc) · 3.04 KB
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
package reversi;
/**
* Simple controller
* All this does is allow any player to play in any space that they wish
*/
public class SimpleController implements IController
{
IModel model;
IView view;
java.util.Random rand = new java.util.Random();
@Override
public void initialise(IModel model, IView view)
{
this.model = model;
this.view = view;
}
@Override
public void startup()
{
// Initialise board
int width = model.getBoardWidth();
int height = model.getBoardHeight();
for ( int x = 0 ; x < width ; x++ )
for ( int y = 0 ; y < height ; y++ )
model.setBoardContents(x, y, 0);
// Consider setting up any initial pieces here in your own controller
// Refresh all messages and frames
view.feedbackToUser(1, "Simple controller - choose a square");
view.feedbackToUser(2, "Simple controller - choose a square");
view.refreshView();
}
@Override
public void squareSelected(int player, int x, int y)
{
// The finished flag never gets set by this controller, but the SimpleTestModel could set it
if ( model.hasFinished() )
{
view.feedbackToUser(player, "Somehow the game has finished!" );
return; // Don't do the set board contents
}
model.setBoardContents(x, y, player);
view.feedbackToUser(player, "You last played in location " + x + "," + y);
view.refreshView();
}
@Override
public void doAutomatedMove(int player)
{
int x = rand.nextInt(model.getBoardWidth());
int y = rand.nextInt(model.getBoardHeight());
model.setBoardContents(x, y, player);
view.feedbackToUser(player, "You last played in location " + x + "," + y );
view.refreshView();
}
@Override
public void update()
{
// Here we will set finished based upon whether there is any space on the board or not...
// Warning: this is not the real finish conditions for the reversi game you are doing.
// Reversi should end if neither player can move - which includes an empty board - see the test cases in SimpleTestModel.
boolean finished = true;
for ( int x = 0 ; x < model.getBoardWidth() ; x++ )
for ( int y = 0 ; y < model.getBoardHeight() ; y++ )
if ( model.getBoardContents(x, y) == 0 )
finished = false; // There is an empty square
model.setFinished(finished); // You should set finished to false if there is a move, or true if there is not.
// We assume that something might have changed so update the labels accordingly, then tell view to update itself
// In this controller though we don't use the finished flag or player number, so we probably just tell the user what was set, for debug purposes
// Note that these are not the messages that you should set, but are useful for debugging purposes, to see what the controller did.
view.feedbackToUser(1, "I just updated: finished = " + model.hasFinished() + ", current player = " + model.getPlayer() );
view.feedbackToUser(2, "I just updated: finished = " + model.hasFinished() + ", current player = " + model.getPlayer() );
view.refreshView();
}
}