My BlackJack Game in C# ConsoleSimple “arrow-throwing” console gameConsole Blackjack gameBasic console...

Typing Amharic inside a math equation?

Why Normality assumption in linear regression

Slow moving projectiles from a hand-held weapon - how do they reach the target?

Would a National Army of mercenaries be a feasible idea?

Find x angle in triangle

Am I a Rude Number?

Quenching swords in dragon blood; why?

Can we use the stored gravitational potential energy of a building to produce power?

What is a jet (unit) shown in Windows 10 calculator?

If I sold a PS4 game I owned the disc for, can I reinstall it digitally?

Can a hotel cancel a confirmed reservation?

How to explain planetary rings pulsating?

How should I handle players who ignore the session zero agreement?

Is casting an attack cantrip from a wand an "attack action made with a magic weapon"?

How do you funnel food off a cutting board?

"Free" Hopf algebra

What is better: yes / no radio, or simple checkbox?

Parsing a string of key-value pairs as a dictionary

Caruana vs Carlsen game 10 (WCC) why not 18...Nxb6?

What is the most triangles you can make from a capital "H" and 3 straight lines?

How would one buy a used TIE Fighter or X-Wing?

cron not executing python3

How does Arcane Armament interact with the Artillerist Artificer's Wand Prototype feature?

Is there a standard way to treat events with unknown times (missing time data)?



My BlackJack Game in C# Console


Simple “arrow-throwing” console gameConsole Blackjack gameBasic console game architectureC# console roulette gamePokemon console gameSingle player console Blackjack gameConsole single player blackjack versus a dealer again2048 console game (C#)My attempt at a Blackjack gameSelf playing console Blackjack game with four players













0












$begingroup$


what do you think of my BlackJack game in regards in Object-Oriented Programming?



My code is at https://github.com/ngaisteve1/BlackJack



using System;
using System.Threading;

public class BlackJackGame
{
private static deckCard deck;
public void Play()
{
bool continuePlay = true;

Console.Title = "Steve BlackJack Game (Version 2)";
Console.Write("Steve BlackJack Game ");
Utility.MakeColor2(" ♠ ",ConsoleColor.White);
Utility.MakeColor2(" ♥ ",ConsoleColor.Red);
Utility.MakeColor2(" ♣ ",ConsoleColor.White);
Utility.MakeColor2(" ♦ ",ConsoleColor.Red);

deck = new deckCard();
Console.Write("nnEnter player's name: ");

// Create player
var player = new Player(Console.ReadLine());

// Create dealer
var dealerComputer = new Player();

while (continuePlay)
{
// Initialize screen and player's certain property - Start
Console.Clear();
player.IsNaturalBlackJack = false;
player.IsBusted = false;
dealerComputer.IsNaturalBlackJack = false;
dealerComputer.IsBusted = false;
// Initialize screen and player's certain property - End

if (deck.GetRemainingDeckCount() < 20)
{
// Get a new shuffled deck.
deck.Initialize();
Console.WriteLine("Low number of cards remaining. New cold deck created.");
}

deck.ShowRemainingDeckCount();

// Show player bank roll
Console.WriteLine($"{player.Name} Chips Balance: {player.ChipsOnHand}");

// Get bet amount from player
Console.Write("Enter chip bet amount: ");
player.ChipsOnBet = Convert.ToInt16(Console.ReadLine());

// Deal first two cards to player
deck.DealHand(player);

// Show player's hand
player.ShowUpCard();
Thread.Sleep(1500);

// Deal first two cards to dealer
deck.DealHand(dealerComputer);

// Show dealer's hand
dealerComputer.ShowUpCard(true);
Thread.Sleep(1500);
// Check natural black jack
if (!checkNaturalBlack(player, dealerComputer))
{
// If both also don't have natural black jack,
// then player's turn to continue.
PlayerAction(player);

Console.WriteLine("n--------------------------------------------------");

PlayerAction(dealerComputer);

Console.WriteLine("n--------------------------------------------------");

//Announce the winner.
AnnounceWinner(player, dealerComputer);
}

Console.WriteLine("This round is over.");

Console.Write("nPlay again? Y or N? ");

continuePlay = Console.ReadLine() == "Y" ? true : false;
// for brevity, no input validation
}

Console.WriteLine($"{player.Name} won {player.TotalWins} times.");
Console.WriteLine($"{dealerComputer.Name} won {dealerComputer.TotalWins} times.");
Console.WriteLine("Game over. Thank you for playing.");

}

private static void PlayerAction(Player currentPlayer)
{
// set to player's turn

bool playerTurnContinue = true;

string opt = "";

while (playerTurnContinue)
{
Console.Write($"n{currentPlayer.Name}'s turn. ");

if (currentPlayer.Name.Equals("Dealer"))
{
Thread.Sleep(2000); // faking thinking time.
// Mini A.I for dealer.
opt = currentPlayer.GetHandValue() < 16 ? "H" : "S";
}
else
{
// Prompt player to enter Hit or Stand.
Console.Write("Hit (H) or Stand (S): ");
opt = Console.ReadLine();
}

switch (opt.ToUpper())
{
case "H":
Console.Write($"{currentPlayer.Name} hits. ");
Thread.Sleep(1500);
// Take a card from the deck and put into player's Hand.
currentPlayer.Hand.Add(deck.DrawCard());
Thread.Sleep(1500);

// Check if there is any Ace in the Hand. If yes, change all the Ace's value to 1.
if (currentPlayer.GetHandValue() > 21 && currentPlayer.CheckAceInHand())
currentPlayer.Hand = currentPlayer.ChangeAceValueInHand();

currentPlayer.ShowHandValue();

break;
case "S":
if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}

break;
default:
Console.WriteLine("Invalid command.");
break;
}

// If current player is busted, turn is over.
if (currentPlayer.GetHandValue() > 21)
{
Utility.MakeColor("Busted!", ConsoleColor.Red);
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
currentPlayer.IsBusted = true;
playerTurnContinue = false;
}
// If current player total card in hand is 5, turn is over.
else if (currentPlayer.Hand.Count == 5)
{
Console.WriteLine($"{currentPlayer.Name} got 5 cards in hand already.");
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


}
}



private static bool checkNaturalBlack(Player _player, Player _dealer)
{
Console.WriteLine();
if (_dealer.IsNaturalBlackJack && _player.IsNaturalBlackJack)
{
Console.WriteLine("Player and Dealer got natural BlackJack. Tie Game!");
_dealer.ShowUpCard();

return true;
}
else if (_dealer.IsNaturalBlackJack && !_player.IsNaturalBlackJack)
{
Console.WriteLine($"{_dealer.Name} got natural BlackJack. {_dealer.Name} won!");
_dealer.ShowUpCard();
_dealer.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand - (int)Math.Floor(_player.ChipsOnBet * 1.5);
return true;
}
else if (!_dealer.IsNaturalBlackJack && _player.IsNaturalBlackJack)
{
Console.WriteLine($"{_player.Name} got natural BlackJack. {_player.Name} won!");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + (int)Math.Floor(_player.ChipsOnBet * 1.5);
return true;
}

// guard block
return false;
}

private static void AnnounceWinner(Player _player, Player _dealer)
{
Console.WriteLine();
if (!_dealer.IsBusted && _player.IsBusted)
{
Console.WriteLine($"{_dealer.Name} won.");
_dealer.AddWinCount();
}
else if (_dealer.IsBusted && !_player.IsBusted)
{
Console.WriteLine($"{_player.Name} won.");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + _player.ChipsOnBet;
}
else if (_dealer.IsBusted && _player.IsBusted)
Console.WriteLine("Tie game.");
else if (!_dealer.IsBusted && !_player.IsBusted)
if (_player.GetHandValue() > _dealer.GetHandValue())
{
Console.WriteLine($"{_player.Name} won.");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + _player.ChipsOnBet;
}
else if (_player.GetHandValue() < _dealer.GetHandValue())
{
Console.WriteLine($"{_dealer.Name} won.");
_dealer.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand - _player.ChipsOnBet;
}

else if (_player.GetHandValue() == _dealer.GetHandValue())
Console.WriteLine("Tie game.");


}


}










share|improve this question







New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 1




    $begingroup$
    Might help to include deckCard, card, Player, etc.
    $endgroup$
    – Shelby115
    2 days ago
















0












$begingroup$


what do you think of my BlackJack game in regards in Object-Oriented Programming?



My code is at https://github.com/ngaisteve1/BlackJack



using System;
using System.Threading;

public class BlackJackGame
{
private static deckCard deck;
public void Play()
{
bool continuePlay = true;

Console.Title = "Steve BlackJack Game (Version 2)";
Console.Write("Steve BlackJack Game ");
Utility.MakeColor2(" ♠ ",ConsoleColor.White);
Utility.MakeColor2(" ♥ ",ConsoleColor.Red);
Utility.MakeColor2(" ♣ ",ConsoleColor.White);
Utility.MakeColor2(" ♦ ",ConsoleColor.Red);

deck = new deckCard();
Console.Write("nnEnter player's name: ");

// Create player
var player = new Player(Console.ReadLine());

// Create dealer
var dealerComputer = new Player();

while (continuePlay)
{
// Initialize screen and player's certain property - Start
Console.Clear();
player.IsNaturalBlackJack = false;
player.IsBusted = false;
dealerComputer.IsNaturalBlackJack = false;
dealerComputer.IsBusted = false;
// Initialize screen and player's certain property - End

if (deck.GetRemainingDeckCount() < 20)
{
// Get a new shuffled deck.
deck.Initialize();
Console.WriteLine("Low number of cards remaining. New cold deck created.");
}

deck.ShowRemainingDeckCount();

// Show player bank roll
Console.WriteLine($"{player.Name} Chips Balance: {player.ChipsOnHand}");

// Get bet amount from player
Console.Write("Enter chip bet amount: ");
player.ChipsOnBet = Convert.ToInt16(Console.ReadLine());

// Deal first two cards to player
deck.DealHand(player);

// Show player's hand
player.ShowUpCard();
Thread.Sleep(1500);

// Deal first two cards to dealer
deck.DealHand(dealerComputer);

// Show dealer's hand
dealerComputer.ShowUpCard(true);
Thread.Sleep(1500);
// Check natural black jack
if (!checkNaturalBlack(player, dealerComputer))
{
// If both also don't have natural black jack,
// then player's turn to continue.
PlayerAction(player);

Console.WriteLine("n--------------------------------------------------");

PlayerAction(dealerComputer);

Console.WriteLine("n--------------------------------------------------");

//Announce the winner.
AnnounceWinner(player, dealerComputer);
}

Console.WriteLine("This round is over.");

Console.Write("nPlay again? Y or N? ");

continuePlay = Console.ReadLine() == "Y" ? true : false;
// for brevity, no input validation
}

Console.WriteLine($"{player.Name} won {player.TotalWins} times.");
Console.WriteLine($"{dealerComputer.Name} won {dealerComputer.TotalWins} times.");
Console.WriteLine("Game over. Thank you for playing.");

}

private static void PlayerAction(Player currentPlayer)
{
// set to player's turn

bool playerTurnContinue = true;

string opt = "";

while (playerTurnContinue)
{
Console.Write($"n{currentPlayer.Name}'s turn. ");

if (currentPlayer.Name.Equals("Dealer"))
{
Thread.Sleep(2000); // faking thinking time.
// Mini A.I for dealer.
opt = currentPlayer.GetHandValue() < 16 ? "H" : "S";
}
else
{
// Prompt player to enter Hit or Stand.
Console.Write("Hit (H) or Stand (S): ");
opt = Console.ReadLine();
}

switch (opt.ToUpper())
{
case "H":
Console.Write($"{currentPlayer.Name} hits. ");
Thread.Sleep(1500);
// Take a card from the deck and put into player's Hand.
currentPlayer.Hand.Add(deck.DrawCard());
Thread.Sleep(1500);

// Check if there is any Ace in the Hand. If yes, change all the Ace's value to 1.
if (currentPlayer.GetHandValue() > 21 && currentPlayer.CheckAceInHand())
currentPlayer.Hand = currentPlayer.ChangeAceValueInHand();

currentPlayer.ShowHandValue();

break;
case "S":
if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}

break;
default:
Console.WriteLine("Invalid command.");
break;
}

// If current player is busted, turn is over.
if (currentPlayer.GetHandValue() > 21)
{
Utility.MakeColor("Busted!", ConsoleColor.Red);
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
currentPlayer.IsBusted = true;
playerTurnContinue = false;
}
// If current player total card in hand is 5, turn is over.
else if (currentPlayer.Hand.Count == 5)
{
Console.WriteLine($"{currentPlayer.Name} got 5 cards in hand already.");
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


}
}



private static bool checkNaturalBlack(Player _player, Player _dealer)
{
Console.WriteLine();
if (_dealer.IsNaturalBlackJack && _player.IsNaturalBlackJack)
{
Console.WriteLine("Player and Dealer got natural BlackJack. Tie Game!");
_dealer.ShowUpCard();

return true;
}
else if (_dealer.IsNaturalBlackJack && !_player.IsNaturalBlackJack)
{
Console.WriteLine($"{_dealer.Name} got natural BlackJack. {_dealer.Name} won!");
_dealer.ShowUpCard();
_dealer.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand - (int)Math.Floor(_player.ChipsOnBet * 1.5);
return true;
}
else if (!_dealer.IsNaturalBlackJack && _player.IsNaturalBlackJack)
{
Console.WriteLine($"{_player.Name} got natural BlackJack. {_player.Name} won!");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + (int)Math.Floor(_player.ChipsOnBet * 1.5);
return true;
}

// guard block
return false;
}

private static void AnnounceWinner(Player _player, Player _dealer)
{
Console.WriteLine();
if (!_dealer.IsBusted && _player.IsBusted)
{
Console.WriteLine($"{_dealer.Name} won.");
_dealer.AddWinCount();
}
else if (_dealer.IsBusted && !_player.IsBusted)
{
Console.WriteLine($"{_player.Name} won.");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + _player.ChipsOnBet;
}
else if (_dealer.IsBusted && _player.IsBusted)
Console.WriteLine("Tie game.");
else if (!_dealer.IsBusted && !_player.IsBusted)
if (_player.GetHandValue() > _dealer.GetHandValue())
{
Console.WriteLine($"{_player.Name} won.");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + _player.ChipsOnBet;
}
else if (_player.GetHandValue() < _dealer.GetHandValue())
{
Console.WriteLine($"{_dealer.Name} won.");
_dealer.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand - _player.ChipsOnBet;
}

else if (_player.GetHandValue() == _dealer.GetHandValue())
Console.WriteLine("Tie game.");


}


}










share|improve this question







New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$








  • 1




    $begingroup$
    Might help to include deckCard, card, Player, etc.
    $endgroup$
    – Shelby115
    2 days ago














0












0








0





$begingroup$


what do you think of my BlackJack game in regards in Object-Oriented Programming?



My code is at https://github.com/ngaisteve1/BlackJack



using System;
using System.Threading;

public class BlackJackGame
{
private static deckCard deck;
public void Play()
{
bool continuePlay = true;

Console.Title = "Steve BlackJack Game (Version 2)";
Console.Write("Steve BlackJack Game ");
Utility.MakeColor2(" ♠ ",ConsoleColor.White);
Utility.MakeColor2(" ♥ ",ConsoleColor.Red);
Utility.MakeColor2(" ♣ ",ConsoleColor.White);
Utility.MakeColor2(" ♦ ",ConsoleColor.Red);

deck = new deckCard();
Console.Write("nnEnter player's name: ");

// Create player
var player = new Player(Console.ReadLine());

// Create dealer
var dealerComputer = new Player();

while (continuePlay)
{
// Initialize screen and player's certain property - Start
Console.Clear();
player.IsNaturalBlackJack = false;
player.IsBusted = false;
dealerComputer.IsNaturalBlackJack = false;
dealerComputer.IsBusted = false;
// Initialize screen and player's certain property - End

if (deck.GetRemainingDeckCount() < 20)
{
// Get a new shuffled deck.
deck.Initialize();
Console.WriteLine("Low number of cards remaining. New cold deck created.");
}

deck.ShowRemainingDeckCount();

// Show player bank roll
Console.WriteLine($"{player.Name} Chips Balance: {player.ChipsOnHand}");

// Get bet amount from player
Console.Write("Enter chip bet amount: ");
player.ChipsOnBet = Convert.ToInt16(Console.ReadLine());

// Deal first two cards to player
deck.DealHand(player);

// Show player's hand
player.ShowUpCard();
Thread.Sleep(1500);

// Deal first two cards to dealer
deck.DealHand(dealerComputer);

// Show dealer's hand
dealerComputer.ShowUpCard(true);
Thread.Sleep(1500);
// Check natural black jack
if (!checkNaturalBlack(player, dealerComputer))
{
// If both also don't have natural black jack,
// then player's turn to continue.
PlayerAction(player);

Console.WriteLine("n--------------------------------------------------");

PlayerAction(dealerComputer);

Console.WriteLine("n--------------------------------------------------");

//Announce the winner.
AnnounceWinner(player, dealerComputer);
}

Console.WriteLine("This round is over.");

Console.Write("nPlay again? Y or N? ");

continuePlay = Console.ReadLine() == "Y" ? true : false;
// for brevity, no input validation
}

Console.WriteLine($"{player.Name} won {player.TotalWins} times.");
Console.WriteLine($"{dealerComputer.Name} won {dealerComputer.TotalWins} times.");
Console.WriteLine("Game over. Thank you for playing.");

}

private static void PlayerAction(Player currentPlayer)
{
// set to player's turn

bool playerTurnContinue = true;

string opt = "";

while (playerTurnContinue)
{
Console.Write($"n{currentPlayer.Name}'s turn. ");

if (currentPlayer.Name.Equals("Dealer"))
{
Thread.Sleep(2000); // faking thinking time.
// Mini A.I for dealer.
opt = currentPlayer.GetHandValue() < 16 ? "H" : "S";
}
else
{
// Prompt player to enter Hit or Stand.
Console.Write("Hit (H) or Stand (S): ");
opt = Console.ReadLine();
}

switch (opt.ToUpper())
{
case "H":
Console.Write($"{currentPlayer.Name} hits. ");
Thread.Sleep(1500);
// Take a card from the deck and put into player's Hand.
currentPlayer.Hand.Add(deck.DrawCard());
Thread.Sleep(1500);

// Check if there is any Ace in the Hand. If yes, change all the Ace's value to 1.
if (currentPlayer.GetHandValue() > 21 && currentPlayer.CheckAceInHand())
currentPlayer.Hand = currentPlayer.ChangeAceValueInHand();

currentPlayer.ShowHandValue();

break;
case "S":
if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}

break;
default:
Console.WriteLine("Invalid command.");
break;
}

// If current player is busted, turn is over.
if (currentPlayer.GetHandValue() > 21)
{
Utility.MakeColor("Busted!", ConsoleColor.Red);
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
currentPlayer.IsBusted = true;
playerTurnContinue = false;
}
// If current player total card in hand is 5, turn is over.
else if (currentPlayer.Hand.Count == 5)
{
Console.WriteLine($"{currentPlayer.Name} got 5 cards in hand already.");
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


}
}



private static bool checkNaturalBlack(Player _player, Player _dealer)
{
Console.WriteLine();
if (_dealer.IsNaturalBlackJack && _player.IsNaturalBlackJack)
{
Console.WriteLine("Player and Dealer got natural BlackJack. Tie Game!");
_dealer.ShowUpCard();

return true;
}
else if (_dealer.IsNaturalBlackJack && !_player.IsNaturalBlackJack)
{
Console.WriteLine($"{_dealer.Name} got natural BlackJack. {_dealer.Name} won!");
_dealer.ShowUpCard();
_dealer.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand - (int)Math.Floor(_player.ChipsOnBet * 1.5);
return true;
}
else if (!_dealer.IsNaturalBlackJack && _player.IsNaturalBlackJack)
{
Console.WriteLine($"{_player.Name} got natural BlackJack. {_player.Name} won!");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + (int)Math.Floor(_player.ChipsOnBet * 1.5);
return true;
}

// guard block
return false;
}

private static void AnnounceWinner(Player _player, Player _dealer)
{
Console.WriteLine();
if (!_dealer.IsBusted && _player.IsBusted)
{
Console.WriteLine($"{_dealer.Name} won.");
_dealer.AddWinCount();
}
else if (_dealer.IsBusted && !_player.IsBusted)
{
Console.WriteLine($"{_player.Name} won.");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + _player.ChipsOnBet;
}
else if (_dealer.IsBusted && _player.IsBusted)
Console.WriteLine("Tie game.");
else if (!_dealer.IsBusted && !_player.IsBusted)
if (_player.GetHandValue() > _dealer.GetHandValue())
{
Console.WriteLine($"{_player.Name} won.");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + _player.ChipsOnBet;
}
else if (_player.GetHandValue() < _dealer.GetHandValue())
{
Console.WriteLine($"{_dealer.Name} won.");
_dealer.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand - _player.ChipsOnBet;
}

else if (_player.GetHandValue() == _dealer.GetHandValue())
Console.WriteLine("Tie game.");


}


}










share|improve this question







New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




what do you think of my BlackJack game in regards in Object-Oriented Programming?



My code is at https://github.com/ngaisteve1/BlackJack



using System;
using System.Threading;

public class BlackJackGame
{
private static deckCard deck;
public void Play()
{
bool continuePlay = true;

Console.Title = "Steve BlackJack Game (Version 2)";
Console.Write("Steve BlackJack Game ");
Utility.MakeColor2(" ♠ ",ConsoleColor.White);
Utility.MakeColor2(" ♥ ",ConsoleColor.Red);
Utility.MakeColor2(" ♣ ",ConsoleColor.White);
Utility.MakeColor2(" ♦ ",ConsoleColor.Red);

deck = new deckCard();
Console.Write("nnEnter player's name: ");

// Create player
var player = new Player(Console.ReadLine());

// Create dealer
var dealerComputer = new Player();

while (continuePlay)
{
// Initialize screen and player's certain property - Start
Console.Clear();
player.IsNaturalBlackJack = false;
player.IsBusted = false;
dealerComputer.IsNaturalBlackJack = false;
dealerComputer.IsBusted = false;
// Initialize screen and player's certain property - End

if (deck.GetRemainingDeckCount() < 20)
{
// Get a new shuffled deck.
deck.Initialize();
Console.WriteLine("Low number of cards remaining. New cold deck created.");
}

deck.ShowRemainingDeckCount();

// Show player bank roll
Console.WriteLine($"{player.Name} Chips Balance: {player.ChipsOnHand}");

// Get bet amount from player
Console.Write("Enter chip bet amount: ");
player.ChipsOnBet = Convert.ToInt16(Console.ReadLine());

// Deal first two cards to player
deck.DealHand(player);

// Show player's hand
player.ShowUpCard();
Thread.Sleep(1500);

// Deal first two cards to dealer
deck.DealHand(dealerComputer);

// Show dealer's hand
dealerComputer.ShowUpCard(true);
Thread.Sleep(1500);
// Check natural black jack
if (!checkNaturalBlack(player, dealerComputer))
{
// If both also don't have natural black jack,
// then player's turn to continue.
PlayerAction(player);

Console.WriteLine("n--------------------------------------------------");

PlayerAction(dealerComputer);

Console.WriteLine("n--------------------------------------------------");

//Announce the winner.
AnnounceWinner(player, dealerComputer);
}

Console.WriteLine("This round is over.");

Console.Write("nPlay again? Y or N? ");

continuePlay = Console.ReadLine() == "Y" ? true : false;
// for brevity, no input validation
}

Console.WriteLine($"{player.Name} won {player.TotalWins} times.");
Console.WriteLine($"{dealerComputer.Name} won {dealerComputer.TotalWins} times.");
Console.WriteLine("Game over. Thank you for playing.");

}

private static void PlayerAction(Player currentPlayer)
{
// set to player's turn

bool playerTurnContinue = true;

string opt = "";

while (playerTurnContinue)
{
Console.Write($"n{currentPlayer.Name}'s turn. ");

if (currentPlayer.Name.Equals("Dealer"))
{
Thread.Sleep(2000); // faking thinking time.
// Mini A.I for dealer.
opt = currentPlayer.GetHandValue() < 16 ? "H" : "S";
}
else
{
// Prompt player to enter Hit or Stand.
Console.Write("Hit (H) or Stand (S): ");
opt = Console.ReadLine();
}

switch (opt.ToUpper())
{
case "H":
Console.Write($"{currentPlayer.Name} hits. ");
Thread.Sleep(1500);
// Take a card from the deck and put into player's Hand.
currentPlayer.Hand.Add(deck.DrawCard());
Thread.Sleep(1500);

// Check if there is any Ace in the Hand. If yes, change all the Ace's value to 1.
if (currentPlayer.GetHandValue() > 21 && currentPlayer.CheckAceInHand())
currentPlayer.Hand = currentPlayer.ChangeAceValueInHand();

currentPlayer.ShowHandValue();

break;
case "S":
if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}

break;
default:
Console.WriteLine("Invalid command.");
break;
}

// If current player is busted, turn is over.
if (currentPlayer.GetHandValue() > 21)
{
Utility.MakeColor("Busted!", ConsoleColor.Red);
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
currentPlayer.IsBusted = true;
playerTurnContinue = false;
}
// If current player total card in hand is 5, turn is over.
else if (currentPlayer.Hand.Count == 5)
{
Console.WriteLine($"{currentPlayer.Name} got 5 cards in hand already.");
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


}
}



private static bool checkNaturalBlack(Player _player, Player _dealer)
{
Console.WriteLine();
if (_dealer.IsNaturalBlackJack && _player.IsNaturalBlackJack)
{
Console.WriteLine("Player and Dealer got natural BlackJack. Tie Game!");
_dealer.ShowUpCard();

return true;
}
else if (_dealer.IsNaturalBlackJack && !_player.IsNaturalBlackJack)
{
Console.WriteLine($"{_dealer.Name} got natural BlackJack. {_dealer.Name} won!");
_dealer.ShowUpCard();
_dealer.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand - (int)Math.Floor(_player.ChipsOnBet * 1.5);
return true;
}
else if (!_dealer.IsNaturalBlackJack && _player.IsNaturalBlackJack)
{
Console.WriteLine($"{_player.Name} got natural BlackJack. {_player.Name} won!");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + (int)Math.Floor(_player.ChipsOnBet * 1.5);
return true;
}

// guard block
return false;
}

private static void AnnounceWinner(Player _player, Player _dealer)
{
Console.WriteLine();
if (!_dealer.IsBusted && _player.IsBusted)
{
Console.WriteLine($"{_dealer.Name} won.");
_dealer.AddWinCount();
}
else if (_dealer.IsBusted && !_player.IsBusted)
{
Console.WriteLine($"{_player.Name} won.");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + _player.ChipsOnBet;
}
else if (_dealer.IsBusted && _player.IsBusted)
Console.WriteLine("Tie game.");
else if (!_dealer.IsBusted && !_player.IsBusted)
if (_player.GetHandValue() > _dealer.GetHandValue())
{
Console.WriteLine($"{_player.Name} won.");
_player.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand + _player.ChipsOnBet;
}
else if (_player.GetHandValue() < _dealer.GetHandValue())
{
Console.WriteLine($"{_dealer.Name} won.");
_dealer.AddWinCount();
_player.ChipsOnHand = _player.ChipsOnHand - _player.ChipsOnBet;
}

else if (_player.GetHandValue() == _dealer.GetHandValue())
Console.WriteLine("Tie game.");


}


}







c# console






share|improve this question







New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









SteveSteve

42




42




New contributor




Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Steve is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    $begingroup$
    Might help to include deckCard, card, Player, etc.
    $endgroup$
    – Shelby115
    2 days ago














  • 1




    $begingroup$
    Might help to include deckCard, card, Player, etc.
    $endgroup$
    – Shelby115
    2 days ago








1




1




$begingroup$
Might help to include deckCard, card, Player, etc.
$endgroup$
– Shelby115
2 days ago




$begingroup$
Might help to include deckCard, card, Player, etc.
$endgroup$
– Shelby115
2 days ago










2 Answers
2






active

oldest

votes


















4












$begingroup$

This is by no means a full review, but I figured I'd mention some of the things I noticed real quick.



Naming & Consistency



What is a deckCard? Oh, you mean a DeckOfCards? Why not just name it what it is?
Secondly, we PascalCase names of classes in C#. See Microsoft Casing Guidelines. Also, looking at your github the Filenames should match the class names (e.g. card.cs should be Card.cs) and you should generally try to restrict each class to its own file. Helps you stay organized when projects get larger.



Modularity & Intent



You could separate your console code into smaller functions with representative names of what they do.



Examples:



bool IsPlayerBust(Player player);
bool HasPlayerWon(Player player);
void Hit(Player player, DeckOfCards deck);
bool CanPlayerStand(Player player);
void Stand(Player player);


This would have two benefits.




  1. Your code will be broken up into smaller more manageable pieces.

  2. You'll be able to give meaningful names to those smaller pieces of code. For example, currentPlayer.GetHandValue() > 21 could be named IsPlayerBust() or added to player as a function currentPlayer.IsBust(). Doing it this way shows intent and gives meaning to your lines of code so they may be more easily read.


Humans read C#; Computers don't.



You seem to take this advice into account in some places and not in others. Try to be consistent in your coding habits. Hope these tips help.






share|improve this answer









$endgroup$













  • $begingroup$
    Thanks a lot for the review. Okay gonna break up into smaller functions.
    $endgroup$
    – Steve
    yesterday



















1












$begingroup$

From a Unit Testing perspective, I would say many of the hard-coded strings in your Console.WriteLine() calls should be retrieved from some function that generates those strings.



Also, it will be near impossible to unit test whether or not your switch/case for getting the user's input on Hit/Stand without having those be their own separate functions.



Each action/input/output should have it's own function that will either return something or set a property on your Player class in order to Unit Test properly. This will also increase readability throughout your code, and will make debugging far easier, as you'll likely only have to step through a function or two to find any problems rather than stepping through that massive PlayerAction function.



This next suggestion is purely personal choice, and maybe nit-picky, but this block here lacks consistency:



            if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


Your if statement doesn't use { }, but your else does.






share|improve this answer









$endgroup$













  • $begingroup$
    Oh, yes. I completely forgot to mention that bracket inconsistency. Which is funny because that was the reason I decided to make an answer. Good catch.
    $endgroup$
    – Shelby115
    2 days ago










  • $begingroup$
    thanks a lot. gonna amend it to make it easier to test. oh the if statement because there is only one line , so i omit the bracket
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    Be consistent with your backets. If the else-statement has brackets it's more readable and expected for your if-statement to have brackets too, even if it's one line. It's completely fine to omit them if both are one line or if there is no else-statement though.
    $endgroup$
    – Shelby115
    yesterday










  • $begingroup$
    oic. thanks. got it.
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    I have refactor my code accordingly already. Regarding the input and output separation, I do understand the benefit. It is just like MVC. But in Console project, I am still thinking how to separate it nicely. Is it like creating a class Screen for both input and output?
    $endgroup$
    – Steve
    yesterday











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Steve is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214390%2fmy-blackjack-game-in-c-console%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4












$begingroup$

This is by no means a full review, but I figured I'd mention some of the things I noticed real quick.



Naming & Consistency



What is a deckCard? Oh, you mean a DeckOfCards? Why not just name it what it is?
Secondly, we PascalCase names of classes in C#. See Microsoft Casing Guidelines. Also, looking at your github the Filenames should match the class names (e.g. card.cs should be Card.cs) and you should generally try to restrict each class to its own file. Helps you stay organized when projects get larger.



Modularity & Intent



You could separate your console code into smaller functions with representative names of what they do.



Examples:



bool IsPlayerBust(Player player);
bool HasPlayerWon(Player player);
void Hit(Player player, DeckOfCards deck);
bool CanPlayerStand(Player player);
void Stand(Player player);


This would have two benefits.




  1. Your code will be broken up into smaller more manageable pieces.

  2. You'll be able to give meaningful names to those smaller pieces of code. For example, currentPlayer.GetHandValue() > 21 could be named IsPlayerBust() or added to player as a function currentPlayer.IsBust(). Doing it this way shows intent and gives meaning to your lines of code so they may be more easily read.


Humans read C#; Computers don't.



You seem to take this advice into account in some places and not in others. Try to be consistent in your coding habits. Hope these tips help.






share|improve this answer









$endgroup$













  • $begingroup$
    Thanks a lot for the review. Okay gonna break up into smaller functions.
    $endgroup$
    – Steve
    yesterday
















4












$begingroup$

This is by no means a full review, but I figured I'd mention some of the things I noticed real quick.



Naming & Consistency



What is a deckCard? Oh, you mean a DeckOfCards? Why not just name it what it is?
Secondly, we PascalCase names of classes in C#. See Microsoft Casing Guidelines. Also, looking at your github the Filenames should match the class names (e.g. card.cs should be Card.cs) and you should generally try to restrict each class to its own file. Helps you stay organized when projects get larger.



Modularity & Intent



You could separate your console code into smaller functions with representative names of what they do.



Examples:



bool IsPlayerBust(Player player);
bool HasPlayerWon(Player player);
void Hit(Player player, DeckOfCards deck);
bool CanPlayerStand(Player player);
void Stand(Player player);


This would have two benefits.




  1. Your code will be broken up into smaller more manageable pieces.

  2. You'll be able to give meaningful names to those smaller pieces of code. For example, currentPlayer.GetHandValue() > 21 could be named IsPlayerBust() or added to player as a function currentPlayer.IsBust(). Doing it this way shows intent and gives meaning to your lines of code so they may be more easily read.


Humans read C#; Computers don't.



You seem to take this advice into account in some places and not in others. Try to be consistent in your coding habits. Hope these tips help.






share|improve this answer









$endgroup$













  • $begingroup$
    Thanks a lot for the review. Okay gonna break up into smaller functions.
    $endgroup$
    – Steve
    yesterday














4












4








4





$begingroup$

This is by no means a full review, but I figured I'd mention some of the things I noticed real quick.



Naming & Consistency



What is a deckCard? Oh, you mean a DeckOfCards? Why not just name it what it is?
Secondly, we PascalCase names of classes in C#. See Microsoft Casing Guidelines. Also, looking at your github the Filenames should match the class names (e.g. card.cs should be Card.cs) and you should generally try to restrict each class to its own file. Helps you stay organized when projects get larger.



Modularity & Intent



You could separate your console code into smaller functions with representative names of what they do.



Examples:



bool IsPlayerBust(Player player);
bool HasPlayerWon(Player player);
void Hit(Player player, DeckOfCards deck);
bool CanPlayerStand(Player player);
void Stand(Player player);


This would have two benefits.




  1. Your code will be broken up into smaller more manageable pieces.

  2. You'll be able to give meaningful names to those smaller pieces of code. For example, currentPlayer.GetHandValue() > 21 could be named IsPlayerBust() or added to player as a function currentPlayer.IsBust(). Doing it this way shows intent and gives meaning to your lines of code so they may be more easily read.


Humans read C#; Computers don't.



You seem to take this advice into account in some places and not in others. Try to be consistent in your coding habits. Hope these tips help.






share|improve this answer









$endgroup$



This is by no means a full review, but I figured I'd mention some of the things I noticed real quick.



Naming & Consistency



What is a deckCard? Oh, you mean a DeckOfCards? Why not just name it what it is?
Secondly, we PascalCase names of classes in C#. See Microsoft Casing Guidelines. Also, looking at your github the Filenames should match the class names (e.g. card.cs should be Card.cs) and you should generally try to restrict each class to its own file. Helps you stay organized when projects get larger.



Modularity & Intent



You could separate your console code into smaller functions with representative names of what they do.



Examples:



bool IsPlayerBust(Player player);
bool HasPlayerWon(Player player);
void Hit(Player player, DeckOfCards deck);
bool CanPlayerStand(Player player);
void Stand(Player player);


This would have two benefits.




  1. Your code will be broken up into smaller more manageable pieces.

  2. You'll be able to give meaningful names to those smaller pieces of code. For example, currentPlayer.GetHandValue() > 21 could be named IsPlayerBust() or added to player as a function currentPlayer.IsBust(). Doing it this way shows intent and gives meaning to your lines of code so they may be more easily read.


Humans read C#; Computers don't.



You seem to take this advice into account in some places and not in others. Try to be consistent in your coding habits. Hope these tips help.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Shelby115Shelby115

1,586517




1,586517












  • $begingroup$
    Thanks a lot for the review. Okay gonna break up into smaller functions.
    $endgroup$
    – Steve
    yesterday


















  • $begingroup$
    Thanks a lot for the review. Okay gonna break up into smaller functions.
    $endgroup$
    – Steve
    yesterday
















$begingroup$
Thanks a lot for the review. Okay gonna break up into smaller functions.
$endgroup$
– Steve
yesterday




$begingroup$
Thanks a lot for the review. Okay gonna break up into smaller functions.
$endgroup$
– Steve
yesterday













1












$begingroup$

From a Unit Testing perspective, I would say many of the hard-coded strings in your Console.WriteLine() calls should be retrieved from some function that generates those strings.



Also, it will be near impossible to unit test whether or not your switch/case for getting the user's input on Hit/Stand without having those be their own separate functions.



Each action/input/output should have it's own function that will either return something or set a property on your Player class in order to Unit Test properly. This will also increase readability throughout your code, and will make debugging far easier, as you'll likely only have to step through a function or two to find any problems rather than stepping through that massive PlayerAction function.



This next suggestion is purely personal choice, and maybe nit-picky, but this block here lacks consistency:



            if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


Your if statement doesn't use { }, but your else does.






share|improve this answer









$endgroup$













  • $begingroup$
    Oh, yes. I completely forgot to mention that bracket inconsistency. Which is funny because that was the reason I decided to make an answer. Good catch.
    $endgroup$
    – Shelby115
    2 days ago










  • $begingroup$
    thanks a lot. gonna amend it to make it easier to test. oh the if statement because there is only one line , so i omit the bracket
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    Be consistent with your backets. If the else-statement has brackets it's more readable and expected for your if-statement to have brackets too, even if it's one line. It's completely fine to omit them if both are one line or if there is no else-statement though.
    $endgroup$
    – Shelby115
    yesterday










  • $begingroup$
    oic. thanks. got it.
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    I have refactor my code accordingly already. Regarding the input and output separation, I do understand the benefit. It is just like MVC. But in Console project, I am still thinking how to separate it nicely. Is it like creating a class Screen for both input and output?
    $endgroup$
    – Steve
    yesterday
















1












$begingroup$

From a Unit Testing perspective, I would say many of the hard-coded strings in your Console.WriteLine() calls should be retrieved from some function that generates those strings.



Also, it will be near impossible to unit test whether or not your switch/case for getting the user's input on Hit/Stand without having those be their own separate functions.



Each action/input/output should have it's own function that will either return something or set a property on your Player class in order to Unit Test properly. This will also increase readability throughout your code, and will make debugging far easier, as you'll likely only have to step through a function or two to find any problems rather than stepping through that massive PlayerAction function.



This next suggestion is purely personal choice, and maybe nit-picky, but this block here lacks consistency:



            if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


Your if statement doesn't use { }, but your else does.






share|improve this answer









$endgroup$













  • $begingroup$
    Oh, yes. I completely forgot to mention that bracket inconsistency. Which is funny because that was the reason I decided to make an answer. Good catch.
    $endgroup$
    – Shelby115
    2 days ago










  • $begingroup$
    thanks a lot. gonna amend it to make it easier to test. oh the if statement because there is only one line , so i omit the bracket
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    Be consistent with your backets. If the else-statement has brackets it's more readable and expected for your if-statement to have brackets too, even if it's one line. It's completely fine to omit them if both are one line or if there is no else-statement though.
    $endgroup$
    – Shelby115
    yesterday










  • $begingroup$
    oic. thanks. got it.
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    I have refactor my code accordingly already. Regarding the input and output separation, I do understand the benefit. It is just like MVC. But in Console project, I am still thinking how to separate it nicely. Is it like creating a class Screen for both input and output?
    $endgroup$
    – Steve
    yesterday














1












1








1





$begingroup$

From a Unit Testing perspective, I would say many of the hard-coded strings in your Console.WriteLine() calls should be retrieved from some function that generates those strings.



Also, it will be near impossible to unit test whether or not your switch/case for getting the user's input on Hit/Stand without having those be their own separate functions.



Each action/input/output should have it's own function that will either return something or set a property on your Player class in order to Unit Test properly. This will also increase readability throughout your code, and will make debugging far easier, as you'll likely only have to step through a function or two to find any problems rather than stepping through that massive PlayerAction function.



This next suggestion is purely personal choice, and maybe nit-picky, but this block here lacks consistency:



            if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


Your if statement doesn't use { }, but your else does.






share|improve this answer









$endgroup$



From a Unit Testing perspective, I would say many of the hard-coded strings in your Console.WriteLine() calls should be retrieved from some function that generates those strings.



Also, it will be near impossible to unit test whether or not your switch/case for getting the user's input on Hit/Stand without having those be their own separate functions.



Each action/input/output should have it's own function that will either return something or set a property on your Player class in order to Unit Test properly. This will also increase readability throughout your code, and will make debugging far easier, as you'll likely only have to step through a function or two to find any problems rather than stepping through that massive PlayerAction function.



This next suggestion is purely personal choice, and maybe nit-picky, but this block here lacks consistency:



            if (currentPlayer.GetHandValue() < 16)
Console.WriteLine($"{currentPlayer.Name} is not allowed to stands when hand value is less than 16.");
else
{
Console.WriteLine($"{currentPlayer.Name} stands.");
Thread.Sleep(1500);
// Show player's hand
currentPlayer.ShowUpCard();
Thread.Sleep(1500);
Console.WriteLine($"{currentPlayer.Name}'s turn is over.");
Thread.Sleep(1500);
playerTurnContinue = false;
}


Your if statement doesn't use { }, but your else does.







share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









Jaken HermanJaken Herman

1429




1429












  • $begingroup$
    Oh, yes. I completely forgot to mention that bracket inconsistency. Which is funny because that was the reason I decided to make an answer. Good catch.
    $endgroup$
    – Shelby115
    2 days ago










  • $begingroup$
    thanks a lot. gonna amend it to make it easier to test. oh the if statement because there is only one line , so i omit the bracket
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    Be consistent with your backets. If the else-statement has brackets it's more readable and expected for your if-statement to have brackets too, even if it's one line. It's completely fine to omit them if both are one line or if there is no else-statement though.
    $endgroup$
    – Shelby115
    yesterday










  • $begingroup$
    oic. thanks. got it.
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    I have refactor my code accordingly already. Regarding the input and output separation, I do understand the benefit. It is just like MVC. But in Console project, I am still thinking how to separate it nicely. Is it like creating a class Screen for both input and output?
    $endgroup$
    – Steve
    yesterday


















  • $begingroup$
    Oh, yes. I completely forgot to mention that bracket inconsistency. Which is funny because that was the reason I decided to make an answer. Good catch.
    $endgroup$
    – Shelby115
    2 days ago










  • $begingroup$
    thanks a lot. gonna amend it to make it easier to test. oh the if statement because there is only one line , so i omit the bracket
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    Be consistent with your backets. If the else-statement has brackets it's more readable and expected for your if-statement to have brackets too, even if it's one line. It's completely fine to omit them if both are one line or if there is no else-statement though.
    $endgroup$
    – Shelby115
    yesterday










  • $begingroup$
    oic. thanks. got it.
    $endgroup$
    – Steve
    yesterday










  • $begingroup$
    I have refactor my code accordingly already. Regarding the input and output separation, I do understand the benefit. It is just like MVC. But in Console project, I am still thinking how to separate it nicely. Is it like creating a class Screen for both input and output?
    $endgroup$
    – Steve
    yesterday
















$begingroup$
Oh, yes. I completely forgot to mention that bracket inconsistency. Which is funny because that was the reason I decided to make an answer. Good catch.
$endgroup$
– Shelby115
2 days ago




$begingroup$
Oh, yes. I completely forgot to mention that bracket inconsistency. Which is funny because that was the reason I decided to make an answer. Good catch.
$endgroup$
– Shelby115
2 days ago












$begingroup$
thanks a lot. gonna amend it to make it easier to test. oh the if statement because there is only one line , so i omit the bracket
$endgroup$
– Steve
yesterday




$begingroup$
thanks a lot. gonna amend it to make it easier to test. oh the if statement because there is only one line , so i omit the bracket
$endgroup$
– Steve
yesterday












$begingroup$
Be consistent with your backets. If the else-statement has brackets it's more readable and expected for your if-statement to have brackets too, even if it's one line. It's completely fine to omit them if both are one line or if there is no else-statement though.
$endgroup$
– Shelby115
yesterday




$begingroup$
Be consistent with your backets. If the else-statement has brackets it's more readable and expected for your if-statement to have brackets too, even if it's one line. It's completely fine to omit them if both are one line or if there is no else-statement though.
$endgroup$
– Shelby115
yesterday












$begingroup$
oic. thanks. got it.
$endgroup$
– Steve
yesterday




$begingroup$
oic. thanks. got it.
$endgroup$
– Steve
yesterday












$begingroup$
I have refactor my code accordingly already. Regarding the input and output separation, I do understand the benefit. It is just like MVC. But in Console project, I am still thinking how to separate it nicely. Is it like creating a class Screen for both input and output?
$endgroup$
– Steve
yesterday




$begingroup$
I have refactor my code accordingly already. Regarding the input and output separation, I do understand the benefit. It is just like MVC. But in Console project, I am still thinking how to separate it nicely. Is it like creating a class Screen for both input and output?
$endgroup$
– Steve
yesterday










Steve is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Steve is a new contributor. Be nice, and check out our Code of Conduct.













Steve is a new contributor. Be nice, and check out our Code of Conduct.












Steve is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f214390%2fmy-blackjack-game-in-c-console%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...