forked from DGXeon/Vimdhayak-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictactoe.js
200 lines (188 loc) · 5.92 KB
/
tictactoe.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
195
196
197
198
199
200
const fs = require('fs')
const dbdir = 'Created_By_MRHRTZ'
function defineSave(db, obj, session) {
mine = db
const dbdir = `./lib/tictactoe/db/${session}.json`
fs.writeFileSync(dbdir, JSON.stringify(obj, null, 2))
}
function setGame(session) {
const matrix = []
const dbdir = `./lib/tictactoe/db/${session}.json`
if (!fs.existsSync(dbdir)) {
matrix[0] = ["1️⃣", "2️⃣", "3️⃣"]
matrix[1] = ["4️⃣", "5️⃣", "6️⃣"]
matrix[2] = ["7️⃣", "8️⃣", "9️⃣"]
const objtic = {
status: true,
session: session,
turn: 'X', // 'O'
X: null,
O: null,
isWin: false,
winner: null,
nine_push: [],
_matrix: matrix
}
defineSave(dbdir, objtic, session)
return objtic
} else {
const read = JSON.parse(fs.readFileSync(dbdir))
return read
}
}
function horizontal(matrix) {
let size = matrix.length;
for (let x = 0; x < size; x++) {
let count = 1;
let previous = null;
for (let y = 0; y < size; y++) {
let current = matrix[x][y];
if (null == previous) {
previous = current;
}
else {
if (current === previous) {
count++;
if (count >= size) {
return current;
}
}
}
}
}
return false;
}
function vertical(matrix) {
let size = matrix.length;
for (let y = 0; y < size; y++) {
let count = 1;
let previous = null;
for (let x = 0; x < size; x++) {
let current = matrix[x][y];
if (null == previous) {
previous = current;
}
else {
if (current === previous) {
count++;
if (count >= size) {
return current;
}
}
}
}
}
return false;
}
function diagonalLTR(matrix) {
let size = matrix.length;
let count = 1;
let previous = null;
for (let x = 0; x < size; x++) {
let y = x;
let current = matrix[x][y];
if (null == previous) {
previous = current;
}
else {
if (current === previous) {
count++;
if (count >= size) {
return current;
}
}
}
}
return false;
}
function diagonalRTL(matrix) {
let size = matrix.length;
let count = 1;
let previous = null;
for (let x = 0; x < size; x++) {
let y = (size - 1) - x;
let current = matrix[x][y];
if (null == previous) {
previous = current;
}
else {
if (current === previous) {
count++;
if (count >= size) {
return current;
}
}
}
}
return false;
}
function move(x, y, sessionS) {
const session = sessionS
const moving = setGame(session)
if (moving.isWin) {
return { status: false, message: 'Game telah dimenangkan oleh ' + moving.winner }
}
if (moving._matrix[x][y] == '❎') return { status: false, message: 'Titik ini telah diisi oleh ❎' }
if (moving._matrix[x][y] == '⭕') return { status: false, message: 'Titik ini telah diisi oleh ⭕' }
if (moving.turn == 'X') {
moving._matrix[x][y] = '❎'
moving.nine_push.push('❎')
moving.turn = 'O'
} else if (moving.turn == 'O') {
moving._matrix[x][y] = '⭕'
moving.nine_push.push('⭕')
moving.turn = 'X'
}
defineSave(dbdir, moving, session)
if (horizontal(setGame(session)._matrix) == "❎" || horizontal(setGame(session)._matrix) == "⭕") {
moving.isWin = true
moving.turn == 'X' ? moving.winner = "O" : moving.winner = "X"
defineSave(dbdir, moving, session)
} else if (vertical(setGame(session)._matrix) == "❎" || vertical(setGame(session)._matrix) == "⭕") {
moving.isWin = true
moving.turn == 'X' ? moving.winner = "O" : moving.winner = "X"
defineSave(dbdir, moving, session)
} else if (diagonalLTR(setGame(session)._matrix) == "❎" || diagonalLTR(setGame(session)._matrix) == "⭕") {
moving.isWin = true
moving.turn == 'X' ? moving.winner = "O" : moving.winner = "X"
defineSave(dbdir, moving, session)
} else if (diagonalRTL(setGame(session)._matrix) == "❎" || diagonalRTL(setGame(session)._matrix) == "⭕") {
moving.isWin = true
moving.turn == 'X' ? moving.winner = "O" : moving.winner = "X"
defineSave(dbdir, moving, session)
} else if (moving.nine_push.length >= 9) {
moving.isWin = true
moving.winner = "SERI"
}
defineSave(dbdir, moving, session)
return moving
}
function validmove(number, sessionS) {
const session = sessionS
if (Number(number) == 1) {
return move(0,0,session)
} else if (Number(number) == 2) {
return move(0,1,session)
} else if (Number(number) == 3) {
return move(0,2,session)
} else if (Number(number) == 4) {
return move(1,0,session)
} else if (Number(number) == 5) {
return move(1,1,session)
} else if (Number(number) == 6) {
return move(1,2,session)
} else if (Number(number) == 7) {
return move(2,0,session)
} else if (Number(number) == 8) {
return move(2,1,session)
} else if (Number(number) == 9) {
return move(2,2,session)
} else {
return false
}
}
// console.log(validmove(process.argv[2], 'Mine'))
// console.log(horizontal(setGame(session)._matrix) == "O")
// console.log(setGame('Mine'))
module.exports.setGame = setGame
module.exports.validmove = validmove