Rock Paper Scissors – 当达到特定分数时停止游戏

huangapple go评论59阅读模式
英文:

Rock Paper Scissors - stop game when specific score is reached

问题

我需要在用户或计算机的得分达到20时停止游戏,并在游戏上显示文本提示:

// 在用户或计算机的得分达到20时停止游戏并显示提示
function checkGameOver() {
  if (userScore === 20 || computerScore === 20) {
    if (userScore === 20) {
      result_div.innerHTML = 'YOU WIN THE GAME!';
    } else {
      result_div.innerHTML = 'COMPUTER WINS THE GAME!';
    }

    // 禁用点击事件,防止继续游戏
    rock_div.removeEventListener('click', () => game('rock'));
    paper_div.removeEventListener('click', () => game('paper'));
    scissors_div.removeEventListener('click', () => game('scissors'));
  }
}

// 在每次更新得分后调用此函数来检查游戏是否结束
function updateScores(userChoice, computerChoice) {
  if (userScore === 20 || computerScore === 20) {
    return; // 游戏已经结束,不再更新得分
  }

  switch (userChoice + computerChoice) {
    case 'paperrock':
    case 'rockscissors':
    case 'scissorspaper':
      win(userChoice, computerChoice);
      break;
    case 'rockpaper':
    case 'scissorsrock':
    case 'paperscissors':
      loses(userChoice, computerChoice);
      break;
    case 'rockrock':
    case 'scissorsscissors':
    case 'paperpaper':
      draw(userChoice, computerChoice);
      break;
  }

  checkGameOver(); // 检查游戏是否结束
}

将上述代码添加到你的JavaScript代码中,以在每次更新得分时检查游戏是否结束。如果任一玩家的得分达到20,游戏将停止并显示相应的提示,同时禁用了进一步的点击事件以防止继续游戏。

英文:

I need to stop game with overlay over game with text notice, when user's or computer's score reach value 20:

// cache the dom (storing for future use)
// & reset everything to 0 value
let userScore = 0;
let computerScore = 0;
const userScore_span = document.getElementById('user-score');
const computerScore_span = document.getElementById('computer-score');
const scoreBoard_div = document.querySelector('.score-board');
const result_div = document.querySelector('.result');
const rock_div = document.getElementById('rock');
const paper_div = document.getElementById('paper');
const scissors_div = document.getElementById('scissors');


// set up the core function for the computer that will use math.random to loop through an array and return that value
function getComputerChoice() {
  const choices = ['rock', 'paper', 'scissors'];
  const randomNumber = Math.floor(Math.random() * 3);
  return choices[randomNumber];
}

// similar to convertcase but just takes lowercase and replaces with titlecase
function convertCase(anythingIwant) {
  if (anythingIwant === 'paper') return 'Paper';
  if (anythingIwant === 'scissors') return 'Scissors';
  return 'Rock';
}

// Winning Condition - this handles what happens when the user clicks one of the choices where the value is them passed through as a parameter
function win(user, computer) {
  userScore++;
  userScore_span.innerHTML = userScore;
  //const userName = ' (user)'.fontsize(3).sup();
  //const compName = ' (comp)'.fontsize(3).sup();
  result_div.innerHTML = 'YOU WIN';
  const roundStatus = document.getElementById(user);
  roundStatus.classList.add('winningStyles');
  setTimeout(() => roundStatus.classList.remove('winningStyles'), 300);
}

// Losing Condition - this handles what happens when the user clicks one of the choices where the value is them passed through as a parameter
function loses(user, computer) {
  computerScore++;
  computerScore_span.innerHTML = computerScore;
  //const userName = ' (user)'.fontsize(3).sup();
  //const compName = ' (comp)'.fontsize(3).sup();
  result_div.innerHTML = 'YOU LOSE';
  const roundStatus = document.getElementById(user);
  roundStatus.classList.add('losingStyles');
  setTimeout(() => roundStatus.classList.remove('losingStyles'), 300);
}

// Draw Condition - this handles what happens when the user clicks one of the choices where the value is them passed through as a parameter
function draw(user, computer) {
  //const userName = ' (user)'.fontsize(3).sup();
  //const compName = ' (comp)'.fontsize(3).sup();
  result_div.innerHTML = 'IT\'S A TIE';
  const roundStatus = document.getElementById(user);
  roundStatus.classList.add('drawStyles');
  setTimeout(() => roundStatus.classList.remove('drawStyles'), 300);
}

// The core game functions that set up and determine the games actual logic aka paper beats rock etc
function game(userChoice) {
  const computerChoice = getComputerChoice();

  switch (userChoice + computerChoice) {
	case 'paperrock':
	case 'rockscissors':
	case 'scissorspaper':
	  win(userChoice, computerChoice);
	  break;
	case 'rockpaper':
	case 'scissorsrock':
	case 'paperscissors':
	  loses(userChoice, computerChoice);
	  break;
	case 'rockrock':
	case 'scissorsscissors':
	case 'paperpaper':
	  draw(userChoice, computerChoice);
	  break;
  }
}

// This function creates and adds an eventlistener to the rock, paper scissors html element and the passes the value of that element to the game function
function main() {
  rock_div.addEventListener('click', () => game('rock'));
  paper_div.addEventListener('click', () => game('paper'));
  scissors_div.addEventListener('click', () => game('scissors'));
}

main();

this is HTML I use:

<div class="game">
	<div class="choices">
		<div id="rock" class="choice">
			<img src="/rock.png" title="Select option rock">
		</div>

		<div id="paper" class="choice">
			<img src="/paper.png" title="Select option paper">
		</div>

		<div id="scissors" class="choice">
			<img src="/scissors.png" title="Select option scissors">
		</div>
	</div>
	
	<div class="score">
		<div id="user-label" class="badge">Client - YOU</div>
		<div id="computer-label" class="badge">Competition</div>
		<span id="user-score">0</span><span class="separator">-</span><span id="computer-score">0</span>
	</div>
	
	<div class="result">
		It's your turn
	</div>
</div>

Everything works great, but I need to set limit of score to maximum 20. After reaching this limit (human or computer will reach score 20), the game will stop with displaying text notice over game to prevent endless clicking.

答案1

得分: 0

以下是您要翻译的代码部分:

const maxScore = 20;

function win(user, computer) {
  userScore++;
  userScore_span.innerHTML = userScore;

  if (userScore === maxScore) {
    gameOver('You Win!');
  } else {
    result_div.innerHTML = 'YOU WIN';
    const roundStatus = document.getElementById(user);
    roundStatus.classList.add('winningStyles');
    setTimeout(() => roundStatus.classList.remove('winningStyles'), 300);
  }
}

function loses(user, computer) {
  computerScore++;
  computerScore_span.innerHTML = computerScore;

  if (computerScore === maxScore) {
    gameOver('You Loose!');
  } else {
    result_div.innerHTML = 'YOU LOOSE';
    const roundStatus = document.getElementById(user);
    roundStatus.classList.add('losingStyles');
    setTimeout(() => roundStatus.classList.remove('losingStyles'), 300);
  }
}

function gameOver(message) {
  result_div.innerHTML = message;
  rock_div.removeEventListener('click', () => game('rock'));
  paper_div.removeEventListener('click', () => game('paper'));
  scissors_div.removeEventListener('click', () => game('scissors'));
}

function game(userChoice) {
  if (userScore === maxScore || computerScore === maxScore) {
    return;
  }

  const computerChoice = getComputerChoice();

  // 您的代码
}

请注意,代码中包含了一些HTML元素(例如userScore_span,result_div,roundStatus等),您需要确保在实际代码中定义了这些元素。

英文:

Actually, you should add counter and handlers to your script.
Rewrite win / loose functions as follows:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const maxScore = 20;

function win(user, computer) {
  userScore++;
  userScore_span.innerHTML = userScore;

  if (userScore === maxScore) {
    gameOver(&#39;You Win!&#39;);
  } else {
    result_div.innerHTML = &#39;YOU WIN&#39;;
    const roundStatus = document.getElementById(user);
    roundStatus.classList.add(&#39;winningStyles&#39;);
    setTimeout(() =&gt; roundStatus.classList.remove(&#39;winningStyles&#39;), 300);
  }
}


function loses(user, computer) {
  computerScore++;
  computerScore_span.innerHTML = computerScore;

  if (computerScore === maxScore) {
    gameOver(&#39;You Loose!&#39;);
  } else {
    result_div.innerHTML = &#39;YOU LOOSE&#39;;
    const roundStatus = document.getElementById(user);
    roundStatus.classList.add(&#39;losingStyles&#39;);
    setTimeout(() =&gt; roundStatus.classList.remove(&#39;losingStyles&#39;), 300);
  }
}

function gameOver(message) {
  result_div.innerHTML = message;
  rock_div.removeEventListener(&#39;click&#39;, () =&gt; game(&#39;rock&#39;));
  paper_div.removeEventListener(&#39;click&#39;, () =&gt; game(&#39;paper&#39;));
  scissors_div.removeEventListener(&#39;click&#39;, () =&gt; game(&#39;scissors&#39;));
}

<!-- end snippet -->

and then add condition to your game

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function game(userChoice) {
  if (userScore === maxScore || computerScore === maxScore) {
    return;
  }

  const computerChoice = getComputerChoice();

  // your code
}

<!-- end snippet -->

With these changes, when either the human or the computer reaches a score of 20, the game will display the appropriate notice, and the click events on the choices would be removed, preventing further clicks and ending the game.

huangapple
  • 本文由 发表于 2023年8月4日 00:38:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830066.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定