Skip to content

Instantly share code, notes, and snippets.

@dmd
Created March 17, 2023 12:28
Show Gist options
  • Save dmd/4f33a6972cbb8aed35608a9e6bf804bc to your computer and use it in GitHub Desktop.
Save dmd/4f33a6972cbb8aed35608a9e6bf804bc to your computer and use it in GitHub Desktop.
import requests
import curses
from fuzzywuzzy import fuzz
# Function to initialize the board with random questions
def initialize_board():
board = []
for i in range(6):
category_data = requests.get("http://jservice.io/api/random?count=5").json()
board.append(category_data)
# Initialize 'answered' key for each question
for i, category in enumerate(board):
for j, question in enumerate(category):
board[i][j]['answered'] = False
return board
# Function to draw the Jeopardy board on screen
def draw_board(screen, board, score):
screen.clear()
screen.addstr(1, 1, "JEOPARDY BOARD")
screen.addstr(1, 60, f"Score: ${score}")
for i in range(6):
screen.addstr(3, 2 + 15 * i, f"{board[i][0]['category']['title'][:12]}")
for j in range(5):
for i in range(6):
if not board[i][j]['answered']:
screen.addstr(5 + 2 * j, 2 + 15 * i, f"${(j+1) * 200}")
else:
screen.addstr(5 + 2 * j, 2 + 15 * i, f"------")
screen.refresh()
# Function to handle mouse click and returns the selected question and its index
def handle_click(screen, board, mouse_y, mouse_x):
selected_question = None
question_index = None
for j in range(5):
for i in range(6):
if (4 + 2 * j < mouse_y < 6 + 2 * j) and (2 + 15 * i < mouse_x < 2 + 15 * (i+1)):
selected_question = board[i][j]
question_index = j
break
return selected_question, question_index
# Function to ask question and receive user's answer
def ask_question(screen, question_data):
screen.clear()
screen.addstr(1, 1, f"Category: {question_data['category']['title']}")
screen.addstr(3, 1, f"Question: {question_data['question']}")
screen.addstr(5, 1, "Your answer: ")
curses.echo()
screen.timeout(-1) # Disable the timeout feature
user_answer = screen.getstr(5, 15).decode("utf-8")
screen.timeout(100) # Re-enable the timeout feature
curses.noecho()
return user_answer
# Function to check the answer using fuzzy matching
def is_correct_answer(user_answer, correct_answer):
similarity = fuzz.ratio(user_answer.lower(), correct_answer.lower())
return similarity > 80
# Main script
def main(stdscr):
curses.mousemask(curses.ALL_MOUSE_EVENTS)
curses.curs_set(0)
stdscr.timeout(100)
board = initialize_board()
score = 0
while True:
draw_board(stdscr, board, score)
event = stdscr.getch()
if event == ord('q'):
break
if event == curses.KEY_MOUSE:
_, mouse_x, mouse_y, _, _ = curses.getmouse()
selected_question, question_index = handle_click(stdscr, board, mouse_y, mouse_x)
if selected_question and not selected_question['answered']:
user_answer = ask_question(stdscr, selected_question)
selected_question['answered'] = True
if is_correct_answer(user_answer, selected_question['answer']):
score += 200 * (question_index + 1) # Update the score based on the question index
stdscr.addstr(7, 1, f"That's correct!")
stdscr.refresh()
curses.napms(3000)
else:
stdscr.addstr(7, 1, f"Sorry, the correct answer is: {selected_question['answer']}")
stdscr.refresh()
curses.napms(3000)
if __name__ == "__main__":
curses.wrapper(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment