Task 03: Complex Programs¶
Implement the game Tic-Tac-Toe.
Each round starts with an empty board of 3×3 fields.
Both players (in our case humans, adding a computer player might be a bit too much) take turns and select one empty field to place their mark. (usually indicated by the symbols
The first one to get three marks in a row wins.
If the board is full before anyone can get three marks in a row it is a draw.
You want to define some constants to make it easier to identify the various elements of the game and help making your code more legible. Consider wich would be a suitable data type for each of the elements: * Define a constant for each of the players * Define a constant for each of the fields on the board
You also need to remember things during the game: * Who the current player is * Which fields carry which symbol, if any
Write some helper functions for the following situations:
* Getting the next move from the current player
* Output: a valid field that the player has chosen
* Changing the current player to the next one
* Checking the board if one of the players have won
* Output: Either the winning player or
* Checking the board if it is a draw
* Printing the current game board with all the symbols on it
Tying it all Together¶
Use your building blocks to implement a whole Tic-Tac-Toe game. Don’t forget to test it!
Hint: This task is about dealing with a complex problem. Start with the building blocks first and test them using the interpreter mode. After they work, tie them together into the whole game.
b) Guitar Hero¶
The chromatic scale is a common way of denoting music (especially in western cultures).
It is composed of infinite half-tone steps of increasing pitch. Often when denoting the scale, the note
C is used as a reference point.
Here are all notes in order (beginning with
- The number indicates the octave in which the note lies
- Each octave contains 12 half-tone steps
- Octaves increase on every transition from
C0is the lowest note humans can hear at around 16.35 Hz
- Fun fact: This is called the International (or scientific) pitch notation, the alternative being the Helmholtz Pitch Notation
#-character is read as
- It corresponds with the black keys on a piano
You probably do want to represent notes as whole numbers, instead of strings for the ease of calculating.
For this you will have to choose which note will be your “0”-note. (In principle this can be any note; Good candidates could be
- Write a function
half_tone_offset(…)that for a given base note and a given half-tone offset calculates the corresponding note.
- Devise also a function
note_to_str(…)that can translate your notes into a string for printing.
- For later convenience have all notes take up 3 characters, so they align nicely (you can pad them with spaces, for example).
>>> my_note = half_tone_offset(base_note=NOTE_C4, offset=18) >>> print(note_to_str(my_note)) F5# >>> my_other_note = half_tone_offset(base_note=NOTE_C4, offset=-1) >>> print(note_to_str(my_other_note)) B3
* This musical scale repeats itself over and over again. In such cases the
%-operator (called modulo) usually is a good friend.
* Solve the three components of the printing task separately:
* What is the base note?
* Which is the correct octave number?
* Does it have the
* Defining some constants can go a long way to create more readable and easier to understand code
A Guitar String¶
On a guitar, each string has a base note that sounds when you pluck it. Along the neck of the guitar, so-called frets are placed. When you pinch behind the first fret the string makes a sound one half-tone higher than the base note. If you pinch behind the second fret the sound becomes two half-tones higher, and so on…
Write a function that for a given base note and a given amount of frets prints all the notes along the guitar strings.
>>> print_guitar_string(base_note=NOTE_E3, frets=15) E3 | F3 F3# G3 G3# A3 A3# B3 C4 C4# D4 D4# E4 F4 F4# G4
A Guitar Cheat-Sheet¶
The standard tuning for the six strings of a guitar is
- Write a function that for a given tuning (i.e. a list of notes) and a given amount of frets prints a guitar cheat sheet. Sort the tuning list beforehand. The strings get printed from the highest pitch on top row to the lowest on the bottom row.
>>> standard_tuning = [NOTE_E2, NOTE_A2, NOTE_D3, NOTE_G3, NOTE_B3, NOTE_E4] >>> guitar_cheat_sheet(tuning=standard_tuning, frets=4) E4 | F4 F4# G4 G4# B3 | C4 C4# D4 D4# G3 | G3# A3 A3# B3 D3 | D3# E3 F3 F3# A2 | A2# B2 C3 C3# E2 | F2 F2# G2 G2#
- Optional: include an option to only print non-sharp notes, but keep the arrangement of the other notes. Include a indicator for the frets at the top and bottom to aid orientation (pay attention if your fret numbers go into the double digits!).
>>> guitar_cheat_sheet(tuning=standard_tuning, frets=4, include_sharp=False) 0 1 2 3 4 ----+---+---+---+---+ E4 | F4 G4 B3 | C4 D4 G3 | A3 B3 D3 | E3 F3 A2 | B2 C3 E2 | F2 G2 ----+---+---+---+---+ 0 1 2 3 4
- Optional: Learn to play the guitar. You have the cheat sheet already :)