-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
136 lines (122 loc) · 3.68 KB
/
Main.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
package DrNim;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
static CombGame g; //the game that is the combination of all the chosen games
static Scanner reader = new Scanner(System.in);
public static void main(String[] args) {
boolean mainloop=true;
while(mainloop){
g=new CombGame("THE Game");
int f=init2(); //'f' whos turn? 0 human 1 Dr. Nim
g.updateTerminal(); //is the game over?
g.updateSG(); //what is the current SG-value
mainloop=play(f); //decides the winner (if the winner is not decided yet player 'f' does a move)
}//end while(mainloop)
}
private static boolean play(int f) {
boolean mainloop=true;
boolean valid;
boolean loop=true;
if(g.terminal){ //this is only the case if g starts in a terminal position
System.out.println("!Game over!");
if(f==0){
System.out.println("The human is the winner.");
}else{
System.out.println("Dr. Nim is the winner.");
}
System.out.println("Dr. Nim: This was a very short game. Do you want to play again?");
System.out.println("0: No");
System.out.println("1: Yes");
valid=true;
int re = 0;
do {
try {
valid=true;
re = reader.nextInt();
} catch (InputMismatchException e) {
reader.next();
valid=false;
System.out.println("You have to input an integer.");
}
} while (valid == false);
if(re==1){
System.out.println("Let's go!");
mainloop=true;
}else{
System.out.println("Dr. Nim: Bye!");
mainloop=false;
}
loop=false;
}
while(loop){
System.out.println(g.toSimpleString() + "SG-value: "+g.curSG);
g.play(f,0);
f--;
f=f*f;
if(g.terminal){
System.out.println("Game over!");
if(f==0){
System.out.println("The human is the winner.");
System.out.println("Dr. Nim: I have to go now... this game sucks anyway.");
mainloop=false;
}else{
System.out.println("Dr. Nim is the winner.");
System.out.println("Dr. Nim: This was fun. Do you want to play again?");
System.out.println("0: No");
System.out.println("1: Yes");
valid=true;
int re = 0;
do {
try {
valid=true;
re = reader.nextInt();
} catch (InputMismatchException e) {
reader.next();
valid=false;
System.out.println("You have to input an integer.");
}
} while (valid == false);
if(re==1){
System.out.println("Let's go!");
mainloop=true;
}else{
System.out.println("Dr. Nim: Bye!");
mainloop=false;
}
}
loop=false;
}
}
return mainloop;
}
private static int init2() { //who moves first?
System.out.println("Do you want to do the first move?");
System.out.println("0: No");
System.out.println("1: Yes");
boolean valid=true;
int f = 0;
do {
try {
valid=true;
f = reader.nextInt();
} catch (InputMismatchException e) {
reader.next();
valid=false;
System.out.println("You have to input an integer.");
}
} while (valid == false);
if(f==0 || f==1){
}else{//if the user can't decide Dr. Nim helps him ;)
System.out.print("Dr. Nim: I count that as a ");
if(g.getSG()==0){
System.out.println("Yes.");
f=1;
}else{
System.out.println("No.");
f=0;
}
}
return f;
}
}