-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode.py
More file actions
68 lines (53 loc) · 1.53 KB
/
code.py
File metadata and controls
68 lines (53 loc) · 1.53 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
// https://en.wikipedia.org/wiki/Peg_solitaire
// English peg solitaire board (European adds 4 pegs, 1 in each corner)
// Written for readability, not performance
// please read
https://amir.rachum.com/blog/2012/08/25/you-cant-handle-the-truth/ first
arr=[]
arr.append[none,none,true,true,true,none,none]
arr.append[none,none,true,true,true,none,none]
arr.append[true,true,true,true,true,true,true]
arr.append[true,true,true,false,true,true,true]
arr.append[true,true,true,true,true,true,true]
arr.append[none,none,true,true,true,none,none]
arr.append[none,none,true,true,true,none,none]
h=0 // horizontal
v=1 // vertical
solution=[]
def peg(x,y):
return arr[x+3][y+3]
def step(x,y,hv):
if possible(x,y,hv):
step_in(x,y,hv)
if len(solution)<30
nextsteps(x,y,hv)
else
print_first_solution()
quit() // this available?
step_back(x,y,hv)
def possible(x,y,hv):
if peg(x,y)==FALSE return false
if hv=h:
return peg(x-1,y) xor peg(x+1,y)
else:
return peg(x,y-1) xor peg(x,y+1)
def nextsteps(x,y,hv):
//6 lines of if-then written down on paper?
//max 13 next steps, depending on x,y,hv
def step_in(x,y,hv):
invert3(x,y,hv)
solution.append([x,y,hv])
def step_back(x,y,hv):
invert3(x,y,hv)
solution.pop()
def invert3(x,y,hv):
invertPeg(x,y)
if hv==h
invertPeg(x-1,y)
invertPeg(x+1,y)
else
invertPeg(x,y-1)
invertPeg(x,y+1)
def invertPeg(x,y):
arr[x+3][y+3]=NOT(arr[x+3][y+3])
step(1,0,h)