TigerJython4Kids micro:bit Robotik
memory
 Deutsch   English

## WORKSHEET 3: MEMORY GAME ### GAME DESCRIPTION

 The goal of the game is to remember the longest possible sequence of left and right arrows. You can use it to train your brain. The program displays a random sequence of left and right arrows. Then you have to play back this episode by pressing the left or right button. If the playback is correct, the process is repeated with a sequence longer by one arrow, otherwise the game is finished. ### STEP 1: GENERATING RANDOM SEQUENCE OF 0 AND 1

 First write a program which defines the function createSeg(n) that fills n random numbers 0 or 1 in the list seq. Test the program with different n. Use the following template: Program: ```from microbit import * from random import randint def createSeq(n): ... seq = [] n = 3 createSeq(n) print(seq) ``` from microbit import * from random import randint def createSeq(n): for i in range(n): x = randint(0, 1) seq.append(x) seq = [] n = 3 createSeq(n) print(seq) ► Copy to clipboard

### STEP 2: GENERATING RANDOM SEQUENCE OF ARROWS

 The functions left() displays a left arrow (image ARROW_E ) for 500 ms and then clears the display. Write this function and test it with a small test program. Write a similar function right() for the right arrow (image ARROW_W) and modify the function createSeg() so that a 0 in the list displays a left arrow and a 1 displays a right arrow. Test the program with different n.

### STEP 3: IMPLEMENTING USER PLAYBACK

 Now you implement the user interaction. When the user presses the left button, the number 0 is appended in the list seg2, when the right button is pressed, the number 1 is appended. Use the following template: ```n = 3 seg2 = [] while len(seq2) < n: if button_a.was_pressed(): seq2.append(0) if button_b.was_pressed(): seq2.append(1) sleep(10) ``` sleep(10) prevents the program from wasting processing time in the repeat loop (which makes it hard to break off). Complete the template so that an arrow is shown when a button is  pressed. Extend the program by comparing the two lists seq and seq2. If they are the same, the playback is correct, otherwise the game is over. To give a user feedback, you can display the images YES or NO.. ```if seq == seq2: display.show(Image.YES) ...```

### STEP 4: INCREASING THE DIFFICULTY LEVEL

 So far, the sequence has still the fixed length 3. Now step you begin the game with n = 2 and increase the length of the sequence by 1, if the answer is correct.

### STEP 5: HANDLING END OF GAME

 Finish the program by stopping the main loop when the game is over. It is common to use a boolean flag gameOver that is True, when the main loop has to be terminated. Use the following template: ```....n = 2 gameOver = False while not gameOver: seq = [] seq2 = [] createSeq(n) while len(seq2) < n: if button_a.was_pressed(): ..... if seq == seq2: .... n += 1 else: .... gameOver = True ``` It is nice to show the length of the last sequence as a scrolling text (the game score). You can restart the game any time by pressing the reset button.

### STEP 6: EXPANDING THE GAME TO OWN IDEAS

 You certainly have many ideas on how to customize or enhance the game. Suggestion: Modify the game to remember sound sequences. Check out the chapter Sound for how to play tones.