1

ブラックジャック コンソール アプリで苦労しており、Seat クラスの設計/関連付けについてサポートが必要です。

このゲームには 6 つの座席があり、1 つの座席には 1 人のプレーヤーのみが参加できますが、座席が空いている場合、プレーヤーは最大 3 つの座席を占有 (または賭け) できます。また、TableGame には 6 席あります。

TableGame クラスに関しては、6 つのシートが必要です。では、TableGame コンストラクターで 6 つの座席を作成する必要がありますか? 配列を使用して利用可能な座席を取得するパブリック プロパティ、つまり SeatsAvailable を作成するのはどうでしょうか。

また、プレーヤーは座席を占有し、それに賭けます。賭けをしているプレーヤーに結びつくように、これらの人を結合するにはどうすればよいですか?

Seat クラスを TableGame と結合する方法についてのアドバイス、Player クラスは、私が注目すべき批評や方向性と同様に、非常に高く評価されます。初心者がOO設計のコツを学ぶのを手伝ってくれてありがとう。

public class TableGame
{
    public TableGame(IEnumerable<string> playerNames)
    {
        List<Player> players = new List<Player>();
        foreach (string name in playerNames)
        {
            players.Add(new Player(name));
            // add the player to a new instance of a seat or to an array of already created seats(6)???
        }

        Dealer dealer = new Dealer();
        players.Add(dealer);

        PlayGame(players, dealer);
    }

    private void PlayGame(List<Player> players, Dealer dealer)
    {
        try
        {
            Deck playingDeck = new Deck();
            Deal(playingDeck, players);
            Play(playingDeck, players);
            DetermineHandResults(players, dealer);
            TransferBets(players, dealer);
        }
        catch (InvalidTransferAmountException ex)
        {
            players.Remove(ex.OffendingPlayer);
        }

        DisplayResults(players);    // I want this here so that DispayResults() is still called even if an InvalidTransferAmount exception is caught.
    }                               // However other types of exceptions (unhandled) will kick me out of PlayGame() and DisplayResults() will not be
                                    // called.  Use a finally block to execute something no matter what exception is thrown- handled or unhandled.
    public void Deal(Deck deck, List<Player> players)
    {
        deck.Shuffle();
        for (int i = 1; i < 3; i++)
            foreach (Player player in players)
                player.ReceiveCard(deck.Deal());                
    }

    public void Play(Deck playingDeck, List<Player> players)
    {
        foreach (Player player in players)
            player.Strategy(playingDeck);
    }

    public void DetermineHandResults(List<Player> players, Dealer dealer)
    {
        foreach (Player player in players)
        {
            if (player.playerHand.HandValue() > 21)
                player.result = Result.Lose;
            else if (player.playerHand.HandValue() <= 21 && dealer.playerHand.HandValue() > 21)
                player.result = Result.Win;
            else if (player.playerHand.HandValue() <= 21 && player.playerHand.HandValue() > dealer.playerHand.HandValue())
                player.result = Result.Win;
            else if (player.playerHand.HandValue() == dealer.playerHand.HandValue())
                player.result = Result.Draw;
            else
                player.result = Result.Lose;
        }
    }

    public void TransferChips(Player playerTo, Player playerFrom, decimal value)
    {
        if (playerFrom.TotalChips < value)
            throw new InvalidTransferAmountException(string.Format("Can not transfer {0} from {1}'s bank roll, only {2:2d} available!",
                value, playerFrom.Name, playerFrom.TotalChips), playerFrom);
        playerTo.TotalChips += value;
        playerFrom.TotalChips -= value;

    }

    public void TransferBets(List<Player> players, Dealer dealer)
    {
        foreach (Player player in players)
        {

            if (player.result == Result.Win)
            {
                TransferChips(player, dealer, player.Bet);
                Console.WriteLine("{0} wins {1} dollars.", player.Name, player.Bet);
            }
            else if (player.result == Result.Lose)
            {
                //if (player.TotalChips < player.Bet)
                TransferChips(dealer, player, player.Bet);
                Console.WriteLine("{0} loses {1} dollars.", player.Name, player.Bet);
            }
            else
                Console.WriteLine("{0} pushes.", player.Name);
        }
    }

    public void DisplayResults(List<Player> players)
    {
        foreach (Player player in players)
        {
            Console.WriteLine("{0} has {1} and {2}s.  Total chips: {3}", player.Name, player.playerHand.HandValue(), player.result, player.TotalChips);
            player.ShowHandValues();
            Console.WriteLine();

        }
    }
}

public class Seat
{
    public Seat()
    {   }

    public bool Occupied;


}

public class Player 
{
    private string name;
    private decimal bet =10, totalChips = 100;
    public Hand playerHand = new Hand();

    public Player()
    {  }

    public Player(string name)
    {
        this.Name = name;
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public Result result { get; set; }

    public decimal TotalChips
    {
        get
        {
            return totalChips;
        }
        set
        {
            totalChips = value;
        }
    }

    public decimal Bet
    {
        get
        {
            return bet;
        }
    }

    public void ReceiveCard(Card card) 
    { playerHand.Hit(card); }

    public void AskForCard(Deck deck)
    {
        playerHand.Hit(deck.Deal());           
    }

    public virtual void Strategy(Deck deck)
    {
        while (playerHand.HandValue() < 17)
        {
            AskForCard(deck);
            if (playerHand.HandValue() == 21)
                Console.WriteLine("Congratulations {0} you got 21!", Name);
            if (playerHand.HandValue() > 21)
                Console.WriteLine("Sorry {0} you busted.", Name);
        }
    }

    public void ShowHandValues()
    {
        foreach (Card card in playerHand.GetCards())
        {
            Console.Write(card.ToString() + "  ");
        }
    }
}

public class InvalidTransferAmountException : ApplicationException 
{
    public InvalidTransferAmountException(string msg, Player offendingPlayer) : base(msg)
    {
        OffendingPlayer = offendingPlayer;
    }

    public Player OffendingPlayer { get; private set; }

}
4

0 に答える 0