-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglePileNim.java
More file actions
119 lines (98 loc) · 2.67 KB
/
SinglePileNim.java
File metadata and controls
119 lines (98 loc) · 2.67 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
package DrNim;
import java.util.InputMismatchException;
public class SinglePileNim extends CombGame{
int curSG;
int chips; //amount of chips
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("It is your turn");
System.out.println("Choose an amount of chips to take");
int take=0;
boolean loop=true;
while(loop){ //asking again if the input is not allowed
loop=false;
boolean valid=true;
do {
try {
valid=true;
take = Main.reader.nextInt();
} catch (InputMismatchException e) {
Main.reader.next();
valid=false;
System.out.println("You have to input an integer.");
}
} while (valid == false);
if(take<1){
System.out.println("Dr.Nim: Hey you are cheating! You have to take at least one chip");
loop=true;
}
if(take>chips){
System.out.println("Dr.Nim: Hey you are cheating! You can't take more chips than there are in the pile");
loop=true;
}
}
chips-=take;
updateSG();
updateTerminal();
}else{ //turn==0 Dr.Nim move
System.out.println("It is the turn of Dr. Nim");
System.out.print("Dr. Nim chooses to take "+(curSG-t)+" chip");
if(curSG-t==1){
System.out.println(".");
}else{
System.out.println("s.");
}
int take=curSG-t;
chips-=take;
updateSG();
updateTerminal();
}
}
protected void updateTerminal() {
if(chips==0){
terminal=true;
}else{
terminal=false;
}
}
boolean getTerminal() {
return terminal;
}
void updateSG() {
curSG=chips;
}
public SinglePileNim(String name){
super(name);
}
public void initDialog(){
boolean loop2=true;
int chipCount=0;
while(loop2){
loop2=false;
System.out.println("What should be the size of this pile?");
boolean valid=true;
do {
try {
valid=true;
chipCount = Main.reader.nextInt();
} catch (InputMismatchException e) {
Main.reader.next();
valid=false;
System.out.println("You have to input an integer.");
}
} while (valid == false);
if(chipCount<0){
System.out.println("Dr. Nim: I don't know how to play with negative chips");
loop2=true;
}
}
chips=chipCount;
}
public String toSimpleString(){
String s=""+chips+"";
return s;
}
int getSG() {
return curSG;
}
}