File size: 7,437 Bytes
c4e4866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aef21d2
 
9541dc1
 
de0b2f1
c4e4866
aef21d2
c4e4866
 
 
 
 
9541dc1
 
de0b2f1
9541dc1
 
 
 
 
 
de0b2f1
9541dc1
 
 
 
 
 
 
 
c4e4866
9541dc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aef21d2
9541dc1
 
 
 
aef21d2
9541dc1
 
 
 
 
 
c4e4866
9541dc1
 
 
 
 
 
 
 
 
 
 
 
c4e4866
 
9541dc1
 
 
 
 
 
 
c4e4866
9541dc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4e4866
9541dc1
 
 
 
 
 
 
 
 
 
c4e4866
 
 
9541dc1
 
c4e4866
9541dc1
 
aef21d2
9541dc1
 
 
 
aef21d2
c4e4866
9541dc1
 
 
 
 
 
 
 
 
 
 
c4e4866
9541dc1
c4e4866
9541dc1
 
c4e4866
 
9541dc1
 
 
c4e4866
9541dc1
 
 
 
 
 
c4e4866
aef21d2
c4e4866
de0b2f1
c4e4866
9541dc1
 
 
 
 
 
 
 
 
 
 
 
 
c4e4866
aef21d2
9541dc1
 
aef21d2
9541dc1
 
c4e4866
 
 
de0b2f1
c4e4866
 
 
 
 
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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Factory Game</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>
        html, body { 
            margin: 0;
            padding: 0;
            height: 100%;
        }
    </style>
</head>
<body>
    <gradio-lite>
        <gradio-file name="app.py" entrypoint>
import gradio as gr
import json

GRID_SIZE = 8

class GameState:
    def __init__(self):
        self.resources = {
            "energy": 50,
            "minerals": 50,
            "circuits": 0
        }
        self.grid = [[None for _ in range(GRID_SIZE)] for _ in range(GRID_SIZE)]
        self.message = ""

    def to_json(self):
        return json.dumps({
            "resources": self.resources,
            "grid": self.grid,
            "message": self.message
        })

def process_click(row, col, building_type, state_json):
    if not state_json:
        return GameState().to_json()
        
    state = json.loads(state_json)
    game = GameState()
    game.resources = state["resources"]
    game.grid = state["grid"]
    
    # Building costs
    costs = {
        "solarPanel": {"minerals": 10},
        "mineralExtractor": {"energy": 10},
        "circuitFactory": {"energy": 15, "minerals": 15}
    }
    
    # Check if we can afford the building
    if building_type in costs:
        can_afford = True
        for resource, cost in costs[building_type].items():
            if game.resources[resource] < cost:
                can_afford = False
                game.message = f"Not enough {resource}!"
                break
                
        if can_afford:
            # Deduct resources
            for resource, cost in costs[building_type].items():
                game.resources[resource] -= cost
            
            # Place building
            if game.grid[row][col] is None:
                game.grid[row][col] = building_type
                game.message = f"Placed {building_type}"
            else:
                game.message = "Space already occupied!"
    
    # Produce resources from buildings
    for r in range(GRID_SIZE):
        for c in range(GRID_SIZE):
            building = game.grid[r][c]
            if building == "solarPanel":
                game.resources["energy"] += 1
            elif building == "mineralExtractor":
                game.resources["minerals"] += 1
            elif building == "circuitFactory":
                game.resources["circuits"] += 1
    
    return game.to_json()

def create_ui():
    building_colors = {
        "solarPanel": "#FEF08A",
        "mineralExtractor": "#D1D5DB",
        "circuitFactory": "#BBF7D0"
    }
    
    html = f"""
    <div style="max-width: 800px; margin: 0 auto; padding: 1rem;">
        <div id="resources" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1rem;">
            <div style="padding: 0.75rem; border-radius: 0.5rem; background: #FEF9C3;">Energy: <span id="energy">50</span></div>
            <div style="padding: 0.75rem; border-radius: 0.5rem; background: #F3F4F6;">Minerals: <span id="minerals">50</span></div>
            <div style="padding: 0.75rem; border-radius: 0.5rem; background: #DCFCE7;">Circuits: <span id="circuits">0</span></div>
        </div>
        
        <div style="display: flex; gap: 1rem; margin-bottom: 1rem;">
            <button onclick="window.selectedBuilding='solarPanel'" style="padding: 1rem; border-radius: 0.5rem; background: #FEF08A;">
                Solar Panel<br>Cost: 10 minerals
            </button>
            <button onclick="window.selectedBuilding='mineralExtractor'" style="padding: 1rem; border-radius: 0.5rem; background: #D1D5DB;">
                Mineral Extractor<br>Cost: 10 energy
            </button>
            <button onclick="window.selectedBuilding='circuitFactory'" style="padding: 1rem; border-radius: 0.5rem; background: #BBF7D0;">
                Circuit Factory<br>Cost: 15 energy, 15 minerals
            </button>
        </div>
        
        <div id="message" style="text-align: center; color: #2563EB; font-weight: 500; margin: 0.5rem 0;"></div>
        
        <div id="grid" style="display: grid; grid-template-columns: repeat({GRID_SIZE}, 1fr); gap: 0.25rem; background: #F3F4F6; padding: 0.5rem; border-radius: 0.5rem;">
            {''''.join(f'''
                <button 
                    onclick="placeBuilding({r}, {c})" 
                    id="cell-{r}-{c}"
                    style="aspect-ratio: 1; border-radius: 0.5rem; border: none; cursor: pointer; background: white;">
                </button>
            ''' for r in range(GRID_SIZE) for c in range(GRID_SIZE))}
        </div>
    </div>

    <script>
        window.selectedBuilding = null;
        window.gameState = null;
        
        function updateUI(state) {
            window.gameState = state;
            
            // Update resources
            document.getElementById('energy').textContent = state.resources.energy;
            document.getElementById('minerals').textContent = state.resources.minerals;
            document.getElementById('circuits').textContent = state.resources.circuits;
            
            // Update grid
            for (let r = 0; r < {GRID_SIZE}; r++) {{
                for (let c = 0; c < {GRID_SIZE}; c++) {{
                    const cell = document.getElementById(`cell-${{r}}-${{c}}`);
                    const building = state.grid[r][c];
                    cell.style.background = building ? buildingColors[building] : 'white';
                }}
            }}
            
            // Update message
            document.getElementById('message').textContent = state.message;
        }}
        
        const buildingColors = {json.dumps(building_colors)};
        
        function placeBuilding(row, col) {{
            if (!window.selectedBuilding) {{
                document.getElementById('message').textContent = 'Select a building first!';
                return;
            }}
            send_click(row, col, window.selectedBuilding, JSON.stringify(window.gameState));
        }}
        
        // Production tick
        setInterval(() => {{
            if (window.gameState) {{
                send_click(-1, -1, null, JSON.stringify(window.gameState));
            }}
        }}, 1000);
    </script>
    """
    return gr.HTML(html)

with gr.Blocks() as demo:
    state = gr.State()
    click_handler = gr.Interface(
        fn=process_click,
        inputs=[
            gr.Number(label="Row"),
            gr.Number(label="Col"),
            gr.Text(label="Building"),
            gr.Text(label="State")
        ],
        outputs=gr.Text(label="New State"),
        live=True
    )
    
    ui = create_ui()
    
    def update_ui(value):
        return value
    
    click_handler.load(lambda: GameState().to_json())
    demo.load(lambda: None, None, _js="() => { window.send_click = (row, col, building, state) => { "+click_handler.jsfn+"(row, col, building, state).then((v) => updateUI(JSON.parse(v))) } }")

demo.launch()
        </gradio-file>

        <gradio-requirements>
        </gradio-requirements>
    </gradio-lite>
</body>
</html>