gpt4 book ai didi

javascript - Tic Tac Toe Javascript 不会改变玩家

转载 作者:行者123 更新时间:2023-11-28 18:17:30 25 4
gpt4 key购买 nike

<html>
<head>
<title>My Tic Tac Toe</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.Row {
display:table-row;
}
.cell {
display: table-cell;
border: solid black;
border-width: medium;
padding: 0 3px;
height: 95px;
width: 95px;
text-align: center;
font-size: 40px;
vertical-align: middle;
}
</style>
<script>
var xPlayer = prompt("Player x what is your name?");
var oPlayer = prompt("Player o what is your name?");
var currentPlayer = "X";
var xGameTotal = 0;
var oGameTotal = 0;
var xWins = 0;
var oWins = 0;
var xMessage = 0;
var oMessage = 0;
var winsArray = [7, 56, 448, 73, 146, 292, 273, 84];

function playerMoved(id,value){
changeMarker(id);
updatePlayerTotal(value);

if (checkWinner(xGameTotal)){
xWins ++;
if (xWins ===1)
{
xMessage = " time";
}
else
{
xMessage = " times";
}
resetBoard();
}
if (checkWinner(oGameTotal)) {
oWins ++;
if (oWins ===1)
{
oMessage = " time";
}
else
{
oMessage = " times";
}
resetBoard();
}

switchPlayers();
}
function changeMarker(box){
if (currentPlayer === "X")
{
box.innerHTML = 'X';
}
else
{
box.innerHTML = 'O';
}
}
function switchPlayers(){
if (currentPlayer === "X")
{
currentPlayer = 'X';
}
else
{
currentPlayer = 'O';
}
}
function updatePlayerTotal(score){
if (currentPlayer === "X")
{
xGameTotal = xGameTotal + score;
}
else
{
oGameTotal = oGameTotal + score;
}
}
function checkWinner(score){
for (var index = 0; index < winsArray.length; index += 1)
{
if ((winsArray[index] & score) === winsArray[index])
return true;
}
return false;
}
function resetBoard(){
var board = document.getElementsByClassName("cell");
for (var i=0; i < board.length; i++){
board[i].innerHTML="";
}
oGameTotal=0;
xGameTotal=0;

}
</script>
</head>
<body onload="start()">
<h1>Tic Tac Toe</h1>
<div class="Row">
<div class="cell" onClick="playerMoved(this,1)"></div>
<div class="cell" onClick="playerMoved(this,2)"></div>
<div class="cell" onClick="playerMoved(this,4)"></div>
</div>
<div class="Row">
<div class="cell" onClick="playerMoved(this,8)"></div>
<div class="cell" onClick="playerMoved(this,16)"></div>
<div class="cell" onClick="playerMoved(this,32)"></div>
</div>
<div class="Row">
<div class="cell" onClick="playerMoved(this,64)"></div>
<div class="cell" onClick="playerMoved(this,128)"></div>
<div class="cell" onClick="playerMoved(this,256)"></div>
</div>
</body>

我无法让播放器切换。当您点击方 block 时,始终是玩家 X。我缺少什么?我想也许我的嵌套是错误的,但我似乎找不到任何东西。任何帮助,将不胜感激。谢谢!

最佳答案

你的if条件(switchPlayers)是错误的。您正在检查当前播放是否为 x,然后再次将当前播放器设置为 x。

只需更改这些行:

function switchPlayers(){
if (currentPlayer === "X") {
currentPlayer = 'X';
} else {
currentPlayer = 'O';
}
}

至:

function switchPlayers(){
if (currentPlayer === "X") {
currentPlayer = 'O';
} else {
currentPlayer = 'X';
}
}

关于javascript - Tic Tac Toe Javascript 不会改变玩家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40555761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com