-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproset.js
More file actions
236 lines (195 loc) · 6.12 KB
/
proset.js
File metadata and controls
236 lines (195 loc) · 6.12 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
const deck = require('./deck')
const chalk = require('chalk')
const rlSync = require('readline-sync')
const { threeCardSet, fourCardSet, fiveCardSet, sixCardSet, sevenCardSet, objectify } = require('./comparison')
class ProSet {
constructor () {
this.deck = deck
this.draw = {}
this.input = null
this.state = ''
this.colorBlindMode = false
}
colorBlind () {
const cbm = rlSync.question('Would you like to turn on colorblind mode? (Replaces colors with numbers) (y/n): ')
if (cbm === 'y') {
this.colorBlindMode = true
}
}
deal () {
/*
Choose seven random cards from the included deck and visualize them for the draw.
*/
this.draw = {}
const cardKeys = []
let count = 0
while (cardKeys.length < 7) {
const cardKey = Math.floor(Math.random() * (Object.keys(this.deck).length - 1))
if (!cardKeys.includes(cardKey)) {
cardKeys.push(cardKey)
count++
this.draw[count] = this.deck[cardKey]
}
}
let top = ''
let line1 = ''
let line2 = ''
let line3 = ''
let bottom = ''
let labels = ''
let a, b, c, d, e, f
for (var val in this.draw) {
if (this.colorBlindMode) {
a = this.draw[val].includes('a') ? '\u2780' : ' '
b = this.draw[val].includes('b') ? '\u2781' : ' '
c = this.draw[val].includes('c') ? '\u2782' : ' '
d = this.draw[val].includes('d') ? '\u2783' : ' '
e = this.draw[val].includes('e') ? '\u2784' : ' '
f = this.draw[val].includes('f') ? '\u2785' : ' '
} else {
a = this.draw[val].includes('a') ? chalk.red('\u25CF') : ' '
b = this.draw[val].includes('b') ? chalk.yellow('\u25CF') : ' '
c = this.draw[val].includes('c') ? chalk.green('\u25CF') : ' '
d = this.draw[val].includes('d') ? chalk.cyan('\u25CF') : ' '
e = this.draw[val].includes('e') ? chalk.blue('\u25CF') : ' '
f = this.draw[val].includes('f') ? chalk.magenta('\u25CF') : ' '
}
top = top + '╭──────╮ '
line1 = line1 + `│ ${a} ${b} │ `
line2 = line2 + `│ ${c} ${d} │ `
line3 = line3 + `│ ${e} ${f} │ `
bottom = bottom + '╰──────╯ '
labels = labels + ` ${val} `
}
console.log(top)
console.log(line1)
console.log(line2)
console.log(line3)
console.log(bottom)
console.log(labels)
this.input = rlSync.question('Create a set (separate choices with commas): ')
this.handleInput()
}
select (arr) {
/*
Evaluate user selection of cards for possible set.
*/
const dotArray = []
for (var card of arr) {
dotArray.push(...this.draw[card])
}
const dotObj = objectify(dotArray)
for (var val in dotObj) {
if (dotObj[val] % 2 !== 0) {
this.state = 'fail'
return
}
}
this.state = 'success'
}
find () {
/*
Find all available sets in the draw.
TODO: Fix these functions
*/
const threeCardSets = threeCardSet(this.draw)
const fourCardSets = fourCardSet(this.draw)
const fiveCardSets = fiveCardSet(this.draw)
const sixCardSets = sixCardSet(this.draw)
const sevenCardSets = sevenCardSet(this.draw)
const allSets = [...threeCardSets, ...fourCardSets, ...fiveCardSets, ...sixCardSets, ...sevenCardSets]
console.log('POSSIBLE SETS: ')
for (var set of allSets) {
console.log(set.split(''))
}
}
handleInput () {
/*
Validate user input looking for possible set.
*/
if (!this.input) {
this.state = 'badInput'
return
}
this.input = this.input.replace(/\s+/g, '').toLowerCase()
if (this.input.match(/[0-9,]/)) {
const keyArray = this.input.split(',')
for (var key of keyArray) {
if (parseInt(key) >= Object.keys(deck).length) {
console.log('A value is out of bounds.')
this.state = 'badInput'
return
}
}
this.select(keyArray)
} else if (this.input === 'help' || this.input === 'h') {
this.find()
this.state = 'helped'
} else {
console.log('Sorry, could not read your input.')
this.state = 'badInput'
}
}
run () {
/*
Main game running function.
*/
const intro = `
${chalk.red('P')}${chalk.yellow('R')}${chalk.green('O')}${chalk.cyan('S')}${chalk.blue('E')}${chalk.magenta('T')}: A Game of Matching
A set is any number of cards between 3 and 7 where there
are totaled either an even number of a colored dot or none of
that color dot.
There is always a set in any given draw of seven cards from the deck.
To submit a set, note the numbers below each card and submit
in a comma separated list. (ex: 1,2,3)
If you need help, you can type 'help' at the prompt and it will provide
you with all of the sets in the given draw.
`
console.log(intro)
this.colorBlind()
this.deal()
let done = false
while (!done) {
switch (this.state) {
case 'success':
this.input = rlSync.question('You found a set! Play again? (y/n): ').toLowerCase()
if (this.input === 'y') {
this.deal()
} else {
console.log('See ya!')
done = true
}
break
case 'fail':
this.input = rlSync.question('That is not a set. Try again or enter q to quit: ').toLowerCase()
if (this.input === 'q') {
console.log('Bye!')
done = true
} else {
this.handleInput()
}
break
case 'helped':
this.input = rlSync.question('Would you ACTUALLY like to play this time? Or just cheat? (y/n): ').toLowerCase()
if (this.input === 'y') {
this.deal()
} else {
console.log('See ya!')
done = true
}
break
case 'badInput':
this.input = rlSync.question('Try again or enter q to quit: ').toLowerCase()
if (this.input === 'q') {
console.log('Bye!')
done = true
} else {
this.handleInput()
}
break
}
}
}
}
const proset = new ProSet()
proset.run()