2010-12-03 6 views
-1

私はブラックジャックプログラム用のコードで遊んでいますが、実際のブラックジャックのようにエースを1または11にする方法を見つけるのが難しいです。これは宿題ではありません。私はちょうどプログラミングに戻っています。誰でも正しい方向に向けることができますか? `class Program { static string [] playerCards = new string [11];1または11を表すAce値を取得する

static string hitOrStay = ""; 

    static int total = 0, count = 1, dealerTotal = 0; 

    static Random cardRandomizer = new Random(); 

    static string name = ""; 

    static void Main(string[] args) 
    { 
     using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
     { 
      Console.Title = "Blackjack"; 
      synth.Speak("Welcome to johns Talking Blackjack Simulator. My name is Alice and I'll be your Dealer. Whom am I speaking with?"); 
      name = Console.ReadLine(); 
      synth.Speak("Thank You"); 
      synth.Speak(name); 
      synth.Speak("lets get started."); 
      Console.Clear(); 

     } 

     Start(); 


    } 



    static void Start() 
    { 

     dealerTotal = cardRandomizer.Next(15, 22); 

     playerCards[0] = Deal(); 

     playerCards[1] = Deal(); 

     do 
     { 

      Console.WriteLine("You were dealed a " + playerCards[0] + " and a " + playerCards[1] + ". \nYour total is " + total + ".\nWould you like to hit or stay? h for hit s for stay."); 

      hitOrStay = Console.ReadLine().ToLower(); 

     } while (!hitOrStay.Equals("h") && !hitOrStay.Equals("s")); 

     Game(); 

    } 



    static void Game() 
    { 

     if (hitOrStay.Equals("h")) 
     { 
      using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
      { 
       synth.Speak(name + "takes a card"); 
      } 
      Hit(); 

     } 

     else if (hitOrStay.Equals("s")) 
     { 

      if (total > dealerTotal && total <= 21) 
      { 

       Console.WriteLine("\nCongrats! You won the game! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); 
       using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
       { 
        synth.Speak("Congratulations! You Win!"); 
       } 
       PlayAgain(); 

      } 

      else if (total < dealerTotal) 
      { 

       Console.WriteLine("\nSorry, you lost! The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); 
       using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
       { 
        synth.Speak("Sorry, you lost! Maybe you should try the slot machines?"); 
       } 
       PlayAgain(); 

      } 



     } 

     Console.ReadLine(); 

    } 



    static string Deal() 
    { 

     string Card = ""; 

     int cards = cardRandomizer.Next(1, 14); 

     switch (cards) 
     { 

      case 1: Card = "Two"; total += 2; 

       break; 

      case 2: Card = "Three"; total += 3; 

       break; 

      case 3: Card = "Four"; total += 4; 

       break; 

      case 4: Card = "Five"; total += 5; 

       break; 

      case 5: Card = "Six"; total += 6; 

       break; 

      case 6: Card = "Seven"; total += 7; 

       break; 

      case 7: Card = "Eight"; total += 8; 

       break; 

      case 8: Card = "Nine"; total += 9; 

       break; 

      case 9: Card = "Ten"; total += 10; 

       break; 

      case 10: Card = "Jack"; total += 10; 

       break; 

      case 11: Card = "Queen"; total += 10; 

       break; 

      case 12: Card = "King"; total += 10; 

       break; 

      case 13: Card = "Ace"; total += 11; 

       break; 

      default: Card = "2"; total += 2; 

       break; 

     } 

     return Card; 

    } 


    static void Hit() 
    { 

     count += 1; 

     playerCards[count] = Deal(); 

     Console.WriteLine("\nYou were dealed a(n) " + playerCards[count] + ".\nYour new total is " + total + "."); 

     if (total == 21) 
     { 

      Console.WriteLine("\nYou got Blackjack! The dealer's total was " + dealerTotal + ".\nWould you like to play again?"); 
      using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
      { 
       synth.Speak("Congradulations! Blackjack!"); 
      } 
      PlayAgain(); 

     } 

     else if (total > 21) 
     { 

      Console.WriteLine("\nYou busted. Sorry. The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); 
      using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
      { 
       synth.Speak("Ouch! Busted! Try your luck again?"); 
      } 
      PlayAgain(); 

     } 

     else if (total < 21) 
     { 

      do 
      { 
       using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
       { 
        synth.Speak("Would you like a card?"); 
       } 

       Console.WriteLine("\nWould you like to hit or stay? h for hit s for stay"); 

       hitOrStay = Console.ReadLine().ToLower(); 

      } while (!hitOrStay.Equals("h") && !hitOrStay.Equals("s")); 

      Game(); 

     } 

    } 



    static void PlayAgain() 
    { 

     string playAgain = ""; 

     do 
     { 

      playAgain = Console.ReadLine().ToLower(); 

     } while (!playAgain.Equals("y") && !playAgain.Equals("n")); 

     if (playAgain.Equals("y")) 
     { 

      Console.WriteLine("\nPress enter to restart the game!"); 

      Console.ReadLine(); 

      Console.Clear(); 

      dealerTotal = 0; 

      count = 1; 

      total = 0; 

      Start(); 

     } 

     else if (playAgain.Equals("n")) 
     { 
      using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
      { 
       synth.Speak("\nPress enter to close Black jack." + dealerTotal); 
      } 

      ConsoleKeyInfo info = Console.ReadKey(); 
      if (info.Key == ConsoleKey.Enter) 
      { 

       Environment.Exit(0); 
      } 
      else 
      { 

       Console.Read(); 
       Environment.Exit(0); 
      } 
     } 
    } 
} 

}

はこれにコードの一部を変更したが、清浄な溶液は、その後、カードの所与のセットに対する全ての可能な合計を計算することであろうように、依然として問題

else if (total > 21) 
     { 

      if (playerCards[0] == "Ace") 
      { 

       total -= 10; 
      } 
      if (playerCards[1] == "Ace") 
      { 

       total -= 10; 
      } 
      do 
      { 
      using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
       { 
        synth.Speak("Would you like a card?"); 
       } 

       Console.WriteLine("\nWould you like to hit or stay? h for hit s for stay"); 

       hitOrStay = Console.ReadLine().ToLower(); 

       if (total > 21) 
        Console.WriteLine("\nYou busted. Sorry. The dealer's total was " + dealerTotal + ".\nWould you like to play again? y/n"); 
       using (SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer()) 
       { 

        synth.Speak("Ouch! Busted! Try your luck again?"); 
       } 
       PlayAgain(); 

答えて

0

らしいです必要に応じて、各ユーザーアクションに適したものを選択します。たとえば、ユーザーが滞在している場合、9-Aは10でなく20になります。ユーザーが9-AAに到着した後、11または30でなく21になります。

ここにいくつかあります1つのゲームの擬似コード:

Deal out cards; show both of the player's cards and one of the dealer's cards 
Determine all possible scores for player's cards 
If (One of the scores is 21) 
    Goto Dealer's Turn (this is a blackjack) 
Else 
    While (Hitting) 
     Add one card to player's hand 
     Determine all possible scores for player's cards 
     If minimum score > 21 
      Goto Dealer's Turn (this is a bust) 
     End If 
    End While  
End If 

Dealer's Turn 
    Determine highest score for player that's 21 or lower 
    Play Out Dealer's Hand 
    Determine Winner  
+0

ニース!今私がこの仕事をすることができるかどうか見てみましょう。ありがとう! –

+0

私は新しいアプローチを試しました。このコードの一部を変更しました –

+0

書き直しを提案しますか?これは小さなプログラムであり、私が設計しているようにエースのロジックを構築できるということです。 –

関連する問題