avatarGary Janosz

Summary

The website content discusses the author's journey and passion for coding board games like Monopoly, Blackjack, and Sorry using Python and Pygame, as a means to enhance problem-solving skills and create engaging multimedia gaming experiences.

Abstract

The author of the content shares a personal account of using Python programming to bring classic board games to life on a computer screen. Starting with an online Python course where the midterm project was to create a Blackjack game, the author found joy in coding games, which led to the development of several popular board games, including Monopoly, Solitaire, Craps, and Risk. The process involved breaking down complex game rules into smaller coding challenges, such as shuffling and dealing cards, tracking player positions, and implementing game strategies. The author emphasizes the satisfaction derived from solving these problems and the continuous improvement of coding skills through this hobby. The article also touches on the use of the Pygame library to create graphical representations of the games and the author's experience with coding an outdoor game console using Python and an Arduino board. The author, a retired teacher, finds coding board games a fulfilling way to spend time and suggests it as an enjoyable method for new coders to improve their skills.

Opinions

  • The author expresses a love for board games and sees them as an opportunity for coding challenges.
  • Monopoly is cited as an example of a game that can be brought to life through coding, with its rules providing a clear outline for programming behavior.
  • The author found the Blackjack project appealing due to familiarity with the game and was motivated to enhance the console-based project with a graphical user interface using Pygame.
  • The author takes pride in adding features like betting, statistics tracking, and the ability to split or double down in Blackjack, as well as a card counter for practice.
  • Coding Risk and Sorry presented significant challenges, with Sorry being surprisingly complex despite its reputation as a simple children's game.
  • The author values the strategic aspects of coding, ensuring computer players in games like Sorry and Risk make the most advantageous moves.
  • The article conveys the author's enjoyment of problem-solving in programming and the satisfaction of creating games that are challenging to beat.
  • The author appreciates the flexibility and tools provided by Python and Pygame for game development and multimedia programming.
  • The author, as a retired teacher, emphasizes the importance of finding one's own problems to solve in programming, suggesting that coding board games is both a challenge and a pleasurable way to hone coding skills.

Coding

Python for Fun

Here’s a great way to improve your problem-solving skills

This little snippet of code keeps track of whose turn it is. Screenshot by author.

I’ve always loved board games. But I never thought of them as a coding challenge. But they are a terrific challenge and it’s all spelled out for you in the directions of the game.

Monopoly is a perfect example. The task is to bring the game to life on your monitor so it looks, feels, and behaves exactly like the board game. The behavior of each player or icon is outlined by the rules. The game board and player icons are readily available online.

I began my journey coding games in the middle of an online Python course. The midterm project was to create a working version of Blackjack, the casino card game. I was familiar with the game and the project appealed to me.

Every coding challenge must be broken down into small chunks. The first challenge for Blackjack was to figure out how to shuffle and deal the cards from a virtual deck. You can never deal the same card twice until the deck is shuffled. The midterm project was meant to be played out on the console, but to me, that was just part of the project. I wanted to see the cards fly off the deck in a virtual representation of the real game. This led me to Pygame.

The Pygame library allows you to create games and rich multimedia programs in Python. Pygame gives you the tools to represent your code with graphics on the screen. Pygame allowed me to deal my virtual Bicycle playing cards directly onto a blaze green virtual Blackjack table.

I’ve spent many enjoyable hours solving problems to bring this game to life. I added betting and a means to track my betting results over time. I added statistics that were displayed at the end of each session. I built it so you could split your hand in the case of a pair or double down if your first two cards added up to twenty or you were dealt a pair of aces just like you can do in the casinos. My son was interested in counting cards, so I added a card counter that the player could toggle off to practice counting, and then toggle it on to check the count.

I’ve coded Solitaire, Sorry, Craps, Monopoly, Blackjack, and Risk. Risk is perhaps the toughest challenge and I revisit it frequently. The more a game relies on player strategy the more difficult it is to code. Surprisingly Sorry is a close second. For those who know it, it’s a fairly simple children’s game, but not one that’s simple to code.

My solitaire — no ads, no interruptions. Screenshot by author.

Sorry uses a deck of cards that determine an icon’s actions. There are four icons per team with board space for four teams. That’s 16 positions to track as they progress around the board. The icons begin the game in the start circle. They move to the perimeter of the board, to what I call the racetrack by drawing a one, two, or the Sorry card. They move clockwise around the board (sixty squares) until they reach their finish lane and each team has a unique finish lane on the board.

The Sorry card allows an icon to leave the start circle to replace any opponent’s icon on the racetrack. The opponent’s icon returns to his start circle.

The three, five, eight, and twelve simply advance an icon a given number of squares. The four moves an icon backward four spaces. The seven can be split and used between any two of a team’s icons on the racetrack. The ten can move forward 10 squares or backward one. Lastly, the eleven allow any one of your team’s icons already on the racetrack to switch places with any opponent on the track.

The starting positions are different for every player. Red starts at 5. Blue starts at 20. Yellow starts at 35 and green at 50. Every time a player crosses over from square 60 to square 1, an adjustment has to be made to keep track of the player’s position on the racetrack. With a -4 or a -1 players frequently cross in reverse. As I stated earlier, the challenge is all spelled out by the rules of the game and the layout of the game board.

Of course, so far I covered just the basic nuts and bolts of the game, the more important part is the coding that informs the computer players when and where to move — the game strategy. My goal is to create games that are hard to beat. In the case of Sorry it’s not enough for the computer to know how to switch two icons when the eleven card is drawn, but to select the most advantageous opponent to switch with. In the game of Risk, once the initial armies are deployed the games are all about where to attack, when to attack, and for what duration to attack — all make for great coding challenges.

I became interested in Python when I wanted to flesh out the code for an outdoor game console.

I read that you could program an Arduino board with Python and I thought an Arduino board might be the perfect controller for my game console. I wrote the program to control the console while I was learning Python.

What I learned from studying Python and coding the game console project was that I enjoyed the problem-solving aspect of programming. Unless I have a problem to solve, I have nothing to code. I’m a retired teacher, not an IT professional, so no one is throwing problems at me — I have to find my own. I found coding board games to be a terrific challenge that gives me hours of satisfaction. All I have to do is sort through our collection of board games to find my next challenge. In between I revisit previous code to make it more concise while discovering new ways to solve old problems.

If you are new to coding and desire to improve your skills this is a enjoyable way to go about it.

Python
Problem Solving
Games
Coding
Hobby
Recommended from ReadMedium