2012-11-03 3 views
2

나는 블랙 잭 게임을 시뮬레이트하기 위해 주사위를 사용하는 자바 프로젝트를하고 있지만 잘못된 코드를 작성한 지역에 충돌했습니다. 선수 점수가 자신보다 높으면 딜러에게 행동 시뮬레이션을하려고합니다. 또한 내 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!"); 
    } 
}​ 
+0

모든'(newGame) {...}이'루프에 대한 방법이 없기 때문에'newGame'에 해당하는 경우 영원히 실행하려고하는 동안 'newGame' 루프 내에서 거짓이 될합니다. –

+0

예 저는 방금 코드를 게시 할 때 참조 대신 문자열을 비교하도록 변경했습니다. –

+0

나는 단지'cards.rollDice()'라고 두 번 눌러 보았다. – cHao

답변

0

플레이어 턴을 완료하면 승자를 확인할 수 없습니다. 딜러가 회전 한 후에해야합니다.

0

코드를 다시 작성하여이를 수정했습니다. 모든 의견을 주시면 감사하겠습니다. 첫

import java.util.*; 


public class BlackJack 
{ 
    static PairOfDice cards = new PairOfDice(); 
    static Scanner scan = new Scanner(System.in); 
    static int playerScore, dealerScore, player, dealer, tempScore, takeHit, stand; 
    private static boolean playGame; 
    static String play= "y", yesPlay; 
    static char hit; 


    public static void main(String[] args) 
    { 
     newGame(); 



    } 

    public static void newGame() 
    { 
     do 
     { 

      playerRoll(); 
      dealerRoll(); 
      stand(); 
      checkWinner(); 
      System.out.println("Would you like to play again? (Y/N)"); 
     } while (scan.next().toLowerCase().charAt(0) == 'y'); 

    } 

    public static int takeHit() 
    { 
     return takeHit = cards.rollDice(); 
    } 

    public static int stand() 
    { 
     return playerScore; 


    } 

    public static void dealerRoll() 
    { 
     while(playGame) 
     { 
      cards.rollDice(); 
     } 

     dealerScore = (cards.rollDice()); 



       do 
       { 
        tempScore = takeHit(); 

       //System.out.println("The dice roll was: " + tempScore); 
       dealerScore += tempScore; 
       System.out.println("Dealer score is: " + dealerScore); 
       }while(dealerScore <= 15 && playerScore > dealerScore); 


    } 

    public static void playerRoll() 
    { 
     while(playGame) 
     { 
      cards.rollDice(); 
     } 

     playerScore = (cards.rollDice()); 

      System.out.println("Your total is " + playerScore); 


      System.out.println("Would you like a hit?(Y/N)"); 
      hit = scan.next().toLowerCase().charAt(0); 

      if(hit == 'y') 
       do 
       { 
        tempScore = takeHit(); 
        //System.out.println("The dice roll was: " + tempScore); 
        System.out.println("The hit gave you : " + tempScore + " Your total is now: " + (playerScore += tempScore)); 
        System.out.println("Would you like a hit?(Y/N)"); 
       }while(scan.next().toLowerCase().charAt(0) == 'y'); 



      if(hit == 'n') 
         stand(); 









    } 

    public static void checkWinner() 
    { 
     if (playerScore > 21) 
      System.out.println("Sorry you busted, Dealer has won!"); 
     else 
     if (dealerScore > 21) 
       System.out.println("Dealer has busted, You win!"); 
     else 
     if (playerScore == 21) 
      System.out.println("You win!"); 
     else 
     if (dealerScore == 21) 
      System.out.println("Sorry, Dealer has won!"); 
     else if (dealerScore > playerScore && dealerScore <= 21) 
      System.out.println("Sorry, Dealer has won!"); 
     else if (playerScore > dealer && playerScore <= 21) 
      System.out.println("You win!"); 





    } 
관련 문제