gpt4 book ai didi

javascript - 使用 JavaScript 创建 TicTacToe?

转载 作者:行者123 更新时间:2023-12-03 04:45:38 25 4
gpt4 key购买 nike

我正在创建一个 TicTacToe 游戏,但我在代码末尾不断收到语法错误。游戏尚未完成,但有人可以解释为什么我收到以下错误吗?问题是语法错误显示在最后一行之后,其中不包含它所指的内容。任何修改和更改都会有所帮助。

    /tmp/source.js:160
});
^

SyntaxError: Unexpected token )
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3`

// start of evaluation code
process.stdin.resume();
process.stdin.setEncoding('utf8');

var stdin = '';
process.stdin.on('data', function (chunk) {
addResultToStdin(chunk);
}).on('end', function() {
var lines = stdin.split('\n');
for(var i=0; i<lines.length; i++) {
process.stdout.write(lines[i]);
}
});

function addResultToStdin(chunk) {
var output = '';
var instructions = chunk.split('|');
var method = instructions[0];
tictactoe.reset();
for (var i = 1; i < instructions.length; i++) {
if(instructions[i] === 'undo') {
if(i === instructions.length - 1) {
output += JSON.stringify(tictactoe.undo());
} else {
tictactoe.undo();
}
} else {
var moves = instructions[i].split(',');
var x = moves[0].trim();
var y = moves[1].trim();
if(i === instructions.length - 1) {
output += JSON.stringify(tictactoe[method](x, y));
} else {
tictactoe[method](x, y);
}
}
}
stdin += output;
}

function TicTacToe() {
this.init();
}

TicTacToe.prototype.getCurrentState = function() {
this.state = TicTacToe.prototype.updatedBoard

TicTacToe.prototype.reset = function() {
this.init();
};

TicTacToe.prototype.init = function() {
this.state = [];
// setting initial state
this.state.push({
activePlayer: 'x',
currentBoard: this.freshBoard()
});
};

TicTacToe.prototype.updatedBoard = function(x,y) {
var state = this.getCurrentState();
var activePlayer = state.activePlayer;
var currentBoard = state.currentBoard;
var newBoard = [];
for (var i = 0; i < currentBoard.length; i++) {
newBoard.push(currentBoard[i].concat());
}
newBoard[x][y] = activePlayer;
return newBoard;
};

TicTacToe.prototype.freshBoard = function() {
return [
[null, null, null],
[null, null, null],
[null, null, null]
];
};

TicTacToe.prototype.undo = function() {
activePlayer = ( activePlayer === 'x' ) ? 'o' : 'x';
return state.currentBoard;
};

TicTacToe.prototype.isValidTurn = function(x, y) {
if ( TicTacToe.prototype.freshBoard ==
[null, null, null],
[null, null, null],
[null, null, null] ) {
return true;
} else {
alert( 'Select a blank space my friend.' );
return false;
}
};

TicTacToe.prototype.isWinningTurn = function(activePlayer, updatedBoard) {
var checkVertical = function () {
for (var i = 0; i < 3; i++){
var sum = this.state[i][0] + this.state[i][1] + this.state[i][2];
if (sum === 3 || sum === -3){
return true
}
}
return false;
};
var checkHorizontal = function () {
for (var i = 0; i < 3; i++){
var sum = this.results[0][i] + this.results[1][i] + this.results[2][i];
if (sum === 3 || sum === -3){
return true
}
}
return false;
};
var checkDiagonal = function () {
for (var i = 0; i < 3; i++){
var sum = this.results[i][i] + this.results[i+1][i+1] + this.results[i+2][i+2];
if (sum === 3 || sum === -3){
return true
}
}
return false;
};

if(checkDiagonal() || checkHorizontal() || checkVertical()) {
return true;
}
return false;
};
TicTacToe.prototype.takeTurn = function(x, y) {
var state = this.getCurrentState();
var valid = false;
var winning = false;
if(this.isValidTurn(x, y)) {
var newState = {
activePlayer: (state.activePlayer === 'x') ? 'o' : 'x'
};
valid = true;
newState.currentBoard = this.updatedBoard(x, y);
newState.winningMove = winning = this.isWinningTurn(state.activePlayer, newState.currentBoard);
this.state.push(newState);
}
return {
board: newState.currentBoard,
moveBy: state.activePlayer,
valid: valid,
winning: winning
};
}
var tictactoe = new TicTacToe();

最佳答案

TicTacToe.prototype.getCurrentState = function() {
this.state = TicTacToe.prototype.updatedBoard

缺少结尾 }

关于javascript - 使用 JavaScript 创建 TicTacToe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42880807/

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