-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.py
More file actions
183 lines (153 loc) · 5.55 KB
/
Player.py
File metadata and controls
183 lines (153 loc) · 5.55 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""
This module implements the logic and the requirements to create a new Player object.
"""
# Import appropriate libraries
import csv
class Player:
"""
A class to represent a player in the game.
Attributes:
- name (str): The player's username.
- password (str): The player's password.
- level (int): The player's current level in the game.
- addition (int): The player's high score in addition questions.
- subtraction (int): The player's high score in subtraction questions.
- multiplication (int): The player's high score in multiplication questions.
- division (int): The player's high score in division questions.
- bosses (int): The number of bosses the player has defeated.
"""
def __init__ (self, name, password):
"""
Constructs all the necessary attributes for the player object.
Parameters:
- name (str): The name of the player.
- password (str): The password of the player.
"""
self.name = name
self.password = password
self.level = 0
self.addition = 0
self.subtraction = 0
self.multiplication = 0
self.division = 0
self.bosses = 0
def load_player(self):
"""
Loads the player's data from a CSV file based on their username and password.
"""
# Open the CSV file and obtian the player's information
with open("data.csv", newline = '') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
username, password = row[0], row[1]
# If the player exists
if username == self.name and password == self.password:
# Set the scores to the player's scores within the file
self.addition = row[2]
self.subtraction = row[3]
self.multiplication = row[4]
self.division = row[5]
self.bosses = row[6]
def save_info(self):
"""
Saves the current player's data to a CSV file, updating their scores and information.
"""
player = []
# Read current info and match with current player
with open("data.csv", "r", newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if row[0] == self.name and row[1] == self.password:
# Update this player's data
row[2] = self.addition
row[3] = self.subtraction
row[4] = self.multiplication
row[5] = self.division
row[6] = self.bosses
player.append(row)
# Write the updated/all data back to the CSV
with open("data.csv", "w", newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerows(player)
def update_bosses (self, new_bosses_score):
"""
Updates the player's boss defeat count and saves the new information.
Parameters:
- new_bosses_score (int): The new boss defeat count.
"""
self.bosses = new_bosses_score
self.save_info()
def update_add (self, new_add_score):
"""
Updates the player's addition high score and saves the new information.
Parameters:
- new_add_score (int): The new addition high score.
"""
self.addition = new_add_score
self.save_info()
def update_mul (self, new_mul_score):
"""
Updates the player's multiplication high score and saves the new information.
Parameters:
- new_mul_score (int): The new multiplication high score.
"""
self.multiplication = new_mul_score
self.save_info()
def update_div (self, new_div_score):
"""
Updates the player's division high score and saves the new information.
Parameters:
- new_div_score (int): The new division high score.
"""
self.division = new_div_score
self.save_info()
def update_sub (self, new_sub_score):
"""
Updates the player's subtraction high score and saves the new information.
Parameters:
- new_sub_score (int): The new subtraction high score.
"""
self.subtraction = new_sub_score
self.save_info()
def get_name (self):
"""
Returns the player's username.
Returns:
- str: The player's username.
"""
return self.name
def get_bosses (self):
"""
Returns the player's boss defeat count.
Returns:
- int: The number of bosses the player has defeated.
"""
return self.bosses
def get_add (self):
"""
Returns the player's addition high score.
Returns:
- int: The player's high score in addition questions.
"""
return self.addition
def get_mul (self):
"""
Returns the player's multiplication high score.
Returns:
- int: The player's high score in multiplication questions.
"""
return self.multiplication
def get_div (self):
"""
Returns the player's division high score.
Returns:
- int: The player's high score in division questions.
"""
return self.division
def get_sub (self):
"""
Returns the player's subtraction high score.
Returns:
- int: The player's high score in subtraction questions.
"""
return self.subtraction