1275.挑出井字棋的胜出者(javascript)FindWinneronaTicTacToeGame

原创
小哥 3年前 (2022-11-10) 阅读数 2 #大杂烩

A 和 B 在一个 3 x 3 在网格上播放tico。

游戏规则如下:

玩家轮流把棋子放在空的方块里。 (" ") 上。
第一个玩家 A 总是用 “X” 作为棋子,而第二个玩家 B 总是用 “O” 作为棋子。
“X” 和 “O” 只能放在空的广场上,不能放在已经被占用的广场上。
只要有 3 当相同(非空)棋子排列成一条直线(行、列、对角线)时,游戏结束。
如果所有的方块都装满了棋子(不是空的),游戏将结束。
比赛结束后,棋子不能再移动了。
给你一个数组 moves,其中每个元素都有大小。 2 另一个数组(元素分别对应于网格的行和列),如下所示 A 和 B 行动顺序(第一 A 后 B)记录了两人各自曲目的位置。

如果游戏中有赢家(A 或 B),则返回游戏的胜利者; “Draw“;如果还有动作(游戏还没有结束),请返回。 “Pending”。

你可以假设 moves 都 有效(遵循tic-tac-toe规则),网格最初为空,A 将首先行动。
Tic-tac-toe is played by two players A and B on a 3 x 3 grid. The rules of Tic-Tac-Toe are:

Players take turns placing characters into empty squares ’ .
The first player A always places ‘X’ characters, while the second player B always places ‘O’ characters.
‘X’ and ‘O’ characters are always placed into empty squares, never on filled ones.
The game ends when there are three of the same (non-empty) character filling any row, column, or diagonal.
The game also ends if all squares are non-empty.
No more moves can be played if the game is over.
Given a 2D integer array moves where moves[i] = [rowi, coli] indicates that the ith move will be played on grid[rowi][coli]. return the winner of the game if it exists (A or B). In case the game ends in a draw return “Draw”. If there are still movements to play return “Pending”.

You can assume that moves is valid (i.e., it follows the rules of Tic-Tac-Toe), the grid is initially empty, and A will play first.

示例 1:

输入:moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
输出:"A"
解释:"A" 要想赢,他总是第一个。
"X  "    "X  "    "X  "    "X  "    "X  "
"   " -> "   " -> " X " -> " X " -> " X "
"   "    "O  "    "O  "    "OO "    "OOX"

示例 2:

输入:moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
输出:"B"
解释:"B" 获胜。
"X  "    "X  "    "XX "    "XXO"    "XXO"    "XXO"
"   " -> " O " -> " O " -> " O " -> "XO " -> "XO " 
"   "    "   "    "   "    "   "    "   "    "O  "

示例 3:

输入:moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
输出:"Draw"
输出:由于无法再次行动,比赛以平局结束。
"XXO"
"OOX"
"XOX"

示例 4:

输入:moves = [[0,0],[1,1]]
输出:"Pending"
说明:游戏还没有结束。
"X  "
" O "
"   "

首先创建3行3列用0填充的阵列
根据A第一条规则,moves下标可以是2分割,此时替换位置B,否则更换A
3行3列-

  • 遍历每一行,当一行数据相等时,返回要返回的行的第一个值。
  • 遍历每一列,当一列数据相等时,返回要返回的列的第一个值。
  • 然后穿过斜对角线,全部相等,返回。
  • 当moves的长度9那时,我还没有回来。Pending
  • 其他时候,Draw

    var tictactoe = function (moves) { const ans = new Array(3).fill(0).map(() => new Array(3).fill(0)); moves.forEach((item, index) => { let [x, y] = item ans[x][y] = index % 2 ? B : A }) for (let i = 0; i < 3; i++) { let [a, b, c] = ans[i] if (a && a == b && b == c) return a } for (let j = 0; j < 3; j++) { let [a, b, c] = [ans[0][j], ans[1][j], ans[2][j]] if (a && a == b && b == c) return a } let [a, b, c] = [ans[0][0], ans[1][1], ans[2][2]] if (a && a == b && b == c) return a let [d, e, f] = [ans[2][0], ans[1][1], ans[0][2]] if (d && d == e && e == f) return d if (moves.length < 9) return Pending return Draw };

leetcode: https://leetcode.cn/problems/find-winner-on-a-tic-tac-toe-game/

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除