在井字中声明平局的正确语法
Proper Syntax for Declaring a Draw in Tic Tac Toe
>我正在做一个井字游戏。已经有人教我如何宣布获胜者并停止进一步的游戏。
有两个持续存在的问题。首先,当获胜是直交叉的时,计数器将 1 添加到 x 获胜,将 2 添加到 o 获胜。第二个问题是我无法让游戏打平局。如果没有结果,我已经尝试过其他方法。
这是整个js,游戏可以在以下位置看到:http://jvonhausen.com/final/p19/
jQuery(function() {
$('#start').click(clear_squares);
$('.square').click(function () {
square_stuff(this);
});
});
var player_x = true;
var complete = false;
var winner = '';
var x_wins = 0;
var o_wins = 0;
var draw = 0;
function clear_squares () {
localStorage.setItem('x_count', x_wins);
localStorage.setItem('o_count', o_wins);
localStorage.setItem('draw_count', draw);
$('.square').html('');
player_x = true;
$('#output').html('turn: x');
complete = false; //allows for new game
}
function square_stuff (square) {
if (!complete) {
if ($(square).html() == '') {
if (player_x == true) {
$(square).html('x');
player_x = false;
} else {
$(square).html('o');
player_x = true;
}
}
}
check_squares();
}
function check_squares () {
complete = true;
var values = new Array();
var winner = '';
$('.square').each(function () {
values.push( $(this).html());
if ($(this).html() == '') complete = false;
if ( !winner && values[0] == values[1] && values[1] == values[2] ) winner = values[0];
if ( !winner && values[3] == values[4] && values[4] == values[5] ) winner = values[4];
if ( !winner && values[6] == values[7] && values[7] == values[8] ) winner = values[7];
if ( !winner && values[0] == values[3] && values[3] == values[6] ) winner = values[3];
if ( !winner && values[1] == values[4] && values[4] == values[7] ) winner = values[1];
if ( !winner && values[2] == values[5] && values[5] == values[8] ) winner = values[5];
if ( !winner && values[0] == values[4] && values[4] == values[8] ) winner = values[8];
if ( !winner && values[2] == values[4] && values[4] == values[6] ) winner = values[2];
if (winner) {
complete = true;
$('#output').html('winner: ' + winner);
if ($(this).html() == 'x') {
localStorage.setItem('x_count', x_wins);
x_wins++;
$('#wins_x').val(x_wins);
}
if ($(this).html() == 'o') {
localStorage.setItem('o_count', o_wins);
o_wins++;
$('#wins_o').val(o_wins);
}
if (winner == '' && complete == true) {
$('#output').html('no winner');
localStorage.setItem('draw_count', draw);
draw++;
$('#draws').val(draw);
}
}
if (winner == '' && complete == false) {
if (player_x == true) {
$('#output').html('turn: x');
} else {
$('#output').html('turn: o');
}
}
});
}
一旦你发现赢家,你就不会打破你的"每个"循环。 我认为这将起作用:
$('.square').each(function () {
if (winner) {
return;
}
values.push( $(this).html());
....
或者,这只是更干净:
function check_squares () {
complete = true;
var values = []; // better way to declare an empty array
var winner = '';
// build the values array first
$('.square').each(function () {
values.push( $(this).html());
}
// then just check for a winner once...
if ( !winner && values[0] == values[1] && values[1] == values[2] ) winner = values[0];
....
您的代码没有检测到抽奖的原因是您的抽奖检查发生在"if(赢家("块内。
if (winner) {
complete = true;
....
// the clause will never get executed, because winner must have already been set to have made it this far
// so winner == '' will aways be false
if (winner == '') {
$('#output').html('no winner');
相关文章:
- 铬:“;未捕获的语法错误:意外的标记:"
- jQuery语法添加了var
- Javascript未捕获语法错误意外的标识符错误
- 为什么忽略了eval()代码中的语法错误
- 难以访问的JS环境中的语法错误
- 如何告诉MathJax对下标使用替代语法
- Javascript语法向设置发出sessionStorage值
- 未捕获的语法错误:意外的标记{
- 使用$scope方法时的ControllerAs语法
- "未捕获的语法错误:意外的标记}"
- javascript自执行函数-不同的语法
- 函数的Javascript语法
- WinJS内联绑定语法
- 使用JS函数来使用另一个函数的语法?node.js
- 未捕获的语法错误:无法在“文档”上执行“查询选择器”
- 有没有针对带有表的JavaScript的Markdown语法解析器
- 在递归生成器函数中,yield后面的*(星号/星号)语法意味着什么
- React JS:未捕获(在承诺中)语法错误:在位置 0 的 JSON 中意外<令牌
- 当使用控制器作为语法时,如何从父指令继承属性
- 在井字中声明平局的正确语法