Skip to content

Commit

Permalink
#12 24.03.14 > 혼자서 하는 틱택토 다시 풀기
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Mar 14, 2024
1 parent c017e3a commit b54b0f5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/programmers/Lv_2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
42 changes: 42 additions & 0 deletions src/programmers/Lv_2/replay/alone.js
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit b54b0f5

Please sign in to comment.