-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNimModel.java
More file actions
146 lines (145 loc) · 4.86 KB
/
NimModel.java
File metadata and controls
146 lines (145 loc) · 4.86 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
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
import java.io.IOException;
import java.util.ArrayList;
/**
* NimModel is the model for each game session. It handles the game logic for
* Nim.
*
* @author Pavel Rozvora (pxr8306)
* @version 2015-12-08
*/
public class NimModel implements ViewListener {
SessionManager manager;
private ArrayList<ModelListener> listenerList = new ArrayList<ModelListener>();
private int[] heap = {3, 4, 5};
private int[] score = {0, 0};
private int turn = 0;
private String[] names = {"", ""};
/**
* Adds a model listener for each player who joins
* @param modelListener The model listener that messages will go through
*/
public void addModelListener(ModelListener modelListener) {
listenerList.add(modelListener);
}
/**
* The join method sends the players name and a model listener to the
* server in order to be placed in a game session.
* @param proxy A reference to the view proxy object for the client
* @param name The player's name
* @throws IOException Thrown when I/O fails or is interrupted
*/
public void join(NimViewProxy proxy, String name) throws IOException {
if (listenerList.size() == 1) {
names[0] = name;
listenerList.get(0).id(0);
listenerList.get(0).name(0, name);
listenerList.get(0).score(0, 0);
} else {
names[1] = name;
listenerList.get(1).id(1);
sendHeapStatus();
sendNames();
sendScore();
sendTurn();
}
}
/**
* This method tells the server what move the player made.
* @param heapId id of heap from which markers were taken
* @param markers how many markers were taken
* @throws IOException Thrown when I/O fails or is interrupted
*/
public void take(int heapId, int markers) throws IOException {
heap[heapId] -= markers;
if (heap[0] == 0 && heap[1] == 0 && heap[2] == 0) {
sendWin();
score[turn] += 1;
sendScore();
sendHeapStatus();
turn = 0;
} else {
turn++;
turn = turn % 2;
sendHeapStatus();
sendTurn();
}
}
/**
* This method informs the server that a player would like to start a new
* game.
* @throws IOException Thrown when I/O fails or is interrupted
*/
public void newGame() throws IOException {
heap[0] = 3;
heap[1] = 4;
heap[2] = 5;
turn = 0;
sendHeapStatus();
sendTurn();
}
/**
* This method informs the server that a player has terminated the program.
* @throws IOException Thrown when I/O fails or is interrupted
*/
public void quit() throws IOException {
sendQuit();
}
/**
* This method communicates the heap status to the players.
* @throws IOException Thrown when I/O fails or is interrupted
*/
private void sendHeapStatus() throws IOException {
for (int i = 0; i <= 2; i++) {
listenerList.get(0).heap(i, heap[i]);
listenerList.get(1).heap(i, heap[i]);
}
}
/**
* This method communicates whose turn it is to the players.
* @throws IOException Thrown when I/O fails or is interrupted
*/
private void sendTurn() throws IOException {
listenerList.get(0).turn(turn);
listenerList.get(1).turn(turn);
}
/**
* This method communicates the winner to the players.
* @throws IOException Thrown when I/O fails or is interrupted
*/
private void sendWin() throws IOException {
listenerList.get(0).win(turn);
listenerList.get(1).win(turn);
}
/**
* This method communicates the score to the players.
* @throws IOException Thrown when I/O fails or is interrupted
*/
private void sendScore() throws IOException {
listenerList.get(0).score(turn, score[turn]);
listenerList.get(1).score(turn, score[turn]);
}
/**
* This method communicates the names of both players to the players.
* @throws IOException Thrown when I/O fails or is interrupted
*/
private void sendNames() throws IOException {
listenerList.get(0).name(0, names[0]);
listenerList.get(0).name(1, names[1]);
listenerList.get(1).name(0, names[0]);
listenerList.get(1).name(1, names[1]);
}
/**
* This method communicates a quit action to the players or the session
* manager.
* @throws IOException Thrown when I/O fails or is interrupted
*/
private void sendQuit() throws IOException {
if (listenerList.size() == 2) {
listenerList.get(0).quit();
listenerList.get(1).quit();
} else {
// the manager only resets the current session if the session isn't full
manager.quit();
}
}
}