-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.py
57 lines (45 loc) · 1.77 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# game.py - A game of concentration.
import concentration
# represents the face of each card
cards = ['☺', '♦', '♥', '☼', '☻', '♫', '☺', '♣', '♠', '☼', '♫', '♥', '♣', '♦', '☻', '♠']
# represents whether to show the face of each card
show = [False]*16
# represents the number of matches made
matches = 0
# represents the number of turns taken
turns = 0
# print the welcome screen and instructions
concentration.intro()
# the game loops until all the matches have been made
while matches != 8:
# print the 'cards' list based on the 'show' list
concentration.print_cards(cards, show)
# get a valid card guess from the user
guess1 = concentration.guess_card(show)
# update the 'show' list to show the face of the card when printed
show[guess1] = True
concentration.print_cards(cards, show)
guess2 = concentration.guess_card(show)
show[guess2] = True
concentration.print_cards(cards, show)
# check if the guessed cards are a matching pair
if cards[guess1] == cards[guess2]:
print('\nMatch! :)')
# increment the number of matches made
matches += 1
else:
print('\nNo match. :(')
# update the 'show' list to hide the face of the card when printed
show[guess1] = False
show[guess2] = False
# increment the number of turns taken
turns += 1
# check if all the matches have been made
# this is to avoid pausing after the game is won
if matches != 8:
# pause before starting the next turn and allow the user to quit or continue playing
concentration.pause()
else: # all the matches have been made
# print the win screen and the number of turns
print('\nCongratulations! Well done!')
print('\nNumber of turns:', turns, '\n')