|
@@ -0,0 +1,149 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html>
|
|
|
+<head>
|
|
|
+ <title>Monopoly Ledger</title>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <h1>Monopoly Ledger</h1>
|
|
|
+
|
|
|
+ <div id="output"></div>
|
|
|
+
|
|
|
+ <div id="transactions">
|
|
|
+
|
|
|
+ <h2>Transactions</h2>
|
|
|
+
|
|
|
+ <form id="form" onsubmit="event.preventDefault();">
|
|
|
+ <label for="fromAcc">From:</label>
|
|
|
+ <select id="fromAcc">
|
|
|
+ <option value="pot">Pot</option>
|
|
|
+ <option value="bank">Bank</option>
|
|
|
+ <!-- Generate player options dynamically -->
|
|
|
+ </select>
|
|
|
+ <br>
|
|
|
+ <label for="toAcc">To:</label>
|
|
|
+ <select id="toAcc">
|
|
|
+ <option value="pot">Pot</option>
|
|
|
+ <option value="bank">Bank</option>
|
|
|
+ <!-- Generate player options dynamically -->
|
|
|
+ <option value="all">All</option>
|
|
|
+ </select>
|
|
|
+ <br>
|
|
|
+ <label for="amount">Amount:</label>
|
|
|
+ <input type="number" id="amount">
|
|
|
+ <br>
|
|
|
+ <button onclick="sendMoney()">Send</button>
|
|
|
+ </form>
|
|
|
+
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <script>
|
|
|
+ var numPlayers = 0;
|
|
|
+ var accounts = [];
|
|
|
+
|
|
|
+ function printBalances() {
|
|
|
+ var output = document.getElementById("output");
|
|
|
+ output.innerHTML = "";
|
|
|
+ accounts.forEach(function(account) {
|
|
|
+ output.innerHTML += account[0] + ": " + account[1] + "<br>";
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ function initialize() {
|
|
|
+ numPlayers = parseInt(prompt("How many players (>1)?"));
|
|
|
+ if (numPlayers < 2) {
|
|
|
+ initialize();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var startingValue = parseInt(prompt("How much for each player?"));
|
|
|
+ accounts.push(["Pot", 0]);
|
|
|
+ for (var i = 1; i <= numPlayers; i++) {
|
|
|
+ accounts.push(["Player " + i, startingValue]);
|
|
|
+ }
|
|
|
+ console.log("Created:");
|
|
|
+ printBalances();
|
|
|
+ console.log("Let the game begin!");
|
|
|
+
|
|
|
+ generatePlayerOptions();
|
|
|
+ }
|
|
|
+
|
|
|
+ function generatePlayerOptions() {
|
|
|
+ var fromAccSelect = document.getElementById("fromAcc");
|
|
|
+ var toAccSelect = document.getElementById("toAcc");
|
|
|
+
|
|
|
+ // Clear existing options
|
|
|
+ fromAccSelect.innerHTML = "";
|
|
|
+ toAccSelect.innerHTML = "";
|
|
|
+
|
|
|
+ // Add options for Pot and Bank
|
|
|
+ var potOption = document.createElement("option");
|
|
|
+ potOption.value = "pot";
|
|
|
+ potOption.text = "Pot";
|
|
|
+ fromAccSelect.add(potOption.cloneNode(true));
|
|
|
+ toAccSelect.add(potOption.cloneNode(true));
|
|
|
+
|
|
|
+ var bankOption = document.createElement("option");
|
|
|
+ bankOption.value = "bank";
|
|
|
+ bankOption.text = "Bank";
|
|
|
+ fromAccSelect.add(bankOption.cloneNode(true));
|
|
|
+ toAccSelect.add(bankOption.cloneNode(true));
|
|
|
+
|
|
|
+ // Add player options
|
|
|
+ for (var i = 1; i <= numPlayers; i++) {
|
|
|
+ var playerOption = document.createElement("option");
|
|
|
+ playerOption.value = i.toString();
|
|
|
+ playerOption.text = "Player " + i;
|
|
|
+ fromAccSelect.add(playerOption.cloneNode(true));
|
|
|
+ toAccSelect.add(playerOption.cloneNode(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Add option for "All" to the "To" dropdown
|
|
|
+ var allOption = document.createElement("option");
|
|
|
+ allOption.value = "all";
|
|
|
+ allOption.text = "All";
|
|
|
+ toAccSelect.add(allOption.cloneNode(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ function sendMoney() {
|
|
|
+ var fromAcc = document.getElementById("fromAcc").value;
|
|
|
+ var toAcc = document.getElementById("toAcc").value;
|
|
|
+ var amount = parseInt(document.getElementById("amount").value);
|
|
|
+
|
|
|
+ // DEBIT
|
|
|
+ if (fromAcc !== "bank") {
|
|
|
+ if (fromAcc === "pot") {
|
|
|
+ fromAcc = 0;
|
|
|
+ } else {
|
|
|
+ fromAcc = parseInt(fromAcc);
|
|
|
+ }
|
|
|
+ accounts[fromAcc][1] -= amount;
|
|
|
+ }
|
|
|
+
|
|
|
+ // CREDIT
|
|
|
+ if (toAcc !== "bank") {
|
|
|
+ if (toAcc === "all") {
|
|
|
+ var remainder = amount;
|
|
|
+ var i = 1;
|
|
|
+ while (remainder > 0) {
|
|
|
+ if (i !== fromAcc) {
|
|
|
+ remainder -= 1;
|
|
|
+ accounts[i][1] += 1;
|
|
|
+ }
|
|
|
+ i = (i % numPlayers) + 1;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (toAcc === "pot") {
|
|
|
+ toAcc = 0;
|
|
|
+ } else {
|
|
|
+ toAcc = parseInt(toAcc);
|
|
|
+ }
|
|
|
+ accounts[toAcc][1] += amount;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ printBalances();
|
|
|
+ }
|
|
|
+
|
|
|
+ initialize();
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|
|
|
+
|