gpt4 book ai didi

Jquery "If statements"当语句不正确时触发

转载 作者:行者123 更新时间:2023-11-28 09:05:50 26 4
gpt4 key购买 nike

我正在构建一个小型四人连线游戏,当我将弹出窗口设置为针对每种可能的组合显示“红色/黄色获胜”时,它导致了一个我不确定的异常错误/错误。

如果我在大约 65 个语句中只有一个 if 语句,当我得到那个组合时,它可以完美地工作:

var a1 = $(".a1").css("background-color");
var a2 = $(".a2").css("background-color");
etc


if (a1 && a2 && a3 && a4 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("1");
}
if (a2 && a3 && a4 && a5 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("1");
}

但是当我有 65 个不同的组合时,它们似乎是随机触发的,甚至不需要 a1、a2、a3、a4 为真来触发 if 语句(它有时也会在没有四种背景颜色为真时触发)

请注意:还有很多不同的 vars 和 ifs 的代码

抱歉,我是 SO 和 JQ 的新手,如果我遗漏了任何内容,我很乐意进行编辑。

@Scott 的代码:(我知道这可能有成千上万种错误的方式以及我应该如何使用套接字,但我对 Jquery 不是很好并且不确定从哪里开始所以我使用了很多 AJAX)

<?php

try {
$db = new PDO("mysql:host=$mysql_host;dbname=$mysql_db", $mysql_user, $mysql_pass);

// Get Game ID/Color/Player
$gameid = $_GET['gameid'];
$checkPlayer = "SELECT * FROM c4_games WHERE gameid='$gameid'";
$result = array();
foreach ($db->query($checkPlayer) as $player) {
$result[] = $player;
$player1 = $result[0][1];
if ($player1 == 0) {
$update = "UPDATE c4_games SET p1_connect='1' WHERE gameid='$gameid'";
$db->exec($update);
$player = "red";
echo $player;
} else {
$update = "UPDATE c4_games SET p2_connect='1' WHERE gameid='$gameid'";
$db->exec($update);
$player = "yellow";
echo $player;
}
}
} catch(PDOException $e) {
$e->getMessage();
}



?>
<!DOCTYPE html>
<html>
<head>
<title>Website Title</title>
<meta charset="UTF-8">
<!-- Stylesheets -->
<link rel="stylesheet" type="text/css" href="css/index.css" />
<!-- Javascript -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<table class="c4-table">
<tr>
<td class="F1">F1</td>
<td class="F2">F2</td>
<td class="F3">F3</td>
<td class="F4">F4</td>
<td class="F5">F5</td>
<td class="F6">F6</td>
<td class="F7">F7</td>
</tr>
<tr>
<td class="E1">E1</td>
<td class="E2">E2</td>
<td class="E3">E3</td>
<td class="E4">E4</td>
<td class="E5">E5</td>
<td class="E6">E6</td>
<td class="E7">E7</td>
</tr>
<tr>
<td class="D1">D1</td>
<td class="D2">D2</td>
<td class="D3">D3</td>
<td class="D4">D4</td>
<td class="D5">D5</td>
<td class="D6">D6</td>
<td class="D7">D7</td>
</tr>
<tr>
<td class="C1">C1</td>
<td class="C2">C2</td>
<td class="C3">C3</td>
<td class="C4">C4</td>
<td class="C5">C5</td>
<td class="C6">C6</td>
<td class="C7">C7</td>
</tr>
<tr>
<td class="B1">B1</td>
<td class="B2">B2</td>
<td class="B3">B3</td>
<td class="B4">B4</td>
<td class="B5">B5</td>
<td class="B6">B6</td>
<td class="B7">B7</td>
</tr>
<tr>
<td class="A1">A1</td>
<td class="A2">A2</td>
<td class="A3">A3</td>
<td class="A4">A4</td>
<td class="A5">A5</td>
<td class="A6">A6</td>
<td class="A7">A7</td>
</tr>
</table>

<form class="submit-move" style="display:none;" method="POST" action="submit.php">
<input type="text" class="move" name="move" />
<input type="hidden" name="gameid" class="gameid" value="<?php echo $gameid ?>"/>
</form>

<?php if ($player != "red") {
echo '<script>
$(document).ready(function() {
$("td").click(function() {
y = 0;
r = 0;
$("td").each(function() {
if ($(this).css("background-color") == "rgb(255, 255, 0)") {
y++;
}
if ($(this).css("background-color") == "rgb(255, 0, 0)") {
r++;
}
});

if (r > y) {
r++;
var column = $(this).attr("class").substring(1,2);
var a = ".A" + column;
var b = ".B" + column;
var c = ".C" + column;
var d = ".D" + column;
var e = ".E" + column;
var f = ".F" + column;

if ($(a).attr("id") != "selected") {
$(a).css("backgroundColor","yellow");
$(a).attr("id","selected");
$(".move").val(a);
} else if ($(b).attr("id") != "selected"){
$(b).css("backgroundColor","yellow");
$(b).attr("id","selected");
$(".move").val(b);
} else if ($(c).attr("id") != "selected"){
$(c).css("backgroundColor","yellow");
$(c).attr("id","selected");
$(".move").val(c);
} else if ($(d).attr("id") != "selected"){
$(d).css("backgroundColor","yellow");
$(d).attr("id","selected");
$(".move").val(d);
} else if ($(e).attr("id") != "selected"){
$(e).css("backgroundColor","yellow");
$(e).attr("id","selected");
$(".move").val(e);
} else if ($(f).attr("id") != "selected"){
$(f).css("backgroundColor","yellow");
$(f).attr("id","selected");
$(".move").val(f);
}

$.ajax({
url: "api/submit-yellow.php",
type: "POST",
data: $(".submit-move").serialize(),
success: function(result){
console.log($(".submit-move").serialize());
}
});
} else {
alert("It isnt your turn!");
}

// CHECK FOR CONNECT 4
// REMOVED SOME CODE BECAUSE OF BODY LIMIT ON SO


});
$("td").hover(function() {
var column = $(this).attr("class").substring(1,2);
var a = ".A" + column;
var b = ".B" + column;
var c = ".C" + column;
var d = ".D" + column;
var e = ".E" + column;
var f = ".F" + column;
$(a).css("border","1px solid yellow");
$(b).css("border","1px solid yellow");
$(c).css("border","1px solid yellow");
$(d).css("border","1px solid yellow");
$(e).css("border","1px solid yellow");
$(f).css("border","1px solid yellow");
});
$("td").mouseout(function() {
var column = $(this).attr("class").substring(1,2);
var a = ".A" + column;
var b = ".B" + column;
var c = ".C" + column;
var d = ".D" + column;
var e = ".E" + column;
var f = ".F" + column;
$(a).css("border","1px solid black");
$(b).css("border","1px solid black");
$(c).css("border","1px solid black");
$(d).css("border","1px solid black");
$(e).css("border","1px solid black");
$(f).css("border","1px solid black");
});

var lastMoveYellow = function load() {
$.ajax({url:"api/move-yellow.php?gameid="+$(".gameid").val(),success:function(result){
var obj = jQuery.parseJSON(result);
var ids = obj.map(function(el) {
$($(el.move).css("backgroundColor","yellow"));
});
}});
};
loadInterval = setInterval(lastMoveYellow, 1000);

var lastMoveRed = function load() {
$.ajax({url:"api/move-red.php?gameid="+$(".gameid").val(),success:function(result){
var obj = jQuery.parseJSON(result);
var ids = obj.map(function(el) {
$($(el.move).css("backgroundColor","red"));
$($(el.move).attr("id","selected"));
});
}});
};
loadInterval = setInterval(lastMoveRed, 1000);
});
</script>';
} else {
echo '<script>
$(document).ready(function() {
$("td").click(function() {
y = 0;
r = 0;
$("td").each(function() {
if ($(this).css("background-color") == "rgb(255, 255, 0)") {
y++;
}
if ($(this).css("background-color") == "rgb(255, 0, 0)") {
r++;
}
});

if (r == y) {
r++;
var column = $(this).attr("class").substring(1,2);
var a = ".A" + column;
var b = ".B" + column;
var c = ".C" + column;
var d = ".D" + column;
var e = ".E" + column;
var f = ".F" + column;

if ($(a).attr("id") != "selected") {
$(a).css("backgroundColor","red");
$(a).attr("id","selected");
$(".move").val(a);
} else if ($(b).attr("id") != "selected"){
$(b).css("backgroundColor","red");
$(b).attr("id","selected");
$(".move").val(b);
} else if ($(c).attr("id") != "selected"){
$(c).css("backgroundColor","red");
$(c).attr("id","selected");
$(".move").val(c);
} else if ($(d).attr("id") != "selected"){
$(d).css("backgroundColor","red");
$(d).attr("id","selected");
$(".move").val(d);
} else if ($(e).attr("id") != "selected"){
$(e).css("backgroundColor","red");
$(e).attr("id","selected");
$(".move").val(e);
} else if ($(f).attr("id") != "selected"){
$(f).css("backgroundColor","red");
$(f).attr("id","selected");
$(".move").val(f);
}

$.ajax({
url: "api/submit-red.php",
type: "POST",
data: $(".submit-move").serialize(),
success: function(result){
console.log("working");
}
});
} else {
alert("It isnt your turn!");
}


// CHECK FOR CONNECT 4
var a1 = $(".a1").css("background-color");
var a2 = $(".a2").css("background-color");
var a3 = $(".a3").css("background-color");
var a4 = $(".a4").css("background-color");
var a5 = $(".a5").css("background-color");
var a6 = $(".a6").css("background-color");
var a7 = $(".a7").css("background-color");
var b1 = $(".b1").css("background-color");
var b2 = $(".b2").css("background-color");
var b3 = $(".b3").css("background-color");
var b4 = $(".b4").css("background-color");
var b5 = $(".b5").css("background-color");
var b6 = $(".b6").css("background-color");
var b7 = $(".b7").css("background-color");
var c1 = $(".c1").css("background-color");
var c2 = $(".c2").css("background-color");
var c3 = $(".c3").css("background-color");
var c4 = $(".c4").css("background-color");
var c5 = $(".c5").css("background-color");
var c6 = $(".c6").css("background-color");
var c7 = $(".c7").css("background-color");
var d1 = $(".d1").css("background-color");
var d2 = $(".d2").css("background-color");
var d3 = $(".d3").css("background-color");
var d4 = $(".d4").css("background-color");
var d5 = $(".d5").css("background-color");
var d6 = $(".d6").css("background-color");
var d7 = $(".d7").css("background-color");
var e1 = $(".e1").css("background-color");
var e2 = $(".e2").css("background-color");
var e3 = $(".e3").css("background-color");
var e4 = $(".e4").css("background-color");
var e5 = $(".e5").css("background-color");
var e6 = $(".e6").css("background-color");
var e7 = $(".e7").css("background-color");
var f1 = $(".b1").css("background-color");
var f2 = $(".b2").css("background-color");
var f3 = $(".b3").css("background-color");
var f4 = $(".b4").css("background-color");
var f5 = $(".b5").css("background-color");
var f6 = $(".b6").css("background-color");
var f7 = $(".b7").css("background-color");


if (a1 && a2 && a3 && a4 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("1");
}
if (a2 && a3 && a4 && a5 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("2");
}
if (a3 && a4 && a5 && a6 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("3");
}
if (a4 && a5 && a6 && a7 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("4");
}
if (b1 && b2 && b3 && b4 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("5");
}
if (b2 && b3 && b4 && b5 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("6");
}
if (b3 && b4 && b5 && b6 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("7");
}
if (b4 && b5 && b6 && b7 == "rgb(255, 0, 0)") {
alert("red wins");
console.log("8");
}
if (c1 && c2 && c3 && c4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c2 && c3 && c4 && c5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c3 && c4 && c5 && c6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c4 && c5 && c6 && c7 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (d1 && d2 && d3 && d4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (d2 && d3 && d4 && d5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (d3 && d4 && d5 && d6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (d4 && d5 && d6 && d7 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e1 && e2 && e3 && e4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e2 && e3 && e4 && e5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e3 && e4 && e5 && e6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e4 && e5 && e6 && e7 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f1 && f2 && f3 && f4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f2 && f3 && f4 && f5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f3 && f4 && f5 && f6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f4 && f5 && f6 && f7 == "rgb(255, 0, 0)") {
alert("red wins");
}

if (a1 && b1 && c1 && d1 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e1 && b1 && c1 && d1 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f1 && e1 && c1 && d1 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a2 && b2 && c2 && d2 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e2 && b2 && c2 && d2 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f2 && e2 && c2 && d2 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a3 && b3 && c3 && d3 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e3 && b3 && c3 && d3 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f3 && e3 && c3 && d3 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a4 && b4 && c4 && d4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e4 && b4 && c4 && d4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f4 && e4 && c4 && d4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a5 && b5 && c5 && d5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e5 && b5 && c5 && d5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f5 && e5 && c5 && d5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a6 && b6 && c6 && d6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e6 && b6 && c6 && d6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f6 && e6 && c6 && d6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a7 && b7 && c7 && d7 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (e7 && b7 && c7 && d7 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (f7 && e7 && c7 && d7 == "rgb(255, 0, 0)") {
alert("red wins");
}

if (c1 && d2 && e3 && f4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (b1 && c2 && d3 && e4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c2 && d3 && e4 && f5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a1 && b2 && c3 && d4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (b2 && c3 && d4 && f5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c3 && d4 && e5 && f6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a2 && b3 && d4 && e5 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (b3 && c4 && d5 && e6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c4 && d5 && e6 && f7 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a3 && b4 && c5 && d6 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (b4 && c5 && d6 && e7 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a4 && b5 && c6 && d7 == "rgb(255, 0, 0)") {
alert("red wins");
}

if (a4 && b3 && c2 && a1 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a5 && b4 && c3 && d2 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (b4 && c3 && d2 && e1 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a6 && b5 && c4 && d3 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (b5 && c4 && d3 && e2 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c4 && d3 && e2 && f1 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (a7 && b6 && c5 && d4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (b6 && c5 && d4 && e3 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c5 && d4 && e3 && f2 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (b7 && c6 && d5 && e4 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c6 && d5 && e4 && f3 == "rgb(255, 0, 0)") {
alert("red wins");
}
if (c7 && d6 && e5 && f4 == "rgb(255, 0, 0)") {
alert("red wins");
}
});

$("td").hover(function() {
var column = $(this).attr("class").substring(1,2);
var a = ".A" + column;
var b = ".B" + column;
var c = ".C" + column;
var d = ".D" + column;
var e = ".E" + column;
var f = ".F" + column;
$(a).css("border","1px solid red");
$(b).css("border","1px solid red");
$(c).css("border","1px solid red");
$(d).css("border","1px solid red");
$(e).css("border","1px solid red");
$(f).css("border","1px solid red");
});
$("td").mouseout(function() {
var column = $(this).attr("class").substring(1,2);
var a = ".A" + column;
var b = ".B" + column;
var c = ".C" + column;
var d = ".D" + column;
var e = ".E" + column;
var f = ".F" + column;
$(a).css("border","1px solid black");
$(b).css("border","1px solid black");
$(c).css("border","1px solid black");
$(d).css("border","1px solid black");
$(e).css("border","1px solid black");
$(f).css("border","1px solid black");
});

var lastMoveYellow = function load() {
$.ajax({url:"api/move-yellow.php?gameid="+$(".gameid").val(),success:function(result){
var obj = jQuery.parseJSON(result);
var ids = obj.map(function(el) {
$($(el.move).css("backgroundColor","yellow"));
$($(el.move).attr("id","selected"));
});
}});
};
loadInterval = setInterval(lastMoveYellow, 1000);

var lastMoveRed = function load() {
$.ajax({url:"api/move-red.php?gameid="+$(".gameid").val(),success:function(result){
var obj = jQuery.parseJSON(result);
var ids = obj.map(function(el) {
$($(el.move).css("backgroundColor","red"));
});
}});
};
loadInterval = setInterval(lastMoveRed, 1000);
});
</script>';
}
?>
</body>
</html>

最佳答案

if (a1 && a2 && a3 && a4 == "rgb(255, 0, 0)")

这将检查 a1 作为我怀疑是什么的条件本身。如果要根据该字符串检查每个值,这就是您想要的:

if ((a1== "rgb(255, 0, 0)") && (a2== "rgb(255, 0, 0)") && (a3== "rgb(255, 0, 0)")  && (a4 == "rgb(255, 0, 0)")))

使用循环和数组可能比写出一堆类似的行更好。例如,这样的事情可能会做你想做的事,而不必像你那样写出所有的行:

for (var i=0; i<winningCombinations.length; i++) {
var a1=$("."+winningCombinations[i][0]).css("background-color");
var a2=$("."+winningCombinations[i][1]).css("background-color");
var a3=$("."+winningCombinations[i][2]).css("background-color");
var a4=$("."+winningCombinations[i][3]).css("background-color");

if ((a1== "rgb(255, 0, 0)") && (a2== "rgb(255, 0, 0)") && (a3== "rgb(255, 0, 0)") && (a4 == "rgb(255, 0, 0)")))
{
// add message here for 4 in a row.
break; // this will exit the for loop.
}

}

我强烈建议创建一个数组来保存您要检查的各种组合。考虑这样的事情:

var winningCombinations = new Array();
winningCombinations.push({'a1','a2','a3','a4'});
winningCombinations.push({'a2','a3,'a4','a5'});
....

请注意,我确实将循环更改为现在使用二维数组,因为 winningCombinations 将在每个条目中有 4 列,可以在此处使用。

关于Jquery "If statements"当语句不正确时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33526593/

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