나는 블랙 잭 게임을 시뮬레이트하기 위해 주사위를 사용하는 자바 프로젝트를하고 있지만 잘못된 코드를 작성한 지역에 충돌했습니다. 선수 점수가 자신보다 높으면 딜러에게 행동 시뮬레이션을하려고합니다. 또한 내 checkWinner()
메서드가 잘못된 시간에 활성화 될 것으로 보인다. 나는 누구에게 그것을 고치라고 부탁하는 것이 아니라 오히려 코드가 잘못된 곳을 말해 준다.주사위를 사용하는 블랙 잭 게임
public class BlackJack {
static PairOfDice cards = new PairOfDice();
static Scanner scan = new Scanner(System. in);
static int playerScore, dealerScore, player, dealer, tempScore;
static boolean newGame;
static String play, hit;
public static void main(String[] args) {
newGame();
dealerRoll();
playerRoll();
// checkWinner();
}
public static void newGame() {
System.out.println("Would you like to play blackjack? y or n?");
play = scan.nextLine();
if (play == "y"); {
newGame = true;
}
if (play != "y") newGame = false;
}
public static void dealerRoll() {
while (newGame) {
cards.rollDice();
}
dealerScore = (cards.rollDice());
System.out.println("Dealer score is: " + dealerScore);
if (dealerScore <= 15 && playerScore > dealerScore) tempScore = cards.rollDice();
System.out.println("The dice roll was: " + tempScore);
dealerScore += tempScore;
System.out.println("Dealer score is: " + dealerScore);
}
public static void playerRoll() {
while (newGame) {
cards.rollDice();
}
playerScore = (cards.rollDice());
System.out.println("You total is " + playerScore);
while (playerScore < 21 || playerScore < dealerScore) {
System.out.println("Would you like to take a hit? y or n?");
hit = scan.nextLine();
if (hit.equals("y")) {
tempScore = cards.rollDice();
System.out.println("The dice roll was: " + tempScore);
playerScore += tempScore;
System.out.println("Your score is now: " + playerScore);
}
else if (hit.equals("n")); {
checkWinner();
}
}
}
public static void checkWinner() {
if (playerScore == 21) System.out.println("You win!");
else if (dealerScore == 21) System.out.println("Sorry, Dealer has won!");
else if (dealerScore > playerScore) System.out.println("Sorry, Dealer has won!");
else if (playerScore > dealer) System.out.println("You win!");
else if (playerScore > 21) System.out.println("Sorry you busted, Dealer has won!");
else if (dealerScore > 21) System.out.println("Dealer has busted, You win!");
}
}
모든'(newGame) {...}이'루프에 대한 방법이 없기 때문에'newGame'에 해당하는 경우 영원히 실행하려고하는 동안 'newGame' 루프 내에서 거짓이 될합니다. –
예 저는 방금 코드를 게시 할 때 참조 대신 문자열을 비교하도록 변경했습니다. –
나는 단지'cards.rollDice()'라고 두 번 눌러 보았다. – cHao