Hands-On Intro to Programming
Python Edition
Summer 2020
(Updated weblink Summer 2022)
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License
Our Goal
Create a computer program that lets us play a simple (very simple) text-based game.
In one hour.
With no prior programming experience!
We can do it!
Our Tools
We will use the Python programming language:
Guess the Number
The computer picks a number between 1 and 10. The user tries to guess the number. That’s it. That’s the game.
I know. It’s not as much fun as Fortnite or 2K.
Extensions to the basic game:
-
Tell the user whether their guess is too high or too low.
-
Keep track of of how many guesses the player took and tell them that at the end of the game.
-
Add a prize of $100 that goes down by $20 after each wrong guess and tell the player what they won at the end of the game.
-
Have the prize go down by 20% after each wrong guess.
-
After the player guesses the number, ask if they want to play again.
-
Keep track of their total winnings over all the games.
-
Keep track of the minimum, maximum, and average (mean) number of guesses over all the games that they play.
-
Make the game involve numbers from 1 to 100.
-
Ask the user for the maximum number before you start playing.
-
Make the game into the “Warmer-Colder” game:
-
Better guesses are warmer, worse guesses are colder.
-
On the first guess, the response depends on how far the guess is from the target. Responses range from “freezing cold” for a guess that’s far from the target to “boiling hot” for a guess that’s very close to the answer, and “you got it” if they get it right first time.
-
After the first guess, the response depends on whether this guess is closer or farther than the previous guess was. Responses range from “much warmer” if they are much closer, through “warmer”, “colder,” to “much colder” if they are much farther. And of course “you got it” when they get it right.
-
How about having the human player pick the number and your program has to guess it? There are some easy ways to do this, and there are some smart ways to do this. Try it!
-
Take turns picking the number and keep track of the human’s and the computer’s scores.
We may get to some of these, you can work on the others or come up with your own ideas between our first and our second meetings.
Other Games
Games like Guess the Number are called “TTY” games because they used to be played on a teletype console attached to a computer. There are lots of other TTY games that you can program using just basic programming skills. Here are a few suggestions:
-
Simulations of real-world phenomena, like launching a projectile. Have the computer pick a target distance. Ask the user for the launch angle and launch speed. Compute the distance that the projectile travels (look up how) and tell the user. Repeat until they get “close enough” to the target.
You can do this type of simulation with any phenomenon for which you have a formal definition (meaning a formula to compute it).
-
Simulations of real-world games, like (American) football. The game consists of a number of plays. The offense decides what to do. The defense decides what to do. The result of the play depends on what they each decide to do. The players (human and computer) take turns playing offense and defense. Repeat until the game is over.
This type of game works best with games that involve discrete (separate, one-at-a-time) turns, like American football, golf, or bowling. Not so much for games like ice hockey and soccer.
-
Games of chance like Black Jack (Twenty-One), Roulette, or Craps, where the player plays against the house and the house doesn’t have to make any smart choices.
-
Games where your program has to play intelligently for it to be interesting for the human player. These include classic games like Tic-Tac-Toe, Checkers, Chess, and Go, as well as card games like Poker.
You should be able to write a program that plays a decent game of 3x3 Tic-Tac-Toe. But writing a program that plays a good game of Chess, Checkers, Go, or Poker? Well, check out the links...
For some of these it might help to know how to use lists in Python. That’s covered in my Intro to Programming (Python Edition) tutorial.
Another great source of ideas is BASIC Computer Games, edited by David H. Ahl and first published in 1978. The programs are written in BASIC, which is harder to understand than Python (despite its name). But the descriptions of the games are what you want anyway.
Happy programming!