- Repository:
php-blackjack
- Type of Challenge:
Learning challenge
- Duration:
2 days
- Team challenge :
solo
- A first dive into OOP (object oriented programming)
Let's make a game in PHP: Blackjack! To keep the code structured we are going to use classes and objects.
Your coach has provide you with some starter classes that you can use for the game, to help you out on your first OOP challenge. First spent some time reading these classes and really understand what they are doing. If something in the syntac is unclear, google it first and then ask your coach.
If this is still an unclear subject for you don't feel bad to google some basic OOP articles, or ask your coach. it is normal that this feels difficult, because object oriented programming is a really complex subject!
You will see you will create a lot of functions that start with "get" to access a property in a class. Why not just make the property public?
This kind of function is called a Getter and it encapsulates the fields of a class by making them accessible only through its public methods and keep the values themselves private.
Setting the property as public is ALWAYS considered a very bad idea because you will lose all control of which value is entered there. Any validation that was provided in your getter function could be avoided if you make the property public.
TL;DR: Never use public properties, make getters!
- Cards are between 1-11 points.
- Faces are worth 10
- Ace is always worth 11
- Getting more than 21 points, means that you lose.
- To win, you need to have more points than the dealer, but not more than 21.
- The dealer is obligated to keep taking cards until they have at least 15 points.
- We are not playing with blackjack rules on the first turn (having 21 on first turn) - we leave this up to you as a nice to have.
- A new deck is shuffled
- Player and dealer get 2 random cards
- Dealer shows first card he drew to player
- Player either keeps getting hit (asks for more cards), or stands down.
- If the player at any point goes above 21, he automatically loses.
- Once the player is done the dealer keeps taking cards until he has at least 15 points. If he hits above 21 he automatically loses.
- At the end display the winner
- Create a class called
Player
in the filePlayer.php
. - Add 2 private properties:
cards
(array)lost
(bool, default = false)
- Add a couple of empty public methods to this class:
hit
surrender
getScore
hasLost
- Create a class called
Blackjack
in the fileBlackjack.php
- Add 3 private properties
player
dealer
deck
- Add the following public methods:
getPlayer
getDealer
- In the constructor do the following:
- Instantiate the Player class twice, insert it into the
player
property and adealer
property. - Create a new
deck
object (code has already been written for you!). - Shuffle the cards with
shuffle
method on deck.
- Instantiate the Player class twice, insert it into the
- In the constructor of the
Player
class;- Make it require the
Deck
object. - Pass this
Deck
from theBlackjack
constructor. - Now draw 2 cards for the player. You have to use existing code for this from the Deck class.
- Make it require the
- Save the instance of the enitre
Blackjack
object in the session (you're gonna need it) - Use forms to send to the
index.php
page what the player's action is. (i.e. hit/stand/surrender) - Use the class' methods to react to these actions.
hit
should add a card to the player. If this brings him above 21, setlost
property totrue
.surrender
should make you surrender the game. (Dealer wins.) This sets the propertylost
in theplayer
instance to true.getScore
loops over all the cards and return the total value of that player.stand
does not have a method in the player class but will instead call hit on thedealer
instance.hasLost
will return the bool of the lost property.
Everything from the player is now done! Job well done!
So far we are assuming the player and dealer play with the same rules, hence they share a class. There is of course an important difference: the dealer does keep playing with the function hit
until he has at least 15.
-
To change this behavior, we have are going extend the
player
class and extend it to a newly createddealer
class. -
Change the
Blackjack
class to create a newdealer
object instead of aplayer
object for the property of the dealer. -
Now create a
hit
function that keeps drawing cards until the dealer has at least 15 points. The tricky part is that we also need thelost
check we already had in thehit
function of the player. We could just copy the code but duplicated code is never the solution, instead you can use the following code to call the oldhit
function:
parent::hit();
All classes are ready, now you just need to write some minimal glue. The final result should be the following:
- When you the hit button call
hit
on player, then check the lost status of the player. - When you the stand button call
hit
on dealer, then check the lost status of the dealer. If he is not lost, compare scores to set the winner (If equal the dealer wins). - Surrender: the dealer auto wins.
- Always display on the page the scores of both players. If you have a winner, display it.
- End of the game: destroy the current
blackjack
variable so the game restarts.
- Implement a betting system
- Every new player (new session) starts with 100 chips
- After the player gets his 2 first cards every round ask how much he wants to bet. He needs to bet at least 5 chips.
- If the player wins the bet he gains double the amount of chips.
- Implement the blackjack first turn rule: if the player draws 21 the first turn: he directly wins. If the dealer draws 21 the first turn, he wins. If both draw it, it is a tie.
- When you implement both nice to have features, a blackjack means an auto win of 10 chips, a blackjack of the dealer a loss of 5 chips for the player.
- Be sure to check the pre made classes and the example.php file. This file shows how you can easily get some graphical presentation for the cards to spice up your game!
- Try to avoid refering to
$_SESSION
inside your objects, because this breaks encapsulation. If you need it, pass it as an argument.