반응형
반응형
룰렛 이란?
룰렛은 카지x 게임 중 하나로, 돌아가는 원반이 있는 테이블 위에 공을 던져서 공이 어느 숫자나 색상에 머무르는지를 맞추는 게임이다.
JAVA 코드이다.
import java.util.*;
public class Roulette {
private static final String[] COLORS = {"Red", "Black"};
private static final int[] NUMBERS = {
0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36,
11, 30, 8, 23, 10, 5, 24, 16, 33, 1, 20, 14, 31, 9, 22,
18, 29, 7, 28, 12, 35, 3, 26
};
private static final Map<Integer, String> NUMBER_COLORS;
static {
NUMBER_COLORS = new HashMap<>();
NUMBER_COLORS.put(0, "Green");
for (int i = 1; i < NUMBERS.length; i++) {
int number = NUMBERS[i];
String color = COLORS[i % 2];
NUMBER_COLORS.put(number, color);
}
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Welcome to Roulette!");
System.out.println("The numbers are from 0 to 36, and are colored as follows:");
for (int number : NUMBERS) {
String color = NUMBER_COLORS.get(number);
System.out.printf("%d: %s\n", number, color);
}
System.out.println();
System.out.print("Enter the amount of money you want to bet: ");
int betAmount = scanner.nextInt();
scanner.nextLine();
System.out.print("Do you want to bet on a number or a color? (number/color): ");
String betType = scanner.nextLine();
if (betType.equalsIgnoreCase("number")) {
System.out.print("Enter the number you want to bet on: ");
int number = scanner.nextInt();
scanner.nextLine();
if (number < 0 || number > 36) {
System.out.println("Invalid number.");
return;
}
playNumberBet(betAmount, number);
} else if (betType.equalsIgnoreCase("color")) {
System.out.print("Enter the color you want to bet on (Red/Black/Green): ");
String color = scanner.nextLine();
if (!Arrays.asList(COLORS).contains(color)) {
System.out.println("Invalid color.");
return;
}
playColorBet(betAmount, color);
} else {
System.out.println("Invalid bet type.");
return;
}
}
private static void playNumberBet(int betAmount, int number) {
int payout = NUMBER_COLORS.containsKey(number) ? 35 : 0;
playBet(betAmount, payout, "the number " + number);
}
private static void playColorBet(int betAmount, String color) {
int payout = 2;
playBet(betAmount, payout, color);
}
private static void playBet(int betAmount, int payout, String betName) {
int randomNumber = getRandomNumber();
String resultColor = NUMBER_COLORS.get(randomNumber);
System.out.printf("The ball lands on %d (%s).\n", randomNumber, resultColor);
if (betName.equalsIgnoreCase(resultColor)) {
int winnings = betAmount * payout;
System.out.printf("Congratulations! You win $%d.\n", winnings);
} else {
System.out.println("Sorry, you lose.");
}
}
private static int getRandomNumber() {
Random random = new Random();
return NUMBERS[random.nextInt(NUMBERS.length)];
}
}
실행결과를 봐보자 !
간단한 설명 !
1.사용자에게 베팅할 금액과 베팅 유형을 입력하도록 요청을 한다.
2.사용자가 숫자에 베팅하려는 경우, 베팅 금액과 해당 숫자를 입력하도록 요청하고 해당 숫자가 0~36 사이에 있는지 확인을 하고, 해당 숫자가 나오면 베팅 금액에 35를 곱한 만큼 보상을 합니다.
3.사용자가 색상에 베팅하려는 경우, 베팅 금액과 해당 색상을 입력하도록 요청합니다. 색상이 "Red" 또는 "Black" 중 하나인지 확인하고, 해당 색상이 나오면 베팅 금액에 2를 곱한 만큼 보상합니다.
4.결과를 표시합니다. 사용자가 이긴 경우 보상금을, 그렇지 않은 경우 "Sorry, you lose." 메시지를 표시합니다.
.
반응형
'JAVA' 카테고리의 다른 글
[JAVA] InetAddress Class를 사용하여 로컬 머신의 IP 주소와 호스트 이름 가져오기 (0) | 2024.07.18 |
---|---|
[JAVA] NetworkInterface Class로 IP주소와 호스트 명 가져오기 (0) | 2024.07.18 |
[JAVA] 카지노 게임 블랙잭 만들기 (0) | 2024.07.18 |
[JAVA] SimpleDateFormat와 Calendar를 사용하여 원하는 날짜출력. (0) | 2022.08.15 |