2016-10-18 8 views
-1

コーディング教授の紹介は、C#の配列を使用してブラックジャックゲームを作成したかったのです。 「ヒットカード」の価値を当初の合計に加算することに問題があります。複数回ヒットしようとすると、最初の「ヒットカード」が置き換えられ、新しいカードが最初の合計に追加されます。あなたは決してあなたの最初の総変数を更新していないしているように見えますコンソールブラックジャックC#、合計の追加

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace Blackjack_Midterm 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 

     //set up multidementional array for card values 
     string[,] cards = new string[52, 3] 
       { 
        {"Hearts", "Ace", "1"}, 
        {"Diamonds", "Ace", "1"}, 
        {"Clubs", "Ace", "1"}, 
        {"Spades", "Ace", "1"}, 
        {"Hearts", "Two", "2"}, 
        {"Diamonds", "Two", "2"}, 
        {"Clubs", "Two", "2"}, 
        {"Spades", "Two", "2"}, 
        {"Hearts", "Three", "3"}, 
        {"Diamonds", "Three", "3"}, 
        {"Clubs", "Three", "3"}, 
        {"Spades", "Three", "3"}, 
        {"Hearts", "Four", "4"}, 
        {"Diamonds", "Four", "4"}, 
        {"Clubs", "Four", "4"}, 
        {"Spades", "Four", "4"}, 
        {"Hearts", "Five", "5"}, 
        {"Diamonds", "Five", "5"}, 
        {"Clubs", "Five", "5"}, 
        {"Spades", "Five", "5"}, 
        {"Hearts", "Six", "6"}, 
        {"Diamonds", "Six", "6"}, 
        {"Clubs", "Six", "6"}, 
        {"Spades", "Six", "6"}, 
        {"Hearts", "Seven", "7"}, 
        {"Diamonds", "Seven", "7"}, 
        {"Clubs", "Seven", "7"}, 
        {"Spades", "Seven", "7"}, 
        {"Hearts", "Eight", "8"}, 
        {"Diamonds", "Eight", "8"}, 
        {"Clubs", "Eight", "8"}, 
        {"Spades", "Eight", "8"}, 
        {"Hearts", "Nine", "9"}, 
        {"Diamonds", "Nine", "9"}, 
        {"Clubs", "Nine", "9"}, 
        {"Spades", "Nine", "9"}, 
        {"Hearts", "Ten", "10"}, 
        {"Diamonds", "Ten", "10"}, 
        {"Clubs", "Ten", "10"}, 
        {"Spades", "Ten", "10"}, 
        {"Hearts", "Jack", "10"}, 
        {"Diamonds", "Jack", "10"}, 
        {"Clubs", "Jack", "10"}, 
        {"Spades", "Jack", "10"}, 
        {"Hearts", "Queen", "10"}, 
        {"Diamonds", "Queen", "10"}, 
        {"Clubs", "Queen", "10"}, 
        {"Spades", "Queen", "10"}, 
        {"Hearts", "King", "10"}, 
        {"Diamonds", "King", "10"}, 
        {"Clubs", "King", "10"}, 
        {"Spades", "King", "10"}, 
       }; 






     //Title and Rules of game 
     Console.WriteLine("BlackJack with FlapJacks \n"); 
     Console.WriteLine("Rules: \n"); 
     Console.WriteLine("  - You are given two cards"); 
     Console.WriteLine("  - The Dealer is given two cards (One   Face-up, one Face-down)"); 
     Console.WriteLine("  - The object of the game is to get closest to 21 without going over"); 
     Console.WriteLine("      - If you go over 21, it's game over"); 
     Console.WriteLine("      - If you receive 21 on the first two cards, automatic win (applies to both the dealer and player) \n"); 
     Console.WriteLine("Press 'ENTER' to begin \n"); 
     Console.ReadKey(); 

     Boolean Broke = false; 

     int Chips; 
     Chips = 100; 

     while (Chips > 0) 
     { 
      Console.WriteLine("Remaining Chips: {0}", Chips); 
      Console.Write("Place your bet(1-{0}): ", Chips); 
      string bet = Console.ReadLine(); 
      int Bet = Convert.ToInt16(bet); 
      Boolean Stand = false; 

      Console.WriteLine("\n"); 

      //Deal Cards out to player and Dealer 
      Console.WriteLine("Dealing Cards \n"); 
      Thread.Sleep(2000); 

      Console.WriteLine("(Player's Hand) \n"); 

      //Set up a RNG to pick from the created arrays 
      //Set up RNG for each card 
      Random rnd = new Random(); 
      int card1 = rnd.Next(52); 
      int card2 = rnd.Next(52); 
      int card3 = rnd.Next(52); 
      int card4 = rnd.Next(52); 


      Console.WriteLine(cards[card1, 1] + " of " + cards[card1, 0]); 



      card2 = rnd.Next(52); 
      Console.WriteLine(cards[card2, 1] + " of " + cards[card2, 0]); 


      int Value1 = Convert.ToInt16(cards[card1, 2]); 
      int Value2 = Convert.ToInt16(cards[card2, 2]); 
      int Total1 = Value1 + Value2; 
      Console.WriteLine("Total: " + Total1); 


      Console.WriteLine("\n"); 


      Console.WriteLine("(Dealer's Hand) \n"); 
      //Show the first card 
      card3 = rnd.Next(52); 
      Console.WriteLine(cards[card3, 1] + " of " + cards[card3, 0]); 

      //Second Card place holder 
      card4 = rnd.Next(52); 
      Console.WriteLine("[Hidden Card]"); 

      //Convert the values to integers 
      //Add and show the total of the two cards 
      int Value3 = Convert.ToInt16(cards[card3, 2]); 
      int Value4 = Convert.ToInt16(cards[card4, 2]); 
      int Total2 = Value3; 
      Console.WriteLine("Total: " + Total2); 

      Console.WriteLine("\n"); 

      while (Stand == false) 
      { 
       //Ask if Player wants to Hit or Stand 
       Console.Write("Hit(1) or Stand(2)?: "); 
       //Set up user input (string) 
       string GuessAsAString = Console.ReadLine(); 
       //Convert String to an Int 
       int Answer = Convert.ToInt16(GuessAsAString); 
       Console.WriteLine("\n"); 


       if (Answer == 1) 
       { 
        Stand = false; 
        //Continually add on to previous player total until  "stand" or "bust" 
        card2 = rnd.Next(52); 
        Console.WriteLine(cards[card2, 1] + " of " + cards[card2, 0]); 
        int Value5 = Convert.ToInt16(cards [card2, 2]); 
        int Total3 = Total1 + Value5; 
        Console.WriteLine("Total: " + Total3); 
        Console.WriteLine("\n"); 

       } 

       else if (Answer == 2) 
       { 
        Stand = true; 
       } 
      } 

      //Add cards to Dealer's hand until over 17 or bust 
      Console.WriteLine("Dealing Dealer's hand"); 
      Thread.Sleep(1000); 


      Console.WriteLine(cards[card4, 1] + " of " + cards[card4, 0]); 



     } 



     Console.ReadKey(); 














} 
} 
} 

答えて

1

Total1あなたwhile (Stand == false)ループの一部として。代わりに、記述したとおりに正確にやっています:新しいカードの値を初期値に加えるだけです。

スタンドループが繰り返されるたびに、Total3の変数を再宣言しないでください。その宣言をループの外側に移動します。例:

int Total3 = Total1; // Intialise the running total with the inital value from the deal 
while (Stand == false) 
{ 
    ... 

    Total3 = Total3 + Value5; // Add value of the newly dealt card to the running total. 
    Console.WriteLine("Total: " + Total3); 

    ... 
} 

コードの可読性を高めるために、変数の名前を変更することを強くおすすめします。変数Total1Total2などの名前を付けるのは良い方法ではなく、今体験しているような問題をデバッグするのが難しくなります。あなたの変数名が実際に何のために記述されているかを指定します。 Total3という名前の代わりにrunningTotalです。

+0

ありがとうございました!それはそれを修正!また –

+0

また、変数の命名についてのアドバイスをありがとうございます。私はちょうどコードを学び始めているので、命名規則の良いテクニックについてはあまり考えていません。私はそれに取り組むつもりです! –

+0

@ParkerSternberger、それが正しい場合、答えを受け入れることを忘れないでください。 – Adrian

関連する問題