repository to manage all files for 1_2_oder_3 interaction game for Inf2/2 Interaktionen SoSe23 from Engert, Caliskan and Bachiri
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. from Database.database import QuestionDataBase
  2. import random
  3. class Game:
  4. def __init__(self, teamsize=0, correct_field=0):
  5. self.teamsize = teamsize
  6. self.scoreboard = {'score_red': 0,
  7. 'score_green': 0,
  8. 'score_blue': 0
  9. }
  10. self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db')
  11. self.available_questions = list(range(1, self.questions.num_rows()))
  12. self.field = correct_field
  13. self.question = {}
  14. self.final_score = {}
  15. def set_teamsize(self, teamsize: int):
  16. self.teamsize = teamsize
  17. def add_score(self, current_score: dict) -> dict:
  18. for key in self.scoreboard.keys():
  19. if key in current_score.keys():
  20. self.scoreboard[key] = self.scoreboard[key] + current_score[key]
  21. return self.scoreboard
  22. def set_scoreboard(self, current_scores: dict) -> dict:
  23. self.scoreboard = self.add_score(current_scores)
  24. return self.scoreboard
  25. def reset_game(self):
  26. self.scoreboard = {'score_red': 0,
  27. 'score_green': 0,
  28. 'score_blue': 0
  29. }
  30. def get_question(self):
  31. questions = self.questions.cursor.execute("Select * from Questions")
  32. field_names = [i[0] for i in questions.description]
  33. questions_data = questions.fetchall()
  34. random_question_number = random.choice(self.available_questions)
  35. self.available_questions.remove(random_question_number)
  36. question= list(questions_data[random_question_number])
  37. self.question = dict(zip(field_names, question))
  38. self.shuffle_answeroptions()
  39. self.add_correct_field()
  40. def shuffle_answeroptions(self):
  41. answeroptions = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3']
  42. keys = []
  43. for answer in answeroptions:
  44. keys.append(self.question[answer])
  45. random.shuffle(keys)
  46. for index, answer in enumerate(answeroptions):
  47. self.question[answer] = keys[index]
  48. def add_correct_field(self):
  49. answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption']
  50. for i in range(0, len(answeroption) -1):
  51. if self.question[answeroption[-1]] == self.question[answeroption[i]]:
  52. index = answeroption.index(answeroption[i]) + 1
  53. self.set_correct_field(index)
  54. break
  55. self.question.update([('Correct_field', index)])
  56. def set_correct_field(self, correct_field: int):
  57. self.field = correct_field
  58. def final_result(self):
  59. self.final_score = dict(sorted(self.scoreboard.items(), key=lambda x: x[1], reverse=True))