Okay, so today I wanted to mess around with the “Shut the Box” game. I’d seen it around, and it looked like a fun little project to try and build myself. I figured I’d just jump in and see how far I could get.

Playing Shut the Box? (Learn the Rules Fast Here)

Getting Started

First, I needed to figure out the actual rules. I’d played a couple of times before, but I wasn’t 100% sure on all the specifics. A quick search, and boom, I had the basics down:

  • You’ve got a box with numbered tiles, usually 1-9 or 1-12.
  • You roll two dice.
  • You flip down tiles that add up to the dice roll.
  • You keep rolling until you can’t make a valid move.
  • Your score is the sum of the remaining tiles. Lower is better.

Building the Thing

I decided to keep it simple. No fancy graphics, just a basic text-based version to start. So I grabbed my trusty text editor, and got to it:

I started by representing the box. A simple list of numbers seemed like the easiest way to go. I put my numbers into a list which from 1 to 9.

Next, I needed a way to roll the dice. I wrote a simple “roll_dice” function with generating two random numbers between 1 and 6.

Then came the tricky part: figuring out if a move was valid. I made a “get_valid_moves” function. This took some back-and-forth, but the core idea was checking all possible combinations of the remaining tiles to see if they summed up to the dice roll.

Playing Shut the Box? (Learn the Rules Fast Here)

After that I put all the logic to play the turn into play. Roll the dice, show the valid moves, let the player choose, flip the tiles, and check if the game was over. I got the player’s move in input(), and used it for next step.

The Game Loop

Now, I tied everything together into a main game loop, and keep playing turns until the player can’t make a move. And calculate the final score. I took all function I wrotes and put them together.

Testing and Tweaking

Of course, the first few runs were a bit messy. I’d forgotten to handle some edge cases, had a couple of off-by-one errors, things like that. Debugging is always part of the process!

I added some print statements to see what was going on, fixed the bugs, and slowly but surely, it started working pretty smoothly.

Playing Shut the Box? (Learn the Rules Fast Here)

It’s still pretty basic, but it’s a working version of “Shut the Box”! I can roll the dice, choose my tiles, and get my score at the end. There’s definitely room for improvement, but for a quick afternoon project, I’m pretty happy with how it turned out.

LEAVE A REPLY

Please enter your comment!
Please enter your name here