diff --git a/src/programmers/Lv_2/README.md b/src/programmers/Lv_2/README.md index 31c8beea..55336a08 100644 --- a/src/programmers/Lv_2/README.md +++ b/src/programmers/Lv_2/README.md @@ -101,5 +101,5 @@ | 97 | [석유 시추](./oil.js) | 24.01.24 | X | | 98 | [당구 연습](./billiard.js) | 24.01.26 | X | [24.03.13](./replay/billiard.js) | | 99 | [순위 검색](./search.js) | 24.01.27 | X | -| 100 | [혼자서 하는 틱택토](./alone.js) | 24.02.07 | X | +| 100 | [혼자서 하는 틱택토](./alone.js) | 24.02.07 | X | [24.03.14](./replay/alone.js) | | 101 | [테이블 해시 함수](./tableHash.js) | 24.03.08 | X | [24.03.12](./tableHash.js) | diff --git a/src/programmers/Lv_2/replay/alone.js b/src/programmers/Lv_2/replay/alone.js new file mode 100644 index 00000000..c34cc332 --- /dev/null +++ b/src/programmers/Lv_2/replay/alone.js @@ -0,0 +1,42 @@ +/** + * [구현] + */ + +function checkTicTacToe(board, sign) { + const lines = [ + [0, 1, 2], + [3, 4, 5], + [6, 7, 8], + [0, 3, 6], + [1, 4, 7], + [2, 5, 8], + [0, 4, 8], + [2, 4, 6], + ]; + + for (const [a, b, c] of lines) { + if (board[a] == sign && board[b] == sign && board[c] == sign) return true; + } + return false; +} + +function solution(board) { + board = board.map((value) => value.split("")).flat(); + let [O, X] = [0, 0]; + + for (const sign of board) { + if (sign === "O") O++; + else if (sign === "X") X++; + } + + if (O < X || 1 < O - X) return 0; + + let oWins = checkTicTacToe(board, "O"); + let xWins = checkTicTacToe(board, "X"); + + if (oWins && xWins) return 0; + if (oWins && O - X !== 1) return 0; + if (xWins && O !== X) return 0; + + return 1; +}