2

I'm struggling on a blackjack console app and need help designing/relating my Seat class.

For the game, there are six seats and a seat can only have one player but a player can occupy (or bet) on up to three seats if the seat is unoccupied. Also, the TableGame has six seats available.

Regarding the TableGame class, it should have six seats. So should I construct the six seats in the TableGame constructor? What about creating a public property i.e. SeatsAvailable that uses an array to catch which seats are available?

Also, a player occupies a seat, and places a bet on it. How should I go about coupling these guys together so that it's tied to the player making the bet?

Advice on how to couple the Seat class with the TableGame, Player class would be greatly appreciated as well as any critiques, direction I should take note of. Thank you in advance for helping a beginner learn the ropes of OO design.

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; }

}

0 Answers0