I am making a card dealer class, a player class, a card class and a blackjack/main class. These will, in my current project, come together to make a blackjack game. However, I have hit a problem. Basically, I have a method that deals the cards. The method basically uses the deck of shuffled cards and 'pops' one card at a time out and stores them in a hand. Currently, I have added functionality to specify how many cards to deal in total, and the method deal alternately to player and dealer (1 to dealer, 1 to player, 1 to dealer etc). The cards are then stored in a stack. Each stack represents a hand. I basically have a hand field (and properties) in the player class. Dealer then inherits from the player class. Therefore, Dealer automatically has it's own hand field also.
I am, from the dealer method in the dealer class, trying to assign 1 card to the dealer using the 'this' keyword (intellisense confirms that the 'this' keyword does indeed reference the current instance of the deal class) and then the next card to the hand field in the player class using the 'base' to keyword (which does reference the player instance according to intellisense). Everything seems fine. However, when I run the code, I find that every single card is being assigned to the dealer class's inherited hand field, and none of the cards are being assigned to the player class's hand field. Effectively, the 'base' keyword is acting like the 'this' keyword for some reason. I have a feeling it is something to do with how I have handled the contructors of both the classes, but,being quite new c#, I haven't quite got the knowledge to know the exact cause of the problem. Any help would be greatly appreciated! Below are the Dealer and Player classes:
Dealer
namespace Games
{
//GOOD
public class Dealer:Player
{
//Fields
private static Stack<Card> shuffledCardDeck;
private static Card[] unshuffledCardDeck; //get built deck from constructor. The constructor is called in the
// black jackclass and gets the deck from the Card class's buildDeck method
//properties
public Stack<Card> getSetShuffledDeck
{
get { return shuffledCardDeck; }
}
//Constructors
public Dealer(Card [] cards) :base()
{
shuffledCardDeck = null;
unshuffledCardDeck = cards;
}
//Methods
public static void shuffleCards() //shuffles deck and places the cards in a stack which is stored in the shuffledDeck field
{
Random ran = new Random();
for (int i = 0; i < unshuffledCardDeck.Length; i++)
{
int j = ran.Next(unshuffledCardDeck.Length - 1);
Card temp = unshuffledCardDeck[i];
unshuffledCardDeck[i] = unshuffledCardDeck[j];
unshuffledCardDeck[j] = temp;
}
shuffledCardDeck = new Stack<Card>();
foreach (Card cardArrayElements in unshuffledCardDeck)
{
shuffledCardDeck.Push(cardArrayElements);
}
}
public void dealCards(int totalNumberToDeal)//problem method - should put cards from shuffledDeck in the hand fields of the Dealer and Player class alternatively.
//All cards are put into the Dealer class's hand inherited hand field though.
{
int i =0;
int j = 0;
while (i < totalNumberToDeal)
{
if (j % 2 != 0)
{
this.setHand = shuffledCardDeck.Pop(); //set hand is the property in the Player class for adding cards to the hand field
}
else //BOTH these statements add cards to the dealer's inherited hand field. The second statement does the same as the first???
{
base.setHand = shuffledCardDeck.Pop();
}
j++;
i++;
}
}
}
}
Player
namespace Games
{
public class Player:IBlackJack
{
//GOOD
//Fields
private Stack<Card> hand;//Field where hands are stored
//Properties
public Stack<Card> getHand
{
get {return hand;}
}
public int getSetBet
{
get;
set;
}
public int getSetBalance
{
get;
set;
}
public int getSetTotals
{
get;
set;
}
public bool IsDoubled
{
get;
set;
}
public Card setHand{ //sets hand by receiving cards to 'push' into hand field
set { hand.Push(value); }
}
//Constructors
public Player() { hand = new Stack<Card>(); }//run when instance of Dealer class is created as dealer doesn't have a balance and doesn't bet
public Player(int defaultBet,int currentBalance)//default bet can be thought to be the common bet a player makes
{
getSetBet = defaultBet;
getSetBalance = currentBalance;
hand = new Stack<Card>();
}
//Methods
public void playCards(TextBox cardListTextBox, int numberOfCards)
{
int i = 0;//controls number of cards to be played
foreach (Card c in hand)
{
if (i == numberOfCards) break;
cardListTextBox.Text += String.Format("{0}\r\n", c.Name); //Cards are, at the moment, still kept in the hand after they have been played
i++;
}
int j = 0;//controls number of cards to be removed from stack (as they have been played in the previous statement)
while (j < numberOfCards)
{
hand.Pop();
i++;
}
}
}
}
Here is the blackjack class also:
namespace Games
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Blackjack : Window
{
//Fields
private Dealer dealer;
private Player player;
public Blackjack()
{
InitializeComponent();
pbust.Content = String.Empty;
dbust.Content = String.Empty;
//new dealer and pass a deck of unshuffled cards
dealer = new Dealer(Card.BuildDeck());
//shuffle cards
Dealer.shuffleCards();
//new player
player = new Player(10, 100);
player.IsDoubled = false;
currentBet_txt.Text = String.Format("£{0}", player.getSetBet);
currentBalance_txt.Text = String.Format("£{0}", player.getSetBalance);
//Stack<Card> f = dealer.getShuffledDeck;
//foreach (Card c in f)
//{
// dealerHand.Text += String.Format("{0}\r\n", c.Name);
//}
}
//GOOD
public int getTotals(Stack<Card> hands)
{
int total = 0;
foreach (Card c in hands)
{
if ((c.Value == 1))//check for aces, as they can equal 1 or 11
{
total += c.Value;//increment by 1
if (total <= 11)//if total is now 11 or less, it means we could have added 11 to total rather than 1...
{
total += 10;//...therefore, add 10 to ensure overall increase is 11
c.Value = 11;//set value of ace to 11 to maintain correct card object for any particular game
}
}
else
{
total += c.Value;
}
}
return total;
}
//GOOD
public void doubleBet(object sender, RoutedEventArgs e)
{
player.getSetBet *= 2;
}
//GOOD
private void increaseBet_Click(object sender, RoutedEventArgs e)
{
if (player.getSetBet >= 100)
{
player.getSetBet = 100;
}
else
{
currentBet_txt.Text = String.Format("£{0}", ++player.getSetBet);
}
}
//GOOD
private void decreaseBet_Click(object sender, RoutedEventArgs e)
{
if (player.getSetBet <= 1)
{
player.getSetBet = 1;
}
else
{
currentBet_txt.Text = String.Format("£{0}", --player.getSetBet);
}
}
private void deal_btn_Click(object sender, RoutedEventArgs e)
{
deal_btn.IsEnabled = false;
dealer.dealCards(4);
dealerHand_txt.Text = String.Format("{0}", dealer.getHand.Count);
//player.playCards(playerHand_txt, 2); //This method, implemented in the player class to play the cards, doesn't function properly because the hand field is 'an
//empty stack', due to the fact that the cards
// are not being added to the Player instance's hand field from the deal the Dealer class
// dealer.playCards(dealerHand_txt, 1)
}
}
}
Thanks a LOT for your time. I really appreciate it.

New Topic/Question
Reply
MultiQuote







|