Kaushik Rajan commited on
Commit
eb8fa29
Β·
1 Parent(s): 4cf9218

Feat: Implement dynamic post-game analysis

Browse files
Files changed (1) hide show
  1. app.py +51 -7
app.py CHANGED
@@ -209,6 +209,55 @@ def ai_strategy(ai_stats, player_stats, quarter):
209
 
210
  return final_allocation, " ".join(reasoning)
211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
212
  # --- Gradio UI ---
213
 
214
  def create_interface():
@@ -363,13 +412,8 @@ def create_interface():
363
  winner = env.get_winner()
364
  status_text = f"Game Over! Winner: {winner}. Final market share: You ({state['player_stats']['market_share']:.1f}%) vs AI ({state['ai_stats']['market_share']:.1f}%)."
365
  submit_btn_update = gr.update(interactive=False)
366
- # Deeper post-game analysis: Analyze trends from history
367
- df = pd.DataFrame(state["history"])
368
- player_rd_trend = df["Player Product Quality"].diff().mean() # Avg quality growth per quarter
369
- player_ms_trend = df["Player Market Share"].diff().mean() # Avg market share change
370
- player_budget_trend = df["Player Budget"].diff().mean() # Avg budget change
371
- quality_gap = state["player_stats"]["product_quality"] - state["ai_stats"]["product_quality"]
372
- analysis_text = f"Post-Game Analysis:\n- Your average quality growth: {player_rd_trend:.1f} per quarter (vs. AI's {df['AI Product Quality'].diff().mean():.1f}). Quality gap: {quality_gap}.\n- Market share trend: {player_ms_trend:.1f}% change per quarter.\n- Budget trend: ${player_budget_trend:.0f} change per quarter.\nInsight: {'Strong long-term product focus paid off in sustained growth.' if quality_gap > 0 and player_ms_trend > 0 else 'Short-term tactics worked initially but quality neglect hurt in the end.' if player_ms_trend < 0 else 'Balanced approach led to steady performance.'}\nRemember: In real markets, superior products often dominate over time through word-of-mouth and premium pricing."
373
  else:
374
  status_text = f"End of Quarter {state['quarter']}. Your turn."
375
 
 
209
 
210
  return final_allocation, " ".join(reasoning)
211
 
212
+ # --- Post-Game Analysis ---
213
+
214
+ def generate_post_game_analysis(history, winner):
215
+ """Generates dynamic feedback based on game results."""
216
+ initial_state = history[0]
217
+ final_state = history[-1]
218
+
219
+ # Calculate key performance indicators
220
+ quality_change_player = final_state['Player Product Quality'] - initial_state['Player Product Quality']
221
+ quality_change_ai = final_state['AI Product Quality'] - initial_state['AI Product Quality']
222
+
223
+ budget_change_player = final_state['Player Budget'] - initial_state['Player Budget']
224
+ budget_change_ai = final_state['AI Budget'] - initial_state['AI Budget']
225
+
226
+ market_share_lead_player_final = final_state['Player Market Share'] - final_state['AI Market Share']
227
+
228
+ mid_game_state = history[len(history) // 2]
229
+ market_share_lead_player_mid = mid_game_state['Player Market Share'] - mid_game_state['AI Market Share']
230
+
231
+ feedback = []
232
+
233
+ if winner == "You":
234
+ feedback.append("πŸ† **Congratulations on your victory!** Here's what led to your success:")
235
+ if quality_change_player > quality_change_ai * 1.2 and quality_change_player > 20:
236
+ feedback.append("β€’ **Innovation Leadership:** Your strong and consistent investment in R&D created a superior product that was difficult for the AI to overcome.")
237
+ if budget_change_player > budget_change_ai * 1.2 and budget_change_player > 200:
238
+ feedback.append("β€’ **Financial Acumen:** You expertly grew your budget, giving you the resources to outmaneuver the AI in the final quarters.")
239
+ if market_share_lead_player_mid < 0 and market_share_lead_player_final > 0:
240
+ feedback.append("β€’ **Strategic Comeback:** Your impressive turnaround in the second half of the game shows excellent adaptation and resilience.")
241
+ if not feedback[1:]: # If no specific points were added
242
+ feedback.append("β€’ **Balanced Strategy:** Your well-rounded approach of investing across R&D, Marketing, and Sales proved to be a winning formula.")
243
+
244
+ elif winner == "AI":
245
+ feedback.append("πŸ“ˆ **A hard-fought game!** The AI is a tough opponent. Here's a quick analysis for next time:")
246
+ if quality_change_ai > quality_change_player * 1.2 and quality_change_ai > 20:
247
+ feedback.append("β€’ **Innovation Gap:** The AI secured a significant product quality advantage. Consider allocating more to R&D earlier on to build a competitive moat.")
248
+ if budget_change_ai > budget_change_player * 1.2 and budget_change_ai > 200:
249
+ feedback.append("β€’ **Budget Disadvantage:** The AI grew its budget more effectively. Prioritizing Sales earlier could provide you with more financial power in later stages.")
250
+ if abs(market_share_lead_player_final) < 10:
251
+ feedback.append(f"β€’ **Narrow Margin:** You lost by only {abs(market_share_lead_player_final):.1f}%! A small, targeted marketing push might have been enough to change the outcome.")
252
+ if not feedback[1:]:
253
+ feedback.append("β€’ **Countering a Balanced Foe:** The AI succeeded with a balanced approach. Experimenting with a more aggressive, focused strategy could help you find and exploit a weakness.")
254
+
255
+ else: # Draw
256
+ feedback.append("🀝 **An incredibly close match, ending in a draw!**")
257
+ feedback.append("β€’ **Strategic Stalemate:** Both you and the AI demonstrated strong, well-balanced strategies, with neither able to gain a decisive, lasting advantage.")
258
+
259
+ return "\n".join(feedback)
260
+
261
  # --- Gradio UI ---
262
 
263
  def create_interface():
 
412
  winner = env.get_winner()
413
  status_text = f"Game Over! Winner: {winner}. Final market share: You ({state['player_stats']['market_share']:.1f}%) vs AI ({state['ai_stats']['market_share']:.1f}%)."
414
  submit_btn_update = gr.update(interactive=False)
415
+ # Generate post-game analysis
416
+ analysis_text = generate_post_game_analysis(state["history"], winner)
 
 
 
 
 
417
  else:
418
  status_text = f"End of Quarter {state['quarter']}. Your turn."
419