-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphGame.java
More file actions
263 lines (242 loc) · 7.39 KB
/
GraphGame.java
File metadata and controls
263 lines (242 loc) · 7.39 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package DrNim;
import java.util.InputMismatchException;
public class GraphGame extends CombGame{
/* example GraphGame:
*
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1: 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 :1
2: 1 0 0 1 0 0 0 0 0 0 0 0 0 0 :2
3: 1 0 0 1 1 0 0 0 1 0 0 0 0 :3
4: 0 0 0 0 0 0 0 0 0 0 0 0 :4
5: 1 0 0 0 0 1 0 0 0 0 0 :5
6: 1 0 0 1 0 0 0 0 0 0 :6
7: 0 0 0 0 1 0 1 1 0 :7
8: 1 0 0 1 0 0 0 0 :8
9: 0 0 0 0 0 0 0 :9
10: 1 0 0 1 0 0 :10
11: 0 0 1 0 0 :11
12: 1 0 1 0 :12
13: 0 0 0 :13
14: 1 0 :14
15: 1 :15
16: :16
*
*
*
*/
GGNode[] nodes;
GGNode curNode;
String map;
public GraphGame(String name){
super(name);
}
public void initDialog(){
boolean loop=true;
int nodeCount=0;
while(loop){
loop=false;
System.out.println("How many nodes do you want?");
boolean valid=true;
do {
try {
valid=true;
nodeCount = Main.reader.nextInt();
} catch (InputMismatchException e) {
Main.reader.next();
valid=false;
System.out.println("You have to input an integer.");
}
} while (valid == false);
if(nodeCount<1){
System.out.println("Dr. Nim: We need at least one node!");
loop=true;
}
}//end while(loop)
//-------------------------------------------------------------------
nodes=new GGNode[nodeCount];
for(int i=nodes.length-1;i>-1; i--){
nodes[i]=new GGNode();
nodes[i].setNumber(i+1);
nodes[i].setSG(-1);
}
curNode=nodes[0];
for(int i=1; i<nodeCount+1;i++){ //i counts one based
boolean loop2=true;
int sucCount=0; //successors (counts 1 based)
while(loop2){
loop2=false;
System.out.println("How many succesors of node #"+i+"?");
System.out.println("remember only nodes with a greater number can be successors."); //this rule avoids cycles
boolean valid=true;
do {
try {
valid=true;
sucCount = Main.reader.nextInt();
} catch (InputMismatchException e) {
Main.reader.next();
valid=false;
System.out.println("You have to input an integer.");
}
} while (valid == false);
if(sucCount<0){
System.out.println("Dr. Nim: A node can not have less than zero succesors!");
loop2=true;
}
if(sucCount>nodeCount-i){ //there are not enough different nodes you could put as successor
System.out.println("Dr. Nim: There are not enough different nodes you could put as successor!");
loop2=true;
}
}
//loop3 for the successors
for(int j=1; j<sucCount+1;j++){
boolean loop3=true;
int sucChoise=0; //successor choice
while(loop3){
loop3=false;
System.out.println("What succesors of node #"+i+"?");
System.out.println("remember only nodes with a greater number can be successors."); //this rule avoids cycles
boolean valid3=true;
do {
try {
valid3=true;
sucChoise = Main.reader.nextInt();
} catch (InputMismatchException e) {
Main.reader.next();
valid3=false;
System.out.println("You have to input an integer.");
}
} while (valid3 == false);
if(sucChoise<=i){
System.out.println("Dr. Nim: Remember only nodes with a greater number can be successors.");
loop3=true;
}
if(sucChoise>nodeCount){
System.out.println("Dr. Nim: This node does not exist.");
loop3=true;
}
if(nodes[i-1].isAlreadyIn(sucChoise)){
System.out.println("Dr. Nim: This node is already a successor of node#"+i+".");
loop3=true;
}
}
nodes[i-1].addSuc(nodes[sucChoise-1]); //sucChoise-1 because we have to go back to zero based counting
}
}
update();
calcMap();
}
public void calcSG(){ //complexity: O(#nodes*O(GGNode.calcSG)) = O(#nodes*(#avg_node.successors^2)) = O(#successors^2)
for(int i=nodes.length-1;i>-1; i--){
nodes[i].calcSG();
}
curSG=curNode.getSG();
}
public void update() {
calcSG();
}
protected void updateTerminal() {
if(curNode==null){
//do nothing
}else{
if(curNode.successors.length==0){
terminal=true;
}else{
terminal=false;
}
}
}
public void play(int f, int t) { //f:=who has to do a move ; t what SG-value does Dr.Nim want to create
if(f==1){ //human turn
System.out.println(map);
System.out.println("It is your turn");
System.out.println("Choose one of the successor");
for(int i=0;i<curNode.successors.length;i++){
System.out.println(""+(i+1)+": node#"+curNode.successors[i].number);
}
int sucChoice=0;
boolean loop=true;
while(loop){ //asking again if the input is not allowed
loop=false;
boolean valid=true;
do {
try {
valid=true;
sucChoice = Main.reader.nextInt();
} catch (InputMismatchException e) {
Main.reader.next();
valid=false;
System.out.println("You have to input an integer.");
}
} while (valid == false);
if(sucChoice<1 || sucChoice>curNode.successors.length){
System.out.println("Dr.Nim: Hey you are cheating! Choose a node from the list");
loop=true;
}
}
curNode=curNode.successors[sucChoice-1];
updateSG();
updateTerminal();
}else{//f=0 Dr.Nim turn
boolean found=false; //found a successor with the SG-value t
int nextNode=0;
for(int i=0;i<curNode.successors.length;i++){
if(curNode.successors[i].getSG()==t){
found=true;
nextNode=i;
break;
}
}
if(found){
System.out.println("Dr. Nim moves from node#"+curNode.number+" to node#"+curNode.successors[nextNode].number+".");
curNode=curNode.successors[nextNode];
}else{ //move to whatever node (Dr. Nim has to move from a position with SG-value 0)
System.out.println("Dr. Nim moves from node#"+curNode.number+" to node#"+curNode.successors[0].number+".");
curNode=curNode.successors[0];
}
updateTerminal();
updateSG();
}
}
public void calcMap() { //calculates a String to represent the Graph (see example below)
/*
* 1 2 3 4
* 1 x 1 0 1 node 1 has as successors node 2 and node 4 (not node 3)
* 2 x x 1 0 node 2 has as successors node 3 (not node 4)
* 3 x x x 1 node 3 has as successor node 4
* 4 x x x x
*/
String s="\t";
for(int i=0;i<nodes.length;i++){
s+=""+(i+1)+"\t";
}
s+="\n";
for(int i=0;i<nodes.length;i++){
s+=""+(i+1)+":\t";
for(int j=0;j<nodes.length;j++){
if(j<=i){
s+="\t";
}else{
if(nodes[i].isAlreadyIn(j+1)){
s+="1\t";
}else{
s+="0\t";
}
}
}
s+=":"+(i+1)+"\n";
}
map=s;
}
public void updateSG(){
if(curNode==null){
curSG=0;
}else{
curSG=curNode.getSG();
}
}
public String toSimpleString(){
String s=name;
s+="("+curNode.number+"/"+nodes.length+")";
return s;
}
}