play-3t 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +4 -0
- package/package.json +14 -0
- package/src/game.js +148 -0
package/bin/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "play-3t",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Tic Tac Toe vs AI in your terminal",
|
|
5
|
+
"bin": {
|
|
6
|
+
"play-3t": "bin/index.js"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"keywords": ["cli", "game", "tic-tac-toe", "terminal", "ai"],
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"chalk": "^5.3.0",
|
|
12
|
+
"readline-sync": "^1.4.10"
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/game.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import readlineSync from "readline-sync";
|
|
3
|
+
|
|
4
|
+
let board;
|
|
5
|
+
|
|
6
|
+
const HUMAN = "X";
|
|
7
|
+
const AI = "O";
|
|
8
|
+
|
|
9
|
+
function reset() {
|
|
10
|
+
board = Array(9).fill(null);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function color(v) {
|
|
14
|
+
if (v === HUMAN) return chalk.red("X");
|
|
15
|
+
if (v === AI) return chalk.cyan("O");
|
|
16
|
+
return " ";
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function draw() {
|
|
20
|
+
console.clear();
|
|
21
|
+
console.log(chalk.yellow("\n TIC TAC TOE vs AI\n"));
|
|
22
|
+
|
|
23
|
+
for (let i = 0; i < 9; i += 3) {
|
|
24
|
+
console.log(
|
|
25
|
+
` ${color(board[i])} | ${color(board[i+1])} | ${color(board[i+2])}`
|
|
26
|
+
);
|
|
27
|
+
if (i < 6) console.log("---+---+---");
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function winner(b) {
|
|
32
|
+
const lines = [
|
|
33
|
+
[0,1,2],[3,4,5],[6,7,8],
|
|
34
|
+
[0,3,6],[1,4,7],[2,5,8],
|
|
35
|
+
[0,4,8],[2,4,6]
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
for (const [a,b2,c] of lines) {
|
|
39
|
+
if (b[a] && b[a] === b[b2] && b[a] === b[c]) return b[a];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!b.includes(null)) return "draw";
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function humanMove() {
|
|
47
|
+
while (true) {
|
|
48
|
+
const pos = readlineSync.questionInt("Choose position (1-9): ") - 1;
|
|
49
|
+
|
|
50
|
+
if (pos >= 0 && pos < 9 && !board[pos]) {
|
|
51
|
+
board[pos] = HUMAN;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
console.log(chalk.red("Invalid move."));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* ======================
|
|
60
|
+
MINIMAX AI MAGIC ðŸ§
|
|
61
|
+
====================== */
|
|
62
|
+
|
|
63
|
+
function minimax(b, depth, isMax) {
|
|
64
|
+
const result = winner(b);
|
|
65
|
+
|
|
66
|
+
if (result === AI) return 10 - depth;
|
|
67
|
+
if (result === HUMAN) return depth - 10;
|
|
68
|
+
if (result === "draw") return 0;
|
|
69
|
+
|
|
70
|
+
if (isMax) {
|
|
71
|
+
let best = -Infinity;
|
|
72
|
+
|
|
73
|
+
for (let i = 0; i < 9; i++) {
|
|
74
|
+
if (!b[i]) {
|
|
75
|
+
b[i] = AI;
|
|
76
|
+
best = Math.max(best, minimax(b, depth + 1, false));
|
|
77
|
+
b[i] = null;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return best;
|
|
82
|
+
} else {
|
|
83
|
+
let best = Infinity;
|
|
84
|
+
|
|
85
|
+
for (let i = 0; i < 9; i++) {
|
|
86
|
+
if (!b[i]) {
|
|
87
|
+
b[i] = HUMAN;
|
|
88
|
+
best = Math.min(best, minimax(b, depth + 1, true));
|
|
89
|
+
b[i] = null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return best;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function aiMove() {
|
|
98
|
+
let bestScore = -Infinity;
|
|
99
|
+
let move = -1;
|
|
100
|
+
|
|
101
|
+
for (let i = 0; i < 9; i++) {
|
|
102
|
+
if (!board[i]) {
|
|
103
|
+
board[i] = AI;
|
|
104
|
+
const score = minimax(board, 0, false);
|
|
105
|
+
board[i] = null;
|
|
106
|
+
|
|
107
|
+
if (score > bestScore) {
|
|
108
|
+
bestScore = score;
|
|
109
|
+
move = i;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
board[move] = AI;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/* ====================== */
|
|
118
|
+
|
|
119
|
+
export function startGame() {
|
|
120
|
+
reset();
|
|
121
|
+
|
|
122
|
+
while (true) {
|
|
123
|
+
draw();
|
|
124
|
+
|
|
125
|
+
humanMove();
|
|
126
|
+
|
|
127
|
+
let result = winner(board);
|
|
128
|
+
if (result) break;
|
|
129
|
+
|
|
130
|
+
aiMove();
|
|
131
|
+
|
|
132
|
+
result = winner(board);
|
|
133
|
+
if (result) break;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
draw();
|
|
137
|
+
|
|
138
|
+
const result = winner(board);
|
|
139
|
+
|
|
140
|
+
if (result === HUMAN)
|
|
141
|
+
console.log(chalk.green("\nYou beat the AI?! Reality bends…"));
|
|
142
|
+
else if (result === AI)
|
|
143
|
+
console.log(chalk.red("\nAI wins. Machines ascend."));
|
|
144
|
+
else
|
|
145
|
+
console.log(chalk.yellow("\nDraw. Equilibrium achieved."));
|
|
146
|
+
|
|
147
|
+
if (readlineSync.keyInYN("Play again?")) startGame();
|
|
148
|
+
}
|