Javascript Tic Tac Toe游戏获胜';t验证对角线的胜利者
我已经创建了一个tic-tac-toe游戏,当你按下“增加棋盘大小”按钮时,它会动态增加棋盘大小,但它不会验证对角线的胜利…我不知道从这里可以走到哪里 javascriptJavascript Tic Tac Toe游戏获胜';t验证对角线的胜利者,javascript,jquery,Javascript,Jquery,我已经创建了一个tic-tac-toe游戏,当你按下“增加棋盘大小”按钮时,它会动态增加棋盘大小,但它不会验证对角线的胜利…我不知道从这里可以走到哪里 javascript $(document).ready(function() { var turn = 1; var number = 3; function addRow(i) { var table = document.getElementById('table'); var newRow = table.i
$(document).ready(function() {
var turn = 1;
var number = 3;
function addRow(i) {
var table = document.getElementById('table');
var newRow = table.insertRow();
newRow.id = "row" + i;
addCols(newRow);
}
function addCols(row) {
for (var i = 1; i <= number; i++){
addTd(i,number,row);
}
}
function addTd(columnnumber,rownumber,row) {
var newCol = document.createElement('td');
newCol.id = "r" + rownumber + "col" + columnnumber;
row.appendChild(newCol);
}
//creating one click event for each td
$('td').one('click', function() {
if (turn % 2 === 0) {
$(this).html('O');
} else {
$(this).html('X');
}
turn++;
checkWinnerTable();
});
$('button.in').on('click', function() {
destroyBoard();
number++;
for (var i = 1; i <= number; i++){
addRow(i);
}
function destroyBoard(){
$('tr').remove();
}
});
function checkWinnerTable() {
//loop to check # of rows
for (var i = 1; i <= number; i++){
var row = document.getElementById('row' + i);
//loop to check # of cols
for (var j = 1; j <= number; j++) {
var col = document.getElementById('r' + i + 'col' + j);
checkValue(row);
}
}
}
function checkValue(row){
var row_value = row.value;
if (row_value === "X" && row_value === row) {
alert("X wins");
}
else if (row_value === "O" && row_value === row){
alert("O wins")
}
}
});
$(文档).ready(函数(){
var-turn=1;
var数=3;
函数addRow(i){
var table=document.getElementById('table');
var newRow=table.insertRow();
newRow.id=“行”+i;
addCols(纽罗);
}
函数addCols(行){
对于(var i=1;i
函数addRow(i){
var table=document.getElementById('table');
var newRow=table.insertRow();
newRow.id=“行”+i;
addCols(纽罗,i);
}
函数addCols(行,行索引){
对于(var i=1;i函数addRow(i){
var table=document.getElementById('table');
var newRow=table.insertRow();
newRow.id=“行”+i;
addCols(纽罗,i);
}
函数addCols(行,行索引){
对于(var i=1;i我重写了您的checkWinnerTable
函数,因为我无法真正理解您是如何试图解决这个问题的
代码如下:
function checkWinnerTable() {
var col;
var cross;
var row;
var matches;
var toMatch;
// Check vertical
for (col = 1; col <= number; col++) {
matches = 1;
toMatch = getValue(1, col);
if (toMatch == "") continue;
for (var row = 2; row <= number; row++) {
if (getValue(row, col) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check horizontal
for (row = 1; row <= number; row++) {
matches = 1;
toMatch = getValue(row, 1);
if (toMatch == "") continue;
for (col = 2; col <= number; col++) {
if (getValue(row, col) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check cross
cross = 1;
matches = 1;
toMatch = getValue(cross, cross);
if (toMatch != "") {
for (cross = 2; cross <= number; cross++) {
if (getValue(cross, cross) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check cross to other way
cross = 1;
matches = 1;
toMatch = getValue(cross, number+1-cross);
if (toMatch != "") {
for (cross = 2; cross <= number; cross++) {
if (getValue(cross, number+1-cross) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
}
function win(which) {
alert("Congrats! " + which + " won!");
}
function getValue(row, col) {
return document.getElementById("r"+row+"col"+col).innerHTML;
}
函数checkWinnerTable(){
var-col;
变量交叉;
var行;
var匹配;
var-toMatch;
//垂直检查
对于(col=1;col我重写了您的checkWinnerTable
函数,因为我无法真正理解您是如何试图解决这个问题的
代码如下:
function checkWinnerTable() {
var col;
var cross;
var row;
var matches;
var toMatch;
// Check vertical
for (col = 1; col <= number; col++) {
matches = 1;
toMatch = getValue(1, col);
if (toMatch == "") continue;
for (var row = 2; row <= number; row++) {
if (getValue(row, col) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check horizontal
for (row = 1; row <= number; row++) {
matches = 1;
toMatch = getValue(row, 1);
if (toMatch == "") continue;
for (col = 2; col <= number; col++) {
if (getValue(row, col) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check cross
cross = 1;
matches = 1;
toMatch = getValue(cross, cross);
if (toMatch != "") {
for (cross = 2; cross <= number; cross++) {
if (getValue(cross, cross) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check cross to other way
cross = 1;
matches = 1;
toMatch = getValue(cross, number+1-cross);
if (toMatch != "") {
for (cross = 2; cross <= number; cross++) {
if (getValue(cross, number+1-cross) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
}
function win(which) {
alert("Congrats! " + which + " won!");
}
function getValue(row, col) {
return document.getElementById("r"+row+"col"+col).innerHTML;
}
函数checkWinnerTable(){
var-col;
变量交叉;
var行;
var匹配;
var-toMatch;
//垂直检查
对于(col=1;col)来说,我根本不清楚检查赢家的逻辑是如何工作的,更不用说对角线了。您可能需要在调试器中逐步了解这一点,并了解发生了什么。使用它会有所帮助too@n.d.to使用if(row\u value==“X”&&row\u value===row),您到底想要实现什么{
?我根本不清楚检查赢家的逻辑是如何工作的,更不用说对角线了。你可能需要在调试器中逐步了解这一点,并了解发生了什么。把它放在上面会有帮助too@n.d.to如果(row_value==“X”&&row_value===row){
,您到底想用实现什么?
function checkWinnerTable() {
var col;
var cross;
var row;
var matches;
var toMatch;
// Check vertical
for (col = 1; col <= number; col++) {
matches = 1;
toMatch = getValue(1, col);
if (toMatch == "") continue;
for (var row = 2; row <= number; row++) {
if (getValue(row, col) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check horizontal
for (row = 1; row <= number; row++) {
matches = 1;
toMatch = getValue(row, 1);
if (toMatch == "") continue;
for (col = 2; col <= number; col++) {
if (getValue(row, col) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check cross
cross = 1;
matches = 1;
toMatch = getValue(cross, cross);
if (toMatch != "") {
for (cross = 2; cross <= number; cross++) {
if (getValue(cross, cross) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
// Check cross to other way
cross = 1;
matches = 1;
toMatch = getValue(cross, number+1-cross);
if (toMatch != "") {
for (cross = 2; cross <= number; cross++) {
if (getValue(cross, number+1-cross) == toMatch) matches++;
}
if (matches == number) {
win(toMatch);
}
}
}
function win(which) {
alert("Congrats! " + which + " won!");
}
function getValue(row, col) {
return document.getElementById("r"+row+"col"+col).innerHTML;
}