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.

game.py 2.8KB

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