In Part 1, I provided details for construction of the game console, and I provided software for 3 games: Tic Tac Toe, Simon, and Dots and Boxes. I also included an arcade sketch where these three games are bundled together in a single sketch and Nextion file.
In this project, we are moving to card games: Blackjack and Five Card Draw poker. I am also adding a arcade of card games. The "Card Arcade" includes both card games. So now we basically have a package of kids games and another one of card games.
The software download package here includes a stand alone versions of Blackjack and Five Card Draw, as well as the Card Arcade. Each one includes a Nextion Editor file, a Nextion output file, and the Arduino Sketch.
Again here we are using the Neo Nextion library to help facilitate the interface between the Arduino Mega and the Nextion display.
If you want information on construction, wiring, parts, the case, etc., please refer to Part 1.
BlackjackCard games are always an interesting programming challenge. The user interface offers some challenges as does the game logic. Blackjack seemed like a good place to start. The rules are fairly straight forward and there are not a lot of cards in play at any one time.
Let's start by defining how I implemented this version of Blackjack. I'm keeping it simple: an infinite supply of cards, like the multi-deck shoes used in Las Vegas, except truly infinite which really keeps things simple, but eliminates any possibility of card counting. Also simplifying - no splits and no insurance. The player starts with $500 and can bet in $10 increments. The dealer has to hit on 16, stay on 17. I didn't want to tie up large amounts of Nextion memory trying to store an image of every card in the deck, so cards are displayed with an image of the suit, along with text describing the cards value, i.e. 3, 4, 10, K, A - again trying to keep things simple.
One thing in my user interface required adding a rule. I only provide places for 5 cards each for the player and dealer. With enough low cards, you can get 5 and still be below 21. Doesn't happen often, but does sometimes. I decided to give the player the hand when he gets 5 cards and stays at or below 21. The dealer just has to stop at 5 cards and use the total he has. This gives a slight edge to the player. The dealer has an edge by going last, so these two things together should even out the odds.
The Nextion user interface is straight forward. Several buttons, text fields, pictures, number fields, but not an excessive number. Arduino code started out simple enough with routines to bet, deal, hit, stand, score, etc. And there is also the usual stuff to interface with all the Nextion objects. But with Aces worth 1 or 11, and natural blackjacks (2 cards) being handled someone differently than multi-card 21s, things quickly got complicated. My Arduino code is not documented as well as it should be as a result.
Another complication I had to incorporate was what I called process steps. Buttons had to work only at their appropriate points in the process. For example you can only bet at the beginning of a hand, you can't hit or stay before you deal. So each button was only active at certain steps in the process.
Five Card Draw PokerFirst, let’s define how this version of Five Card Draw will work. Like Blackjack, there is one player against the computer/dealer. Player and dealer each start with $500, and there is a $10 ante. After the deal, the player can Bet up to $50 or Fold. Then the dealer can Call, Raise up to $50 or Fold. The player can then Call or Fold. The player then marks the cards he wants to discard and hits the Draw button. The dealer will then also discard. Both get replacements for their discards. Then the player can Bet up to $50 or Fold. Dealer can Call, Raise up to $50, or Fold. The player can then Call or Fold. Then cards are shown and the winner takes the pot. This is a fairly simple version of Five Card Draw, but has the essentials of the game.
Our poker game recognizes highest card, a pair, two-pair, three-of-a-kind, a straight, a flush, a full-house and four-of-a-kind. Straight flushes (including a Royal Flush) are so rare that I decided to ignore them.
I am doing some things differently with Five Card Draw than I did with Blackjack. Here we shuffle and use a deck of 52 cards – not the infinite “shoe” of cards we used in Blackjack. So card counting should help, if you want to do it. (The dealer doesn’t count cards.) Also, I am making the buttons dynamic. Their functions will change as each hand progresses, so we don’t need as many. Also, we need to be able to select cards for discard, so the upper portion of each player’s card is a button with which it can be selected.
Creating a poker game looked like a lot of work. But after I had Blackjack working, it looked more reasonable - some of the required pieces were already there, especially for the Nextion user interface. Even so, the Arduino coding for a poker game was a huge task - easily more work as the other four games put together! There are simply too many variations in play that have to be dealt with – the Arduino sketch adds up to about 40 pages of code!
I am not much of a poker player myself, and I have not given my dealer any great insights or strategy into playing the game. He will recognize good hands, avoid dumb mistakes, and try not to be too predictable, but that’s about it. He’ll be just an average player.
Getting the basic functionality of the game all working was the first major programming task. But the second task was creating the algorithms and setting the proper limits within them to make the dealer a descent player. As the song “The Gambler” says “you’ve gotta know when to hold 'em, know when to fold 'em” and it’s not as easy as it sounds! When to raise and by how much is another challenge. When the hand is over and the cards are all turn up, our program has to score each hand and give the pot to the winner. That too can be a mess, as both players can have two pair, Queens high, etc. I ended up allowing very similar hands to be judged equal making the hand a “push”, meaning the pot is split, rather than going after a winner no matter how similar the hands might appear.
Our dealer has some predictable behaviors, but I will leave it to the player to figure them out. He can and will bluff, so he is not too predictable. If you replicate my poker game yourself, you might find the most interesting part would be improving my dealer's skills as a poker player!
Arcades (Multiple games in one app/sketch)It might be worth discussing the issues with combining multiple games on a single app. I didn't discuss this much in Part 1, but it does add a few complications.
First, we have to resolve any variable naming conflicts, as games tend to revolve around common names like score, dealer, startButton. But a bigger problem is that we are dealing with multiple pages in Nextion. To play a new hand or restart a game, we need to initialize Nextion. It is the only way to quickly restore everything to it initial condition. But this causes the Nextion to return to page 0, which we don't want in the Arcade versions. The Neo-Nextion library's page change didn't work, so I had to set up a second software serial interface, bypassing Neo-Nextion and send the command to Nextion directly. Then following that, it seems Nextion wanted to ignore the first new command sent back through Neo Nextion. But eventually it all worked.
All of that was the bad news. But there is also a lot of good news. Both the Nextion and the Arduino Mega have plenty of memory capacity to handle multiple games. I never came close to any limitations. Paying close attention to initializing all variables is necessary when moving back and forth between games, but having done that, moving between games works great.
Comments
Please log in or sign up to comment.