File size: 3,783 Bytes
655125f
2a68adc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4752626
2a68adc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3bc863b
2a68adc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e9dbb9
0ea320c
 
3bc863b
 
0ea320c
 
 
5e9dbb9
2a68adc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a95498
2a68adc
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import gradio as gr 
import formatters
from game_manager import games, new_game

with gr.Blocks(css="style.css") as app:
    started = gr.Variable(False)
    player = gr.Variable()
    last_update = gr.Variable(0)

    with gr.Column() as opening:
        gr.Markdown("# Crossword GPT")
        gr.Markdown(
            """
        Welcome to Crossword GPT, a game that dynamically creates a crossword and uses GPT to create clues.

        - At the start of the game, a crossword puzzle will be created, with single word already solved.
        
        - At any time, a riddle with three clues will be shown, corresponding to three words that branch off the solved part of the puzzle. Riddles are regenerated every 30 seconds if not solved. 

        - You can play against friends, in which case enter a shared room name below. To play alone, leave the fields blank and start the game.

        - Game ends when there is no more space to add words. Winner in competitive mode is the player with the most words.
        """
        )

        room_name = gr.Text(label="Room Name")
        player_name = gr.Text(label="Player Name")
        start_btn = gr.Button("Let's Go!")

    with gr.Column(visible=False) as game_col:
        with gr.Row():
            with gr.Column(min_width=500):
                grid = gr.HTML()
                score_table = gr.DataFrame(headers=["team", "score"], label="Scores")

            with gr.Column():
                clue1 = gr.Textbox(label="Clue 1", elem_id="clue-1")
                clue2 = gr.Textbox(label="Clue 2", elem_id="clue-2")
                clue3 = gr.Textbox(label="Clue 3", elem_id="clue-3")
                guess = gr.Textbox(
                    label="Guess",
                    placeholder="Answer any clue here...",
                    elem_id="guess",
                )
                guess_btn = gr.Button("Guess")

    def start_game(data):
        game = new_game(data[room_name])
        game.add_player(data[player_name])

        return {
            game_col: gr.update(visible=True),
            opening: gr.update(visible=False),
            room_name: game.room_name,
            player: data[player_name],
        }

    start_btn.click(
        start_game,
        {room_name, player_name},
        [game_col, opening, player, room_name],
    )

    def submit_guess(data):
        game = games[data[room_name]]
        game.player_guess(data[player], data[guess])

    guess.submit(submit_guess, {room_name, player, guess}, None)
    guess.submit(
        None,
        None,
        None,
        _js="""() => {document.querySelector("gradio-app").querySelector("#guess textarea").setSelectionRange(0, 9999)}""",
        status_tracker=None,
    )
    guess_btn.click(submit_guess, {room_name, player, guess}, None)
    guess_btn.click(
        None,
        None,
        None,
        _js="""() => {document.querySelector("gradio-app").querySelector("#guess textarea").setSelectionRange(0, 9999)}""",
        status_tracker=None,
    )

    def update_game(data):
        if data[room_name] is None or data[room_name] not in games:
            return {grid: gr.skip()}
        game = games[data[room_name]]
        no_up = data[last_update] == game.last_update_index
        return {
            grid: gr.skip() if no_up else formatters.crossword(game.grid, game.clues),
            score_table: [[k, v] for k, v in game.player_scores.items()],
            clue1: formatters.clue_riddle(game.clues[0]),
            clue2: formatters.clue_riddle(game.clues[1]),
            clue3: formatters.clue_riddle(game.clues[2]),
        }

    start_btn.click(
        update_game,
        {room_name, last_update},
        [grid, clue1, clue2, clue3, score_table],
        every=1,
    )


app.queue().launch()