-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
200 lines (172 loc) · 4.91 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
195
196
197
198
199
200
const _ = require('lodash')
// const prompt = require('prompt')
var readlineSync = require('readline-sync')
const { rotate90, rotate270 } = require('2d-array-rotation')
const KEYS_IN_A_ROW_WIN_LIMIT = 5
const dim = 3
const getMiniBoard = () => {
const mini = []
for (let r = 0; r < dim; r += 1) {
const inner = []
for (let c = 0; c < dim; c += 1) {
inner.push(null)
}
mini.push(inner)
}
return mini
}
const A = getMiniBoard()
const B = getMiniBoard()
const C = getMiniBoard()
const D = getMiniBoard()
const BOARD = {
A,
B,
C,
D,
}
const getBoard = () => {
return [
[...BOARD.A[0], ...BOARD.B[0]],
[...BOARD.A[1], ...BOARD.B[1]],
[...BOARD.A[2], ...BOARD.B[2]],
[...BOARD.C[0], ...BOARD.D[0]],
[...BOARD.C[1], ...BOARD.D[1]],
[...BOARD.C[2], ...BOARD.D[2]]
]
};
const putPiece = (piece, mini, rowIndex, colIndex) => {
if (mini[rowIndex][colIndex] === null) {
mini[rowIndex][colIndex] = piece;
} else {
throw new Error('This place is already occupied!')
}
}
const rotateMini = (mini, isCCW = false) => {
const rotatedMini = isCCW ? rotate270(mini) : rotate90(mini);
rotatedMini.forEach((r, ri) => {
r.forEach((c, ci) => {
mini[ri][ci] = rotatedMini[ri][ci];
})
});
}
const action = (piece, miniToPutPiece, rowIndex, colIndex, miniToRotate, ccw) => {
try {
putPiece(piece, miniToPutPiece, rowIndex, colIndex);
} catch (e) {
throw e
}
const winnerAfterPutPiece = isGameOver()
if (winnerAfterPutPiece) {
return winnerAfterPutPiece
}
rotateMini(miniToRotate, ccw);
const winnerAfterRotateMini = isGameOver()
if (winnerAfterRotateMini) {
return winnerAfterRotateMini
}
return null
};
const isGameOver = () => {
const winners = []
const board = getBoard();
for (let r = 0; r < board.length; r += 1) {
for (let c = 0; c < board[0].length; c += 1) {
const winner = search(board, r, c)
if (winner) {
winners.push(winner)
}
}
}
const uniqWinners = _.uniq(winners)
if (uniqWinners.length === 0) {
return null
}
return _.uniq(winners).length === 1 ? winners[0] : 0
}
const directions = {
upRight: [-1, 1],
right: [0, 1],
downRight: [1, 1],
down: [1, 0]
}
const search = (board, r, c) => {
if (board[r][c] === null) {
return null
}
for (let d = 0; d < Object.keys(directions).length; d += 1) {
const dir = Object.keys(directions)[d]
const winner = searchInDirection(board, r, c, 1, dir)
if (winner) {
return winner
}
}
return null
}
const searchInDirection = (board, r, c, keysInARow, dir) => {
const [rr, cc] = directions[dir]
if (r + rr < 0 || c + cc < 0 || r + rr >= board.length || c + cc >= board[0].length) {
return null
}
if (keysInARow === KEYS_IN_A_ROW_WIN_LIMIT) {
return board[r][c]
}
if (board[r][c] === board[r + rr][c + cc]) {
return searchInDirection(board, r + rr, c + cc, keysInARow + 1, dir)
}
return null
}
search(getBoard(), 0, 0)
async function init() {
let winner = null
// prompt.start();
console.log('Game is On!')
const players = [1, -1]
const boardsArray = ['A', 'B', 'C', 'D']
let turn = 0
while (winner === null) {
// const { userAction } = await prompt.get(['userAction']);
// const [boardToPutPiece, rIndex, cIndex, boardToRotate, isCCW] = userAction.split('_')
let putPieceBoardIndex = readlineSync.keyInSelect(boardsArray, 'Which board to put piece? ', {
guide: false,
cancel: false,
})
let rowIndex = readlineSync.questionInt(`Which row of ${boardsArray[putPieceBoardIndex]}: `)
let colIndex = readlineSync.questionInt(`Which column of ${boardsArray[putPieceBoardIndex]}: `)
while (rowIndex < 0 || rowIndex > 2 || colIndex < 0 || colIndex > 2) {
console.log(`Please give valid numbers up to ${dim - 1}`)
rowIndex = readlineSync.questionInt(`Which row of ${boardsArray[putPieceBoardIndex]}: `, {
min: 1,
max: 3,
})
colIndex = readlineSync.questionInt(`Which column of ${boardsArray[putPieceBoardIndex]}: `, {
min: 1,
max: 3,
})
}
let rotateBooardIndex = readlineSync.keyInSelect(boardsArray, 'Which board to rotate? ', {
guide: false,
cancel: false,
})
isCCW = readlineSync.keyInYN('Do you want turn CCW? Y: Yes N: No')
try {
winner = action(players[turn % 2], BOARD[boardsArray[putPieceBoardIndex]], +rowIndex - 1, +colIndex - 1, BOARD[boardsArray[rotateBooardIndex]], isCCW)
console.log(getBoard())
turn++
} catch (e) {
console.log('\n')
console.log(e.message)
console.log('\n')
console.log('Please choose another place to move!')
}
}
if (winner === 0) {
console.log(`It is a draw`)
} else {
console.log(`Player ${winner} won in ${turn} turns!`)
}
}
init()
// Only accept valid userActions
// Better interface for entering user actions
// If user tries to put piece on a non-empty space, it should fail