I am learning c# and trying to wrap my head around OOP. This blackjack code is my first attempt. It works well enough, but it is probably terrible. Please let me know what to work on.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
bool playGame = true;
int playerWins = 0;
int dealerWins = 0;
while (playGame)
{
bool hit = true;
bool getResult = true;
int getValues;
Dealer dealer = new Dealer();
Player player = new Player();
while (hit)
{
player.AddCard();
Console.Clear();
if (player.CountValues(out getValues))
{
getResult = false;
++dealerWins;
break;
}
Console.WriteLine("Dealer shows one of his cards:");
dealer.DisplayHand(0);
Console.WriteLine("\nYour hand:");
player.DisplayHand();
Console.WriteLine("\nWhat will you do?");
Console.WriteLine("(1) Hit");
Console.WriteLine("(2) Stay");
hit = userInput();
}
if (getResult)
{
player.CountValues(out getValues);
Console.Clear();
if (dealer.CountValues() >= getValues)
{
Console.WriteLine("Dealer wins");
Console.WriteLine("Dealers hand:");
dealer.DisplayHand();
Console.WriteLine("\nDealer Points: {0}",dealer.CountValues());
Console.WriteLine("\nYour hand:");
player.DisplayHand();
Console.WriteLine("\nPlayer Points: {0}",getValues);
++dealerWins;
}
else
{
Console.WriteLine("Player wins");
Console.WriteLine("Dealers hand:");
dealer.DisplayHand();
Console.WriteLine("\nDealer Points: {0}",dealer.CountValues());
Console.WriteLine("\nYour hand:");
player.DisplayHand();
Console.WriteLine("\nPlayer Points: {0}",getValues);
++playerWins;
}
}
Console.WriteLine("\nDealer has won {0} times and you have won {1} times.",dealerWins,playerWins);
Console.WriteLine("What will you do?");
Console.WriteLine("(1) Play Again");
Console.WriteLine("(2) Quit");
playGame = userInput();
}
}
public static bool userInput()
{
bool validInput = false;
do
{
int number;
string getValue = Console.ReadLine();
bool result = Int32.TryParse(getValue, out number);
if (result)
{
if (number == 1)
{
validInput = true;
return true;
}
else if (number == 2)
{
validInput = true;
return false;
}
}
Console.WriteLine("Choose a valid option.");
}
while (validInput == false);
return false;
}
}
public class Dealer
{
List<Card> DealerHand = new List<Card>();
private int _sumPoints;
public Dealer()
{
DealerHand.Add(new Card());
DealerHand.Add(new Card());
}
public void DisplayHand()
{
foreach (var card in DealerHand)
{
Console.WriteLine(card.Name);
}
}
public void DisplayHand(int CardIndex)
{
Console.WriteLine(DealerHand[CardIndex].Name);
}
public int CountValues()
{
_sumPoints = 0;
foreach (var card in DealerHand)
{
_sumPoints += card.Points;
}
return _sumPoints;
}
}
public class Player
{
List<Card> PlayerHand = new List<Card>();
private int _sumPoints;
public Player()
{
PlayerHand.Add(new Card());
}
public void DisplayHand()
{
foreach (var card in PlayerHand)
{
Console.WriteLine(card.Name);
}
}
public void AddCard()
{
PlayerHand.Add(new Card());
}
public bool CountValues(out int getValues)
{
_sumPoints = 0;
foreach (var card in PlayerHand)
{
_sumPoints += card.Points;
}
if (_sumPoints > 21)
{
foreach (var card in PlayerHand)
{
if (card.Name == "Ace")
{
_sumPoints -= 9;
getValues = _sumPoints;
return false;
}
}
Console.WriteLine("Bust! Your hand value is over 21");
Console.WriteLine("Your hand:");
DisplayHand();
Console.WriteLine("\nvalue: {0}",_sumPoints);
getValues = _sumPoints;
return true;
}
else
{
getValues = _sumPoints;
return false;
}
}
}
public class Card
{
private string _name;
private int _points;
public Card()
{
Random rnd = new Random();
int randomCard = rnd.Next(1,14);
switch (randomCard)
{
case 1:
this._name = "Two";
this._points = 2;
break;
case 2:
this._name = "Three";
this._points = 3;
break;
case 3:
this._name = "Four";
this._points = 4;
break;
case 4:
this._name = "Five";
this._points = 5;
break;
case 5:
this._name = "Six";
this._points = 6;
break;
case 6:
this._name = "Seven";
this._points = 7;
break;
case 7:
this._name = "Eight";
this._points = 8;
break;
case 8:
this._name = "Nine";
this._points = 9;
break;
case 9:
this._name = "Ten";
this._points = 10;
break;
case 10:
this._name = "Jack";
this._points = 10;
break;
case 11:
this._name = "Queen";
this._points = 10;
break;
case 12:
this._name = "King";
this._points = 10;
break;
case 13:
this._name = "Ace";
this._points = 10;
break;
}
System.Threading.Thread.Sleep(100);
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Points
{
get { return _points; }
set { _points = value; }
}
}
case <n>
lines? On the lines withthis.something
. \$\endgroup\$switch
/case
stuff using a dictionary. \$\endgroup\$