def present_question_hh( display_num, question_id, question_text, buzzes, final, correct, score, power="10" ): even_delta = 0 odd_delta = 0 question_value = 15 final_system = random.choice(list(final.keys())) final_answer = final[final_system] # Find out where the computer would buzz computer_position = (max(question_text) + 1, 0) for ss in sorted(question_text): words = question_text[ss].split() for ii, ww in enumerate(words): current_guesses = buzzes.current_guesses(question_id, ss, ii - 1) buzz_now = [x for x in current_guesses.values() if x.final] if len(buzz_now) > 0: computer_position = ss, ii computer_guess = buzz_now[0].page computer_system = buzz_now[0].system break question_text[ss] += "~ ~ ~ ~ ~" question_done = False human_delta = 0 computer_delta = 0 for ss in sorted(question_text): words = question_text[ss].split() for ii, ww in enumerate(words): if question_done: break if (ww == "~" and ss == max(question_text)) and ( computer_delta == 0 and human_delta <= 0 ): # Computer has reached end of question # answer(final_answer.split('(')[0], final_system) if final == correct: computer_delta = 10 if computer_position[0] == ss and computer_position[1] == ii: # This is where the computer buzzes if human_delta <= 0: if computer_guess == correct: # os.system("afplay sounds/applause.wav") if human_delta <= 0: computer_delta = question_value else: # answer(computer_guess, computer_system) # os.system("afplay sounds/sad_trombone.wav") if human_delta == 0: computer_delta = -5 else: # answer(computer_guess, computer_system) question_done = True computer_delta = 0 current_guesses = buzzes.current_guesses(question_id, ss, ii) if ww.lower().startswith(power.lower()): question_value = 10 press = interpret_keypress() if isinstance(press, int): # Check to see if buzz is valid if human_delta > 0: continue if even_delta != 0 and press % 2 == 0: continue if odd_delta != 0 and press % 2 != 0: continue os.system("afplay /System/Library/Sounds/Glass.aiff") response = None while response is None: response = input("Player %i, provide an answer:\t" % press) if "+" in response: if press % 2 == 0: even_delta = question_value else: odd_delta = question_value if computer_delta < 0 and human_delta == 0: # Computer got it wrong before human_delta = question_value question_done = True elif computer_delta == 0 and human_delta < 0: # Other team guessed wrong before computer_delta = question_value elif computer_delta == 0: human_delta = question_value question_done = True question_done = True elif "-" in response: if even_delta == 0 and press % 2 != 0: odd_delta = -5 if odd_delta == 0 and press % 2 == 0: even_delta = -5 if computer_delta == 0: human_delta = -5 # Break if both teams have answered if even_delta != 0 and press % 2 != 0: if computer_delta == 0 and final_answer == correct: computer_delta = 10 question_done = True if odd_delta != 0 and press % 2 == 0: if computer_delta == 0 and final_answer == correct: computer_delta = 10 question_done = True else: response = None # Don't buzz if anyone else has gotten it wrong else: show_score( score.even + even_delta, score.odd + odd_delta, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW", ) show_score( score.human + human_delta, score.computer + computer_delta, "HUMAN", "COMPUTER", flush=False, ) print(human_delta, computer_delta, even_delta, odd_delta) print( format_display( display_num, question_text, ss, ii + 1, current_guesses, answer=correct, points=question_value, ) ) return Score(even_delta, odd_delta, human_delta, computer_delta)
def present_question_hh(display_num, question_id, question_text, buzzes, final, correct, score, power="10"): even_delta = 0 odd_delta = 0 question_value = 15 final_system = random.choice(list(final.keys())) final_answer = final[final_system] # Find out where the computer would buzz computer_position = (max(question_text) + 1, 0) for ss in sorted(question_text): words = question_text[ss].split() for ii, ww in enumerate(words): current_guesses = buzzes.current_guesses(question_id, ss, ii - 1) buzz_now = [x for x in current_guesses.values() if x.final] if len(buzz_now) > 0: computer_position = ss, ii computer_guess = buzz_now[0].page computer_system = buzz_now[0].system break question_text[ss] += "~ ~ ~ ~ ~" question_done = False human_delta = 0 computer_delta = 0 for ss in sorted(question_text): words = question_text[ss].split() for ii, ww in enumerate(words): if question_done: break if (ww == "~" and ss == max(question_text)) and \ (computer_delta == 0 and human_delta <= 0): # Computer has reached end of question # answer(final_answer.split('(')[0], final_system) if final == correct: computer_delta = 10 if computer_position[0] == ss and computer_position[1] == ii: # This is where the computer buzzes if human_delta <= 0: if computer_guess == correct: # os.system("afplay sounds/applause.wav") if human_delta <= 0: computer_delta = question_value else: # answer(computer_guess, computer_system) # os.system("afplay sounds/sad_trombone.wav") if human_delta == 0: computer_delta = -5 else: # answer(computer_guess, computer_system) question_done = True computer_delta = 0 current_guesses = buzzes.current_guesses(question_id, ss, ii) if ww.lower().startswith(power.lower()): question_value = 10 press = interpret_keypress() if isinstance(press, int): # Check to see if buzz is valid if human_delta > 0: continue if even_delta != 0 and press % 2 == 0: continue if odd_delta != 0 and press % 2 != 0: continue os.system("afplay /System/Library/Sounds/Glass.aiff") response = None while response is None: response = input("Player %i, provide an answer:\t" % press) if '+' in response: if press % 2 == 0: even_delta = question_value else: odd_delta = question_value if computer_delta < 0 and human_delta == 0: # Computer got it wrong before human_delta = question_value question_done = True elif computer_delta == 0 and human_delta < 0: # Other team guessed wrong before computer_delta = question_value elif computer_delta == 0: human_delta = question_value question_done = True question_done = True elif '-' in response: if even_delta == 0 and press % 2 != 0: odd_delta = -5 if odd_delta == 0 and press % 2 == 0: even_delta = -5 if computer_delta == 0: human_delta = -5 # Break if both teams have answered if even_delta != 0 and press % 2 != 0: if computer_delta == 0 and final_answer == correct: computer_delta = 10 question_done = True if odd_delta != 0 and press % 2 == 0: if computer_delta == 0 and final_answer == correct: computer_delta = 10 question_done = True else: response = None # Don't buzz if anyone else has gotten it wrong else: show_score(score.even + even_delta, score.odd + odd_delta, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW") show_score(score.human + human_delta, score.computer + computer_delta, "HUMAN", "COMPUTER", flush=False) print(human_delta, computer_delta, even_delta, odd_delta) print(format_display(display_num, question_text, ss, ii + 1, current_guesses, answer=correct, points=question_value)) return(Score(even_delta, odd_delta, human_delta, computer_delta))
def check_hh_tie(score): """ For the computer-human and human-human programs, this needs to be different. This is why it's a silly function. """ return score.even == score.odd if __name__ == "__main__": flags = create_parser() questions, buzzes = load_data(flags) print("Done loading data") clear_screen() buzzer_check(flags.players) score = question_loop(flags, questions, buzzes, present_question_hh, check_hh_tie) show_score(score.human, score.computer, "HUMAN", "COMPUTER") show_score( score.even, score.odd, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW", )
def present_question(display_num, question_id, question_text, buzzes, final, correct, score, power="10"): even_delta = 0 odd_delta = 0 question_value = 15 # Find out where the computer would buzz computer_position = (max(question_text) + 1, 0) for ss in sorted(question_text): words = question_text[ss].split() for ii, ww in enumerate(words): current_guesses = buzzes.current_guesses(question_id, ss, ii) buzz_now = [x for x in current_guesses.values() if x.final] if len(buzz_now) == 1: computer_position = ss, ii computer_guess = buzz_now[0].page break question_text[ss] += " WAIT WAIT WAIT WAIT WAIT " question_done = False human_delta = 0 computer_delta = 0 for ss in sorted(question_text): words = question_text[ss].split() for ii, ww in enumerate(words): if question_done: break if computer_position[0] == ss and computer_position[1] == ii: # This is where the computer buzzes if human_delta <= 0: if computer_guess == correct: os.system("afplay sounds/applause.wav") if human_delta <= 0: computer_delta = question_value else: answer(computer_guess) os.system("afplay sounds/sad_trombone.wav") if human_delta == 0: computer_delta = -5 else: answer(computer_guess) question_done = True computer_delta = 0 current_guesses = buzzes.current_guesses(question_id, ss, ii) if lower(ww).startswith(lower(power)): question_value = 10 press = interpret_keypress() if press: # Check to see if buzz is valid if human_delta > 0: continue if even_delta != 0 and press % 2 == 0: continue if odd_delta != 0 and press % 2 != 0: continue os.system("afplay /System/Library/Sounds/Glass.aiff") response = None while response is None: response = raw_input("Player %i, provide an answer:\t" % press) if '+' in response: if press % 2 == 0: even_delta = question_value else: odd_delta = question_value if computer_delta < 0 and human_delta == 0: human_delta = question_value question_done = True elif computer_delta == 0: human_delta = question_value elif '-' in response: if even_delta == 0 and press % 2 != 0: odd_delta = -5 if odd_delta == 0 and press % 2 == 0: even_delta = -5 if computer_delta < 0: human_delta = -5 # Break if both teams have answered if even_delta != 0 and press % 2 != 0: question_done = True if odd_delta != 0 and press % 2 == 0: question_done = True else: response = None # Don't buzz if anyone else has gotten it wrong else: show_score(score.even + even_delta, score.odd + odd_delta, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW") show_score(score.human + human_delta, score.computer + computer_delta, "HUMAN", "COMPUTER", flush=False) print(human_delta, computer_delta, even_delta, odd_delta) print( format_display(display_num, question_text, ss, ii + 1, current_guesses, answer=correct, points=question_value)) # Now see what the computer would do if computer_delta == 0 and human_delta <= 0: answer(final.split('(')[0]) if final == correct: computer_delta = 10 else: print("Computer guesses incorrectly: %s" % final) elif computer_delta > 0: answer(computer_guess) format_display(display_num, question_text, computer_position[0], computer_position[1], current_guesses, answer=correct, points=computer_delta) return score.add(Score(even_delta, odd_delta, human_delta, computer_delta))
return(Score(even_delta, odd_delta, human_delta, computer_delta)) def check_hh_tie(score): """ For the computer-human and human-human programs, this needs to be different. This is why it's a silly function. """ return score.even == score.odd if __name__ == "__main__": flags = create_parser() questions, buzzes = load_data(flags) print("Done loading data") clear_screen() buzzer_check(flags.players) score = question_loop(flags, questions, buzzes, present_question_hh, check_hh_tie) show_score(score.human, score.computer, "HUMAN", "COMPUTER") show_score(score.even, score.odd, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW")
def present_question(question, state, events, player=-1): assert state in kSTATE, "Invalid state %s" % state if question > 26: return odd_score = sum(events[x] for x in events if x[1] % 2 == 1) even_score = sum(events[x] for x in events if x[1] % 2 == 0) show_score(odd_score, even_score, "ODD TEAM", "EVEN TEAM", left_color="RED", right_color="YELLOW") print("Question %i" % question) if state == "AB": print("Free buzz!") press = interpret_keypress() if press is None: present_question(question, state, events) if press == " ": present_question(question + 1, "AB", events) if press % 2 == 0: os.system("say -v Tom Player %s" % press) present_question(question, "ES", events, press) if press % 2 == 1: os.system("say -v Tom Player %s" % press) present_question(question, "OS", events, press) elif state == "EB": print("Even buzz!") press = interpret_keypress() if press is None: present_question(question, state, events) if press == " ": present_question(question + 1, "AB", events) if press % 2 == 1: present_question(question, "EB", events) if press % 2 == 0: os.system("say -v Tom Player %s" % press) present_question(question, "ES", events, press) elif state == "OB": print("Odd buzz!") press = interpret_keypress() if press is None: present_question(question, state, events) if press == " ": present_question(question + 1, "AB", events) if press % 2 == 0: present_question(question, "OB", events) if press % 2 == 1: os.system("say -v Tom Player %s" % press) present_question(question, "OS", events, press) elif state == "ES" or state == "OS": assert player != -1, "invalid player" tossup = '0' while not tossup in kALLOWABLE_TOSSUP: print("Tossup %i result [%s]:" % (question, kALLOWABLE_TOSSUP), end='') tossup = interpret_keypress(kALLOWABLE_TOSSUP) if tossup == "P": tossup = 15 elif tossup == "N": tossup = -5 elif tossup == "G": tossup = 0 elif tossup == "C": tossup = 10 if tossup <= 0: if any(x[0] == question for x in events): # Both have buzzed events[(question, player, "TU")] = 0 present_question(question + 1, "AB", events) else: # Nobody buzzed events[(question, player, "TU")] = tossup present_question(question, "EB" if state == "OS" else "OB", events) else: events[(question, player, "TU")] = tossup odd_score = sum(events[x] for x in events if x[1] % 2 == 1) even_score = sum(events[x] for x in events if x[1] % 2 == 0) show_score(odd_score, even_score, "ODD TEAM", "EVEN TEAM", left_color="RED", right_color="YELLOW") bonus = "0" while not bonus in kALLOWABLE_BONUS: print("Bonus %i result [%s]:" % (question, kALLOWABLE_BONUS), end='') bonus = interpret_keypress(kALLOWABLE_BONUS) if bonus == "Z": events[(question, player, "BONUS")] = 0 elif bonus == "F": events[(question, player, "BONUS")] = 10 elif bonus == "S": events[(question, player, "BONUS")] = 20 elif bonus == "T": events[(question, player, "BONUS")] = 30 pickle.dump(events, open(kOUTPUT, 'w')) present_question(question + 1, "AB", events)
for ii in question_ids: question_num += 1 o.write("%i) " % question_num) for jj in questions[ii]: o.write("%s " % questions[ii][jj]) o.write("\nANSWER: %s\n\n" % questions.answer(ii)) question_num = 0 question_ids = list(question_ids)[flags.skip:] for ii in question_ids: # print(ii, questions[ii]) question_num += 1 power_mark = power(ii) score = present_question(question_num, ii, questions[ii], buzzes, finals[ii], questions.answer(ii), score=score, power=power(ii)) print("Correct answer of Question %i: %s" % (question_num, questions.answer(ii))) sleep(kPAUSE) if question_num > flags.max_questions - 1: break show_score(score.human, score.computer)
def present_question(display_num, question_id, question_text, buzzes, final, correct, score, power="10"): even_delta = 0 odd_delta = 0 question_value = 15 # Find out where the computer would buzz computer_position = (max(question_text) + 1, 0) for ss in sorted(question_text): words = question_text[ss].split() for ii, ww in enumerate(words): current_guesses = buzzes.current_guesses(question_id, ss, ii) buzz_now = [x for x in current_guesses.values() if x.final] if len(buzz_now) == 1: computer_position = ss, ii computer_guess = buzz_now[0].page break question_text[ss] += " %s " % " ".join([kPADDING] * 5) question_done = False human_delta = 0 computer_delta = 0 human_answers = 0 computer_answers = 0 for ss in sorted(question_text): words = question_text[ss].split() for ii, ww in enumerate(words): if question_done or human_answers==2: break if computer_position[0] == ss and computer_position[1] == ii: # This is where the computer buzzes if human_delta <= 0: if computer_guess == correct: if human_answers == 2: answer(computer_guess.split("(")[0]) computer_answers += 1 question_done = True os.system("afplay sounds/applause.wav") if human_delta <= 0: computer_delta = question_value else: answer(computer_guess.split("(")[0]) computer_answers += 1 os.system("afplay sounds/sad_trombone.wav") if human_delta == 0: computer_delta = -5 else: answer(computer_guess.split("(")[0]) computer_answers += 1 question_done = True computer_delta = 0 current_guesses = buzzes.current_guesses(question_id, ss, ii) if lower(ww).startswith(lower(power)): question_value = 10 press = interpret_keypress() if isinstance(press, int): # Check to see if buzz is valid if human_delta > 0: continue if even_delta != 0 and press % 2 == 0: continue if odd_delta != 0 and press % 2 != 0: continue human_answers += 1 os.system("afplay /System/Library/Sounds/Glass.aiff") response = None while response is None: response = raw_input("Player %i, provide an answer:\t" % press) if '+' in response: if press % 2 == 0: even_delta = question_value else: odd_delta = question_value if computer_delta < 0 and human_delta == 0: human_delta = question_value question_done = True elif computer_delta == 0 and human_delta == 0: human_delta = question_value elif '-' in response: if even_delta == 0 and press % 2 != 0 and ww != kPADDING: odd_delta = -5 if odd_delta == 0 and press % 2 == 0 and ww != kPADDING: even_delta = -5 if computer_delta == 0: human_delta = -5 # Break if both teams have answered if even_delta != 0 and press % 2 != 0: question_done = True if odd_delta != 0 and press % 2 == 0: question_done = True else: response = None # Don't buzz if anyone else has gotten it wrong else: show_score(score.even + even_delta, score.odd + odd_delta, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW") show_score(score.human + human_delta, score.computer + computer_delta, "HUMAN", "COMPUTER", flush=False) print(format_display(display_num, question_text, ss, ii + 1, current_guesses, answer=correct, points=question_value)) # Now see what the computer would do if computer_delta == 0 and human_delta <= 0 and computer_answers == 0: show_score(score.even + even_delta, score.odd + odd_delta, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW") show_score(score.human + human_delta, score.computer + computer_delta, "HUMAN", "COMPUTER", flush=False) print(format_display(display_num, question_text, max(question_text) - 1, 1000, current_guesses, answer=correct, points=computer_delta)) answer(final.split('(')[0]) if final == correct: computer_delta = 10 else: print("Computer guesses incorrectly: %s" % final) elif computer_delta > 0 and computer_answers == 0: format_display(display_num, question_text, computer_position[0], computer_position[1], current_guesses, answer=correct, points=computer_delta) answer(computer_guess) return score.add(Score(even_delta, odd_delta, human_delta, computer_delta))
if flags.readable != "": write_readable(flags.readable, question_ids, questions, power) question_num = 0 question_ids = list(question_ids)[flags.skip:] for ii in question_ids: # print(ii, questions[ii]) question_num += 1 power_mark = power(ii) score = present_question(question_num, ii, questions[ii], buzzes, finals[ii], questions.answer(ii), score=score, power=power(ii)) print("Correct answer of Question %i: %s" % (question_num, questions.answer(ii))) sleep(kPAUSE) if question_num > flags.max_questions - 1: break show_score(score.even, score.odd, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW") show_score(score.human, score.computer)
question_num = 0 question_ids = list(question_ids)[flags.skip:] for ii in question_ids: # print(ii, questions[ii]) question_num += 1 power_mark = power(ii) score = present_question(question_num, ii, questions[ii], buzzes, finals[ii], questions.answer(ii), score=score, power=power(ii)) print("Correct answer of Question %i: %s" % (question_num, questions.answer(ii))) sleep(kPAUSE) if question_num > flags.max_questions - 1: break show_score(score.even, score.odd, "TEAM A", "TEAM B", left_color="RED", right_color="YELLOW") show_score(score.human, score.computer)
if flags.readable != "": question_num = 0 o = open(flags.readable, 'w') for ii in question_ids: question_num += 1 o.write("%i) " % question_num) for jj in questions[ii]: o.write("%s " % questions[ii][jj]) o.write("\nANSWER: %s\n\n" % questions.answer(ii)) question_num = 0 question_ids = list(question_ids)[flags.skip:] for ii in question_ids: # print(ii, questions[ii]) question_num += 1 power_mark = power(ii) score = present_question(question_num, ii, questions[ii], buzzes, finals[ii], questions.answer(ii), score=score, power=power(ii)) print("Correct answer of Question %i: %s" % (question_num, questions.answer(ii))) sleep(kPAUSE) if question_num > flags.max_questions - 1: break show_score(score.human, score.computer)