Skip to content

Instantly share code, notes, and snippets.

@edhedges
Created November 29, 2011 14:29
Show Gist options
  • Save edhedges/1404983 to your computer and use it in GitHub Desktop.
Save edhedges/1404983 to your computer and use it in GitHub Desktop.
Player class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HedgesBlackjack
{
class Player
{
private decimal _money;
private int _score;
private List<Card> _hand = new List<Card>();
public Player(decimal m)
{
_money = m;
}
public decimal Money
{
get
{
return _money;
}
set
{
_money = value;
}
}
public int Score
{
get
{
return _score;
}
set
{
_score = value;
}
}
public List<Card> Hand
{
get
{
return _hand;
}
set
{
_hand = value;
}
}
public void Deal(Card c, bool f)
{
_hand.Add(c);
if (f == false) c.FaceUp = false;
else c.FaceUp = true;
}
public void ReturnHand(Deck d)
{
for (int i = 0; i < _hand.Count; i++)
{
d.AddCard(_hand[i]);
}
_hand.Clear();
}
public int CalcScore()
{
int ace = 0;
int score = 0;
for (int i = 0; i < _hand.Count; i++)
{
if (_hand[i].Value > 9 && _hand[i].Value < 14) score += 10;
else if (_hand[i].Value == 14 && 11 + score < 22)
{
score += 11;
ace++;
}
else if (_hand[i].Value == 14)
{
score += 1;
}
else score += _hand[i].Value;
}
if (score > 21 && (score - 10) < 22 && ace > 0)
{
score -= 10;
}
if (score - 10 > 21) score -= 10;
return score;
}
public override string ToString()
{
StringBuilder buffer = new StringBuilder();
foreach(Card c in _hand)
{
buffer.Append(" " + c.ToString());
}
return buffer.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment