了解しました。 以下に、スマホで遊べる自動販売機の対決ゲームを実装したHTMLファイルとJavaScriptコードを示します。 【ゲーム概要】 2人のプレイヤー(Aさん、Bさん)が自動販売機の前に立ち、お金を投入してドリンクを購入する対決ゲームです。プレイヤーは1回ずつ交互に行動し、手持ちのお金で購入できるドリンクを選んで投入します。購入後は、ドリンクの種類と残り在庫数が表示されます。在庫がなくなったドリンクは購入できません。先にお金がなくなったプレイヤーが敗北となります。 【HTMLコード】
自動販売機の対決
Player A
Money: 10
Player B
Money: 10
Vending Machine
Stock: 3Stock: 2Stock: 1
[JavaScript code]. // Set initial values for Player A let moneyA = 10; let drinkA = [, 0, 0, 0]; // type of drink selected and amount paid let imgA = document.querySelector(’#playerA img’); let moneyElemA = document.querySelector(’#moneyA’); // Set initial values for player B let moneyB = 10; let drinkB = [, 0, 0, 0]; let imgB = document.querySelector(’#playerB img’); let moneyElemB = document.querySelector(’#moneyB’); // Set initial values for vending machine let stock = [0, 3, 2, 1]; // 在庫数 let price = [0, 3, 2, 1]; // 価格 // Function to purchase drinks function buy(d) { let player, money, drink, img, moneyElem; if (moneyA > 0 if (d >= 1 && d <= 3 && stock[d] > 0) { // 有効なドリンクを選択した場合 // Alternate actions. if (drinkA[1] == 0) { // Player As action player = A; money = moneyA; drink = drinkA; img = imgA; moneyElem = moneyElemA; drink[1] = price[d]; drink[2] = d; money -= price[d]; moneyElem.innerHTML = money; stock[d] -= 1; document.querySelector('#' + d[0].toLowerCase() + d.slice(1)).innerHTML = 'Stock: ' + stock[d]; } else { // Player Bs action player = B; money = moneyB; drink = drinkB; img = imgB; moneyElem = moneyElemB; drink[1] = price[d]; drink[2] = d; money -= price[d]; moneyElem.innerHTML = money; stock[d] -= 1; document.querySelector('#' + d[0].toLowerCase() + d.slice(1)).innerHTML = 'Stock: ' + stock[d]; // Determination of action result if (drinkA[2] == drinkB[2]) { // if both parties select the same drink if (drinkA[1] < drinkB[1]) { // player A pays more moneyB += drinkB[1] - drinkA[1]; } else if (drinkA[1] > drinkB[1]) { // player B pays more moneyA += drinkA[1] - drinkB[1]; } } else if (drinkA[2] < drinkB[2]) { // if player A selects a lower drink img.style.opacity = 0.5; moneyB += drinkB[1]; } else { // if player B selects a lower drink img.style.opacity = 0.5; moneyA += drinkA[1]; } drinkA = [, 0, 0, 0]; // initialize drink information for player A drinkB = [, 0, 0, 0]; // initialize drink information for player B imgA.style.opacity = 1; // return player As image imgB.style.opacity = 1; // return player Bs image moneyElemA.innerHTML = moneyA; // update player As money moneyElemB.innerHTML = moneyB; // update player Bs money } } } else { // if either player runs out of money alertHisa('Game Over'); // ゲーム終了のアラートを表示 ; // ページをリロードして初期状態に戻す } } Note] This program is designed to、For a competitive game in which two players take turns、In the JavaScript code、To determine which player will act、The drink information object is used. Each player is assigned a、First, it has an empty drink information object、Update price and type when selecting drinks. Compare the drinks selected by both parties.、Determining winners and、Update money and inventory counts. Player A and drink types and prices are、Convenient values are set as an example of implementation. Also、Style sheets are implemented with CSS, but、To display pictograms、I use the service of TWEMOJI.