가위바위보 게임을 할 건데, 몇 판할지 입력 받음
입력받은 판 수만큼 반복
컴퓨터 : Math.random() : 0.0 ~ 1.0 난수 생성
1~3 사이 난수 생성
1이면 가위, 2이면 바위, 3이면 보 지정
컴퓨터와 플레이어 가위바위보 판별
플레이어 승! / 졌습니다ㅠㅠ
매판마다 - 현재 기록 : 2승 1무 0패
public void RSPGame() {
Scanner sc = new Scanner(System.in);
System.out.println("[가위 바위 보 게임~!!]");
System.out.print("몇 판 ? : ");
int round = sc.nextInt();
// 승패 기록용 변수
int win = 0;
int draw = 0;
int lose = 0;
for(int i = 1; i <= round; i++) { // 입력받은 판 수 만큼 반복
System.out.println("\n" + i + "번째 게임");
System.out.print("가위/바위/보 중 하나를 입력 : ");
String input = sc.next();
int random = (int)(Math.random() * 3 + 1); // 1 2 3
// Math.random() : 0.0 ~ 1.0 사이
// 0.0 <= x < 1.0
// 0.0 <= x * 3 < 3.0
// 1.0 <= x * 3 + 1 < 4.0
// 1 <= (int)(x * 3 + 1) < 4
// -> 1 이상 4 미만 정수 -> 1 2 3
// switch로 random 값을 문자열 바꾸기
String com = null;
// null : 아무것도 참조하고 있지 않음.
switch(random) {
case 1 : com = "가위"; break;
case 2 : com = "바위"; break;
case 3 : com = "보"; break;
}
// 컴퓨터는 [바위]를 선택했습니다
System.out.printf("컴퓨터는 [%s]를 선택했습니다.\n", com);
// 컴퓨터와 플레이어 가위바위보 판별
if( input.equals(com) ) {
System.out.println("비겼습니다");
draw++;
} else { // 지거나, 이긴경우
boolean win1 = input.equals("가위") && com.equals("보");
boolean win2 = input.equals("바위") && com.equals("가위");
boolean win3 = input.equals("보") && com.equals("바위");
if(win1 || win2 || win3) {
System.out.println("플레이어 승!");
win++;
} else {
System.out.println("졌습니다ㅠㅠ");
lose++;
}
}
System.out.printf("현재 기록 : %d승 %d무 %d패\n", win, draw, lose);
}
}
}
// 코드 실행용 클래스
public class BranchRun {
public static void main(String[] args) {
BranchExample branchEx = new BranchExample();
branchEx.RSPGame();
}
}