def play(game): """ Greet the user and start the main loop :param game: the game logic module :return: """ print(COMMON_INTRO_TEXT) print(game.DESCRIPTION) username = ask("May I have your name? ") print('Hello,', username) win_counter = 0 # counts while win_counter < NUMBER_OF_SUCCESSFUL_TRIES: # main loop question, right_answer = game.get_question_and_right_answer() print("Question: ", question) user_answer = ask('Your answer: ') if right_answer == user_answer: win_counter += 1 print("Correct!") else: # lose situation print(' \'', user_answer, '\' is wrong answer ;(. ' 'Correct answer was \'', right_answer, '\'') print('Let\'s try again, ', username, '!') break else: # all games sessions are successful print('Congratulations, ', username, '!')
def run(game): """Run game flow. Parameters: game: object with function get_round_data -> (quest, answer) and 'INSTRUCTION' for game. """ cli.print_text(config.HELLO_PATTERN) cli.print_text(game.INSTRUCTION, big_gap=True) name = cli.ask(text=config.NAME_PATTERN) cli.print_text(config.HELLO_NAME_PATTERN.format(name=name), big_gap=True) attempt = 0 while attempt < config.NUMBER_OF_ATTEMPTS: quest, right_answer = game.get_round_data() cli.print_text(config.QUESTION_PATTERN.format(quest=quest)) answer = ask_user(right_answer) if answer != right_answer: cli.print_text(config.WRONG_PATTERN.format( wrong_answer=answer, right_answer=right_answer, )) cli.print_text(config.LOSE_PATTERN.format(name=name), big_gap=True) break cli.print_text(config.CORRECT_PATTERN, big_gap=True) attempt += 1 else: cli.print_text(config.WIN_PATTERN.format(name=name), big_gap=True)
def ask_user(right_answer: str) -> str: """Ask user answer. It can be: number answer, yes/no answer or else. Parameters: right_answer: expect answer Returns: User answer """ if re.search(config.RE_ANSWER_PATTERN_NUMBER, right_answer): return cli.ask(pattern=config.RE_ANSWER_PATTERN_NUMBER) elif re.search(config.RE_ANSWER_PATTERN_YES_OR_NO, right_answer): return cli.ask(pattern=config.RE_ANSWER_PATTERN_YES_OR_NO) return cli.ask()
def run(game): """ This is a game engine for Brain Games. Parameteres: game (module): module with implementation of the game which user chosed """ cli.welcome() # print welcome to brain games game.show_description() # print game description imported from game file name = cli.get_name() # request user name wins = 0 # counter for wins while wins < 3: question, answer = game.get_question() # generate question cli.ask(question) # print the question for the user guess = cli.get_guess() # receive guess from the user if guess != answer: cli.inform_about_wrong_answer(guess, answer, name) # show message when the answer is wrong return cli.inform_about_correct_answer() # show message when the answer is correct wins += 1 cli.congratulate(name) # print congratulations for user
def run(game, rounds=3): """Template for brain game engine.""" print('Welcome to the Brain Games!') print(game.RULES) user_name = welcome_user() while rounds > 0: question, true_answer = game.play_round() print('Question: {0}'.format(question)) answer = ask('Your answer: ') if answer != true_answer: print(WRONG_SAMPLE.format(answer, true_answer, user_name)) break print('Correct!') rounds -= 1 else: print('Congratulations!')
def run(game): """Define game engine. Argument: game -- module than contains description and game logic """ username = welcome_user(game.DESCRIPTION) wins = 0 while wins < MAX_WINS: question, correct_answer = game.logic() print(f'Question: {question}') user_answer = ask(f'Your answer: ') if user_answer == correct_answer: print('Correct!') wins += 1 else: print(f"'{user_answer}' is wrong answer ;(. Correct answer" f" was '{correct_answer}'. " f"Let's try again, {username}!") return print(f"Congratulations, {username}!")