Home / Widgets / Local Fundamentals
Duplicate Snippet

Embed Snippet on Your Site

Local Fundamentals

Code Preview
html
  <div class="container">
    <div class="wp-block-button is-style-outline">
      <button id="startButton" class="button">Start New Game</button>
      <button id="loadButton" class="button">Load Game</button>
    </div>
    <div id="gameControls" style="display: none;">
      <h2>Round: <span id="roundNumber">1</span></h2>
      <button id="progressButton" class="button">Progress to Next Round</button>
      <button id="saveButton" class="button">Save Game</button>
      <div class="output">
        <h3>Market Prices</h3>
        <pre id="marketPrices"></pre>
        <h3>Nation Data</h3>
        <pre id="nationData"></pre>
      </div>
    </div>
  </div>
  <script>
    // Constants
    const NATIONS = Array.from({ length: 15 }, (_, i) => `Nation${i + 1}`);
    const INDUSTRIES = {
      IT: 1000,
      Auto: 2000,
      Agriculture: 500,
      Defense: 5000,
      FinancialServices: 4000,
    };
    const ROUNDS = 10;
    const SAVE_KEY = "game_save";
    // Game state
    let gameState = null;
    // Initialize game
    function initializeGame() {
      gameState = {
        round: 1,
        marketPrices: { ...INDUSTRIES },
        nations: NATIONS.reduce((acc, nation) => {
          acc[nation] = { ...INDUSTRIES };
          return acc;
        }, {}),
      };
      updateUI();
    }
    // Save game
    function saveGame() {
      localStorage.setItem(SAVE_KEY, JSON.stringify(gameState));
      alert("Game saved!");
    }
    // Load game
    function loadGame() {
      const savedGame = localStorage.getItem(SAVE_KEY);
      if (savedGame) {
        gameState = JSON.parse(savedGame);
        updateUI();
        alert("Game loaded!");
      } else {
        alert("No saved game found.");
      }
    }
    // Progress to next round
    function progressRound() {
      if (gameState.round < ROUNDS) {
        gameState.round += 1;
        // Simulate market price fluctuations
        for (const industry in gameState.marketPrices) {
          gameState.marketPrices[industry] *= 1 + (Math.random() * 0.2 - 0.1); // Random fluctuation
        }
        updateUI();
      } else {
        alert("Game over! You've reached the final round.");
      }
    }
    // Update UI
    function updateUI() {
      document.getElementById("roundNumber").textContent = gameState.round;
      document.getElementById("marketPrices").textContent = JSON.stringify(gameState.marketPrices, null, 2);
      document.getElementById("nationData").textContent = JSON.stringify(gameState.nations, null, 2);
    }
    // Event listeners
    document.getElementById("startButton").addEventListener("click", () => {
      initializeGame();
      document.getElementById("gameControls").style.display = "block";
    });
    document.getElementById("loadButton").addEventListener("click", () => {
      loadGame();
      document.getElementById("gameControls").style.display = "block";
    });
    document.getElementById("progressButton").addEventListener("click", progressRound);
    document.getElementById("saveButton").addEventListener("click", saveGame);
  </script>

Comments

Add a Comment