grid 不允许被点击两次

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

grid not allowed to be clicked twice

问题

我有一个名为“gridbox”的网格,项目“box1”不应允许第二次点击。这是为了我的井字游戏。

在函数check()中,应该阻止第二次点击该方块。

```html
<section class="gridbox">
	<div class="box1" onclick="check()"></div>
</section>

<details>
<summary>英文:</summary>

I have a grid called &quot;gridbox&quot; and the item &quot;box1&quot; should not be allowed to get clicked a 2nd time. Its for my Tik Tak Toe game.


In the function check(), it should prevent the box from getting clicked a 2nd time.


<section class="gridbox">
<div class="box1" onclick="check()"></div>
</section>



</details>


# 答案1
**得分**: 0

欢迎来到Stack Overflow!

在没有你的其他代码的情况下,我不知道你如何准确地存储井字棋棋盘上每个单元格的状态,所以我使用了一个数组来表示它。

我们将创建一个名为`boardState`的变量来表示棋盘上每个单元格的X和O状态:

```js
// board state将包含井字棋棋盘上每个单元格的当前状态
// 可以是'', 'X'或'O',其中''表示空单元格
//
// 这行代码基本上创建了一个包含9个元素的数组(每个单元格一个元素)
// 并将每个元素设置为''。
var boardState = Array(9).fill('')

你的check函数将看起来像这样:

// 我们的check函数将检查是否可以在该单元格放置标记
function check(target, i) {
    if (boardState[i] === '') { // 如果单元格为空
        boardState[i] = currentTurn // 更新boardState
        target.innerHTML = currentTurn // 填充<div>的值

        // 切换到另一个玩家的回合
        if (currentTurn === 'X') {
            currentTurn = 'O'
        } else {
            currentTurn = 'X'
        }
    }
}

这个函数接受两个参数,

  • target:被点击的元素
  • i:被点击的单元格的索引

现在,在调用check函数时,你可以传递每个单元格的目标和索引到你的onclick事件中,棋盘上的每个

都有一个递增的索引。我创建了3个

,展示了我在谈论什么:

<section class="gridbox">
    <div class="box1" onclick="check(this, 0)"></div>
    <div class="box1" onclick="check(this, 1)"></div>
    <div class="box1" onclick="check(this, 2)"></div>
</section>

现在,当你点击

时,它只会在单元格已经为空的情况下填充值 - 否则什么都不会发生。

这是一个用于3个框的基本工作示例的完整代码:

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-html -->

<style>
    div.box1 {
        width: 100px;
        height: 100px;
        border: solid 1px black;

        /* 这部分是为了居中文本 */
        display: flex;
        align-items: center;
        justify-content: center;
    }
</style>

<script>
    // board state将包含井字棋棋盘上每个单元格的当前状态
    // 可以是'', 'X'或'O',其中''表示空单元格
    //
    // 这行代码基本上创建了一个包含9个元素的数组(每个单元格一个元素)
    // 并将每个元素设置为''。
    var boardState = Array(9).fill('')

    var currentTurn = 'X'

    // 我们的check函数将检查是否可以在该单元格放置标记
    function check(target, i) {
        if (boardState[i] === '') { // 如果单元格为空
            boardState[i] = currentTurn
            target.innerHTML = currentTurn

            if (currentTurn === 'X') {
                currentTurn = 'O'
            } else {
                currentTurn = 'X'
            }
        }
    }
</script>

<section class="gridbox">
    <div class="box1" onclick="check(this, 0)"></div>
    <div class="box1" onclick="check(this, 1)"></div>
    <div class="box1" onclick="check(this, 2)"></div>
</section>

<!-- 结束代码片段 -->
英文:

Welcome to Stack Overflow!

Without the rest of your code I don't know exactly how you're storing the state of each cell on the Tic Tac Toe board so I used an array to represent it.

We'll create a variable called boardState to represent the X,O state of each cell on the board:

// board state will contain the current state of each cell
// of the tic tac toe board. &#39;&#39;,&#39;X&#39;, &#39;O&#39;, where &#39;&#39; indicates
// an empty cell
//
// This line is basically creating an array of 9 elements (one for each cell)
// and setting each to &#39;&#39;.
var boardState = Array(9).fill(&#39;&#39;)

Your check function will look something like this:

// our check function will check whether or not the cell can have the marker placed
function check(target, i) {
    if (boardState[i] === &#39;&#39;) { // if the cell is empty
        boardState[i] = currentTurn // update boardState
        target.innerHTML = currentTurn // fill &lt;div&gt; with value

        // switch to other person&#39;s turn
        if (currentTurn === &#39;X&#39;) {
            currentTurn = &#39;O&#39;
        } else {
            currentTurn = &#39;X&#39;
        }
    }
}

This function takes 2 arguments,

  • target: The element that was clicked
  • i: The index of the cell that was clicked

Now, when calling the check function on your onclick event, you can pass the target and index of each, with each div on the board having an increasing index. I've created 3 divs here that show what I'm talking about:

&lt;section class=&quot;gridbox&quot;&gt;
    &lt;div class=&quot;box1&quot; onclick=&quot;check(this, 0)&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;box1&quot; onclick=&quot;check(this, 1)&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;box1&quot; onclick=&quot;check(this, 2)&quot;&gt;&lt;/div&gt;
&lt;/section&gt;

Now when you click on the div it'll fill in the value only if it's already empty - otherwise, nothing will happen.

Here's the full code to get a basic working example for 3 boxes:

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

<!-- language: lang-html -->

&lt;style&gt;
    div.box1 {
        width: 100px;
        height: 100px;
        border: solid 1px black;

        /* This part is to center the text */
        display: flex;
        align-items: center;
        justify-content: center;
    }
&lt;/style&gt;

&lt;script&gt;
    // board state will contain the current state of each cell
    // of the tic tac toe board. &#39;&#39;,&#39;X&#39;, &#39;O&#39;, where &#39;&#39; indicates
    // an empty cell
    //
    // This line is basically creating an array of 9 elements (one for each cell)
    // and setting each to &#39;&#39;.
    var boardState = Array(9).fill(&#39;&#39;)

    var currentTurn = &#39;X&#39;

    // our check function will check whether or not the cell can have the marker placed
    function check(target, i) {
        if (boardState[i] === &#39;&#39;) { // if the cell is empty
            boardState[i] = currentTurn
            target.innerHTML = currentTurn

            if (currentTurn === &#39;X&#39;) {
                currentTurn = &#39;O&#39;
            } else {
                currentTurn = &#39;X&#39;
            }
        }
    }
&lt;/script&gt;

&lt;section class=&quot;gridbox&quot;&gt;
    &lt;div class=&quot;box1&quot; onclick=&quot;check(this, 0)&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;box1&quot; onclick=&quot;check(this, 1)&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;box1&quot; onclick=&quot;check(this, 2)&quot;&gt;&lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

答案2

得分: 0

这段代码主要是关于点击事件处理和类的使用。当点击一个框时,会检查它是否有一个名为"active"的类,如果没有,则添加这个类,如果已经有了,则不进行任何操作。这个示例中,当点击框时只是添加了一个背景颜色,你可以根据这个逻辑来实现你的井字棋游戏。

.grid { display: grid; grid-template-columns: 1fr 50px; width: 102px; gap: 2px; }
.box { width: 50px; aspect-ratio: 1; background-color: #efefef; border: 1px solid #666; }
.box:hover:not(.active) { cursor: pointer; background-color: lightblue; }
.active { background-color: lightskyblue; }
// 获取网格
const grid = document.querySelector('.grid');

// 给它附加一个事件监听器
// 这允许我们使用事件委托来捕获
// 其子元素的事件,因为它们在DOM中“冒泡”
grid.addEventListener('click', handleClick);

function handleClick(e) {
  const el = e.target;

  // 如果它是一个带有box类的元素
  if (el.matches('.box')) {
    
    // 如果元素没有一个active类,则添加一个
    // 否则什么都不做
    if (!el.classList.contains('active')) {
      el.classList.add('active');
    }

  }
}
<section class="grid">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</section>

额外的文档:

英文:

It's less about whether the box can be clicked but more that nothing should happen if a box is clicked more than once. If you use classes you can check to see if a button has an "active" class (for example), and if it doesn't add an "active" class to it. If the code does have an active class nothing happens.

In this example I'm just adding a background colour to a boxes when it's clicked. But you can use this to implement your TTT logic.

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

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

// Grab the grid
const grid = document.querySelector(&#39;.grid&#39;);

// Attach an event listener to it.
// This allows us to use event delegation to catch
// events from its children as they &quot;bubble up&quot; the DOM
grid.addEventListener(&#39;click&#39;, handleClick);

function handleClick(e) {

  const el = e.target;

  // If it&#39;s an element with a box class
  if (el.matches(&#39;.box&#39;)) {
    
    // If the element doesn&#39;t have an active class add one
    // otherwise do nothing
    if (!el.classList.contains(&#39;active&#39;)) {
      el.classList.add(&#39;active&#39;);
    }

  }

}

<!-- language: lang-css -->

.grid { display: grid; grid-template-columns: 1fr 50px; width: 102px; gap: 2px; }
.box { width: 50px; aspect-ratio: 1; background-color: #efefef; border: 1px solid #666; }
.box:hover:not(.active) { cursor: pointer; background-color: lightblue; }
.active { background-color: lightskyblue; }

<!-- language: lang-html -->

&lt;section class=&quot;grid&quot;&gt;
  &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;box&quot;&gt;&lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

Additional documentation

huangapple
  • 本文由 发表于 2023年4月19日 17:42:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053019.html
匿名

发表评论

匿名网友

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

确定