문제
A양이 가위바위보를 선택하면 B군이 임의로 가위바위보를 선택하고 가위바위보의 승패를 출력하는 문제입니다 이를 해결하는 프로그램의 다음 실행상태에 대해 빈칸을 채우세요
프로그램의 실행순서 및 실행상태
1 public static void main( String[] args ) {
3 new RockPaperScissors();
| | main() | | | | | | | | | |
| playerA | | | player | | SCISSORS | | ROCK | | PAPER | | select(),win(),equals(), ... | (obj01) |
R1b public RockPaperScissors() {
R1e }
2 RockPaperScissors playerA =
5 new RockPaperScissors();
| | main() | | | | | | | | | |
| playerA | (obj01) | | player | 0 | SCISSORS | 0 | ROCK | 1 | PAPER | 2 | select(),win(),equals(), ... | (obj01) |
| playerB | | | player | | SCISSORS | | ROCK | | PAPER | | select(),win(),equals(), ... | (obj02) |
R1b public RockPaperScissors() {
R1e }
4 RockPaperScissors playerB =
6 playerA.select( new Scanner( System.in ) );
R2b public void select( Scanner scan ) {
가위, 바위, 보 중 하나를 입력하세요 : 가위
R2e }
7 playerB.select();
R3b public void select() {
R3e }
8 System.out.println( "A양은 " +
A양은
9 playerA
R4b public String toString() {
R4e }
가위를 냈습니다.
10 + "를 냈습니다." );
11 System.out.println( "B군은 " +
B군은
12 playerB
R4b public String toString() {
R4e }
보를 냈습니다.
13 + "를 냈습니다." );
14 if ( playerA.equals( playerB ) )
R5b public boolean equals( RockPaperScissors counterpart ) {
R5e }
16 else if ( playerA.win( playerB ) )
R6b public boolean win( RockPaperScissors counterpart ) {
R61 return win( counterpart.toInteger() );
R7b public boolean win( int counterpart ) {
R7e }
R6e }
17 System.out.println( "A양이 이겼습니다" );
A양이 이겼습니다
20 }
프로그램 코드
import java.util.Scanner;
import rpsGame.RockPaperScissors;
public class RockPaperScissorsGame
{
1 public static void main( String[] args ) {
2 RockPaperScissors playerA =
3 new RockPaperScissors();
4 RockPaperScissors playerB =
5 new RockPaperScissors();
6 playerA.select( new Scanner( System.in ) );
7 playerB.select();
8 System.out.println( "A양은 " +
9 playerA
10 + "를 냈습니다." );
11 System.out.println( "B군은 " +
12 playerB
13 + "를 냈습니다." );
14 if ( playerA.equals( playerB ) )
15 System.out.println( "A양과 B군이 비겼습니다" );
16 else if ( playerA.win( playerB ) )
17 System.out.println( "A양이 이겼습니다" );
18 else
19 System.out.println( "B군이 이겼습니다" );
20 }
}
package rpsGame;
import java.util.Scanner;
public class RockPaperScissors
{
private int player;
private final int SCISSORS = 0;
private final int ROCK = 1;
private final int PAPER = 2;
R1b public RockPaperScissors() {
this.player = SCISSORS;
R1e }
R2b public void select( Scanner scan ) {
String input = "";
do {
System.out.print( "가위, 바위, 보 중 하나를 입력하세요 : ");
input = scan.next();
} while( !input.equals( "가위" ) && !input.equals( "바위" ) && !input.equals( "보" ) );
scan.close();
if ( input.equals( "가위" ) )
this.player = SCISSORS;
else if ( input.equals( "바위" ) )
this.player = ROCK;
else
this.player = PAPER;
R2e }
R3b public void select() {
this.player = (int)( Math.random() * 3 );
R3e }
R4b public String toString() {
if ( this.player == SCISSORS )
return "가위";
else if ( this.player == ROCK )
return "바위";
else
return "보";
R4e }
public int toInteger() {
return this.player;
}
R5b public boolean equals( RockPaperScissors counterpart ) {
return this.player == counterpart.toInteger();
R5e }
R6b public boolean win( RockPaperScissors counterpart ) {
R61 return win( counterpart.toInteger() );
R6e }
R7b public boolean win( int counterpart ) {
if ( ( ( this.player == SCISSORS ) && ( counterpart == PAPER ) )
|| ( ( this.player == PAPER ) && ( counterpart == ROCK ) )
|| ( ( this.player == ROCK ) && ( counterpart == SCISSORS ) ) )
return true;
else
return false;
R7e }
}