61 lines
1.8 KiB
Text
61 lines
1.8 KiB
Text
V ascii_lowercase = ‘abcdefghij’
|
||
V digits = ‘0123456789’ // to get round ‘bug in MSVC 2017’[https://developercommunity.visualstudio.com/t/bug-with-operator-in-c/565417]
|
||
|
||
V n = 3
|
||
|
||
V board = [[0] * n] * n
|
||
|
||
F setbits(&board, count = 1)
|
||
L 0 .< count
|
||
board[random:(:n)][random:(:n)] (+)= 1
|
||
|
||
F fliprow(i)
|
||
L(j) 0 .< :n
|
||
:board[i - 1][j] (+)= 1
|
||
|
||
F flipcol(i)
|
||
L(&row) :board
|
||
row[i] (+)= 1
|
||
|
||
F shuffle(board, count = 1)
|
||
L 0 .< count
|
||
I random:(0 .< 2) != 0
|
||
fliprow(random:(:n) + 1)
|
||
E
|
||
flipcol(random:(:n))
|
||
|
||
F pr(board, comment = ‘’)
|
||
print(comment)
|
||
print(‘ ’(0 .< :n).map(i -> :ascii_lowercase[i]).join(‘ ’))
|
||
print(‘ ’enumerate(board, 1).map((j, line) -> ([‘#2’.format(j)] [+] line.map(i -> String(i))).join(‘ ’)).join("\n "))
|
||
|
||
F init(&board)
|
||
setbits(&board, count' random:(:n) + 1)
|
||
V target = copy(board)
|
||
L board == target
|
||
shuffle(board, count' 2 * :n)
|
||
V prompt = ‘ X, T, or 1-#. / #.-#. to flip: ’.format(:n, :ascii_lowercase[0], :ascii_lowercase[:n - 1])
|
||
R (target, prompt)
|
||
|
||
V (target, prompt) = init(&board)
|
||
pr(target, ‘Target configuration is:’)
|
||
print(‘’)
|
||
V turns = 0
|
||
L board != target
|
||
turns++
|
||
pr(board, turns‘:’)
|
||
V ans = input(prompt).trim(‘ ’)
|
||
I (ans.len == 1 & ans C ascii_lowercase & ascii_lowercase.index(ans) < n)
|
||
flipcol(ascii_lowercase.index(ans))
|
||
E I ans != ‘’ & all(ans.map(ch -> ch C :digits)) & Int(ans) C 1 .. n
|
||
fliprow(Int(ans))
|
||
E I ans == ‘T’
|
||
pr(target, ‘Target configuration is:’)
|
||
turns--
|
||
E I ans == ‘X’
|
||
L.break
|
||
E
|
||
print(" I don't understand '#.'... Try again. (X to exit or T to show target)\n".format(ans[0.<9]))
|
||
turns--
|
||
L.was_no_break
|
||
print("\nWell done!\nBye.")
|