File: 15puzzle.kbs

package info (click to toggle)
basic256 2.0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 15,076 kB
  • sloc: cpp: 16,791; yacc: 3,979; lex: 1,446; makefile: 25
file content (151 lines) | stat: -rwxr-xr-x 2,793 bytes parent folder | download | duplicates (5)
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
# 15puzzle.kbs - slide the tiles to get them back in order
# 2010-01-14 j.m.reneau
fastgraphics

nx = 4 # number of boxes in a row
ny = 4 # number of boxes in a column
bw = 5 # border width
xw = (graphwidth - ((nx+1)*bw)) / nx # calculate size of a box
yw = (graphheight - ((ny+1)*bw)) / ny
zx = 0 # position of the empty tile
zy = 0

dim board(nx, ny)

font "Tahoma", 120/nx, 100

print "slide puzzle"
print "click on tile to slide.  try to get all tiles in order."

print "shufflle..."
gosub initialboard
gosub shuffle
gosub drawboard

clickclear
moves = 0

print "click tile to move"
do
   gosub getclick
   gosub makemove
   gosub drawboard
   gosub isdone
until done

print "Game Over - You solved it in "+ moves +"."

end

shuffle: #
for t = 1 to nx * ny * 10
   cx = zx
   cy = zy
   r = int(rand*4)
   if r = 0 and cx > 0 then cx = cx - 1
   if r = 1 and cx < nx-1 then cx = cx + 1
   if r = 2 and cy > 0 then cy = cy - 1
   if r = 3 and cy < ny-1 then cy = cy + 1
   if cx<>zx or cy<> zy then
      board[zx, zy] = board[cx, cy]
      board[cx, cy] = 0
      zx = cx
      zy = cy
   end if
   gosub drawboard
   pause .01
next t
return

makemove: #
# shift cells
if (zx = cx) or (zy = cy) then
   moves = moves + 1
   if zx<>cx then
      # row shift
      if cx>zx then
         dx = 1
         dy = 0
      else
         dx = -1
         dy = 0
      end if
   else
      # column shift
      if cy>zy then
         dx = 0
         dy = 1
      else
         dx = 0
         dy = -1
      end if
   end if
   # do shift
   while zx <> cx or zy <> cy
      board[zx, zy] = board[zx+dx, zy+dy]
      board[zx+dx, zy+dy] = 0
      zx = zx + dx
      zy = zy + dy
   end while
endif
return

getclick: #
# return cx and cy for where the user clicked the board
while clickb = 0
   pause .01
end while
cx = int(clickx/(xw+bw))
if cx >= nx then cx = nx-1
cy = int(clicky/(yw+bw))
if cy >= ny then cy = ny-1
clickclear
return

initialboard: #
# setup the initial board array
for x= 0 to nx-1
   for y = 0 to ny-1
      board[x, y] = (y*nx+x+1)
   next y
next x
zx = nx-1
zy = ny-1
board[zx, zy] = 0
return

isdone: #
# return the variable done if we have solved the puzzle
done = true
for x= 0 to nx-1
   for y = 0 to ny-1
      if board[x, y] <>  (y*nx+x+1)  and (x <> zx or y <> zy) then
         done = false
         return
      end if
   next y
next x
return

drawboard: #
clg
color black
rect 0, 0, graphwidth, graphheight
for y = 0 to ny-1
   for x = 0 to nx-1
      b =  board[x, y]
      color white
      rect (x+1)*bw+x*xw, (y+1)*bw+y*yw ,xw, yw
      
      if b <> 0 then
         if zx = x or zy = y then
            color blue
         else
            color darkblue
         endif
         text (x+1)*bw+x*xw, (y+1)*bw+y*yw, b
      end if
   next x
next y
refresh
return