我正在构建一个小型连接四游戏,当我设置弹出窗口时说“红色/黄色赢”"对于每种可能的组合,它会导致一个我不确定的异常错误/错误。
如果我在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)< / p>
<?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>
&#13;
答案 0 :(得分:6)
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'});
....
请注意,我确实将循环更改为现在使用二维数组,因为winsCombinations在每个条目中都有4列可以在这里使用。
答案 1 :(得分:3)
您正在测试的是a1,a2,a3是否存在以及a4的背景颜色是否为红色。
我相信你要测试的是它们都是红色的。这将是:
if (a1 == "rgb(255, 0, 0)" && a2 == "rgb(255, 0, 0)" && a3 == "rgb(255, 0, 0)" && a4 == "rgb(255, 0, 0)") { ... }
你可能想要做的是构造一个辅助函数,你传入一个对象数组并测试它们的背景颜色是否相同。如果是,它将返回颜色,否则它将返回NULL。
e.g。
function testColour(objects){
var colour = null;
for (var i = 0;i<objects.length;i++){
if (i == 0){
//set colour from first one
colour = objects[i].css("background-color");
else if (objects[i].css("background-color")!=colour){
//check if colour is the same as first. If not, return null
return null;
}
}
//all objects are of the same colour, return colour.
return colour
}
colour = testColour([$(".a1"),$(".a2"),$(".a3"),$(".a4")])
if (colour!=null){
//colour wins
if (colour=="rgb(255,0,0)"){
alert("Red wins!");
}else{
alert("Yellow wins!");
}
break;
}
为简化检查所有这些,您可能需要将网格视为矩阵,然后使用矩阵运算来检查连续四个
//define array for conversions between letters and numbers
var nToL = ["a","b","c","d","e","f"];
//define direction in which a connect 4 is allowed
var allowedVectors = [{"x":1,"y":0},{"x":0,"y":1},{"x":1,"y":1}];
//for each row
for (i=0;i<7;i++){
//for each column
for (j=0;j<6;j++){
//for each allowed vector
for (v=0;v<allowedVectors.length;v++){
var currentVector = allowedVectors[v];
//make sure vector does not go outside grid
if (i+currentVector.x*3<7||j+currentVector.y*3<6){
var objectArray = [];
//create array of objects to test
for (n=0;n<4;n++){
var square = $("."+nToL[j+currentVector.y*n]+(i+1+currentVector.x*n));
objectArray.push(square);
}
if (testColour(objectArray)!=null){
//colour wins
if (colour=="rgb(255,0,0)"){
alert("Red wins!");
}else{
alert("Yellow wins!");
}
break;
}
}
}
}
}