JavaScript TicTacToe if ...获胜者检测 [英] JavaScript TicTacToe if... Winner detection
问题描述
我只是遇到一个小问题。
I just have a small problem.
我计算机科学课的最后一项任务是用JavaScript编写一个Tic-Tac-Toe游戏。游戏通过单击表格中的单元格并使用函数()将img源(一个空白图像直到被点击)切换为X或O来工作。它刚刚完成,但有一件事我无法获得。
The final assignment in my computer science class is to write a game of Tic-Tac-Toe in JavaScript. The game works by clicking on cells within a table and using a function() to switch the img source (a blank image until clicked on) to an X or an O. It's just about done, but there's one thing I can't get.
很明显,这场比赛是由两名球员进行的。我需要的最后一件事就是让它(一个函数)检测玩家何时获胜并在发生这种情况时杀死脚本。我只是想不出来。我知道它涉及if语句,我尝试了几种不同的代码,但是我编码的所有代码都打破了游戏。
The game is played, obviously, by two players. The last thing I need is to get it (a function) to detect when a player wins and kill the script when this happens. I just can't figure this out. I know it involves if statements, and I've attempted several different codes, but everything I code just breaks the game.
我确实看到过这个问题之前已经被问过了,我已经尝试点击右侧类似问题框中的几个链接,但所有这些链接都是我没有学到的语言(例如C#)。
I do see that this question has been asked before, and I've tried clicking on several links in the 'similar questions' box to the right, but all of them are in languages I haven't learned anything about (such as C#).
这是我的脚本:
<script type="text/javascript" charset="ISO-8859-1">
function placeMove(value)
{
if (document.getElementById(value).src.indexOf("FC")!=-1) //"FC" because of the FC contained in the file name. I intended for it to stand for "free cell", for easy indentification.
{
var turn = document.getElementById("playerturn").innerHTML;
if (turn == "X")
{
document.getElementById(value).src="../Images/TicTacToeX.jpg";
document.getElementById("playerturn").innerHTML="O";
}
if (turn == "O")
{
document.getElementById(value).src="../Images/TicTacToeO.jpg";
document.getElementById("playerturn").innerHTML="X";
}
}
else
{
window.alert("Tile is in use. Please select another tile.");
}
}
function detectWin()
{
}
</script>
这是游戏发生的表格,如果它有帮助:
This is the table that the game takes place in, if it helps:
<table class="gametable" id="gametable" border="1">
<tr>
<td><img onclick="placeMove('r1c1')" id="r1c1" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
<td><img onclick="placeMove('r1c2')" id="r1c2" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
<td><img onclick="placeMove('r1c3')" id="r1c3" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
</tr>
<tr>
<td><img onclick="placeMove('r2c1')" id="r2c1" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
<td><img onclick="placeMove('r2c2')" id="r2c2" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
<td><img onclick="placeMove('r2c3')" id="r2c3" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
</tr>
<tr>
<td><img onclick="placeMove('r3c1')" id="r3c1" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
<td><img onclick="placeMove('r3c2')" id="r3c2" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
<td><img onclick="placeMove('r3c3')" id="r3c3" class="blank" alt="blank space" src="../Images/TicTacToeFC.jpg"></img></td>
</tr>
</table>
明天将完成作业,我需要的只是最后一点代码。
The assignment is due tomorrow, and all I need is this last little bit of code.
非常感谢任何帮助。这是我在学期的最后一次作业。
Any help is greatly appreciated. This is my last assignment of the semester.
谢谢,
Kyle
编辑:经过一些建议,这是我一直在尝试的。输入此代码将不允许玩游戏。如果只是缺少分号,请原谅我。它涉及将每个单元格中的img源设置为变量,并检查这些变量是否匹配。
After some suggestions, here is what I've been trying. Entering this code will not allow the game to be played. If it's just a matter of missing semicolons, please forgive me. It involves setting the img source in each cell to a variable, and checking to see if those variables match.
我创建了9个变量,每个单元格/ X或O图像:
There are 9 variables I created, one for each cell/X or O image:
var pic1 = document.getElementById("r1c1").src;
var pic2 = document.getElementById("r1c2").src;
var pic3 = document.getElementById("r1c3").src;
var pic4 = document.getElementById("r2c1").src;
var pic5 = document.getElementById("r2c2").src;
var pic6 = document.getElementById("r2c3").src;
var pic7 = document.getElementById("r3c1").src;
var pic8 = document.getElementById("r3c2").src;
var pic9 = document.getElementById("r3c3").src;
这是在第一个脚本的最后:
This is at the very end of the first script:
function detectWin()
{
if (var pic1 == var pic2 && var pic3)
{
window.alert("Game won. Please reset the game.");
}
}
这显然只能检测第一行的胜利。如果我可以让它工作,我会知道其余代码必须是什么。我仍然不知道如何杀死剧本。
This obviously only detects a win for the first row. If I can get this to work, I'll know what the rest of the code will have to be. I still don't know how to kill the script, though.
编辑删除一些不必要的评论和语法错误。
Edited to remove some unnecessary remarks and grammatical errors.
推荐答案
我从开头的代码中写道,你可以查看我的查看谁赢得游戏的版本。如果我的游戏版本不是你,请不要贬低。我只是想告诉你如何在不知道任何算法的情况下编写代码。
I write from begining code where you can check my version of checking who win game. Dont downvote if my version of game is another than you. I just want to show you how you can write code without knowledge of any algorithm.
你只需要动力。不要太快放弃下一次。
You just need motivation. Don't give up next time so fast.
我的版本用于检查谁赢了:
My version for checking who win:
var checkResult = function(){
$("table tr").each(function(i, val){
$(this).find('td').each(function(j, val2){
arr[i][j] = parseInt($(this).attr("data-points"));
});
});
for(var i = 0; i<3;i++){
var rowSum = 0;
for(var j = 0; j<3;j++){
rowSum += arr[i][j];
}
if(rowSum === 3)
alert("Circle WIN!");
else if(rowSum === -3)
alert("Cross WIN!");
}
for(var i = 0; i<3;i++){
var colSum = 0;
for(var j = 0; j<3;j++){
colSum += arr[j][i];
}
if(colSum === 3)
alert("Circle WIN!");
else if(colSum === -3)
alert("Cross WIN!");
}
if(arr[0][0] + arr[1][1] + arr[2][2] === 3)
alert("Circle WIN!");
else if(arr[0][0] + arr[1][1] + arr[2][2] === -3)
alert("Cross WIN!");
if(arr[2][0] + arr[1][1] + arr[0][2] === 3)
alert("Circle WIN!");
else if(arr[2][0] + arr[1][1] + arr[0][2] === -3)
alert("Cross WIN!");
};
这篇关于JavaScript TicTacToe if ...获胜者检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!