1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- from Database.database import QuestionDataBase
- import random
-
-
-
- class Game:
- def __init__(self, teamsize=0, correct_field=0):
- self.teamsize = teamsize
- self.scoreboard = {'score_red': 0,
- 'score_green': 0,
- 'score_blue': 0
- }
- self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db')
- self.available_questions = list(range(1, self.questions.num_rows()))
- self.field = correct_field
- self.question = {}
- self.final_score = {}
-
- def set_teamsize(self, teamsize: int):
- self.teamsize = teamsize
-
- def add_score(self, current_score: dict) -> dict:
- for key in self.scoreboard.keys():
- if key in current_score.keys():
- self.scoreboard[key] = self.scoreboard[key] + current_score[key]
- return self.scoreboard
-
- def set_scoreboard(self, current_scores: dict) -> dict:
- self.scoreboard = self.add_score(current_scores)
- return self.scoreboard
-
- def reset_game(self):
- self.scoreboard = {'score_red': 0,
- 'score_green': 0,
- 'score_blue': 0
- }
-
- def get_question(self):
- questions = self.questions.cursor.execute("Select * from Questions")
- field_names = [i[0] for i in questions.description]
-
- questions_data = questions.fetchall()
- random_question_number = random.choice(self.available_questions)
- self.available_questions.remove(random_question_number)
- question= list(questions_data[random_question_number])
- self.question = dict(zip(field_names, question))
- self.shuffle_answeroptions()
- self.add_correct_field()
-
- def shuffle_answeroptions(self):
- answeroptions = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3']
- keys = []
-
- for answer in answeroptions:
- keys.append(self.question[answer])
- random.shuffle(keys)
-
- for index, answer in enumerate(answeroptions):
- self.question[answer] = keys[index]
-
- def add_correct_field(self):
- answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption']
- for i in range(0, len(answeroption) -1):
- if self.question[answeroption[-1]] == self.question[answeroption[i]]:
- index = answeroption.index(answeroption[i]) + 1
- self.set_correct_field(index)
- break
- self.question.update([('Correct_field', index)])
-
- def set_correct_field(self, correct_field: int):
- self.field = correct_field
-
- def final_result(self):
- self.final_score = dict(sorted(self.scoreboard.items(), key=lambda x: x[1], reverse=True))
|