Tic Tac Toe绘制功能

时间:2013-11-30 19:51:20

标签: javascript html html5 button tic-tac-toe

我用javascript制作了一个tic tac toe游戏。 我有2个问题。
我想要一个“开始”按钮。因此,如果您点击按钮,则只能看到/玩游戏。

现在最困难的是: 如果是绘制,则计算机必须重置1个随机平方。只要有必要,游戏就会继续进行,直到有胜利者为止。

function drawCheck() {
    vari()
    moveCount = sqr1T + sqr2T + sqr3T + sqr4T + sqr5T + sqr6T + sqr7T + sqr8T + sqr9T 
    if(moveCount == 9) {
        reset()
        alert ("draw!")
    }
}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

关于您的第一个问题,您需要以下HTML代码:

<button id="startbutton" onclick="startGame()">Start Game</button>

以下JavaScript:

function startGame() {
    //Get rid of the button
    document.getElementById('startbutton').style.display = 'none'
    //Start the game
}

对于第二个问题,您需要以下JavaScript:

//Generate random number from 1 to 9
var random = Math.floor(Math.random() * 9) + 1
//Reset the square
if(random == 1) {
    sqr1T = 0
} else if(random == 2) {
    sqr2T = 0
} else if(random == 3) {
    sqr3T = 0
} else if(random == 4) {
    sqr4T = 0
} else if(random == 5) {
    sqr5T = 0
} else if(random == 6) {
    sqr6T = 0
} else if(random == 7) {
    sqr7T = 0
} else if(random == 8) {
    sqr8T = 0
} else if(random == 9) {
    sqr9T = 0
}