Game
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package jse.alabama.rps;
import lombok.Data;
@Data
public class Game {
int rock;
int paper;
int scissors;
int comVal;
String rpsVal;
public void setRpsVal(int val) {
switch (val) {
case 1: rpsVal = "rock";break;
case 2: rpsVal = "paper";break;
case 3: rpsVal = "scissors";break;
default:break;
}
}
public void setComVal() {
this.comVal = (int) (Math.random()*3+1);
}
}
|
cs |
IGame
1
2
3
4
5
6
7
|
package jse.alabama.rps;
public interface IGame {
public String getResult(int result);
public String playGame(int a, int b);
}
|
cs |
GameValidation
1
2
3
4
5
6
7
8
9
10
11
12
|
package jse.alabama.rps;
public class GameValidation {
public String showRange(int begin, int end){
return String.format("Please enter only values from %d to %d.", begin, end);
}
public String showInt() {
return "Please enter an integer value only.";
}
}
|
cs |
GameService
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
|
package jse.alabama.rps;
public class GameService implements IGame {
Game game = new Game();
@Override
public String getResult(int a) {
String result = "";
switch (a) {
case 1:result = "win";break;
case 2:result = "loss";break;
case 3:result = "tie";break;
default:break;
}
return result;
}
@Override
public String playGame(int gamer, int computer) {
int win = 0;
if (gamer==computer) {
return getResult(3);
}
switch (Math.abs(gamer-computer)) {
case 1: win = (gamer > computer) ? gamer : computer;break;
case 2: win = (gamer > computer) ? computer : gamer;break;
default:
break;
}
String winner = (win==gamer) ? "Gamer":"Computer";
game.setRpsVal(gamer);
String userRPS = game.getRpsVal();
game.setRpsVal(computer);
String comRPS = game.getRpsVal();
return "Computer : "+comRPS+" , Gamer : "+userRPS+" \n Winnder is "+ winner;
}
}
|
cs |
GameController
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
|
package jse.alabama.rps;
import java.util.Scanner;
public class GameController {
public static void main(String[] args) {
System.out.println("Let's start the rock-paper-scissors game..");
IGame service = new GameService(); // 깊은 복사(deep copy)
GameValidation valid = new GameValidation();
Game game = new Game();
Scanner scanner = new Scanner(System.in);
int start=1,limit=3;
while (true) {
System.out.println("Please enter Scissors:1 Rock:2 Paper:3");
int userVal = scanner.nextInt();
if (userVal<start||userVal>limit) {
System.out.println(valid.showRange(start, limit));
continue;
}
game.setComVal();
System.out.println(service.playGame(userVal, game.getComVal()));
}
}
}
|
cs |
'Java' 카테고리의 다른 글
19. 회원관리 Swing 앱 :: EmployeeUI.java (0) | 2020.04.30 |
---|---|
[자바 Swing] 로또 LottoMain.java LottoUI.java Lotto.java (0) | 2020.04.30 |
[자바 객체지향] 인터페이스 예제 Bank.java (0) | 2020.04.30 |
[자바 알고리즘] 행렬을 이용한 풀이 Lotto.java (0) | 2020.04.30 |
40. 회원관리 Swing 앱 :: MemberView.java (0) | 2020.04.14 |