Having spent most of my life working, I am not much of a games person, so I have to spend time researching and learning games before I can write programs to simulate them and then write about those games. Even Kaggle, the popular website for data science enthusiasts, has several contests that are actually game simulations. I therefore decided that in order to progress in my knowledge of data science and Python programming, I need to progress beyond the tabular competition questions that I have been working on and explore other types of programming. I therefore asked my partner if he knew of any really easy card games I could write a program on and he suggested the card game, 21.
The rules for 21 are stated below:-
“Take the Joker out of the pack and start with 2 cards.
Keep adding cards until they total 21 or as high as you can get.
An Ace equals 1 or 11
Court Cards equal 10
The player that has the highest amount up to 21 wins.
If a player gets higher than 21 he loses.”
With the rules of the game written down, I began my foray into games programming. I created the program in Google Colab, which is a free online Jupyter Notebook that has Python and several relevant libraries already installed on it. One bad thing about Google Colab is the fact that it does not make back-up copies, so I ended up writing the code for the body of the program twice when Google Colab had a saving error. I am therefore a bit disappointed in Google Colab for having such an obvious deficiency in their program, so if anyone knows of another online Jupyter Notebook then please let me know.
After I created the Jupyter Notebook, I imported the libraries I would need, namely numpy, pandas and random.
The random library is used to create random numbers and I needed to use the randint() function to make random integers. I researched how the random number generator in Python works but was unable to get the answer I was searching for. Most of the websites say to use randint() but they don’t go into the basics of how it works. I can say that from my post work in making macros in Excel, the random number generator in that program uses a seed in the battery clock and I suspect the random number generator in Python works in the same manner.
After I imported the libraries I would need, I initialised the values that would be used in the program. There are going to be 1,000 games, and an empty array where the winners are ging to be placed. Game_no and game are set to 0:-
I then programmed the main body of the game, which used a for loop to play the game 1,000 times, a while loop to keep each game going until the number 21 is reached, and if and if-elif statement to establish the rules and determined who the winner:-
A small portion of the output of the game is in the screenshot below:-
When the 1,000 games have been played, I look at the statistics involved in the playing of the game. The statistics revealed that player1 won roughly 40% 0f the time, player2 won roughly 40% of the time, and nobody won roughly 20% of the time. Interesting.
In summary, I tested this program out and found the average scores were about 40% for each player and 20% where no player won. There seems to be a kind of symmetry in this experiment where each player scores roughly the same.
The code for this post can be found in its entirety in my personal GitHub account, the link of which is here:- Games/AI_Card_game_21.ipynb at main · TracyRenee61/Games (github.com)