[ 작업 환경 ]
Windows10
jdk-15
[ 문제 ]
• 가위(Scissors), 바위(Rock), 보(Paper) 게임을 5회 동안 컴퓨터와 user가 대결하는
RockScissorsPaper 클래스를 만드시오. 첨부하는RockScissorsPaper를 이용하시오.
• RSP 패키지에 넣어서 만드시오.
We should be able to show the result by the command window, cmd, and
java RSP.RockScissorsPaper
// 아래 이미지는 실행 예시이며, 필요한 경우 RockScissorsPaper.java_part를 참고하시오.
//************************************************************
// RockScissorsPaper.java_part
//************************************************************
package
public class RockScissorsPaper {
public static int HowManyRuns = 5;
public static final int ROCK = 1;
public static final int Scissors = 2;
public static final int Paper = 3;
public static void main(String[] args) {
int userScore = 0;
int compScore = 0;
int userMove, compMove;
int winner;
System.out.println("ROCK - PAPER - SCISSORS");
System.out.println("This game plays "+ HowManyRuns + " rounds.");
// Loop once for each round of the game.
for (int rnd = 1; rnd <= RockScissorsPaper.HowManyRuns; rnd++) {
// Get the moves for this round.
userMove = getUserMove();
compMove = getComputerMove();
System.out.println("Computer's move: " + getMoveName(compMove));
// Determine the winner of this round.
winner = determineRoundWinner(compMove, userMove);
// Print a message and add one to the score of the winner.
if (winner == 1) {
System.out.println(getMoveName(compMove) + " beats "
+ getMoveName(userMove)
+ " the computer wins this round.");
compScore++;
} else if (winner == -1) {
System.out.println(getMoveName(userMove) + " beats "
+ getMoveName(compMove)
+ " the user wins this round.");
userScore++;
} else {
System.out.println(getMoveName(userMove) + " ties "
+ getMoveName(compMove)
+ " nobody wins this round.");
}
System.out.println("Score: User=" + userScore
+ " Computer=" + compScore);
System.out.println();
}
// The ten rounds are over...
// Print out the winner of the game.
displayGameWinner(userScore, compScore);
}
/**
* Change move numbers to appropriate string values
* @return The appropriate string values for the moveNumber
*/
static String getMoveName(int moveNumber) {
}
/**
* Read in a move from the user. Valid responses are Rock, Paper or
* Scissors. Prompt until a valid move is entered.
*
* @return The user's move as one of: Rock, Paper, Scissors
*/
static int getUserMove() {
}
/**
* Pick the computer's move at random.
*
* @return The computer's move as one of: Rock, Paper, Scissors
*/
static int getComputerMove() {
}
/**
* Generate a random integer in the range [lowEnd...highEnd].
*
* @param lowEnd the low end of the range of possible numbers.
* @param highEnd the high end of the range of possible numbers.
* @return a random integer in [lowEnd...highEnd]
*/
public static int randomInt(int lowEnd, int highEnd) {
}
/**
* Compare the user's move to the computer's move to determine the winner of
* this round.
*
* @param userMove the user's move.
* @param compMove the computer's move.
* @return 1 if the computer wins. 0 if it is a tie. -1 if the user wins.
*/
static int determineRoundWinner(int userMove,
int compMove) {
}
/**
* Display a message showing the score of the game and declaring the winner.
*
* @param userScore the user's score for the 10 rounds.
* @param compScore the computer's score for the 10 rounds.
*/
static void displayGameWinner(int userScore,
int compScore) {
System.out.println("\n\nFinal Score:");
System.out.println(" User=" + userScore
+ " Computer=" + compScore);
System.out.println();
if (userScore > compScore) {
System.out.println("The user wins!");
} else if (compScore > userScore) {
System.out.println("The computer wins!");
} else {
System.out.println("Its a tie, nobody wins.");
}
}
}
1. RockScissorsPaper.java 작성
//********************************************************************
// RockScissorsPaper.java
//
//********************************************************************
package RSP;
import java.util.Scanner;
public class RockScissorsPaper {
public static int HowManyRuns = 5;
public static final int ROCK = 1;
public static final int Scissors = 2;
public static final int Paper = 3;
public static void main(String[] args) {
int userScore = 0;
int compScore = 0;
int userMove, compMove;
int winner;
System.out.println("ROCK - PAPER - SCISSORS");
System.out.println("This game plays "+ HowManyRuns + " rounds.");
// Loop once for each round of the game.
for (int rnd = 1; rnd <= RockScissorsPaper.HowManyRuns; rnd++) {
// Get the moves for this round.
userMove = getUserMove();
compMove = getComputerMove();
System.out.println("Computer's move: " + getMoveName(compMove));
// Determine the winner of this round.
winner = determineRoundWinner(compMove, userMove);
// Print a message and add one to the score of the winner.
if (winner == 1) {
System.out.println(getMoveName(compMove) + " beats "
+ getMoveName(userMove)
+ " the computer wins this round.");
compScore++;
} else if (winner == -1) {
System.out.println(getMoveName(userMove) + " beats "
+ getMoveName(compMove)
+ " the user wins this round.");
userScore++;
} else {
System.out.println(getMoveName(userMove) + " ties "
+ getMoveName(compMove)
+ " nobody wins this round.");
}
System.out.println("Score: User=" + userScore
+ " Computer=" + compScore);
System.out.println();
}
// The ten rounds are over...
// Print out the winner of the game.
displayGameWinner(userScore, compScore);
}
/**
* Change move numbers to appropriate string values
* @return The appropriate string values for the moveNumber
*/
static String getMoveName(int moveNumber) {
if (moveNumber == 1) {
return "Rock";
} else if (moveNumber == 2) {
return "Scissors";
} else {
return "Paper";
}
}
/**
* Read in a move from the user. Valid responses are Rock, Paper or
* Scissors. Prompt until a valid move is entered.
*
* @return The user's move as one of: Rock, Paper, Scissors
*/
static int getUserMove() {
Scanner scan = new Scanner(System.in);
System.out.print("Enter your move [ROCK <1>, SCISSORS <2>, PAPER <3>]: ");
return scan.nextInt();
}
/**
* Pick the computer's move at random.
*
* @return The computer's move as one of: Rock, Paper, Scissors
*/
static int getComputerMove() {
return randomInt(1, 3);
}
/**
* Generate a random integer in the range [lowEnd...highEnd].
*
* @param lowEnd the low end of the range of possible numbers.
* @param highEnd the high end of the range of possible numbers.
* @return a random integer in [lowEnd...highEnd]
*/
public static int randomInt(int lowEnd, int highEnd) {
return (int) (lowEnd + Math.random() * (highEnd-lowEnd+1));
}
/**
* Compare the user's move to the computer's move to determine the winner of
* this round.
*
* @param userMove the user's move.
* @param compMove the computer's move.
* @return 1 if the computer wins. 0 if it is a tie. -1 if the user wins.
*/
static int determineRoundWinner(int compMove,
int userMove) {
if (compMove == userMove) {
return 0;
} else if ((compMove == 1 && userMove == 2) || (compMove == 2 && userMove == 3) || (compMove == 3 && userMove == 1)) {
return 1;
} else {
return -1;
}
}
/**
* Display a message showing the score of the game and declaring the winner.
*
* @param userScore the user's score for the 10 rounds.
* @param compScore the computer's score for the 10 rounds.
*/
static void displayGameWinner(int userScore,
int compScore) {
System.out.println("\n\nFinal Score:");
System.out.println(" User=" + userScore
+ " Computer=" + compScore);
System.out.println();
if (userScore > compScore) {
System.out.println("The user wins!");
} else if (compScore > userScore) {
System.out.println("The computer wins!");
} else {
System.out.println("Its a tie, nobody wins.");
}
}
}
2. 소스 실행 예시