简体   繁体   English

我怎样才能将这两个功能轮流转为 Naughts 和 cross

[英]How can i turn these two functions into a turn by turn for Naughts and crosses

I have two functions (one that runs) I'm wanting to use these and create two player turn variables to switch between each other marking an X or an O when it's each players turn.我有两个函数(一个可以运行)我想使用它们并创建两个玩家回合变量,以便在每个玩家回合时在彼此之间切换,标记 X 或 O。 I'm wanting to keep it as simple as possible but cannot even fathom where to go.我想让它尽可能简单,但甚至无法理解 go 的位置。 I know it's not great but if I can I would like to keep these functions or something close to it.我知道这不是很好,但如果可以的话,我想保留这些功能或接近它的东西。

   <section class="allBoxes">
        <div class="box1"></div>
        <div class="box2"></div>
        <div class="box3"></div>
        <div class="box4"></div>
        <div class="box5"></div>
        <div class="box6"></div>
        <div class="box7"></div>
        <div class="box8"></div>
        <div class="box9"></div>
        <script src="tictac.js"></script>
      </section>

function clickBoxPlayerOne(event) {
  var boxClicked = event.target;
  if ((boxClicked.textContent = " ")) boxClicked.textContent = "X";
}
var allBoxes = document.querySelector(".allBoxes");
allBoxes.addEventListener("click", clickBoxPlayerOne);

function clickBoxPlayerTwo(event) {
  var boxClicked = event.target;
  if (boxClicked.textContent != "X") boxClicked.textContent = "O";
}
var allBoxes = document.querySelector(".allBoxes");
allBoxes.addEventListener("click", clickBoxPlayerTwo);

I think what you need is a global variable to keep the current player state and update it whenever the user clicks on any box.我认为您需要一个全局变量来保留当前播放器 state 并在用户单击任何框时对其进行更新。 Like in the Tic-tac-toe:)就像井字游戏一样:)

Adding a sample working code for reference to see how the usage is.添加示例工作代码以供参考,以查看使用情况。

 const player1 = "X", player2 = "O"; let currentPlayer = player1; const elements = document.getElementsByClassName("tile"); // Click handler for (var i = 0; i < elements.length; i++) { elements[i].addEventListener('click', playMe, false); } function playMe(event) { if(event.target.innerText == "") { event.target.innerText = currentPlayer; currentPlayer = currentPlayer == player1? player2: player1; } }
 .tile { padding: 10px; border: 1px solid #CCC; width: 40px; display: inline-block; text-align: center; margin-right: 10px; }.row { margin: 10px; }
 <div> <div class="row"><span class="tile"></span><span class="tile"></span><span class="tile"></span></div> <div class="row"><span class="tile"></span><span class="tile"></span><span class="tile"></span></div> <div class="row"><span class="tile"></span><span class="tile"></span><span class="tile"></span></div> </div>

To remove the need for global variables I would use the function that's called from addEventListener to set up those variables, and return a new function (closure) that is the function that's returned to the listener.为了消除对全局变量的需求,我将使用从addEventListener调用的 function 来设置这些变量,并返回一个新的 function(闭包),即 ZC1C425268E68385D1AB5074C 返回给监听器。 The closure maintains the variables in its local lexical environment so it can update them.闭包在其本地词法环境中维护变量,以便可以更新它们。

I would also use CSS Grid to handle the display.我还将使用 CSS Grid 来处理显示。

 const allBoxes = document.querySelector('.allboxes'); allBoxes.addEventListener('click', handleClick(), false); function handleClick() { // Initialise the player variable let player = 1; // Return the function that will be used // when the listener is called return function (e) { // Get the text content of the clicked element const { textContent } = e.target; if (,textContent) { // Update the content of the square? and // then swap the player const content = player === 1: 'X'; 'O'. e.target;textContent = content? player = player === 1: 2; 1; } } }
 .allboxes { width: 50%; display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5em; }.allboxes div { display: flex; border: 1px solid black; height: 2em; width: 2em; align-items: center; justify-content: center;}
 <div class="allboxes"> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM