File size: 56,407 Bytes
e75654e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gradio Choose Your Own Adventure</title>
    <script type="module" crossorigin src="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
    <style>
        body {
            font-family: 'Courier New', monospace;
            background-color: #f0f0f0;
            margin: 0;
            padding: 20px;
        }
        
        h1 {
            color: #c00;
            text-align: center;
            margin-bottom: 20px;
        }
        
        .container {
            max-width: 900px;
            margin: 0 auto;
            background-color: white;
            border: 5px solid #d4a017;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
        }
        
        .game-description {
            text-align: center;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #d4a017;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>CHOOSE YOUR OWN ADVENTURE</h1>
        <div class="game-description">
            <p>An interactive text adventure game powered by Gradio-lite</p>
            <p><em>Can you defeat the Evil Power Master and save the land?</em></p>
        </div>
        
        <gradio-lite theme="light">
<gradio-file name="app.py" entrypoint>
import gradio as gr
import random
import json
from game_data import game_data, illustrations, enemies_data, items_data
from game_engine import GameState, create_svg_illustration

def initialize_game():
    """Initialize a new game with default state"""
    game_state = GameState()
    current_page = 1
    page_data = game_data[str(current_page)]
    
    # Create the SVG illustration
    svg_content = create_svg_illustration(page_data["illustration"])
    
    # Build page content
    content = f"<h2>{page_data['title']}</h2>"
    content += page_data["content"]
    
    # Build options
    options = []
    for opt in page_data["options"]:
        options.append(opt["text"])
    
    # Update game statistics display
    stats = f"""
    <div style='display: flex; flex-wrap: wrap; gap: 10px; margin-top: 15px;'>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Courage:</strong> {game_state.stats['courage']}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Wisdom:</strong> {game_state.stats['wisdom']}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Strength:</strong> {game_state.stats['strength']}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>HP:</strong> {game_state.current_hp}/{game_state.max_hp}
        </div>
    </div>
    """
    
    # Initialize inventory as empty
    inventory = ""
    
    # Set story path
    story_path = "You are at the beginning of your adventure."
    
    return svg_content, content, gr.Dropdown(choices=options, label="What will you do?"), game_state.to_json(), stats, inventory, story_path

def update_game(choice, game_state_json):
    """Update game based on player choice"""
    # Parse game state from JSON
    game_state_dict = json.loads(game_state_json)
    game_state = GameState.from_dict(game_state_dict)
    
    # Get current page data
    current_page = game_state.current_page
    page_data = game_data[str(current_page)]
    
    # Find the selected option
    selected_option = None
    for i, opt in enumerate(page_data["options"]):
        if opt["text"] == choice:
            selected_option = opt
            break
    
    if selected_option is None:
        return "Error: Option not found", "Please try again", gr.Dropdown(choices=["Restart"]), game_state.to_json(), "", "", ""
    
    # Check if this option requires an item
    if "requireItem" in selected_option and selected_option["requireItem"] not in game_state.inventory:
        content = f"<p>You need the <strong>{selected_option['requireItem']}</strong> for this option, but you don't have it.</p>"
        options = [opt["text"] for opt in page_data["options"]]
        return create_svg_illustration(page_data["illustration"]), content, gr.Dropdown(choices=options), game_state.to_json(), generate_stats_display(game_state), generate_inventory_display(game_state), f"You remain on page {current_page}."
    
    # Process special items to collect
    if "addItem" in selected_option and selected_option["addItem"] not in game_state.inventory:
        game_state.inventory.append(selected_option["addItem"])
    
    # Move to the next page
    next_page = selected_option["next"]
    game_state.current_page = next_page
    game_state.visited_pages.append(next_page)
    
    # Update journey progress based on page transitions
    game_state.journey_progress += 5  # Increment progress with each decision
    if game_state.journey_progress > 100:
        game_state.journey_progress = 100
    
    # Check for random battle (20% chance if page has randomBattle flag)
    if "randomBattle" in page_data and page_data["randomBattle"] and random.random() < 0.2:
        battle_result = simulate_battle(game_state)
        if not battle_result:
            # Player died in battle
            content = "<h2>Game Over</h2><p>You have been defeated in battle!</p>"
            return create_svg_illustration("game-over"), content, gr.Dropdown(choices=["Restart"]), game_state.to_json(), generate_stats_display(game_state), generate_inventory_display(game_state), "Game Over - You were defeated in battle."
    
    # Get new page data
    page_data = game_data[str(next_page)]
    
    # Create the SVG illustration
    svg_content = create_svg_illustration(page_data["illustration"])
    
    # Process page stat effects
    if "statIncrease" in page_data:
        stat = page_data["statIncrease"]["stat"]
        amount = page_data["statIncrease"]["amount"]
        game_state.stats[stat] += amount
    
    # Process HP loss
    if "hpLoss" in page_data:
        game_state.current_hp -= page_data["hpLoss"]
        if game_state.current_hp <= 0:
            game_state.current_hp = 0
            content = "<h2>Game Over</h2><p>You have died from your wounds!</p>"
            return create_svg_illustration("game-over"), content, gr.Dropdown(choices=["Restart"]), game_state.to_json(), generate_stats_display(game_state), generate_inventory_display(game_state), "Game Over - You died from your wounds."
    
    # Check if this is a challenge page
    if "challenge" in page_data:
        challenge = page_data["challenge"]
        success = perform_challenge(game_state, challenge)
        
        # Update story based on challenge result
        if success:
            next_page = challenge["success"]
        else:
            next_page = challenge["failure"]
        
        game_state.current_page = next_page
        game_state.visited_pages.append(next_page)
        page_data = game_data[str(next_page)]
        svg_content = create_svg_illustration(page_data["illustration"])
    
    # Handle game over
    if "gameOver" in page_data and page_data["gameOver"]:
        content = f"<h2>{page_data['title']}</h2>"
        content += page_data["content"]
        if "ending" in page_data:
            content += f"<div style='text-align: center; margin-top: 20px; font-weight: bold; color: #c00;'>THE END</div>"
            content += f"<p style='font-style: italic;'>{page_data['ending']}</p>"
        
        return svg_content, content, gr.Dropdown(choices=["Restart"]), game_state.to_json(), generate_stats_display(game_state), generate_inventory_display(game_state), "Game Over - " + page_data['title']
    
    # Build page content
    content = f"<h2>{page_data['title']}</h2>"
    content += page_data["content"]
    
    # Build options
    options = []
    for opt in page_data["options"]:
        # Check if option requires an item
        if "requireItem" in opt and opt["requireItem"] not in game_state.inventory:
            continue
        # Check if option requires any item from a list
        if "requireAnyItem" in opt:
            has_required_item = False
            for item in opt["requireAnyItem"]:
                if item in game_state.inventory:
                    has_required_item = True
                    break
            if not has_required_item:
                continue
                
        options.append(opt["text"])
    
    # Handle alternative option if necessary
    if "alternativeOption" in page_data and not options:
        alt_opt = page_data["alternativeOption"]
        if "showIf" not in alt_opt or any(item in game_state.inventory for item in alt_opt["showIf"]):
            options.append(alt_opt["text"])
    
    # Update game progress
    story_path = f"You are on page {next_page}: {page_data['title']}"
    if game_state.journey_progress >= 80:
        story_path += " (Nearing the conclusion)"
    elif game_state.journey_progress >= 50:
        story_path += " (Middle of your journey)"
    elif game_state.journey_progress >= 25:
        story_path += " (Adventure beginning)"
    
    return svg_content, content, gr.Dropdown(choices=options), game_state.to_json(), generate_stats_display(game_state), generate_inventory_display(game_state), story_path

def generate_stats_display(game_state):
    """Generate HTML for displaying player stats"""
    # Calculate HP percentage for the progress bar
    hp_percent = (game_state.current_hp / game_state.max_hp) * 100
    hp_color = "#4CAF50"  # Green
    if hp_percent < 30:
        hp_color = "#F44336"  # Red
    elif hp_percent < 70:
        hp_color = "#FFC107"  # Yellow
    
    stats = f"""
    <div style='display: flex; flex-wrap: wrap; gap: 10px; margin-top: 15px;'>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Courage:</strong> {game_state.stats['courage']}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Wisdom:</strong> {game_state.stats['wisdom']}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px;'>
            <strong>Strength:</strong> {game_state.stats['strength']}
        </div>
        <div style='background: #f0f0f0; padding: 5px 10px; border-radius: 5px; position: relative;'>
            <strong>HP:</strong> {game_state.current_hp}/{game_state.max_hp}
            <div style='width: 100px; height: 5px; background: #e0e0e0; margin-top: 3px; border-radius: 3px;'>
                <div style='width: {hp_percent}%; height: 100%; background: {hp_color}; border-radius: 3px;'></div>
            </div>
        </div>
    </div>
    """
    
    # Add journey progress
    stats += f"""
    <div style='margin-top: 10px;'>
        <div style='display: flex; justify-content: space-between; font-size: 12px;'>
            <span>Journey Progress:</span>
            <span>{game_state.journey_progress}%</span>
        </div>
        <div style='width: 100%; height: 5px; background: #e0e0e0; border-radius: 3px;'>
            <div style='width: {game_state.journey_progress}%; height: 100%; background: #2196F3; border-radius: 3px;'></div>
        </div>
    </div>
    """
    
    return stats

def generate_inventory_display(game_state):
    """Generate HTML for displaying player inventory"""
    if not game_state.inventory:
        return "<em>Your inventory is empty.</em>"
    
    inventory_html = "<div style='display: flex; flex-wrap: wrap; gap: 10px;'>"
    
    for item in game_state.inventory:
        item_data = items_data.get(item, {"type": "unknown", "description": "A mysterious item."})
        bg_color = "#e0e0e0"
        if item_data["type"] == "weapon":
            bg_color = "#ffcdd2"
        elif item_data["type"] == "armor":
            bg_color = "#c8e6c9"
        elif item_data["type"] == "spell":
            bg_color = "#bbdefb"
        elif item_data["type"] == "quest":
            bg_color = "#fff9c4"
        
        inventory_html += f"""
        <div style='background: {bg_color}; padding: 5px 10px; border-radius: 5px; position: relative;' 
             title="{item_data['description']}">
            {item}
        </div>
        """
    
    inventory_html += "</div>"
    return inventory_html

def perform_challenge(game_state, challenge):
    """Perform a skill challenge and determine success"""
    stat = challenge["stat"]
    difficulty = challenge["difficulty"]
    
    # Roll dice (1-6) and add stat
    roll = random.randint(1, 6)
    total = roll + game_state.stats[stat]
    
    # Determine if successful
    success = total >= difficulty
    
    # Bonus for great success
    if success and total >= difficulty + 3:
        stat_increase = random.randint(1, 2)
        game_state.stats[stat] += stat_increase
    
    # Penalty for bad failure
    if not success and total <= difficulty - 3:
        stat_decrease = random.randint(1, 2)
        game_state.stats[stat] = max(1, game_state.stats[stat] - stat_decrease)
    
    # Record challenge outcome
    if success:
        game_state.challenges_won += 1
    
    return success

def simulate_battle(game_state):
    """Simulate a battle with a random enemy"""
    # Select a random enemy type
    enemy_types = list(enemies_data.keys())
    enemy_type = random.choice(enemy_types)
    enemy = enemies_data[enemy_type]
    
    # Simple battle simulation
    player_hp = game_state.current_hp
    enemy_hp = enemy["hp"]
    
    player_attack = game_state.stats["strength"]
    player_defense = 1  # Base defense
    
    # Add weapon bonus if the player has a weapon
    for item in game_state.inventory:
        item_data = items_data.get(item, {})
        if "attackBonus" in item_data:
            player_attack += item_data["attackBonus"]
        if "defenseBonus" in item_data:
            player_defense += item_data["defenseBonus"]
    
    enemy_attack = enemy["attack"]
    enemy_defense = enemy["defense"]
    
    # Simple turn-based combat
    while player_hp > 0 and enemy_hp > 0:
        # Player attacks
        damage = max(1, player_attack - enemy_defense)
        enemy_hp -= damage
        
        if enemy_hp <= 0:
            break
        
        # Enemy attacks
        damage = max(1, enemy_attack - player_defense)
        player_hp -= damage
    
    # Update player HP after battle
    game_state.current_hp = player_hp
    
    # Return True if player won, False if player lost
    return player_hp > 0

def restart_game(choice, game_state_json):
    """Restart the game when the player chooses to restart"""
    if choice == "Restart":
        return initialize_game()
    else:
        # Parse existing game state
        game_state_dict = json.loads(game_state_json)
        game_state = GameState.from_dict(game_state_dict)
        
        # Return current game state
        current_page = game_state.current_page
        page_data = game_data[str(current_page)]
        
        svg_content = create_svg_illustration(page_data["illustration"])
        content = f"<h2>{page_data['title']}</h2>"
        content += page_data["content"]
        
        options = []
        for opt in page_data["options"]:
            options.append(opt["text"])
        
        return svg_content, content, gr.Dropdown(choices=options), game_state.to_json(), generate_stats_display(game_state), generate_inventory_display(game_state), f"You are on page {current_page}: {page_data['title']}"

# Create Gradio interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# Choose Your Own Adventure")
    gr.Markdown("### Can you defeat the Evil Power Master?")
    
    with gr.Row():
        with gr.Column(scale=2):
            illustration = gr.HTML(label="Scene")
            content = gr.HTML(label="Story")
            choice = gr.Dropdown(label="What will you do?")
            
            # Hidden field to store game state as JSON
            game_state = gr.Textbox(visible=False)
        
        with gr.Column(scale=1):
            story_path = gr.Markdown(label="Your Path")
            stats = gr.HTML(label="Character Stats")
            inventory = gr.HTML(label="Inventory")
            map_btn = gr.Button("View Story Map")
    
    # Initialize or restart the game
    choice.change(update_game, [choice, game_state], [illustration, content, choice, game_state, stats, inventory, story_path])
    
    # Handle restart
    demo.load(initialize_game, [], [illustration, content, choice, game_state, stats, inventory, story_path])

demo.launch()
</gradio-file>

<gradio-file name="game_engine.py">
import json
import random

class GameState:
    """Class to manage game state"""
    def __init__(self):
        self.current_page = 1
        self.visited_pages = [1]
        self.inventory = []
        self.max_hp = 30
        self.current_hp = 30
        self.stats = {
            "courage": 7,
            "wisdom": 5,
            "strength": 6,
            "luck": 4
        }
        self.status_effects = []
        self.enemies_defeated = 0
        self.challenges_won = 0
        self.current_path = "main"
        self.journey_progress = 0
    
    def to_json(self):
        """Convert game state to JSON string"""
        return json.dumps(self.__dict__)
    
    @classmethod
    def from_dict(cls, data):
        """Create a GameState object from a dictionary"""
        game_state = cls()
        for key, value in data.items():
            setattr(game_state, key, value)
        return game_state

def create_svg_illustration(illustration_key):
    """Return SVG illustration based on the illustration key"""
    # If illustration is not found, use a default illustration
    if illustration_key not in illustrations:
        illustration_key = "default"
    
    # Wrap the SVG in a container div with appropriate styling
    return f"""
    <div style="width: 100%; height: 250px; border: 2px solid #999; border-radius: 5px; overflow: hidden;">
        {illustrations[illustration_key]}
    </div>
    """
</gradio-file>

<gradio-file name="game_data.py">
# Game content data - contains all pages with branching storylines

game_data = {
    "1": {
        "title": "The Beginning",
        "content": """<p>The Evil Power Master has been terrorizing the land for years. With his dark magic, he has enslaved villages, summoned terrible monsters, and threatens to plunge the world into eternal darkness.</p>
        <p>You, a skilled adventurer, have been called upon to stop him. You stand at the entrance to the last free city, Silverhold, ready to begin your quest.</p>
        <p>The fate of the world rests on your shoulders. How will you prepare for your journey?</p>""",
        "options": [
            { "text": "Visit the local weaponsmith for equipment", "next": 2 },
            { "text": "Seek wisdom at the ancient temple", "next": 3 },
            { "text": "Meet with the resistance leader for information", "next": 4 }
        ],
        "illustration": "city-gates"
    },
    
    "2": {
        "title": "The Weaponsmith",
        "content": """<p>The city's renowned weaponsmith, Gorn, welcomes you to his forge. Weapons of all kinds line the walls, from simple daggers to exotic blades that pulse with magic.</p>
        <p>"I've been expecting you," Gorn says. "The Evil Power Master's fortress is treacherous. You'll need more than common steel to face what lies ahead."</p>
        <p>He presents you with three weapons, each crafted with special materials and enchantments.</p>""",
        "options": [
            { "text": "Take the Flaming Sword, powerful but dangerous", "next": 5, "addItem": "Flaming Sword" },
            { "text": "Choose the Whispering Bow, silent and precise", "next": 5, "addItem": "Whispering Bow" },
            { "text": "Select the Guardian Shield, defensive and protective", "next": 5, "addItem": "Guardian Shield" }
        ],
        "illustration": "weaponsmith"
    },
    
    "3": {
        "title": "The Ancient Temple",
        "content": """<p>The Temple of Eternal Light stands at the heart of Silverhold. Inside, the air is thick with incense, and soft chanting echoes through the chambers.</p>
        <p>High Priestess Alara greets you with concern in her eyes. "The Evil Power Master grows stronger each day. Before you face him, you should prepare your mind and spirit."</p>
        <p>She offers to teach you one of the temple's secret arts to aid your quest.</p>""",
        "options": [
            { "text": "Learn the Healing Light technique", "next": 5, "addItem": "Healing Light Spell" },
            { "text": "Master the Shield of Faith protection", "next": 5, "addItem": "Shield of Faith Spell" },
            { "text": "Study the ancient Binding Runes", "next": 5, "addItem": "Binding Runes Scroll" }
        ],
        "illustration": "temple"
    },
    
    "4": {
        "title": "The Resistance Leader",
        "content": """<p>In a secluded tavern, you meet Lyra, leader of the resistance against the Evil Power Master. Battle-scarred but determined, she unfurls a map across the table.</p>
        <p>"His fortress has three possible entry points," she explains, pointing to marked locations. "Each has its dangers and advantages. You must choose your path carefully."</p>
        <p>She also offers you a special item from the resistance's limited supplies.</p>""",
        "options": [
            { "text": "Take the map showing the secret tunnel entrance", "next": 5, "addItem": "Secret Tunnel Map" },
            { "text": "Accept the poison-tipped daggers for stealth", "next": 5, "addItem": "Poison Daggers" },
            { "text": "Choose the master key that unlocks ancient doors", "next": 5, "addItem": "Master Key" }
        ],
        "illustration": "resistance-meeting"
    },
    
    "5": {
        "title": "The Journey Begins",
        "content": """<p>Prepared as best you can be, you leave Silverhold behind. The path to the Evil Power Master's fortress takes you through the Shadowwood Forest, a place once beautiful but now corrupted by dark magic.</p>
        <p>Strange sounds echo among the twisted trees, and the feeling of being watched raises the hair on your neck. The main road is the fastest route, but there are alternatives.</p>""",
        "options": [
            { "text": "Take the main road through the forest", "next": 6 },
            { "text": "Follow the winding river path", "next": 7 },
            { "text": "Brave the ancient ruins shortcut", "next": 8 }
        ],
        "randomBattle": True,
        "illustration": "shadowwood-forest"
    },
    
    "6": {
        "title": "Ambush on the Road",
        "content": """<p>The main road through Shadowwood is overgrown but still visible. As you make your way forward, the trees suddenly rustle with movement.</p>
        <p>A group of the Evil Power Master's scouts emerges from hiding! They've been patrolling the road, looking for those who might oppose their master.</p>
        <p>"Surrender now," their leader snarls, "or face the consequences!"</p>""",
        "challenge": {
            "title": "Escape the Ambush",
            "description": "You need to either fight your way through or find an escape route.",
            "stat": "courage",
            "difficulty": 5,
            "success": 9,
            "failure": 10
        },
        "illustration": "road-ambush"
    },
    
    "7": {
        "title": "The Mist-Shrouded River",
        "content": """<p>The river path is longer but less traveled. Thick mist clings to the water's surface, reducing visibility but potentially hiding you from enemies.</p>
        <p>As you follow the winding shore, you notice strange glowing lights beneath the water's surface. Suddenly, the mist parts to reveal a mysterious figure standing on the water itself.</p>
        <p>"Few choose this path," the figure says in a melodic voice. "To proceed, you must answer my riddle."</p>""",
        "challenge": {
            "title": "The River Spirit's Riddle",
            "description": "Answer correctly to gain passage and a blessing. Answer wrongly and face the river's peril.",
            "stat": "wisdom",
            "difficulty": 6,
            "success": 11,
            "failure": 12
        },
        "illustration": "river-spirit"
    },
    
    "8": {
        "title": "The Forgotten Ruins",
        "content": """<p>The ancient ruins rise from the forest floor like the bones of a forgotten civilization. Moss-covered stones and crumbling arches create a labyrinth of passages and dead ends.</p>
        <p>This path is the shortest route to the fortress, but the ruins are said to be haunted by the spirits of those who built this place long ago.</p>
        <p>As you enter the heart of the ruins, the temperature drops dramatically, and whispers echo through the stone corridors.</p>""",
        "challenge": {
            "title": "Navigate the Haunted Ruins",
            "description": "Find the correct path through the ruins while avoiding the malevolent spirits.",
            "stat": "wisdom",
            "difficulty": 5,
            "success": 13,
            "failure": 14
        },
        "illustration": "ancient-ruins"
    },
    
    "9": {
        "title": "Breaking Through",
        "content": """<p>With courage and quick thinking, you manage to fight your way through the ambush. Several scouts fall to your attacks, and the rest scatter into the forest.</p>
        <p>Though you've escaped, the commotion may have alerted other enemies in the area. You need to move quickly before reinforcements arrive.</p>
        <p>Ahead, the forest begins to thin, revealing the barren lands that surround the Evil Power Master's fortress.</p>""",
        "options": [
            { "text": "Rush across the open ground to the fortress", "next": 15 },
            { "text": "Wait for nightfall before crossing", "next": 16 },
            { "text": "Search for underground passages", "next": 17 }
        ],
        "randomBattle": True,
        "illustration": "forest-edge"
    },
    
    "10": {
        "title": "Captured!",
        "content": """<p>Despite your best efforts, the scouts overwhelm you. Disarmed and bound, you're dragged through the forest to a small outpost.</p>
        <p>"The Master will be pleased," the leader laughs. "He always enjoys interrogating those who think they can stand against him."</p>
        <p>As night falls, you're locked in a crude cell. All seems lost, but one guard looks at you with sympathy when the others aren't watching.</p>""",
        "options": [
            { "text": "Try to escape during the night", "next": 18 },
            { "text": "Appeal to the sympathetic guard", "next": 19 },
            { "text": "Wait for transport to the fortress", "next": 20 }
        ],
        "hpLoss": 5,
        "illustration": "prisoner-cell"
    },
    
    "11": {
        "title": "The Spirit's Blessing",
        "content": """<p>"Wisdom flows in you like the river itself," the spirit says, impressed by your answer. The mist swirls around you, and you feel a surge of energy.</p>
        <p>"Take my blessing. The waters will aid you against the darkness." The spirit touches your forehead, and a cool sensation spreads through your body.</p>
        <p>The mist parts, revealing a clear path forward and the distant silhouette of the Evil Power Master's fortress.</p>""",
        "options": [
            { "text": "Continue to the barren plains", "next": 15 },
            { "text": "Ask the spirit for more information", "next": 16 },
            { "text": "Follow the underwater passage the spirit reveals", "next": 17 }
        ],
        "addItem": "Water Spirit's Blessing",
        "statIncrease": { "stat": "wisdom", "amount": 2 },
        "illustration": "spirit-blessing"
    },
    
    "12": {
        "title": "The Spirit's Wrath",
        "content": """<p>"Incorrect," the spirit hisses, its melodic voice turning harsh. "The river does not suffer fools!"</p>
        <p>The mist thickens around you, and the calm water suddenly churns. A powerful current grabs your legs, pulling you into the depths.</p>
        <p>You struggle against the supernatural force, barely managing to break free and scramble onto the shore, soaked and battered.</p>""",
        "options": [
            { "text": "Continue along the riverbank", "next": 15 },
            { "text": "Find another path through the forest", "next": 16 },
            { "text": "Rest and recover before moving on", "next": 17 }
        ],
        "hpLoss": 8,
        "illustration": "river-danger"
    },
    
    "13": {
        "title": "Ancient Allies",
        "content": """<p>Your wisdom guides you through the ruins' labyrinth. As you navigate the crumbling corridors, the whispers change from threatening to curious.</p>
        <p>In the central chamber, transparent figures materialize - the ancient spirits of this place. They were not malevolent but protective, testing those who pass through.</p>
        <p>"You have shown respect and intelligence," their leader says. "We too oppose the darkness that has corrupted our land."</p>""",
        "options": [
            { "text": "Accept their offer of a magical artifact", "next": 15, "addItem": "Ancient Amulet" },
            { "text": "Ask for knowledge about the fortress", "next": 16 },
            { "text": "Request they guide you through secret paths", "next": 17 }
        ],
        "illustration": "ancient-spirits"
    },
    
    "14": {
        "title": "Lost in Time",
        "content": """<p>The ruins become an impossible maze. Each turn leads to unfamiliar passages, and the whispers grow louder, disorienting you further.</p>
        <p>Hours pass as you wander, and malevolent spirits tug at your life force. By the time you finally stumble out of the ruins, you're exhausted and drained.</p>
        <p>Worse, you've emerged far from your intended path, and valuable time has been lost.</p>""",
        "options": [
            { "text": "Rest to recover your strength", "next": 15 },
            { "text": "Push on despite your exhaustion", "next": 16 },
            { "text": "Find a local guide in a nearby village", "next": 17 }
        ],
        "hpLoss": 10,
        "illustration": "lost-ruins"
    },
    
    "15": {
        "title": "The Looming Fortress",
        "content": """<p>The Evil Power Master's fortress dominates the landscape - a massive structure of black stone with towers that seem to pierce the clouds.</p>
        <p>Lightning crackles around its highest spires, and dark shapes patrol the battlements. The main gate is heavily guarded, but there must be other ways in.</p>
        <p>As you survey the imposing structure, you consider your options for infiltration.</p>""",
        "options": [
            { "text": "Approach the main gate in disguise", "next": 21 },
            { "text": "Scale the outer wall under cover of darkness", "next": 22 },
            { "text": "Look for the secret tunnel (requires Secret Tunnel Map)", "next": 23, "requireItem": "Secret Tunnel Map" }
        ],
        "alternativeOption": { "text": "Search for another entrance", "next": 21, "showIf": ["Secret Tunnel Map"] },
        "illustration": "evil-fortress"
    },
    
    "16": {
        "title": "Strategic Approach",
        "content": """<p>You take time to observe the fortress from a distance. Patrols move in predictable patterns, and supply wagons come and go at regular intervals.</p>
        <p>As night falls, different sections of the fortress light up, revealing potential weaknesses in security. Your careful observation provides valuable insights.</p>
        <p>You identify several possible entry points, each with its own risks and advantages.</p>""",
        "options": [
            { "text": "Infiltrate with an incoming supply wagon", "next": 21 },
            { "text": "Use magic to create a distraction (requires any spell)", "next": 22, "requireAnyItem": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing"] },
            { "text": "Bribe a guard to let you in (requires special item)", "next": 23, "requireAnyItem": ["Ancient Amulet", "Poison Daggers", "Master Key"] }
        ],
        "alternativeOption": { "text": "Wait for another opportunity", "next": 21, "showIf": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing", "Ancient Amulet", "Poison Daggers", "Master Key"] },
        "illustration": "fortress-observation"
    },
    
    "17": {
        "title": "The Hidden Way",
        "content": """<p>Your search reveals an unexpected approach to the fortress - an ancient maintenance tunnel that runs beneath the barren plains, likely forgotten by the Evil Power Master.</p>
        <p>The tunnel is old and could be unstable, but it would allow you to bypass most of the outer defenses entirely.</p>
        <p>As you consider this option, you hear patrol horns in the distance - they're sweeping the area for intruders.</p>""",
        "options": [
            { "text": "Enter the tunnel immediately", "next": 24 },
            { "text": "Hide and wait for the patrol to pass", "next": 21 },
            { "text": "Set a trap for the patrol", "next": 22 }
        ],
        "illustration": "hidden-tunnel"
    },
    
    "18": {
        "title": "Night Escape",
        "content": """<p>You wait until the dead of night, when most guards are dozing at their posts. Using a loose stone you discovered in your cell, you work on the rusted lock.</p>
        <p>With a satisfying click, the cell door unlocks. Moving silently through the shadows, you retrieve your confiscated equipment from a nearby storeroom.</p>
        <p>Now you must leave the outpost without raising the alarm.</p>""",
        "challenge": {
            "title": "Stealthy Escape",
            "description": "Slip past the sleeping guards without alerting them.",
            "stat": "wisdom",
            "difficulty": 6,
            "success": 16,
            "failure": 10
        },
        "illustration": "night-escape"
    },
    
    "19": {
        "title": "Unexpected Ally",
        "content": """<p>When the guard passes your cell alone, you whisper, asking why they seemed sympathetic. The guard looks around nervously before responding.</p>
        <p>"My village was enslaved by the Evil Power Master. My family serves him under threat of death, but many of us secretly hope for his downfall."</p>
        <p>The guard offers to help you escape, but warns there will be a price for this betrayal if discovered.</p>""",
        "options": [
            { "text": "Accept the guard's help", "next": 16 },
            { "text": "Decline - you don't want to put them at risk", "next": 18 },
            { "text": "Convince them to join your cause", "next": 20 }
        ],
        "addItem": "Guard Disguise",
        "illustration": "guard-ally"
    },
    
    "20": {
        "title": "Journey to the Fortress",
        "content": """<p>At dawn, you're bound and loaded onto a prison wagon with other captured travelers. The journey to the fortress is uncomfortable, but you use the time to observe and plan.</p>
        <p>The other prisoners share what they know about the fortress. One mentions rumors of a resistance operating within the Evil Power Master's ranks.</p>
        <p>As the wagon approaches the massive gates, you begin to formulate a plan.</p>""",
        "options": [
            { "text": "Look for an opportunity to escape during transfer", "next": 21 },
            { "text": "Remain captive to get inside, then escape", "next": 22 },
            { "text": "Try to contact the internal resistance", "next": 23 }
        ],
        "illustration": "prison-wagon"
    },
    
    "21": {
        "title": "Infiltration",
        "content": """<p>Using your skills and preparation, you manage to infiltrate the outer perimeter of the fortress. The black stone walls loom overhead, even more intimidating up close.</p>
        <p>Guards patrol in pairs, and magical wards pulse with dark energy. You'll need to be extremely careful as you navigate deeper into enemy territory.</p>
        <p>The sound of a bell signals a shift change, providing a momentary distraction.</p>""",
        "options": [
            { "text": "Head towards the central tower", "next": 25 },
            { "text": "Search for the dungeons", "next": 26 },
            { "text": "Follow a group of mages", "next": 27 }
        ],
        "randomBattle": true,
        "illustration": "fortress-interior"
    },
    
    "22": {
        "title": "Discovered!",
        "content": """<p>As you make your way through the fortress, an alarm suddenly sounds! Your presence has been detected, and you hear the heavy footsteps of guards approaching.</p>
        <p>"Intruder in the south corridor!" a voice shouts. "Seal all exits!"</p>
        <p>You have moments to react before you're surrounded.</p>""",
        "challenge": {
            "title": "Escape Detection",
            "description": "You need to escape from the approaching guards before they trap you.",
            "stat": "courage",
            "difficulty": 7,
            "success": 25,
            "failure": 28
        },
        "illustration": "fortress-alarm"
    },
    
    "23": {
        "title": "Secret Resistance",
        "content": """<p>Through luck or skill, you make contact with members of the secret resistance operating within the fortress. They're skeptical of you at first, but your actions have convinced them of your intentions.</p>
        <p>"We've been working to undermine the Evil Power Master for years," their leader whispers. "Now with your help, we might finally have a chance to end his reign."</p>
        <p>They share crucial information about the fortress layout and the Evil Power Master's weaknesses.</p>""",
        "options": [
            { "text": "Join their planned uprising", "next": 29 },
            { "text": "Ask them to help you reach the Power Master directly", "next": 30 },
            { "text": "Share your knowledge with them", "next": 31 }
        ],
        "illustration": "secret-meeting"
    },
    
    "24": {
        "title": "Underground Passage",
        "content": """<p>The ancient tunnel is damp and narrow, forcing you to crouch as you make your way forward. Roots hang from the ceiling, and the air feels thick with age.</p>
        <p>After what seems like hours of careful navigation, you notice light ahead. The tunnel opens into what appears to be a forgotten storeroom beneath the fortress.</p>
        <p>Dust covers everything, but you can hear movement in the rooms above.</p>""",
        "options": [
            { "text": "Explore the storeroom for useful items", "next": 32 },
            { "text": "Find a way up to the main fortress", "next": 33 },
            { "text": "Listen carefully to determine what's happening above", "next": 34 }
        ],
        "illustration": "underground-passage"
    },
    
    "25": {
        "title": "The Central Tower",
        "content": """<p>The central tower rises from the heart of the fortress like a black spear. As you get closer, you can feel powerful magic emanating from its peak.</p>
        <p>Guards are more numerous here, and you spot several dark mages performing rituals in alcoves along the walls. Whatever the Evil Power Master is planning, it seems to be approaching its climax.</p>
        <p>A massive door blocks the entrance to the tower, engraved with strange symbols.</p>""",
        "options": [
            { "text": "Try to decipher the symbols", "next": 35 },
            { "text": "Look for another entrance", "next": 36 },
            { "text": "Use magic to open the door (requires any spell)", "next": 37, "requireAnyItem": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing"] }
        ],
        "alternativeOption": { "text": "Wait and observe the mages", "next": 36, "showIf": ["Healing Light Spell", "Shield of Faith Spell", "Binding Runes Scroll", "Water Spirit's Blessing"] },
        "illustration": "central-tower"
    },
    
    "26": {
        "title": "The Dungeons",
        "content": """<p>The dungeons are a maze of dark, damp corridors lined with cells. Moans and whispers echo off the stone walls, creating an eerie atmosphere of despair.</p>
        <p>Many of the cells contain prisoners - ordinary people from villages conquered by the Evil Power Master, resistance fighters, and even a few magical creatures.</p>
        <p>At the end of one corridor, you spot a heavily guarded cell that seems more important than the others.</p>""",
        "options": [
            { "text": "Try to free some prisoners to cause a distraction", "next": 38 },
            { "text": "Investigate the heavily guarded cell", "next": 39 },
            { "text": "Look for a passage leading upward", "next": 40 }
        ],
        "illustration": "dungeon-corridors"
    },
    
    "27": {
        "title": "The Ritual Chamber",
        "content": """<p>Following the group of mages leads you to a large circular chamber. Hidden in the shadows, you observe as they prepare for what appears to be an important ritual.</p>
        <p>In the center of the room hovers a dark crystal, pulsing with malevolent energy. The mages arrange themselves around it, beginning a chant in an ancient language.</p>
        <p>"Soon the master's power will be unstoppable," one of them mutters. "The alignment is nearly perfect."</p>""",
        "options": [
            { "text": "Disrupt the ritual", "next": 41 },
            { "text": "Quietly observe to learn more", "next": 42 },
            { "text": "Steal the crystal when they're distracted", "next": 43 }
        ],
        "illustration": "ritual-chamber"
    },
    
    "28": {
        "title": "Captured Again",
        "content": """<p>The guards overwhelm you, and this time, there's no easy escape. You're brought before a high-ranking officer, who smiles coldly.</p>
        <p>"The Master will be most pleased," he says. "He so rarely gets to meet those foolish enough to challenge him directly."</p>
        <p>You're escorted under heavy guard toward the central tower, where the Evil Power Master awaits.</p>""",
        "options": [
            { "text": "Look for another chance to escape", "next": 44 },
            { "text": "Prepare yourself mentally to face the Power Master", "next": 45 },
            { "text": "Try to gather information from the guards", "next": 46 }
        ],
        "illustration": "prisoner-escort"
    },
    
    "29": {
        "title": "The Uprising",
        "content": """<p>The resistance has planned carefully. When the signal is given, chaos erupts throughout the fortress. Guards find themselves fighting servants, and magical wards fail as saboteurs destroy key components.</p>
        <p>In the confusion, you and a small team of resistance fighters make your way toward the central tower where the Evil Power Master resides.</p>
        <p>"This is our only chance," the resistance leader whispers. "We must reach him before he realizes what's happening."</p>""",
        "options": [
            { "text": "Lead the charge directly", "next": 47 },
            { "text": "Split up to create more distractions", "next": 48 },
            { "text": "Sneak ahead while the others draw attention", "next": 49 }
        ],
        "illustration": "fortress-uprising"
    },
    
    "30": {
        "title": "The Final Approach",
        "content": """<p>With the resistance's help, you navigate through secret passages unknown to most of the fortress inhabitants. These narrow corridors, built during the fortress's construction, lead directly to the upper levels.</p>
        <p>"The Power Master's chambers are at the top of the central tower," your guide explains. "He rarely leaves them these days, too focused on some grand ritual. This may be our only chance to stop him."</p>
        <p>The passage ends at a hidden door, beyond which lies the base of the central tower.</p>""",
        "options": [
            { "text": "Enter the tower cautiously", "next": 50 },
            { "text": "Ask about the ritual the Power Master is performing", "next": 51 },
            { "text": "Request additional help for the final confrontation", "next": 52 }
        ],
        "illustration": "secret-passage"
    },
    
    "31": {
        "title": "Knowledge Exchange",
        "content": """<p>You share everything you've learned on your journey - about the river spirit, the ancient ruins, and any magical artifacts you've collected. In return, the resistance provides crucial information.</p>
        <p>"The Evil Power Master draws his power from an ancient crystal," their sage explains. "It amplifies his natural abilities tenfold, but it's also a weakness. Destroy the crystal, and his power will be greatly diminished."</p>
        <p>They also reveal that the Power Master is preparing for a cosmic alignment that will make him virtually unstoppable.</p>""",
        "options": [
            { "text": "Ask how to locate the crystal", "next": 53 },
            { "text": "Find out when the alignment will occur", "next": 54 },
            { "text": "Learn if there are any weapons that can harm the crystal", "next": 55 }
        ],
        "illustration": "knowledge-sharing"
    },
    
    "48": {
        "title": "The Final Confrontation",
        "content": """<p>At last, you stand before the Evil Power Master in his chamber at the top of the central tower. Dark energy crackles around him as he turns to face you, his eyes gleaming with malice.</p>
        <p>"So, you're the one who's caused so much trouble," he says, his voice echoing with unnatural power. "I must admit, I'm impressed you made it this far. But your journey ends here."</p>
        <p>The dark crystal hovers behind him, pulsing with energy. This is the moment all your choices have led to.</p>""",
        "options": [
            { "text": "Attack the Evil Power Master directly", "next": 56 },
            { "text": "Try to destroy the crystal", "next": 57 },
            { "text": "Attempt to reason with him", "next": 58 }
        ],
        "illustration": "final-confrontation"
    },
    
    "56": {
        "title": "Direct Confrontation",
        "content": """<p>You charge at the Evil Power Master, drawing on all your courage and strength. He meets your attack with dark magic, the air between you distorting with energy.</p>
        <p>The battle is intense, pushing you to your limits. Each blow you land seems to be absorbed by his power, while his attacks grow increasingly dangerous.</p>
        <p>As the fight continues, you realize that as long as the crystal empowers him, this might be an unwinnable battle.</p>""",
        "challenge": {
            "title": "Battle with the Evil Power Master",
            "description": "You must overcome his dark magic through sheer determination and skill.",
            "stat": "strength",
            "difficulty": 9,
            "success": 59,
            "failure": 60
        },
        "illustration": "power-battle"
    },
    
    "57": {
        "title": "Crystal Destruction",
        "content": """<p>Recognizing the true source of his power, you ignore the Evil Power Master and lunge toward the floating crystal. He shouts in alarm, desperately trying to stop you.</p>
        <p>"No! Stay away from that, you fool! You have no idea what forces you're tampering with!"</p>
        <p>As you get closer to the crystal, its energy intensifies, creating a barrier of dark magic that you must break through.</p>""",
        "challenge": {
            "title": "Breaking the Crystal Barrier",
            "description": "You must overcome the crystal's protective magic to reach and destroy it.",
            "stat": "wisdom",
            "difficulty": 8,
            "success": 61,
            "failure": 62
        },
        "illustration": "crystal-barrier"
    },
    
    "59": {
        "title": "A Hero's Victory",
        "content": """<p>Through sheer determination and skill, you manage to overcome the Evil Power Master's defenses. As he staggers backwards from your decisive blow, his connection to the crystal weakens momentarily.</p>
        <p>Seizing the opportunity, you strike the crystal with all your remaining strength. It cracks, then shatters in a blinding flash of light and energy.</p>
        <p>The Evil Power Master screams as his power dissipates. "Impossible! I was so close to ultimate power!"</p>
        <p>As the light fades, you stand victorious. The land will heal from his corruption, and the people are free once more.</p>""",
        "gameOver": true,
        "ending": "You've defeated the Evil Power Master and destroyed the source of his power. Songs will be sung of your bravery for generations to come. The land begins to heal, and you are hailed as a hero throughout the realm.",
        "illustration": "hero-victory"
    },
    
    "60": {
        "title": "Darkness Prevails",
        "content": """<p>Despite your best efforts, the Evil Power Master's power is too great. His dark magic overwhelms your defenses, bringing you to your knees.</p>
        <p>"Valiant, but futile," he says, looking down at you. "You could have joined me, you know. Now you'll witness my ascension to godhood before your end."</p>
        <p>As your vision fades, you see the crystal's glow intensifying. You've failed, and darkness will soon cover the land.</p>""",
        "gameOver": true,
        "ending": "The Evil Power Master was too powerful, and your quest ends in defeat. Darkness spreads across the land as he completes his ritual and ascends to even greater power. Perhaps another hero will rise in the future to challenge his reign.",
        "illustration": "dark-victory"
    },
    
    "61": {
        "title": "The Crystal Shatters",
        "content": """<p>Drawing on all your wisdom and willpower, you break through the crystal's barrier. Reaching out, you touch its surface, which burns cold against your skin.</p>
        <p>The Evil Power Master shrieks in desperation, "Stop! You'll doom us all!"</p>
        <p>With a final effort, you channel your energy into the crystal. Cracks appear across its surface, spreading rapidly until it shatters with a deafening explosion of light and sound.</p>
        <p>When your vision clears, the Evil Power Master lies defeated, his power broken along with the crystal. Peace can now return to the land.</p>""",
        "gameOver": true,
        "ending": "By destroying the source of the Evil Power Master's strength, you've saved the realm from his tyranny. The fortress begins to crumble as its dark magic fades, but you escape to tell the tale. Your name becomes legend throughout the land.",
        "illustration": "crystal-destroyed"
    },
    
    "62": {
        "title": "A Desperate Gambit",
        "content": """<p>The crystal's barrier proves too strong, repelling your attempts to break through. As you struggle against it, the Evil Power Master regains his composure and approaches.</p>
        <p>"Did you really think it would be that easy?" he mocks. "The crystal has existed for millennia. It cannot be destroyed by someone like you."</p>
        <p>In a final, desperate move, you use all your remaining strength to hurl your most powerful weapon or spell at the crystal.</p>
        <p>To your surprise and the Evil Power Master's horror, this creates a resonance effect. The crystal begins to vibrate violently, its energy becoming unstable.</p>
        <p>"What have you done?" he cries as the crystal's energy spirals out of control, engulfing both of you in blinding light.</p>""",
        "gameOver": true,
        "ending": "Your desperate attack destabilized the crystal, causing a catastrophic release of energy that destroyed the Evil Power Master and his fortress. Though you didn't survive, your sacrifice saved the realm from darkness. Bards sing of your heroism for generations to come.",
        "illustration": "heroic-sacrifice"
    }
}

# SVG templates for illustrations
illustrations = {
    "city-gates": """
        <svg viewBox="0 0 400 250" xmlns="http://www.w3.org/2000/svg">
            <!-- Sky -->
            <rect x="0" y="0" width="400" height="150" fill="#6b88a2" />
            
            <!-- Ground -->
            <rect x="0" y="150" width="400" height="100" fill="#7d6c54" />
            
            <!-- City walls -->
            <rect x="50" y="50" width="300" height="100" fill="#c9c0a8" />
            
            <!-- Gate -->
            <rect x="150" y="80" width="100" height="120" fill="#5a3c28" />
            <rect x="195" y="80" width="10" height="120" fill="#3a281e" />
            
            <!-- Towers -->
            <rect x="50" y="30" width="40" height="120" fill="#d6cdb7" />
            <rect x="310" y="30" width="40" height="120" fill="#d6cdb7" />
            
            <!-- Tower tops -->
            <polygon points="50,30 70,10 90,30" fill="#bf9969" />
            <polygon points="310,30 330,10 350,30" fill="#bf9969" />
            
            <!-- Path -->
            <path d="M150,250 L250,250 L250,200 L150,200 Z" fill="#a89783" />
            
            <!-- Flags -->
            <rect x="70" y="10" width="2" height="20" fill="#222" />
            <rect x="72" y="10" width="10" height="5" fill="#bf2121" />
            <rect x="330" y="10" width="2" height="20" fill="#222" />
            <rect x="332" y="10" width="10" height="5" fill="#bf2121" />
        </svg>
    """,
    "weaponsmith": """
        <svg viewBox="0 0 400 250" xmlns="http://www.w3.org/2000/svg">
            <!-- Background -->
            <rect x="0" y="0" width="400" height="250" fill="#3d3123" />
            
            <!-- Forge -->
            <rect x="250" y="100" width="100" height="70" fill="#8c5b3f" />
            <rect x="270" y="120" width="60" height="30" fill="#e63d16" />
            <ellipse cx="300" cy="135" rx="25" ry="10" fill="#f7d046" />
            
            <!-- Anvil -->
            <rect x="180" y="160" width="40" height="20" fill="#888888" />
            <rect x="190" y="150" width="20" height="10" fill="#999999" />
            <rect x="195" y="140" width="10" height="10" fill="#aaaaaa" />
            
            <!-- Weapons on wall -->
            <line x1="50" y1="60" x2="90" y2="70" stroke="#c0c0c0" stroke-width="4" />
            <line x1="120" y1="50" x2="150" y2="70" stroke="#c0c0c0" stroke-width="3" />
            <rect x="60" y="90" width="30" height="80" fill="#814d2b" />
            <rect x="65" y="90" width="20" height="5" fill="#c0c0c0" />
            <rect x="140" y="90" width="5" height="100" fill="#814d2b" />
            <ellipse cx="142.5" cy="90" rx="15" ry="3" fill="#c0c0c0" />
            
            <!-- Table -->
            <rect x="30" y="180" width="120" height="10" fill="#8c5b3f" />
            
            <!-- Weaponsmith -->
            <circle cx="220" cy="120" r="25" fill="#d8a77a" />
            <rect x="210" y="145" width="20" height="35" fill="#603813" />
            <rect x="205" y="110" width="30" height="20" fill="#d8a77a" />
            <circle cx="213" cy="115" r="3" fill="#000" />
            <circle cx="227" cy="115" r="3" fill="#000" />
            <path d="M215,125 Q220,130 225,125" fill="none" stroke="#000" stroke-width="1" />
            
            <!-- Special weapons -->
            <rect x="60" y="60" width="10" height="30" fill="#603813" />
            <rect x="62" y="30" width="6" height="30" fill="#ff5722" opacity="0.7">
                <animate attributeName="opacity" values="0.7;1;0.7" dur="2s" repeatCount="indefinite" />
            </rect>
            
            <rect x="130" y="50" width="6" height="40" fill="#603813" />
            <ellipse cx="133" cy="50"