-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.coffee
44 lines (35 loc) · 1.07 KB
/
prompt.coffee
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
stdin = process.openStdin()
stdin.setEncoding 'utf8'
inputCallback = null
stdin.on 'data', (input) ->
inputCallback input
promptForTile1 = ->
console.log "Please enter coordinates for the first tile."
inputCallback = (input) ->
promptForTile2() if strToCoordinates input
promptForTile2 = ->
console.log "Please enter coordinates for the second tile."
inputCallback = (input) ->
if strToCoordinates input
console.log "Swapping tile...done!"
promptForTile1()
GRID_SIZE = 5
inRange = (x, y) ->
0 <= x < GRID_SIZE and 0 <= y < GRID_SIZE
isInteger = (num) ->
num is Math.round(num)
strToCoordinates = (input) ->
halves = input.split(',')
if halves.length is 2
x = parseFloat halves[0]
y = parseFloat halves[1]
if !isInteger(x) or !isInteger(y)
console.log "Each coordinate must be an integer."
else if not inRange x - 1, y - 1
console.log "Each coordinate must be between 1 and #{GRID_SIZE}."
else
{x, y}
else
console.log "Input must be of the form 'x, y'."
console.log "Welcome to 5x5!"
promptForTile1()