-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathGamer.java
69 lines (59 loc) · 1.5 KB
/
Gamer.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
package java.memento;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class Gamer {
private int money;
private List fruits = new ArrayList();
private Random random = new Random();
private static String[] fruitsname = { "사과", "포도", "바나나", "귤", };
public Gamer(int money) {
this.money = money;
}
public int getMoney() {
return money;
}
public void bet() {
int dice = random.nextInt(6) + 1;
if (dice == 1) {
money += 100;
System.out.println("소지금이 증가했습니다.");
} else if (dice == 2) {
money /= 2;
System.out.println("소지금이 절반이 되었습니다.");
} else if (dice == 6) {
String f = getFruit();
System.out.println("과일(" + f + ")을 받았습니다.");
fruits.add(f);
} else {
System.out.println("변한 것이 없습니다.");
}
}
public Memento createMemento() {
Memento m = new Memento(money);
Iterator it = fruits.iterator();
while (it.hasNext()) {
String f = (String) it.next();
if (f.startsWith("맛있는 ")) {
m.addFruit(f);
}
}
return m;
}
public void restoreMemento(Memento memento) {
this.money = memento.money;
this.fruits = memento.fruits;
}
@Override
public String toString() {
return "[money = " + money + ", fruits = " + fruits + "]";
}
private String getFruit() {
String prefix = "";
if (random.nextBoolean()) {
prefix = "맛있는 ";
}
return prefix + fruitsname[random.nextInt(fruitsname.length)];
}
}