-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryGame.py
More file actions
93 lines (68 loc) · 2.6 KB
/
Copy pathMemoryGame.py
File metadata and controls
93 lines (68 loc) · 2.6 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
This program runs the "Memory Game". The default of the board
is 4x4, however this can be changed by solely changing the values
of the constants at the beginning.
Name: Eduardo Pareja Lema
"""
import random
import string
import time
NUM_ROWS = 4
NUM_COLUMNS = 4
ordered_nums = [i for i in range(1, NUM_ROWS*NUM_COLUMNS+1)]
nums = [i for i in range(1, NUM_ROWS*NUM_COLUMNS+1)]
letters = list(string.ascii_uppercase)[0:(NUM_ROWS*NUM_COLUMNS//2)]*2
random.shuffle(letters)
def random_board():
"""Displays the board of numbers"""
for i in range(len(nums)):
print(str(nums[i]).ljust(3), end='')
if ordered_nums[i]%NUM_COLUMNS == 0:
print()
def play_game():
"""Runs the memory game"""
random_board()
count = 0
num_stops = 0
correct_guesses = []
initial_time = time.time()
while True:
guesses = input("Guess two squares: ")
guess1 = guesses.split()[0]
guess2 = guesses.split()[1]
if (int(guess1)>int(ordered_nums[-1]) or int(guess2)>int(ordered_nums[-1])
or guess1 in correct_guesses or guess2 in correct_guesses
or guess1 == guess2):
print("Invalid number(s). ")
elif letters[int(guess1)-1] == letters[int(guess2)-1]: #If letters are equal
nums[int(guess1)-1] = letters[int(guess1)-1]
nums[int(guess2)-1] = letters[int(guess2)-1] #Update the nums list
correct_guesses.append(guess1)
correct_guesses.append(guess2)
random_board()
count += 1
else:
nums[int(guess1)-1] = letters[int(guess1)-1]
nums[int(guess2)-1] = letters[int(guess2)-1]
random_board()
time.sleep(2)
#Updates the nums list for 2 seconds
for _ in range(50):
print()
nums[int(guess1)-1] = ordered_nums[int(guess1)-1]
nums[int(guess2)-1] = ordered_nums[int(guess2)-1]#Undo after 2 seconds
random_board()
count += 1
num_stops += 1
if nums == letters:
final_time = time.time()
game_duration = final_time - initial_time
for _ in range(50):
print()
print("You win!")
random_board()
print("It took you " + str(count) + " guesses and "
+ str(int(game_duration)) + " seconds.")
break
if __name__ == '__main__':
play_game()