Reader small image

You're reading from  Learn C Programming. - Second Edition

Product typeBook
Published inAug 2022
PublisherPackt
ISBN-139781801078450
Edition2nd Edition
Right arrow
Author (1)
Jeff Szuhay
Jeff Szuhay
author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay

Right arrow

Chapter 27:Creating Two Card Programs

In Chapter 16, Creating and Using More Complex Structures, we modeled a set of cards as complex structures and implemented some basic actions done with cards. The actions were implemented as functions that operated upon the complex structures. That chapter ended with a program that dealt out fours from the deck of cards and printed the result. This would have been the beginning of a complete card game, say whist, gin, or even bridge. However, far more game logic would be required and many complex problems would need to be solved (one such problem might be playing such a game over a network with each player on their own computer) to make a complete application.

Since we are learning C and are not game programmers per se, we stopped short of developing any such card games. Instead, we’ve reused the dealer.c program to expand our knowledge of C features, such as working with multiple files in Chapter 24, Working with Multi-File Programs...

Technical requirements

Continue to use the tools you chose in the Technical requirements section of Chapter 1, Running Hello, World!.

The source code for this chapter can be found at https://github.com/PacktPublishing/Learn-C-Programming-Second-Edition/tree/main/Chapter27.

Game one – Blackjack

This is the first of two games we will implement using our library of structures and functions from dealer.c. We will have a complete and playable Blackjack card game between the dealer and one player.

Introducing Blackjack

Blackjack is a casino card game played between the dealer and one or more players. It is also known as 21. In our implementation, there will only be the dealer and you, the player. Blackjack uses all 52 cards from 1 to 8 decks. In our implementation, there will be only one deck. The deck is shuffled and each player is given two initial cards, as is the dealer. One of the dealer’s dealt cards is kept hidden until it is the dealer’s turn to play. In our implementation, all cards will be visible.

The face value of each hand is calculated. Each card’s face value is its number. Jack, queen, and king count as 10. Aces count as either 1 or 11. In our implementation, aces will always count as 11.

The...

Game two – One-Handed Solitaire

This is the second of two games we will implement using our pre-built collection of structures and functions from dealer.c. We will have a complete and playable One-Handed Solitaire card game that's playable by a single player.

When people used to kill time by playing cards with an actual deck of cards, they would often play one form of solitaire or another. After playing for a while, they would find that the deck was not always adequately shuffled, especially after a game progressed fairly far and there were definite patterns in the unshuffled cards. So, another type of solitaire game would be played to mix up the deck sufficiently so that the deck could again be effectively shuffled. One-Handed Solitaire is one of those games. I must admit that it is not a particularly challenging or complex game.

For our book’s purposes, it is a fairly easy game to implement, which we will do using dealer.c structures and functions.

Introducing...

Summary

This final chapter is the culmination of the various aspects of C programs we’ve developed throughout this book. We started by modifying and extending dealer.c to implement a simple but complete game of Blackjack. In doing so, we introduced various considerations when reusing pre-existing source code and using pre-existing libraries – source code intentionally built to be reused.

We finished by implementing a second game, very similar structurally to Blackjack, called One-Handed Solitaire. In this implementation, we not only built upon and extended dealer.c (via what we’d done with Blackjack); we also demonstrated how to incorporate some library code we unknowingly built earlier, a rudimentary linked list library. Note that when we employed this library, we also verified it with test code to exercise all of its functions. In this manner, we could rely on the library with confidence.

The development of both of these games provides a rather thorough...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn C Programming. - Second Edition
Published in: Aug 2022Publisher: PacktISBN-13: 9781801078450
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay