这一次我正在为我在vanilla JS中的工作做一个tictactoe游戏。我可以通过使用if语句让它工作,但我现在需要它与一个我正在努力工作的添加函数一起工作(这是一个要求)。代码是我到目前为止想出的。
这是我的代码:
const userGame = [
row1 = ["O", "O", "O"],
row2 = ["", "", ""],
row3 = ["", "", ""]
]
// Made this for easier referencing
cells = [
cell1 = row1[0],
cell2 = row1[1],
cell3 = row1[2],
cell4 = row2[0],
cell5 = row2[1], // C = Center
cell6 = row2[2],
cell7 = row3[0],
cell8 = row3[1],
cell9 = row3[2]
]
// Defining win conditions (row, column, diagonal)
const rowWinO = [
["O", "O", "O"]
]
const rowWinX = [
["X", "X", "X"]
]
const colWinO = [
["O"],
["O"],
["O"]
]
const colWinX = [
["X"],
["X"],
["X"]
]
const diagonalWinO = [
["", "", "O"],
["", "O", ""],
["O", "", ""]
]
const diagonalWinX = [
["", "", "X"],
["", "X", ""],
["X", "", ""]
]
const diagonalWinInverseO = [
["O", "", ""],
["", "O", ""],
["", "", "O"]
]
const diagonalWinInverseX = [
["X", "", ""],
["", "X", ""],
["", "", "X"]
]
// Placement of X and O
Xcell = 'X'
Ocell = 'O'
let gameWin = false
// Where the struggle is.
// The function isn't calling (for example)
// rowWinO for the if statements below this function.
// This program should be returning rowWinO and
// declaring that 'O has won on a row'
function evaluatePlay(board) {
board = userGame
if (cell1 == Ocell && cell2 == Ocell && cell3 == Ocell
|| cell4 == Ocell && cell5 == Ocell && cell6 == Ocell
|| cell7 == Ocell && cell8 == Ocell && cell9 == Ocell) {
rowWinO == true
}
else if (cell1 == Xcell && cell2 == Xcell && cell3 == Xcell
|| cell4 == Xcell && cell5 == Xcell && cell6 == Xcell
|| cell7 == Xcell && cell8 == Xcell && cell9 == Xcell) {
rowWinX == true
}
else if (cell1 == Ocell && cell4 == Ocell && cell7 == Ocell
|| cell2 == Ocell && cell5 == Ocell && cell8 == Ocell
|| cell3 == Ocell && cell6 == Ocell && cell9 == Ocell) {
colWinO == true
}
else if (cell1 == Xcell && cell4 == Xcell && cell7 == Xcell
|| cell2 == Xcell && cell5 == Xcell && cell8 == Xcell
|| cell3 == Xcell && cell6 == Xcell && cell9 == Xcell) {
colWinX == true
}
else if (board == diagonalWinO ) {
// Done this way because the board can only get
// a win like this in one way
diagonalWinO == true
}
else if (board == diagonalWinInverseO) {
diagonalWinInverseO == true
}
else if (board == diagonalWinX) {
diagonalWinX == true
}
else if (board == diagonalWinInverseX) {
diagonalWinInverseX == true
}
if (rowWinO == true || rowWinX == true || colWinO == true
|| colWinX == true || diagonalWinO == true || diagonalWinX == true
|| diagonalWinInverseO == true || diagonalWinInverseX == true) {
// If the gameboard matches a win state, return that we have a
// winner
gameWin == true
}
return;
}
evaluatePlay(userGame)
if (gameWin == true) {
if (rowWinO == true ) {
// O wins on a row
console.log('O wins on a row!')
}
// X Wins on a row
else if(rowWinX == true) {
console.log('X wins on a row! ')
}
// O Wins on a column
else if(colWinO == true) {
console.log('O wins on a column!')
}
// X Wins on a column
else if(colWinX == true) {
console.log('X wins on a column!')
}
// O wins on a diagonal
else if(diagonalWinO == true) {
console.log('O wins on a diagonal!')
}
else if(diagonalWinInverseO == true) {
console.log('O wins on a diagonal!')
}
// X wins on a diagonal
else if(diagonalWinX == true) {
console.log('X wins on a diagonal!')
}
else if(diagonalWinInverseX == true) {
console.log('X wins on a diagnoal!')
}
}
else if (gameWin == false) {
console.log('Nothing happens')
}
我定义了一个硬编码井字游戏板。
const userGame = [
row1 = ["O", "O", "O"],
row2 = ["", "", ""],
row3 = ["", "", ""]
]
因为我必须针对多个变量检查它,我很遗憾地与其他人的工作混淆了,因为他们在单个数组中定义了一个获胜状态。
程序的目标是将其与获胜的游戏状态进行比较,如赢在一行或对角线上。然而,不是使用数组来定义所有的赢弊,而是为我的工作预定义这些赢弊,如
const rowWinO = [ ["O", "O", "O"] ]
const diagonalWinX = [
["", "", "X"],
["", "X", ""],
["X", "", ""]
]
我的问题是我的函数。它很长,所以它的要点是检查原始游戏数组的索引,并将其与游戏状态进行比较。正如你可以看到上面的gameBoard连续获胜,所以...
预期产出
函数应该识别出这个并返回true。如果满足任何一个win条件,那么它应该识别出游戏已经(gamewin == true)
获胜。使用这个,下面的if语句应该检查(rowWinO == true && gameWin == true)
是否显示消息('X won a row.')
注意,对于if
语句,成功条件嵌套在gameWin
中
else if <the game is not a winner> `console.log('Nothing happens')`
我试过通过将X = 'X'
更改为Xcell = 'X'
来调整它,但老实说,我不确定还能再更改什么。
前面的程序处理if语句如下:
else if(row1[0] == 'O' && row2[0] == 'O' && row3[0] == 'O') {
console.log('O wins on column')
gameWin == true
}
程序不需要检查平局,只需要检查胜局。
1条答案
按热度按时间4sup72z81#
这个解决方案采用了一个2d游戏板,但是把它压平了,这样它就可以在1d环境下运行。
游戏的改变,如棋盘大小和更长的序列可能会改变事情,因此,动态生成获胜条件或通过算法确定它们将是最佳的。然而,没有足够的获胜模式使您的条件下有必要这样做,所以我们只是与已知的模式进行匹配。