Skip to content

Commit

Permalink
finish code for playing against computer
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiwenge2 committed Sep 1, 2021
1 parent 8da8d64 commit 5f30776
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 52 deletions.
2 changes: 1 addition & 1 deletion public/js/game/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down
3 changes: 3 additions & 0 deletions public/js/game/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
67 changes: 16 additions & 51 deletions public/js/game/playagainstcomputer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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})`;
Expand All @@ -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)){
Expand All @@ -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(() => {});
});
}

0 comments on commit 5f30776

Please sign in to comment.