From b54b0f5137320e2d231934668575b70fc001ae2c Mon Sep 17 00:00:00 2001 From: beurmuz Date: Thu, 14 Mar 2024 23:31:11 +0900 Subject: [PATCH] =?UTF-8?q?#12=2024.03.14=20>=20=ED=98=BC=EC=9E=90?= =?UTF-8?q?=EC=84=9C=20=ED=95=98=EB=8A=94=20=ED=8B=B1=ED=83=9D=ED=86=A0=20?= =?UTF-8?q?=EB=8B=A4=EC=8B=9C=20=ED=92=80=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/programmers/Lv_2/README.md | 2 +- src/programmers/Lv_2/replay/alone.js | 42 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/programmers/Lv_2/replay/alone.js 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; +}