Skip to content

Commit 339d538

Browse files
authored
Update 1275-find-winner-on-a-tic-tac-toe-game.js
1 parent d02b194 commit 339d538

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

1275-find-winner-on-a-tic-tac-toe-game.js

+21
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,24 @@ function chk(ch, grid) {
4747

4848
return false
4949
}
50+
51+
// another
52+
53+
/**
54+
* @param {number[][]} moves
55+
* @return {string}
56+
*/
57+
const tictactoe = function(moves) {
58+
const aRow = Array(3).fill(0), aCol = Array(3).fill(0), bRow= Array(3).fill(0), bCol =Array(3).fill(0)
59+
let ad = 0, ads = 0, bd = 0, bds = 0
60+
for(let i = 0; i < moves.length; i++) {
61+
const [r, c] = moves[i]
62+
if(i % 2===0) {
63+
if(++aRow[r] === 3 || ++aCol[c] === 3 || r === c && ++ad === 3 || r + c === 2&& ++ads === 3 ) return 'A'
64+
}else {
65+
if(++bRow[r] === 3 || ++bCol[c] === 3 || r === c && ++bd === 3 || r + c === 2&& ++bds === 3 ) return 'B'
66+
}
67+
}
68+
69+
return moves.length >= 9 ? 'Draw' : 'Pending'
70+
};

0 commit comments

Comments
 (0)