PowerShell 19: Classes - BlackJack Game

preview_player
Показать описание
*Chapters*
1. (00:15) A GUI for BLackJack: But We Will Code the Back End from Console FIRST
2. (02:25) Playing the Game from the Console
3. (15:27) Coding Black Jack: Globals and Constants
4. (22:46) Creating the CARD Class
5. (23:19) Overriding and Overloading the Default Constructor to Write Polymorphic Code
6. (29:10) The CARD Class Member Methods: DisplayCard()
7. (30:47) The MAIN() Function
8. (33:59) The Instructions() Function
9. (34:38) The PAUSE() Helper Method
10. (36:02) The The_Game() Function
11. (37:21) Building a Deck of 52 Cards With 4 Faces Using An Array of CARD Objects and Nested FOR Loops
12. (45:59) The Draw() Function: Drawing CARD Class Objects from the DECK
13. (50:58) The DisplayHand() Function: Diplaying an Entire Hand of CARD Class Objects
14. (55:56) The Deal() Function: Enabling the DEALER to DEAL Out CARD Objects
15. (56:19) The Game's Main Repetition Engine
16. (59:02) 15. Game Logic: What is Our "Dealer" Thinking? How Does She Decide What to Do Next?
17. (01:00:29) The Hit() Function: Adding CARD Objects to the HAND Array
18. (01:01:18) Converting an ACE's Point Value from 11 to 1 if Either the Player or Dealer Goes Over 21
19. (01:04:26) What are the Possible Outcomes? The Conclusion of a Complete Black Jack Sequence
20. (01:07:01) A Teachable Moment: My Logic Error! Learn from My Mistake.

Encapsulation of what a CARD is, and what it does - in a CARD class.

Рекомендации по теме
Комментарии
Автор

Source Code: Black Jack Game: Globals, Constants and Class Specification
# #>

#Constants

[int] $NUMCARDS = 13;
[int] $NUMFACES = 4;
[int] $NUMCARDSINDECK = 52;
[int] $MaxHandSize = 20;
[int] $NumCardsToStartWith = 2;
[int] $HouseLimit = 17;

#The Card Values
[int] $DEUCE = 2;
[int] $THREE = 3;
[int] $FOUR = 4;
[int] $FIVE = 5;
[int] $SIX = 6;
[int] $SEVEN = 7;
[int] $EIGHT = 8;
[int] $NINE = 9;
[int] $TEN = 10;
[int] $JACK = 11;
[int] $QUEEN = 12;
[int] $KING = 13;
[int] $ACE = 14;

#The Faces
[int] $SPADES = 0;
[int] $HEARTS = 1;
[int] $DIAMONDS = 2;
[int] $CLUBS = 3;

#Dealer and Player
[int] $PLAYER = 1;
[int] $DEALER = 2;

[int] $global:DealerTotal = 0;
[int] $global:PlayerTotal = 0;
[int] $global:DealerStay = $false;
[int] $global:PlayerStay = $false;

#Count the Number of Deals for Indexing the Cards
#Used to advance array forward to hold new cards in hand
[int] $global:PlayerDeal = 0;
[int] $global:DealerDeal = 0;

[String] $PlayerName = "";
[boolean] $DisplayAllDealerCards = $false;
[boolean] $CHEAT = $false;

[int] $global:DealerWins = 0;
[int] $global:PlayerWins = 0;

#Build 3 Arrays to hold deck and different hands of CARDs
$global:DeckOfCards = New-Object CARD[] $NUMCARDSINDECK;
$global:DealerHand = New-Object CARD[] $MaxHandSize;
$global:PlayerHand = New-Object CARD[] $MaxHandSize;


<# #>

class CARD
{
#Data Members
[int] $TheFace;
[int] $TheCard;
[int] $PointValue = 0;
[boolean] $Drawn = $false;

#The Card Values
[int] $DEUCE = 2;
[int] $THREE = 3;
[int] $FOUR = 4;
[int] $FIVE = 5;
[int] $SIX = 6;
[int] $SEVEN = 7;
[int] $EIGHT = 8;
[int] $NINE = 9;
[int] $TEN = 10;
[int] $JACK = 11;
[int] $QUEEN = 12;
[int] $KING = 13;
[int] $ACE = 14;

#The Faces
[int] $SPADES = 0;
[int] $HEARTS = 1;
[int] $DIAMONDS = 2;
[int] $CLUBS = 3;

#Overloaded Constructors
CARD()
{
Write-Host("Creating a generic CARD object.");
$this.TheCard = $this.DEUCE;
$this.TheFace = $this.HEARTS;
$this.Drawn = $false;
}

CARD([int] $card, [int] $face)
{
Write-Host("Creating a specific CARD object.");
$this.TheCard = $card;
$this.TheFace = $face;
$this.Drawn = $false;

switch($this.TheCard)
{
$this.DEUCE { $this.PointValue = 2; break; }
$this.THREE { $this.PointValue = 3; break; }
$this.FOUR { $this.PointValue = 4; break; }
$this.FIVE { $this.PointValue = 5; break; }
$this.SIX { $this.PointValue = 6; break; }
$this.SEVEN { $this.PointValue = 7; break; }
$this.EIGHT { $this.PointValue = 8; break; }
$this.NINE { $this.PointValue = 9; break; }
$this.TEN { $this.PointValue = 10; break; }
$this.JACK { $this.PointValue = 10; break; }
$this.QUEEN { $this.PointValue = 10; break; }
$this.KING { $this.PointValue = 10; break; }
$this.ACE { $this.PointValue = 11; break ; }
default { break; }
}
}

#Member Methods
DisplayCard()
{
[String] $OUT = " A ";

switch($this.TheCard)
{
$this.DEUCE { $OUT = $OUT + "DEUCE"; break; }
$this.THREE { $OUT = $OUT + "THREE"; break; }
$this.FOUR { $OUT = $OUT + "FOUR"; break; }
$this.FIVE { $OUT = $OUT + "FIVE"; break; }
$this.SIX { $OUT = $OUT + "SIX"; break;; }
$this.SEVEN { $OUT = $OUT + "SEVEN"; break; }
$this.EIGHT { $OUT = $OUT + "EIGHT"; break; }
$this.NINE { $OUT = $OUT + "NINE"; break; }
$this.TEN { $OUT = $OUT + "TEN"; break; }
$this.JACK { $OUT = $OUT + "JACK"; break; }
$this.QUEEN { $OUT = $OUT + "QUEEN"; break; }
$this.KING { $OUT = $OUT + "KING"; break; }
$this.ACE { $OUT = $OUT + "ACE"; break; }
default { break; }
}

$OUT = $OUT + " of ";

switch($this.TheFace)
{
$this.SPADES { $OUT = $OUT + "SPADES"; break; }
$this.HEARTS { $OUT = $OUT + "HEARTS"; break; }
$this.DIAMONDS { $OUT = $OUT + "DIAMONDS"; break; }
$this.CLUBS { $OUT = $OUT + "CLUBS"; break; }
default { break; }
}

$OUT = $OUT + ". Points = " + $this.PointValue + ".";

Write-Host($OUT);
}
}

<# #>

#Title: PowerShell - Module 07 - Classes - Black Jack
#Author: Carly S. Germany
#Date: 06/10/2020

OneByteAtATime