Simon game

# Simon game

In this project we build a simple version of the Simon game.

In this game, the computer displays a sequence of blinking lights, which the human player is supposed to remember.

You need to study the sections "Basic Usage" and "Updating the display" of the cs109ui documentation.

Let's look at an example run:

$ktc simon.kt$ kt SimonKt
Press Enter when ready (or x to exit):


The computer now waits for us to press the Enter key (this is simply a readString statement). After that, it flashes a sequence of colored rectangles, for instance this sequence:

Each square appears for about 600 milliseconds, then the next square appears. After showing the sequence, the window turns entirely white for 800 milliseconds, then finally the following window appears:

At this point we need to look at the terminal again:
$kt SimonKt Press Enter when ready (or x to exit): Which sequence did I show you?  The computer now wants us to enter the sequence that we have just seen. If we do not remember, we can simply press Enter, and the computer will display the same sequence again. Otherwise, we can try our luck: Which sequence did I show you? rgb That is correct Points: 3 Rounds: 1 Press Enter when ready (or x to exit):  We get three points, because we correctly remembered a sequence of length three. Because the answer was correct, the length of the sequence will now be increased by one. The speed will also be increased slightly. Here is what happens when we fail to answer correctly: Which sequence did I show you? rgbyr That is not correct. I showed you 'rbgyr'. Points: 4 Rounds: 2 Press Enter when ready (or x to exit):  In this case, the computer displays the correct sequence. We got no points in this round. The sequence length will now be decreased by two, and the speed will be decreased by two steps as well (see below). Finally, when we are tired, we can simply say 'x' to quit the game: Press Enter when ready (or x to exit): x You achieved 18 points in 5 rounds. Good bye  #### Implementation Start by writing a function generateSequence(seqLen: Int): String that returns a random string of length seqLen consisting of the letters "rgby". Note that the same letter does not appear consecutively. Here are some example calls: $ ktc simon.kt
\$ ktc
Welcome to Kotlin version 1.0.4
Type :help for help, :quit for quit
>>> generateSequence(3)
rgb
>>> generateSequence(5)
grygr
>>> generateSequence(10)
rygygybrgr
>>> generateSequence(20)
bgrgyrgyrbgrygybyryg


Next, write a function

fun draw(g: ImageCanvas, color: Char) {
// ...
}

that fills the canvas with white, and then draws the square with the color indicated by the letter color. When color is '*', draw all four squares. When color is a space, draw no square. (In my implementation the canvas was 500x500 pixels, and each square is 200x200 pixels.)

You can now play the entire sequence with the following function:

fun performSequence(image: BufferedImage, canvas: ImageCanvas, seq: String,
for (ch in seq) {
draw(canvas, ch)
show(image)
}
draw(canvas, ' ')
show(image)
waitForMs(800)
draw(canvas, '*')
show(image)
}


Note that blink determines the speed for displaying the sequence. Other important variables are points and seqLen (which determines the length of the sequence that is generated).

When the program starts, we set these as follows:

  var blink = 600
var seqLen = 4
var points = 0


When the sequence is guessed correctly, we update them like this:

  points += seqLen
seqLen += 1


When the guess is wrong, we update like this:

  seqLen -= 2

(and points remains the same).

#### Bonus: Keyboard input

Read the section "Keyboard input" of the cs109ui documentation.

Convert your program so that all user interaction is done through the window (no more println and readLine statements).

You should display the message "Ready?" and the current number of points and rounds in the window. When the user presses Enter, the sequence is animated as before. The program then again waits with all four squares displayed. Here the user can use the keys 'r', 'g', 'b', and 'y' to enter the sequence they remember. Flash the correct square briefly when the key is pressed. Afterwards, the message "Correct" or "Wrong!" is displayed in the window. (In this version you don't need to show the correct sequence if the guess was wrong.) When the user presses Enter, the program goes back to the "Ready?" message. Pressing 'x' terminates the program.

#### Bonus: Android mini-app

Write a mini-app for the CS109 Android mini-app framework.

Instead of the keyboard, the user should press on the colored squares to enter the sequence.

 Simon game