-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcard_matching_game_with_class_2.1.py
74 lines (60 loc) · 2.41 KB
/
card_matching_game_with_class_2.1.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'''
Name: card_matching_game_with_class_2.1.py
2023.04.22.15.19.36.279
initialize code 2.1
'''
import random
class CardGame:
def __init__(self):
self.board = ["▓" for _ in range(16)]
self.pairedList = self.getPairedList()
self.play()
def play(self):
self.displayBoard()
while not self.isGameOver():
try:
x1 = int(input("\nPlease choose the x-coordinate of the first card: "))
y1 = int(input("Please choose the y-coordinate of the first card: "))
cardIndex1 = self.getCardIndex(x1, y1)
self.flipCard(cardIndex1)
self.displayBoard()
x2 = int(input("\nPlease choose the x-coordinate of the second card: "))
y2 = int(input("Please choose the y-coordinate of the second card: "))
cardIndex2 = self.getCardIndex(x2, y2)
self.flipCard(cardIndex2)
self.displayBoard()
if x1 == x2 and y1 == y2:
print("Hey! You can't pick the same number twice.\n")
print("please try again")
elif self.board[cardIndex1] == self.board[cardIndex2]:
print("Congratulations! You found a match.")
self.pairedList[cardIndex1] = None
self.pairedList[cardIndex2] = None
else:
print(
f"Sorry, the cards {self.pairedList[cardIndex1]} and {self.pairedList[cardIndex2]} do not match.")
self.board[cardIndex1] = "▓"
self.board[cardIndex2] = "▓"
except ValueError:
print("Invalid input. Please enter integers for the coordinates.")
def getPairedList(self):
numbers = [str(i) for i in range(1, 9)]
pairedList = numbers + numbers
random.shuffle(pairedList)
return pairedList
def getCardIndex(self, x, y):
row = x - 1
col = y - 1
return (row * 4) + col
def flipCard(self, index):
self.board[index] = self.pairedList[index]
def displayBoard(self):
for i in range(4):
print(" ".join(self.board[i*4:i*4+4]))
#print("\n")
def isGameOver(self):
return all(card is None for card in self.pairedList)
if __name__ == "__main__":
# game = CardGame()
# game.play()
game = CardGame()