-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHand.java
More file actions
55 lines (46 loc) · 1.28 KB
/
Hand.java
File metadata and controls
55 lines (46 loc) · 1.28 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
package Strategy_Pattern;
public enum Hand {
// 가위, 바위, 보를 나타내는 세 개의 enum 상수
ROCK("바위", 0),
SCISSORS("가위", 1),
PAPER("보", 2);
// enum이 가진 필드
private String name;
private int handvalue;
// 손의 값으로 상수를 얻기 위한 배열
private static Hand[] hands = {
ROCK, SCISSORS, PAPER
};
// 생성자
private Hand(String name, int handvalue) {
this.name = name;
this.handvalue = handvalue;
}
// 손의 값으로 enum 상수를 가져온다
public static Hand getHand(int handvalue) {
return hands[handvalue];
}
// this가 h보다 강할 때 true
public boolean isStrongerThan(Hand h) {
return fight(h) == 1;
}
// this가 h보다 약할 때 true
public boolean isWeakerThan(Hand h) {
return fight(h) == -1;
}
// 무승부는 0, this가 이기면 1, h가 이기면 -1
private int fight(Hand h) {
if(this == h) {
return 0;
} else if((this.handvalue + 1) % 3 == h.handvalue) {
return 1;
} else {
return -1;
}
}
// 가위, 바위, 보의 문자열 표현
@Override
public String toString() {
return name;
}
}