From 5f307767a72e781b71773821ea365396a1239ffa Mon Sep 17 00:00:00 2001 From: ruiwenge2 <85425620+ruiwenge2@users.noreply.github.com> Date: Wed, 1 Sep 2021 01:07:10 +0000 Subject: [PATCH] finish code for playing against computer --- public/js/game/all.js | 2 +- public/js/game/helper.js | 3 ++ public/js/game/playagainstcomputer.js | 67 +++++++-------------------- 3 files changed, 20 insertions(+), 52 deletions(-) diff --git a/public/js/game/all.js b/public/js/game/all.js index 43a7fb9..89ffe30 100644 --- a/public/js/game/all.js +++ b/public/js/game/all.js @@ -252,7 +252,7 @@ class Voldemort extends Character { if(this.health <= 50){ let difference = 100 - this.health; this.health += difference; - print("Voldemort got {} more health.".format(difference)); + print(`Voldemort got ${difference} more health.`); } else { this.health += 50; print("Voldemort got 50 more health."); diff --git a/public/js/game/helper.js b/public/js/game/helper.js index 0a9bdcb..bab489b 100644 --- a/public/js/game/helper.js +++ b/public/js/game/helper.js @@ -22,6 +22,9 @@ function clear(){ function showWinner(character, username, character2, username2){ setTimeout(function(){alertmodal("Game Ended!", `Because ${character2.name} (${username2}) has no more health, ${character.name} (${username}) has won the game!`).then(() => location.href = "/join")}, 1500); } +function showWinner2(character, username, character2, username2){ + setTimeout(function(){alertmodal("Game Ended!", `Because ${character2.name} (${username2}) has no more health, ${character.name} (${username}) has won the game!`, ok="Play Again").then(() => location.reload())}, 1500); +} function focusMoves(){ document.getElementById("allmoves").focus(); window.scrollTo(0,document.body.scrollHeight); diff --git a/public/js/game/playagainstcomputer.js b/public/js/game/playagainstcomputer.js index e33fe83..7add6d8 100644 --- a/public/js/game/playagainstcomputer.js +++ b/public/js/game/playagainstcomputer.js @@ -30,41 +30,26 @@ document.getElementById("allmoves-h1").style.display = "block"; updateStatus(); showMoves(); -socket.on("move", async data => { +async function computerMove(){ othermove = true; - switch(data.type){ - case "attack": - othervalue = data.value; - break - case "heal": - othervalue = data.value; - break; - case "chances": - othervalue1 = data.value1; - othervalue2 = data.value2; - otherrandint = data.randint; - break; - case "owndamage": - othervalue1 = data.value1; - otheroppovalue = data.oppovalue; - otherrandint = data.randint; - break; - } + await sleep(random(1, 5)); clear(); focusMoves(); - print(`${otherchar.name} (${otheruser}): ${data.move}`); - await otherchar.choosemove(data.move, char); + let allmoves = Object.keys(otherchar.moves); + let move = allmoves[random(0, allmoves.length - 1)]; + print(`${otherchar.name} (${otheruser}): ${move}`); + await otherchar.choosemove(move, char); updateStatus(); if(char.health <= 0){ - showWinner(otherchar, otheruser, char, user); + showWinner2(otherchar, otheruser, char, user); return; } if(otherchar.health <= 0){ - showWinner(char, user, otherchar, otheruser); + showWinner2(char, user, otherchar, otheruser); return; } showMoves(); -}); +} function updateStatus(){ document.getElementById("names").innerHTML = `${char.name} (${user})`; @@ -88,6 +73,7 @@ function showMoves(){ if(othermove){ document.getElementById("player1-message").innerHTML = "The other player has chosen their move. Your turn. Choose a move:" } + document.getElementById("player2-message").innerHTML = ""; document.getElementById("player1-moves").innerHTML = ""; let moves = char.moves; for(let i of Object.keys(moves)){ @@ -105,39 +91,18 @@ function move(text){ focusMoves(); print(`${char.name} (${user}): ${text}`); char.choosemove(text, otherchar).then(() => { - let data = {}; - data.move = text; - switch(type){ - case "attack": - data.value = value; - break; - case "heal": - data.value = value; - break; - case "chances": - data.value1 = value1; - data.value2 = value2; - data.randint = randint; - break; - case "owndamage": - data.value1 = value1; - data.oppovalue = oppovalue; - data.randint = randint; - break; - } - data.type = type; - socket.emit("move", room, data); + updateStatus(); if(char.health <= 0){ - showWinner(otherchar, otheruser, char, user); + showWinner2(otherchar, otheruser, char, user); return; - } + } if(otherchar.health <= 0){ - showWinner(char, user, otherchar, otheruser); + showWinner2(char, user, otherchar, otheruser); return; } document.getElementById("player1-message").innerHTML = ""; document.getElementById("player1-moves").innerHTML = ""; - document.getElementById("player2-message").innerHTML = "Other player's turn to choose their move."; - updateStatus(); + document.getElementById("player2-message").innerHTML = "Computer's turn to choose their move."; + computerMove().then(() => {}); }); } \ No newline at end of file