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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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_blue': 0,
  8. 'score_green': 0}
  9. self.questions = QuestionDataBase('src_folder/BackEnd/Database/EinsZweiOderDrei.db')
  10. self.available_questions = list(range(1, self.questions.num_rows()))
  11. self.field = correct_field
  12. self.question = {}
  13. def set_teamsize(self, teamsize: int):
  14. self.teamsize = teamsize
  15. def add_score(self, current_score: dict):
  16. for key in self.scoreboard.keys():
  17. if key in current_score.keys():
  18. self.scoreboard[key] = self.scoreboard[key] + current_score[key]
  19. return self.scoreboard
  20. def set_scoreboard(self, current_scores: dict):
  21. self.scoreboard = self.add_score(current_scores)
  22. return self.scoreboard
  23. def reset_game(self):
  24. self.__init__()
  25. def get_question(self) -> dict:
  26. questions = self.questions.cursor.execute("Select * from Questions")
  27. field_names = [i[0] for i in questions.description]
  28. questions_data = questions.fetchall()
  29. random_question_number = random.choice(self.available_questions)
  30. self.available_questions.remove(random_question_number)
  31. question= list(questions_data[random_question_number])
  32. self.question = dict(zip(field_names, question))
  33. self.add_correct_field()
  34. return self.question
  35. def add_correct_field(self):
  36. answeroption = ['Answeroption_1', 'Answeroption_2', 'Answeroption_3', 'Correct_answeroption']
  37. for i in range(0, len(answeroption) -1):
  38. if self.question[answeroption[-1]] == self.question[answeroption[i]]:
  39. index = answeroption.index(answeroption[i]) + 1
  40. self.set_correct_field(index)
  41. break
  42. self.question.update([('Correct_field', index)])
  43. def set_correct_field(self, correct_field: int):
  44. self.field = correct_field