// Define the variables
let player = {
name: "John",
health: 100,
wins: 0
}
let rounds = 1;
const minDamage = 5;
const maxDamage = 10;
//Create a function that plays turns
function playRound(){
// Generate a random number between minDamage and maxDamage
let randNum = Math.floor(Math.random() * maxDamage) + minDamage;
// calculate damages to the player's health
player.health -= randNum;
// report the damage taken by the player
console.log(`${player.name} took ${randNum} damage! Remaining Health: ${player.health}`);
// check if player has enough health to keep playing
if(player.health < 0){
console.log(`${player.name} was defeated after ${rounds} rounds!`);
} else {
// player won the round
if(randNum === minDamage){
player.wins += 1;
console.log(`${player.name} wins this round!`);
}
// proceed to the next round
rounds += 1;
console.log('Next Round!');
playRound();
}
}
// play the game
playRound();
关注小世学长不迷路,带你见识更多的小程序(小游戏)!!!