0

我正在尝试构建一个二十一点游戏的控制台应用程序。BlackJackTable : 桌上游戏,有 6 个 BettingSpots。我想我想要一个包含 BettingSpots 的数组。但我在尝试填充 BettingSpot[] 时遇到错误。任何关于如何更好地进行设计的建议将不胜感激。

public abstract class TableGame
{
    // Can have 5-7 bettings spots.  Has a dealer, players, Rules
    public abstract void Rules();
    public abstract BettingSpot[] bettingSpotArray;
    public Dealer dealer = new Dealer();

}

public class BlackJackTable : TableGame
{

    // A blackjack tablegame "has a" 6 BettingSpots available that a Player chooses to occupy.  Have the Game ask the player which bettingspots they'd
    // like to place a bet on.  Bet amount must be the same for each.  Use a try catch block to make sure TotalChips >= all bets. 
    public BlackJackTable(int tableNumber)
    {
        _tableNumber = tableNumber;
    }

    public override void BlackJackRules()
    {  }

        BettingSpot spot1 = new BettingSpot(1);
        BettingSpot spot2 = new BettingSpot(2);
        BettingSpot spot3 = new BettingSpot(3);
        BettingSpot spot4 = new BettingSpot(4);
        BettingSpot spot5 = new BettingSpot(5);
        BettingSpot spot6 = new BettingSpot(6);

    public override BettingSpot[] bettingSpotArray = new BettingSpot[5];

    for (int i = 0; i < bettingSpotArray.Length; i++)
    {
        bettingSpotArray[i] = new BettingSpot[i+1];
    }

    public void QueryPlayerForBettingSpots(BettingSpot[] bettingSpotArray)
    {
        int[] BettingSpotsAvailable = new BettingSpot[5];
        for (int idx = 0; idx < 5; idx++)
            if (bettingSpotArray[idx] == 0)
                BettingSpotsAvailable[idx] 

        Console.WriteLine("Place up to 3 bets on the following available BettingSpots: {0}", bettingSpotArray.Where<BettingSpot.
    }

}

public class BettingSpot
{
    protected decimal bet = 0;
    public int _bettingSpotNumber;
    // How many spots are on the Blackjack table will determine how many seats there will be. There are six betting spots allowed, 
    // so six bettingspots are created. THere are just 6 BettingSpots available and a player can
    // occupy up to 3 BettingSpots at a time.  A bettingspot "has a" bet.  If no bet, put '0' in BettingSpotArray

    public BettingSpot(int number)
    {
        _bettingSpotNumber = number;
    }

    public Player player



    public decimal Bet
    {
        get
        {
            return bet;
        }
        set
        {
            bet = value;

    }

}
4

1 回答 1

0

我在您的代码中注意到了一些事情。

首先,我相信您在尝试填充时遇到了麻烦,bettingSpotArray[]因为您试图在课程主体内进行填充。您需要在方法或属性的主体内执行此操作 - 我建议使用构造函数或Rules()方法。

其次,由于您没有在抽象类中定义任何代码,您可能会发现让抽象类成为接口并让您的游戏类实现该接口更容易——这样类就会知道它必须实现什么,但是您不必覆盖所有内容。如果您对大多数游戏类都有某种默认逻辑,那么我同意使用具有该基本逻辑的抽象类,并且“特殊”类将根据需要覆盖它。但这只是我的看法;您可能有自己的正当理由来使用在您的问题中并不明显的抽象类。

我不确定您发布的代码是否会真正编译,但我会提供一些代码示例,它们应该可以帮助您到达您想要去的地方。

public class BlackJackTable : TableGame
{

    // If other classes need access to this I'd set it up as public
    // property, not a public field.  If not, I'd set the field to
    // private
    public override BettingSpot[] bettingSpotArray = new BettingSpot[5];

    public BlackJackTable(int tableNumber)
    {

        // I don't see a _tableNumber field in your abstract class or your 
        // inheriting class - if you don't have that field you'll get an error
        // in the compiler
        _tableNumber = tableNumber;
    }

    // Your posted code had public override void BlackJackRules, but there is
    // no BlackJackRules() method to override
    public override void Rules()
    {  

        // These are superfulous since you'll be using an array
        // for the betting spots
        //BettingSpot spot1 = new BettingSpot(1);
        //BettingSpot spot2 = new BettingSpot(2);
        //BettingSpot spot3 = new BettingSpot(3);
        //BettingSpot spot4 = new BettingSpot(4);
        //BettingSpot spot5 = new BettingSpot(5);
        //BettingSpot spot6 = new BettingSpot(6);

        // Now you can initialize your array
        for (int i = 0; i < bettingSpotArray.Length; i++)
        {
             bettingSpotArray[i] = new BettingSpot[i+1];
        }
    }
}

QueryPlayerForBettingSpots也没有多大意义,因为它提示用户输入,但不接受任何输入。也许你还没有完成那个方法?

我认为您的总体方法是正确的方法,但是您的实现存在一些问题(我在上面已经提到过)。希望这将对您在项目中取得进展有所帮助。

于 2013-06-27T07:20:30.720 回答