【问题标题】:C# Trouble wiith Blackjack Game二十一点游戏的 C# 问题
【发布时间】:2016-06-21 22:44:32
【问题描述】:

计数器变量有问题。每次我离开每个方法时,count都会重新初始化为0。我还没有完成Stay()方法Hit()方法正在杀死。每次命中后,我需要显示所有用户的卡片。我不知道该怎么做。必须有更有效的方法。我认为我所有的问题都源于 Count 变量的问题。

感谢您的帮助。 下面是我的主要课程。下面是我的甲板课。 我遗漏了 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)
    }

}
}

【问题讨论】:

    标签: c# blackjack


    【解决方案1】:

    看看这段代码

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

    您正在传递count副本,并增加该副本。您传入的原始值永远不会受到影响。直截了当的修复包括

    • 通过引用ref int count 计数。
    • 在 Main() 中将 count 设为静态类字段而不是局部变量

    更好的方法是将您的逻辑封装在一个类中,并使count 成为该类的字段或属性,以便类方法可以查看和更改它。

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2015-08-02
      • 2011-02-04
      • 2020-05-18
      • 1970-01-01
      • 2014-10-22
      • 2022-11-12
      • 2015-06-02
      • 1970-01-01
      相关资源
      最近更新 更多