-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
194 lines (179 loc) · 4.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env node
import chalk from "chalk";
import chalkAnimation from "chalk-animation";
import inquirer from "inquirer";
import { board } from "./utils/board.js";
let playerName;
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
console.clear();
const rainbow = chalkAnimation.rainbow("Welcome to Tic-Tac-Toe"); // Animation starts
await intro();
await main();
async function intro() {
await sleep(1000);
rainbow.stop();
console.log(`
${chalk.bgBlue("How to play?")}
Keep in mind your sign is ${chalk.cyan.bold("O")}
and my sign is ${chalk.green.bold("X")} if you
lose you'll get ${chalk.bgRed("killed")}.
`);
await sleep(800);
await setName();
console.log(`${chalk.bgHex("#d43e02")("hold")} on ...`);
await sleep(800);
console.log(`
${await board({
1: "1",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
})}
just add ${chalk.bgHex("#5d5100")("one-number")} as your input..
`);
await sleep(1000);
}
async function setName() {
const input = await inquirer.prompt({
name: "player_name",
message: "what's your name",
});
playerName = input.player_name;
}
async function main() {
const inputHash = {};
let toggle = true;
while (toggle) {
let cpuInput = Math.floor(Math.random() * 9) + 1;
while (cpuInput in inputHash) {
cpuInput = Math.floor(Math.random() * 9) + 1;
}
console.log(`🔹 ${chalk.bgGreen.black(" My turn ")}`);
inputHash[cpuInput] = chalk.green("X");
await sleep(700);
console.log(`${await board(inputHash)}`);
await sleep(700);
let user = await inquirer.prompt({
name: "input",
message: `🔹 ${chalk.bgCyan(" Your turn ")} -> `,
});
while (user.input in inputHash) {
user = await inquirer.prompt({
name: "input",
message: `🔹 ${chalk.bgCyan(" Your turn ")} -> `,
});
}
inputHash[user.input] = chalk.cyanBright("O");
await sleep(700);
console.log(`${await board(inputHash)}`);
toggle = checkResult(inputHash);
}
}
function checkResult(inputHash) {
const o = "\x1B[96mO\x1B[39m";
const x = "\x1B[32mX\x1B[39m";
if (
inputHash[1] &&
inputHash[1] == inputHash[2] &&
inputHash[1] == inputHash[3]
) {
if (inputHash[1] == x) {
console.log(chalk.bgRed("I Won"));
} else {
console.log(chalk.yellow("You Won"));
}
return false;
}
if (
inputHash[1] &&
inputHash[1] == inputHash[4] &&
inputHash[1] == inputHash[7]
) {
if (inputHash[1] == x) {
console.log(chalk.bgRed("I Won"));
} else {
console.log(chalk.yellow("You Won"));
}
return false;
}
if (
inputHash[3] &&
inputHash[3] == inputHash[6] &&
inputHash[3] == inputHash[9]
) {
if (inputHash[3] == x) {
console.log(chalk.bgRed("I Won"));
} else {
console.log(chalk.yellow("You Won"));
}
return false;
}
if (
inputHash[7] &&
inputHash[7] == inputHash[8] &&
inputHash[7] == inputHash[9]
) {
if (inputHash[7] == x) {
console.log(chalk.bgRed("I Won"));
} else {
console.log(chalk.yellow(`${playerName} You Won`));
}
return false;
}
if (
inputHash[4] &&
inputHash[4] == inputHash[5] &&
inputHash[4] == inputHash[6]
) {
if (inputHash[4] == x) {
console.log(chalk.bgRed("I Won"));
} else {
console.log(chalk.yellow(`${playerName} You Won`));
}
return false;
}
if (
inputHash[1] &&
inputHash[1] == inputHash[5] &&
inputHash[1] == inputHash[9]
) {
if (inputHash[1] == x) {
console.log(chalk.bgRed("I Won"));
} else {
console.log(chalk.yellow(`${playerName} You Won`));
}
return false;
}
if (
inputHash[3] &&
inputHash[3] == inputHash[5] &&
inputHash[3] == inputHash[7]
) {
if (inputHash[3] == x) {
console.log(chalk.bgRed("I Won"));
} else {
console.log(chalk.yellow(`${playerName} You Won`));
}
return false;
}
if (
inputHash[1] &&
inputHash[2] &&
inputHash[3] &&
inputHash[4] &&
inputHash[5] &&
inputHash[6] &&
inputHash[7] &&
inputHash[8] &&
inputHash[9]
) {
console.log(chalk.bgMagentaBright(`GAME DRAW`));
return false;
}
return true;
}