tic-tac-toe 相关问题

Tic Tac Toe是开始编码的流行练习,因为有限的资源和游戏机制可以很容易地掌握并以多种方式表现出来。因为它是一个简短的游戏,所以可以创建一个永不丢失的算法。

如何在 Python Turtle 的 Tic-Tac-Toe 游戏中交替 2 个玩家?

我正在上计算机科学课,我们的第一个Python项目(海龟)是一个小井字游戏。该项目已接近完成,但我正在努力弄清楚如何在 2 之间切换

回答 1 投票 0

我正在制作井字游戏,在这个玩家类中我有玩家 X 和 O。我希望能够添加另一个玩家

添加另一个玩家时,如果有 3 个玩家,则角色应该是 X O P,如果有 4 个玩家,则应该是 X O P Q。我正在考虑使用带有 NumbOfPlayers 的 if 循环和

回答 1 投票 0

onClick 事件在 SVG Tic Tac Toe 中不起作用

我目前正在创建一个 Tic Tac Toe 游戏,并拥有使 x 和 o 出现的代码。然而,当点击方块时,onclick 事件并不总是被调用,并且是随机的

回答 1 投票 0

如何在tkinter python中获取按钮网格化的框架名称

我正在尝试在 tkinter python 中制作超级井字游戏。如果我单击窗口中的任何按钮,标签就会打包在新的 tkinter 框架中。 我想要标签代替按钮,即

回答 1 投票 0

如何在 tic tac toe 中制作 3 排检测系统?

我正在制作井字游戏,我已经实现了标记未占用的字段。有一个基于回合的系统,该系统会在其中一个字段被标记后切换玩家标记。问题...

回答 1 投票 0

尝试修复此浏览器井字游戏中的错误

我正在尝试用人工智能制作井字游戏。到目前为止,我已经应用了游戏逻辑、人工智能逻辑、玩家和游戏板处理。一切正常,但选择角色有问题,如果我选择......

回答 1 投票 0

在 p5.js 中实现 Tic Tac Toe 的 Minimax 算法时遇到问题 - 需要帮助调试 AI 逻辑

问题: 我目前正在使用 p5.js 中的极小极大算法开发 Tic Tac Toe 游戏,并且遇到了 AI 行为的问题。尽管我很努力,但我似乎无法让人工智能...

回答 1 投票 0

如何更改税务 tac toe 游戏中玩家的颜色?

我正在用JS制作一个井字棋游戏。代码如下。 我正在用 JS 制作一个 tic tac toe 游戏。代码如下。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Tic Tac Toe</title> <link rel="preconnect" href="https://fonts.googleapis.com" /> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet" /> <link rel="stylesheet" href="tis.css" /> </head> <body> <div id="gameContainer"> <h1>Tic Tac Toe</h1> <h2 id="statusText">Testing...</h2> <div id="cellContainer"> <div cellIndex="0" class="cell"></div> <div cellIndex="1" class="cell"></div> <div cellIndex="2" class="cell"></div> <div cellIndex="3" class="cell"></div> <div cellIndex="4" class="cell"></div> <div cellIndex="5" class="cell"></div> <div cellIndex="6" class="cell"></div> <div cellIndex="7" class="cell"></div> <div cellIndex="8" class="cell"></div> </div> <button id="restartBtn">RESTART</button> </div> <script src="tic.js"></script> </body> </html> body { padding: 0; margin: 0; font-family: "Press Start 2P", sans-serif; background-color: black; height: 100vh; /*height is the height of the browser vwindow*/ text-align: center; } h1 { color: white; text-align: center; font-size: 50px; margin-top: 10%; } #statusText { color: white; text-align: center; font-size: 15px; margin-top: 2em; /*1m is margin size of the font size}*/ margin-bottom: 2em; } #cellContainer { width: 300px; height: 300px; margin: 0 auto; /*centre the element to its parent container*/ color: #fff; border: 2px solid white; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); /*each column and each row a third of the grid*/ } .cell { border: 2px solid white; border-radius: 2px; font-weight: bold; font-size: 50px; display: flex; justify-content: center; align-items: center; } #restartBtn { padding: 15px; margin: 1.5em; border-radius: 10px; border: none; cursor: pointer; font-size: 30px; font-family: "Press Start 2P", sans-serif; background-color: #d62839; color: white; } const cells = document.querySelectorAll(".cell"); const statusText = document.querySelector("#statusText"); const restartBtn = document.querySelector("#restartBtn"); const winConditions = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6], ]; let options = ["", "", "", "", "", "", "", "", ""]; //empty board. array of empty trings representing the board options let currentPlayer = "X"; //starting player, to keep track let isGameActive = false; //keep track of wheteher game is running. will be stwitched to true when game is initialised initialiseGame(); //this function will be called whenever we open the browser function initialiseGame() { cells.forEach((cell) => cell.addEventListener("click", cellClicked)); //for each cell carry out the cell clicked function restartBtn.addEventListener("click", restartGame); //when we click restarttn, run the restart game statusText.textContent = `Waiting for Player ${currentPlayer} to start game`; isGameActive = true; } function cellClicked() { const cellIndex = this.getAttribute("cellIndex"); //get the attribute. we want the elements with the attribute cellIndex to execute the cunction //if the cell isnt empty or game isnt running dont do anything if cell is clicked if (options[cellIndex] != "" || !isGameActive) { return; } updateCell(this, cellIndex); //otherwise reun the updateCellFunction with this as a parameter? checkWinner(); } function updateCell(cell, index) { options[index] = currentPlayer; //update the placeholders in the options array, aka generate options array using index and place current players value within the index of the cell clicked cell.textContent = currentPlayer; //display the above within the cell addColour(); } function addColour() { if (currentPlayer == "X") { document.querySelector(".cell").style.color = "red"; } } function changePlayer() { currentPlayer = currentPlayer == "X" ? "O" : "X"; statusText.textContent = `Waiting for Player ${currentPlayer} to play turn`; } function checkWinner() { let roundWon = false; for (let i = 0; i < winConditions.length; i++) { const condition = winConditions[i]; //we take the index of all the win conditions const cellA = options[condition[0]]; //first index in winconditions const cellB = options[condition[1]]; //second index in winconditions const cellC = options[condition[2]]; //third index in winconditions //if we check wincondition's index 0 aka 0,1,2. cell a will be 0, cellb will be 1 cell c will be 2 for index 2 a=3 b=4 c=5, for index 7 a =2 =4 c=6 //3 cells as 3 indices ber wincondition and 3x3 grid if (cellA == "" || cellB == "" || cellC == "") { continue; //aka keep loop running if the indices arent complete therefore condition hasnt been met } //checking if all the indices are Xs or Os for the winning condition if (cellA == cellB && cellB == cellC) { roundWon = true; break; //break out of the for loop if win condition me } } if (roundWon) { statusText.textContent = `${currentPlayer} has Won!`; isGameActive = false; //end game aka make nothing be able to be clicked anymore since game has been won //if options does not include any spaces then update status aka all the board has been played with no winners so far } else if (!options.includes("")) { statusText.textContent = "Match draw"; //if noone has won yet and noone has drawn (aka there are still empty cells, run the function to change player) } else { changePlayer(); } } function restartGame() { currentPlayer = "X"; options = ["", "", "", "", "", "", "", "", ""]; statusText.textContent = `${currentPlayer}'s turn`; cells.forEach((cell) => (cell.textContent = "")); isGameActive = true; } 我是一个初学者(就像非常初学者一样),这是我的第一个项目。我已经按照教程成功制作了一款可以运行的游戏。我想让玩家 X 为一种颜色,而玩家 O 为另一种颜色。这样,当 X 单击时,单元格内文本内容的颜色样式将为特定颜色,并且与 O 相同。 function updateCell(cell, index) { options[index] = currentPlayer; //update the placeholders in the options array, aka generate options array using index and place current players value within the index of the cell clicked cell.textContent = currentPlayer; //display the above within the cell addColour(); } function addColour() { if (currentPlayer == "X") { document.querySelector(".cell").style.color = "red"; } } 我尝试在 updateCell 函数中添加一个函数,我认为可以这样做,但它只会将第一个 X 变成红色(出于我无法理解的原因。 addColour()函数正在查询单元格类别,并为所有单元格设置颜色。更改它以接受单元格作为其参数。根据当前玩家仅设置该单元格的颜色。像这样... function addColour(cell) { const color = (currentPlayer == "X") ? "red" : "green"; cell.style.color = color; } 仅进行此更改,应用程序运行如下... const cells = document.querySelectorAll(".cell"); const statusText = document.querySelector("#statusText"); const restartBtn = document.querySelector("#restartBtn"); const winConditions = [ [0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6], ]; let options = ["", "", "", "", "", "", "", "", ""]; //empty board. array of empty trings representing the board options let currentPlayer = "X"; //starting player, to keep track let isGameActive = false; //keep track of wheteher game is running. will be stwitched to true when game is initialised initialiseGame(); //this function will be called whenever we open the browser function initialiseGame() { cells.forEach((cell) => cell.addEventListener("click", cellClicked)); //for each cell carry out the cell clicked function restartBtn.addEventListener("click", restartGame); //when we click restarttn, run the restart game statusText.textContent = `Waiting for Player ${currentPlayer} to start game`; isGameActive = true; } function cellClicked() { const cellIndex = this.getAttribute("cellIndex"); //get the attribute. we want the elements with the attribute cellIndex to execute the cunction //if the cell isnt empty or game isnt running dont do anything if cell is clicked if (options[cellIndex] != "" || !isGameActive) { return; } updateCell(this, cellIndex); //otherwise reun the updateCellFunction with this as a parameter? checkWinner(); } function updateCell(cell, index) { options[index] = currentPlayer; //update the placeholders in the options array, aka generate options array using index and place current players value within the index of the cell clicked cell.textContent = currentPlayer; //display the above within the cell addColour(cell); } function addColour(cell) { const color = (currentPlayer == "X") ? "red" : "green"; cell.style.color = color; } function changePlayer() { currentPlayer = currentPlayer == "X" ? "O" : "X"; statusText.textContent = `Waiting for Player ${currentPlayer} to play turn`; } function checkWinner() { let roundWon = false; for (let i = 0; i < winConditions.length; i++) { const condition = winConditions[i]; //we take the index of all the win conditions const cellA = options[condition[0]]; //first index in winconditions const cellB = options[condition[1]]; //second index in winconditions const cellC = options[condition[2]]; //third index in winconditions //if we check wincondition's index 0 aka 0,1,2. cell a will be 0, cellb will be 1 cell c will be 2 for index 2 a=3 b=4 c=5, for index 7 a =2 =4 c=6 //3 cells as 3 indices ber wincondition and 3x3 grid if (cellA == "" || cellB == "" || cellC == "") { continue; //aka keep loop running if the indices arent complete therefore condition hasnt been met } //checking if all the indices are Xs or Os for the winning condition if (cellA == cellB && cellB == cellC) { roundWon = true; break; //break out of the for loop if win condition me } } if (roundWon) { statusText.textContent = `${currentPlayer} has Won!`; isGameActive = false; //end game aka make nothing be able to be clicked anymore since game has been won //if options does not include any spaces then update status aka all the board has been played with no winners so far } else if (!options.includes("")) { statusText.textContent = "Match draw"; //if noone has won yet and noone has drawn (aka there are still empty cells, run the function to change player) } else { changePlayer(); } } function restartGame() { currentPlayer = "X"; options = ["", "", "", "", "", "", "", "", ""]; statusText.textContent = `${currentPlayer}'s turn`; cells.forEach((cell) => (cell.textContent = "")); isGameActive = true; } body { padding: 0; margin: 0; font-family: "Press Start 2P", sans-serif; background-color: black; height: 100vh; /*height is the height of the browser vwindow*/ text-align: center; } h1 { color: white; text-align: center; font-size: 50px; margin-top: 10%; } #statusText { color: white; text-align: center; font-size: 15px; margin-top: 2em; /*1m is margin size of the font size}*/ margin-bottom: 2em; } #cellContainer { width: 300px; height: 300px; margin: 0 auto; /*centre the element to its parent container*/ color: #fff; border: 2px solid white; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); /*each column and each row a third of the grid*/ } .cell { border: 2px solid white; border-radius: 2px; font-weight: bold; font-size: 50px; display: flex; justify-content: center; align-items: center; } #restartBtn { padding: 15px; margin: 1.5em; border-radius: 10px; border: none; cursor: pointer; font-size: 30px; font-family: "Press Start 2P", sans-serif; background-color: #d62839; color: white; } <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Tic Tac Toe</title> <link rel="preconnect" href="https://fonts.googleapis.com" /> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin /> <link href="https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap" rel="stylesheet" /> <link rel="stylesheet" href="tis.css" /> </head> <body> <div id="gameContainer"> <h1>Tic Tac Toe</h1> <h2 id="statusText">Testing...</h2> <div id="cellContainer"> <div cellIndex="0" class="cell"></div> <div cellIndex="1" class="cell"></div> <div cellIndex="2" class="cell"></div> <div cellIndex="3" class="cell"></div> <div cellIndex="4" class="cell"></div> <div cellIndex="5" class="cell"></div> <div cellIndex="6" class="cell"></div> <div cellIndex="7" class="cell"></div> <div cellIndex="8" class="cell"></div> </div> <button id="restartBtn">RESTART</button> </div> <script src="tic.js"></script> </body>

回答 1 投票 0

TicTacToe 游戏不接受用户输入

我正在制作一个井字棋游戏,到目前为止一切正常,但它没有占用我的坐标,我需要将给我的图标放在我在板上编写的位置。这是我的代码: 主要

回答 1 投票 0

为什么我的Python tic tac toe 游戏无法正确检查玩家是否获胜?

我是新手,我不知道如何检查某人是否赢得了我的井字棋游戏。有些代码是西班牙语的,如果你需要的话我可以翻译。 随机导入 Tablero = [[" ", &quo...

回答 1 投票 0

TicTacToe 反应 - 序列突出显示

任何人都可以研究我的代码并告诉我如何在井字棋游戏中突出显示序列吗? 下面是我的代码。 板.jsx 从“react”导入React,{useState}; 从 &... 导入 Square

回答 1 投票 0

如何使用 numpy 缩短井字棋检查

如何使用 numpy 来缩短我的代码,我会检查你是否赢了 我需要找到一种方法来使用 numpy 来适应我的代码,因为它又长又难。 我检查了 diag、horizontle 和 row 但我需要...

回答 2 投票 0

Java TicTacToe 在玩家 O 获胜时显示 2 倍的获胜文本,但在玩家 X 获胜时不显示

公共课井字棋{ static boolean won = false; static boolean firstRound = true; 静态列表 字段 = new ArrayList<>(); 静态列表 winLis...

回答 0 投票 0

检查屏幕中的字符 8086 [关闭]

我有一个任务,我必须在 DOSbox 的 8086 汇编中制作终极井字游戏。 我们得到了一个带有游戏板的 .txt。我如何检查玩家是否赢了。 首先我想...

回答 0 投票 0

我正在为井字游戏中的 minimax 算法苦苦挣扎

我目前正在尝试使用 minimax 算法制作井字游戏。我在这个项目中使用带有 p5js 框架的 JavaScript。 这是 sketch.js 代码: 变种行= 3; 可变列 = 3; 变量 scl =...

回答 0 投票 0

运行我的 tic tac toe JavaScript 游戏时,X 和 O 没有出现。我该如何解决?

我一直在关注 tic tac toe 项目的几个在线教程,但是我尝试了两次,我都遇到了相同的错误,其中 X 和 O 没有显示,但另一个

回答 2 投票 0

python 中井字游戏的绘制函数

board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 获胜 = [ ((0, 0), (0, 1), (0, 2)), ((1, 0), (1, 1), (1, 2)), ((2, 0), (2, 1), (2, 2)), ((0, 0), (1, 0), (2, 0)), ((0,...

回答 0 投票 0

我想使用Java语言中的minimax算法将代码Tic Tac Toe (Caro) 3x3升级为5x5

我想使用Java语言中的minimax算法将代码Tic Tac Toe (Caro) 3x3升级为5x5 Caro minimax 5x5,java netbeans,人类与计算机 包 CaroMinimax; 导入 java.awt.*; 进口...

回答 0 投票 0

React 的 TicTacToe 教程中的 JSX 语法问题

在我的本地开发环境中尝试 React 的 TicTacToe 教程 (https://react.dev/learn/tutorial-tic-tac-toe) 时,我在从

回答 0 投票 0

尝试使用 javascript 编写 Tic-Tac-Toe 游戏。让计算机决定播放的最佳方式是什么?

我正在使用 code.org AppLab,并尝试制作 Tic-Tac-Toe 游戏。目前我有它所以计算机会从可用的地方随机选择一个位置,但我正在努力让它成为...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.