使用JavaScript创建TicTacToe?

时间:2017-03-18 22:49:20

标签: javascript json tic-tac-toe

我正在创建一个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();
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

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

缺少结尾}