Week 14: Exercises

1. Ghost

Consider the following game. Let W be a set of words, each containing only lowercase letters from 'a' to 'z'. Two players take turns adding a letter to a string S, which is initially empty. A player loses if after their turn either

  1. they have made a word (i.e. S is a word in W), or

  2. no more words are possible (i.e. S is not a prefix of any word in W).

For example, suppose that W = { "chirp", "cat", "dark", "dog", "donate", "donut" }. The game might proceed as follows:

  1. A chooses d

  2. B chooses o

  3. A chooses n

  4. B chooses u

Now A has lost: if he chooses 't' he has made the word 'donut', and he cannot choose any other letter since then no other word will be possible.

Write a program that can play Ghost. Read the list of possible English words from this file. Only use words that consist only of lowercase letters a-z from the Latin alphabet. Your program should be able to player either as player 1 or 2.

Can you defeat your own program? If not, make it easier by modifying your program to choose its first move randomly from the set of all possible moves that don't lose immediately.

2. Starfield

Write a program that displays a black background with 50 random stars, which appear as small white dots.

3. Spaceship

Extend the previous program so that a triangular spaceship appears at the center of the starfield. The user should be able to rotate the ship by holding down the left and right arrow keys.

4. Moving Spaceship

Extend the previous program so that the spaceship can move. If the user holds the up arrow key, the ship should accelerate forward in the direction that it's currently pointing. If they release it, the ship should decelerate until it comes to a halt.