Skip to content

Commit 4dc252a

Browse files
committed
Final Jeopardy. Closes #6 and #7
1 parent c4f29b4 commit 4dc252a

File tree

5 files changed

+126
-9
lines changed

5 files changed

+126
-9
lines changed

config.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
questions_file: questions.json
33
persist_file: database.pickle
44
show_standard_error = false
5+
final_category = The final category
6+
final_question_file = final_question.txt
7+
final_answer_file = final_answer.txt
58

69
[buzzer]
710
server_ip: localhost

curses_drawing.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
from question_states import *
1616

17+
(FINAL_STATE_BEGIN, FINAL_STATE_CATEGORY, FINAL_STATE_QUESTION,
18+
FINAL_STATE_GO_AROUND, FINAL_STATE_GO_AROUND_ANSWER,
19+
FINAL_STATE_ALL_SCORES ) = range(6)
20+
1721
SPLASH_TEXT = "Hacker Jeopardy!!!"
1822
SPLASH_ANY_KEY_MSG = " any key to continue"
1923

@@ -327,3 +331,27 @@ def draw_daily_double_splash(screen, player_names, player_scores):
327331
screen.addstr(height-i, 2, "%s : %s" % (player_name, player_score),
328332
CURSES_COLOUR_PAIR_MEH )
329333
screen.refresh()
334+
335+
def draw_final_jeopardy_splash(screen, msg, final_state,
336+
player_names, player_scores):
337+
screen.clear()
338+
height, width = screen.getmaxyx()
339+
340+
color = {
341+
FINAL_STATE_BEGIN: COLOUR_PAIR_BAD_FEEL,
342+
FINAL_STATE_CATEGORY: COLOUR_PAIR_GOOD_FEEL,
343+
FINAL_STATE_QUESTION: COLOUR_PAIR_MAX_CONTRAST,
344+
FINAL_STATE_GO_AROUND: COLOUR_PAIR_GOOD_FEEL,
345+
FINAL_STATE_GO_AROUND_ANSWER: COLOUR_PAIR_REALLY_GOOD,
346+
FINAL_STATE_ALL_SCORES: COLOUR_PAIR_GOOD_FEEL,
347+
}[final_state]
348+
349+
text_in_screen_center(screen, msg, color=color)
350+
for i, (player_score, player_name) in enumerate(
351+
sorted(zip(player_scores, player_names),
352+
reverse=True),
353+
1):
354+
screen.addstr(height-i-1, 2, "%s: %s" % (player_name, player_score),
355+
CURSES_COLOUR_PAIR_MEH )
356+
screen.refresh()
357+

final_answer.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The final answer.
2+

final_question.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The final question?
2+

jeopardy.py

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
(draw_window_grid_and_refresh,
2525
draw_window_question_prompts_and_refresh,
2626
init_colors, draw_splash, draw_daily_double_splash,
27+
draw_final_jeopardy_splash,
28+
FINAL_STATE_BEGIN, FINAL_STATE_CATEGORY, FINAL_STATE_QUESTION,
29+
FINAL_STATE_GO_AROUND, FINAL_STATE_GO_AROUND_ANSWER,
30+
FINAL_STATE_ALL_SCORES
2731
)
2832
from game_audio import build_audio_engine
2933
from question_states import *
@@ -82,16 +86,21 @@ def edit_scores(screen, scores):
8286
event = screen.getch()
8387
if event in score_codes:
8488
code = int(chr(event))
85-
curses.echo()
86-
while True:
87-
try:
88-
scores[code] += int( screen.getstr(height-1, 2) )
89-
except ValueError: pass
90-
else:
91-
curses.noecho()
92-
break
89+
scores[code] += edit_score_prompt(screen, code)
9390
break
9491

92+
def edit_score_prompt(screen, code):
93+
height, width = screen.getmaxyx()
94+
95+
curses.echo()
96+
while True:
97+
try:
98+
score_change = int( screen.getstr(height-1, 2) )
99+
except ValueError: pass
100+
else:
101+
curses.noecho()
102+
return score_change
103+
95104
def pick_dd_player(screen, player_names):
96105
height, width = screen.getmaxyx()
97106

@@ -126,13 +135,80 @@ def input_dd_player_wager(screen, min_wager, max_wager):
126135
curses.noecho()
127136
return how_much
128137

138+
def do_final_jeopardy(screen, player_names, scores):
139+
draw_final_jeopardy_splash(
140+
screen, "Final Jeopardy!", FINAL_STATE_BEGIN,
141+
player_names, scores)
142+
run_until_space(screen)
143+
draw_final_jeopardy_splash(
144+
screen, config.get("core", "final_category"), FINAL_STATE_CATEGORY,
145+
player_names, scores )
146+
run_until_space(screen)
147+
148+
question = \
149+
open(config.get("core", "final_question_file")).readline().strip()
150+
answer = open(config.get("core", "final_answer_file")).readline().strip()
151+
152+
draw_final_jeopardy_splash(
153+
screen,
154+
question,
155+
FINAL_STATE_QUESTION,
156+
player_names, scores )
157+
run_until_space(screen)
158+
159+
state = FINAL_STATE_GO_AROUND
160+
q_or_a = question
161+
for score, player_code, player_name in sorted(zip(
162+
tuple(scores), # make a copy of scores as we're changing in place
163+
tuple( range(len(player_names)) ),
164+
player_names,
165+
) ):
166+
if score > 0:
167+
draw_final_jeopardy_splash(
168+
screen,
169+
q_or_a,
170+
state,
171+
(player_name,), (score,) )
172+
delta = -1
173+
while not (0<= delta <= score):
174+
delta = edit_score_prompt(screen, player_code)
175+
scores[player_code] += delta
176+
if delta > 0:
177+
state = FINAL_STATE_GO_AROUND_ANSWER
178+
q_or_a = answer
179+
draw_final_jeopardy_splash(
180+
screen,
181+
q_or_a,
182+
state,
183+
player_names, scores )
184+
run_until_space(screen)
185+
186+
winning_score = max(scores)
187+
winners = [ player_name
188+
for score, player_name in zip(scores, player_names)
189+
if score == winning_score ]
190+
191+
draw_final_jeopardy_splash(
192+
screen, "The winner is "+ ", ".join(winners) + "!",
193+
FINAL_STATE_ALL_SCORES,
194+
player_names, scores )
195+
run_until_space(screen)
196+
197+
draw_final_jeopardy_splash(
198+
screen, "Thanks for playing. Happy Hacking!",
199+
FINAL_STATE_ALL_SCORES,
200+
player_names, scores )
201+
run_until_space(screen)
129202

130203
def run_questions_menu(screen, questions, answered_questions, player_names,
131204
scores, daily_doubles, answer_server):
205+
number_of_rows = len(questions[0]["questions"])
206+
total_avail_questions = len(questions) * number_of_rows
207+
132208
selected_question = [0, 100]
133209

134210
# initialize selected question bounds
135-
max_question = int(len(questions[0]["questions"]) * 100)
211+
max_question = number_of_rows * 100
136212
max_category = len(questions) - 1
137213

138214
draw_window_grid_and_refresh(
@@ -192,6 +268,12 @@ def run_questions_menu(screen, questions, answered_questions, player_names,
192268
save_database(answered_questions, player_names,
193269
scores, daily_doubles)
194270

271+
if ( len(answered_questions) >= total_avail_questions):
272+
# should never answer more questions then we have...
273+
assert( len(answered_questions) == total_avail_questions)
274+
do_final_jeopardy(screen, player_names, scores)
275+
break
276+
195277
elif event == ord("e"):
196278
edit_scores(screen, scores)
197279
save_database(answered_questions, player_names,

0 commit comments

Comments
 (0)