Let’s Play a PowerShell Game


The 2021 edition of the PowerShell + DevOps Global Summit is here. Sadly, due to the state of the world, this year’s event will be virtual. And sad only in the sense that we can’t come together and celebrate the community. This year’s virtual even will have the same terrific content. However, there won’t be the traditional Iron Scripter event. Instead The Chairman offers this challenge to test your PowerShell skills.

The challenge is open to everyone, regardless of whether you are attending the virtual conference. There aren’t any prizes other than bragging rights. This is not the traditional Iron Scripter team challenge, although you are certainly welcome to work on the problem with friends.

Shut the Box

For your challenge, The Chairman would like you to create a PowerShell version of a dice game, most often referred to as “Shut the Box”. The game is played with a pair of dice and a box with numbered levers 1-9.

The player rolls the dice. In the photo above, the player rolled 10. The player must now “close” any combination of open numbers that add to 10 such as:

  • 7+3
  • 9+1
  • 5+2+3

The selected numbers are flipped down and are now out of play. The player continues rolling and closing until nothing else can be closed. The sum of the remaining numbers is the player’s score. The player with the lowest score wins the round. If the player closes all numbers, that is considered “Shutting the Box” and the player immediately wins the round.

The only other “rule” The Chairman would like you to follow is that if there are open numbers above 6, the player must roll 2 dice. When only numbers 6 and less remain, the player can opt to roll 1 OR 2 dice.

The PowerShell Challenge

Your challenge is to create a PowerShell version of this game that can be played at a PowerShell console prompt. You will need to create and display the “box” in some fashion, indicating what numbers are open. The game should include these features:

  • Support 2-4 players.
  • Keep a running score of the round for all players.
  • You will need to make sure the player “rolls” the correct number of dice.
  • You will need to generate the dice results.
  • You will need to prompt the player for their number selections and verify the sum matches the dice total.
  • You will need to update the box display.
  • If the player “shuts the box” the round is immediately over.
  • Prompt the players to play another round.
  • The basic game code should run cross-platform.
  • You should follow community-accepted PowerShell scripting best practices.

Advanced Features

If you are feeling the need to be challenged above the basic requirements, see if you can include any of these features.

  • A cheat prompt to show the user all the possible combinations for the roll.
  • Support 2-6 players.
  • Assign a name to each player other than “Player One”.
  • Create a version of the game where the board is numbered 1-12.
  • Keep a log of each round showing what was rolled. what was played, and the player’s score.
  • Let the players also wager money. The winner of each round gets the pot. Keep track of total winnings.
  • Use ANSI to color the game display.
  • Add a sound and/or visual effect if the player “shuts the box”.
  • Create a WPF version.
  • Any other enhancement you can think of such as dice color.

Let the Games Begin

The point of these challenges isn’t to write a game. It is to offer a learning challenge. How you build the solution, what techniques you had to learn, and the coding experience, are the true goals. The site has many other challenges for PowerShell scripters of all levels and The Chairman hopes you’ll browse and find other challenges to tackle. If you would like to work on this or any other challenge with others, that is a great way to learn.

This challenge, like the rest, is open-ended. There is no deadline or closing date. Submit a comment with links to your work. Please do not submit any code in your comments. They will not be approved.

You are encouraged to check other solutions. Not so much to see whose code is “better”, but rather to see what scripting techniques were used. This is also a great way to learn.

Good Luck and Have Fun!

4 Replies to “Let’s Play a PowerShell Game”

  1. PSO

    Even though the challenge is interesting, it doesn’t seems many people went to try it.
    To offer another solution than Ryan’s, here is my initial commit.


    There are still plenty of stuff to improve but I tried to cover a few of the advanced features :
    cheat sheet
    up to 6 players
    up to 12 tiles
    all players named in order (player_1, player_2 …)

    I’ll try to add latter a log of each round, some collor / sound effect as requested.

Comments are closed.