2016-03-08 315 views
0

我遇到了與計數器變量麻煩。每次我離開每種方法,計數重新初始化爲0.我還沒有完成Stay()方法Hit()方法正在殺死。每次擊中後,我需要顯示所有用戶的卡片。我不知道該怎麼做。必須有更有效的方法。我認爲我所有的問題都來自Count變量的問題。C#麻煩與大酒杯遊戲

感謝您的任何幫助。 以下是我的主要課程。下面是我的Deck課程。 我忽略了Card,Suit和Rank類。

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

namespace BlkJack 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     string name; 
     int Chips = 500; 
     int Wage; 
     int count = 0; 
     string HS; 
     Card pCard1, dCard1, pCard2, dCard2; 
     int playerHand, dealerHand; 
     //int chipsWon; 

     Console.WriteLine("Welcome to Johnny's BlackJack!"); 
     Console.WriteLine("You are given $500 to play with."); 
     Console.WriteLine("Table Limit is $250 per hand. <Enter a 0 to quit>"); 
     name = GetName("Enter name: "); 
     Console.WriteLine("Hello {0}, you are ${1} ahead.", name, Chips); 
     Wage = getWager("What is your wager?: ", 1, 250, "?Value must be an integer.", "Max bet is 250!"); 

     Deck d = new Deck(); 


     startingHand(count, d, out pCard1, out dCard1, out pCard2, out dCard2, out playerHand, out dealerHand); 
     Console.WriteLine("Your hand: {0}, {1} <{2}> ", pCard1, pCard2, playerHand); 
     Console.WriteLine("<Dealer's show card is {0}>", dCard1); 
     count = count + 4; 
     HS = HitorStay("Do you want to <H>it or <S>tay?: ", count, playerHand, d); 
     while (HS == "H" || HS == "HIT") 
     { 
      HS = HitorStay("Do you want to <H>it or <S>tay?: ", count, playerHand, d); 
      Console.WriteLine("Your cards: {0} {1} <{2}>", pCard1, pCard2, playerHand); 
      //Console.WriteLine("{0}", count); 
     } 






    } 


    static string GetString(string prompt, string[] valid, string error) 
    { 
     string response; 
     bool OK = false; 

     do 
     { 
      Console.Write(prompt); 
      response = Console.ReadLine().ToUpper(); 
      foreach (string s in valid) if (response == s) OK = true; 
      if (!OK) Console.WriteLine(error); 
     } 

     while (!OK); 
     return response; 

    } 

    static string GetName(string prompt) 
    { 
     string response; 

     Console.Write(prompt); 
     response = Console.ReadLine(); 

     while (response == "0") 
     { 
      Environment.Exit(0); 
     } 

     return response; 

    } 


    static bool GetYesNo(string prompt) 
    { 
     string[] valid = { "YES", "Y", "NO", "N" }; 
     string ans = GetString(prompt, valid, "Invalid response. Please reenter."); 
     return (ans == "YES" || ans == "Y"); 
    } 

    static int getWager(string prompt, int low, int high, string errorInt, string errorRange) 
    { 

     int Wager; 
     string userInput; 
     bool OKInt = false, OKRange = false; 

     do 
     { 
      Console.Write(prompt); 
      userInput = Console.ReadLine(); 
      OKInt = Int32.TryParse(userInput, out Wager); 
      if (OKInt) 
      { 
       OKRange = low <= Wager && Wager <= high; 
       if (!OKRange) Console.WriteLine(errorRange); 
      } 
      else 
       Console.WriteLine(errorInt); 
     } 
     while (!OKInt || !OKRange); 
     return Wager; 

    } 

    public static int startingHand(int count, Deck d, out Card pCard1, out Card dCard1, out Card pCard2, out Card dCard2, out int playerHand, out int dealerHand) 
    { 
     playerHand = 0; dealerHand = 0; 

     if (count == 0 || count >= 42) d.Shuffle(); 

     for (int i = 0; i < 52; i++) 
      Console.Write("{0},", d.GetCard(i)); 

     pCard1 = d.GetCard(count); 
     count++; 
     dCard1 = d.GetCard(count); 
     count++; 
     pCard2 = d.GetCard(count); 
     count++; 
     dCard2 = d.GetCard(count); 
     count++; 

     playerHand = pCard1.GetValue() + pCard2.GetValue(); 
     dealerHand = dCard1.GetValue() + dCard2.GetValue(); 

     return count; 

    } 

    static string HitorStay(string prompt, int count, int playerHand, Deck d) 
    { 
     string[] valid = { "HIT", "H", "STAY", "S" }; 
     string HS = GetString(prompt, valid, "?Invalid Response. (H)it or (S)tay?"); 
     if (HS == "HIT" || HS == "H") 
     { 
      Hit(count, playerHand, d); 
     } 
     //else if (HS == "STAY" || HS == "S") 
     //{ 
      //Stay(count, playerHand, dealerHand, out chipStay); 
     //} 
     else Environment.Exit(0); 
     return HS; 
    } 

    public static int Hit(int count, int playerHand, Deck d) 
    { 
     count += 1; 
     playerHand += d.GetCard(count).GetValue(); 


     return playerHand; 
    } 

} 
} 

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

namespace BlkJack 
{ 
    class Deck 
     { 
     private Card[] cards = new Card[52]; 

     public Deck() 
     { 
     for (int suitVal=0; suitVal<4; suitVal++) 
     { 
      for (int rankVal = 0; rankVal < 13; rankVal++) 
      { 
       cards[suitVal * 13 + rankVal] = 
        new Card((Suit)suitVal, (Rank)(rankVal)); 
       cards[suitVal * 13 + rankVal].SetValue(rankVal); 

       if (rankVal > 9) cards[suitVal * 13 + rankVal].SetValue(10); 
       if (rankVal == 1) cards[suitVal * 13 + rankVal].SetValue(11); 
       if (rankVal == 0) cards[suitVal * 13 + rankVal].SetValue(10); 

      } 
     } 
    } 

    public Card GetCard(int cardNum) 
    { 
     return cards[cardNum]; 
    } 


    public void Shuffle() 
    { 
     Card[] newDeck = new Card[52]; // cards randomly assigned to locs in newDeck 
     bool[] assigned = new bool[52]; // keep track of what locs used in newDeck 
     int seed = 0; 
     Console.Write("Enter seed: "); 
     seed = Convert.ToInt32(Console.ReadLine()); // yes, stupid user can break 
     Random rGen = new Random(seed); 
     for (int i=0; i<52; i++) 
     { 
      int destCard = 0; // where card is going to be put 
      bool foundCard = false; 
      while (foundCard == false) 
      { 
       destCard = rGen.Next(52); 
       if (assigned[destCard] == false) 
        foundCard = true; 
      } 
      assigned[destCard] = true; 
      newDeck[destCard] = cards[i]; 
     } 
     newDeck.CopyTo(cards, 0); //.CopyTo(destination, start index) 
    } 

} 
} 

回答

0

看看這段代碼

public static int Hit(int count, int playerHand, Deck d) 
{ 
    count += 1; 

您傳遞副本count,並增加該副本。您傳入的原始值不會受到影響。直截了當的修復包括:

  • 通過引用計數ref int count
  • 製作算靜態類領域,而不是在main(局部變量)

一個更好的辦法是自己的邏輯封裝在一個類中,使count這個類的字段或屬性,從而使類方法可以看到並更改它。