Sokoban: Difference between revisions

Content added Content deleted
(Sokoban en Befunge)
(Added 11l)
Line 17: Line 17:
For more information, see [http://www.sokobano.de/wiki/index.php?title=Main_Page the Sokoban wiki].
For more information, see [http://www.sokobano.de/wiki/index.php?title=Main_Page the Sokoban wiki].



=={{header|11l}}==
{{trans|Python}}

<lang 11l>[String] data
V nrows = 0
V px = 0
V py = 0
V sdata = ‘’
V ddata = ‘’

F init(board)
:data = board.split("\n")
:nrows = max(:data.map(r -> r.len))

V maps = [‘ ’ = ‘ ’, ‘.’ = ‘.’, ‘@’ = ‘ ’, ‘#’ = ‘#’, ‘$’ = ‘ ’]
V mapd = [‘ ’ = ‘ ’, ‘.’ = ‘ ’, ‘@’ = ‘@’, ‘#’ = ‘ ’, ‘$’ = ‘*’]

L(row) :data
V r = L.index
L(ch) row
V c = L.index
:sdata ‘’= maps[ch]
:ddata ‘’= mapd[ch]
I ch == ‘@’
:px = c
:py = r

F push(x, y, dx, dy, &data)
I :sdata[(y + 2 * dy) * :nrows + x + 2 * dx] == ‘#’
| data[(y + 2 * dy) * :nrows + x + 2 * dx] != ‘ ’
data = ‘’
R

data[y * :nrows + x] = ‘ ’
data[(y + dy) * :nrows + x + dx] = ‘@’
data[(y + 2 * dy) * :nrows + x + 2 * dx] = ‘*’

F is_solved(data)
L(i) 0 .< data.len
I (:sdata[i] == ‘.’) != (data[i] == ‘*’)
R 0B
R 1B

F solve()
V open = Deque([(:ddata, ‘’, :px, :py)])
V visited = Set([:ddata])
V dirs = ((0, -1, ‘u’, ‘U’), ( 1, 0, ‘r’, ‘R’),
(0, 1, ‘d’, ‘D’), (-1, 0, ‘l’, ‘L’))

V lnrows = :nrows
L !open.empty
V (cur, csol, x, y) = open.pop_left()

L(di) dirs
V temp = copy(cur)
V (dx, dy) = (di[0], di[1])

I temp[(y + dy) * lnrows + x + dx] == ‘*’
push(x, y, dx, dy, &temp)
I temp != ‘’ & temp !C visited
I is_solved(temp)
R csol‘’di[3]
open.append((temp, csol‘’di[3], x + dx, y + dy))
visited.add(temp)
E
I :sdata[(y + dy) * lnrows + x + dx] == ‘#’ | temp[(y + dy) * lnrows + x + dx] != ‘ ’
L.continue

temp[y * lnrows + x] = ‘ ’
temp[(y + dy) * lnrows + x + dx] = ‘@’

I temp !C visited
I is_solved(temp)
R csol‘’di[2]
open.append((temp, csol‘’di[2], x + dx, y + dy))
visited.add(temp)

R ‘No solution’

V level =
‘#######
# #
# #
#. # #
#. $$ #
#.$$ #
#.# @#
#######’
init(level)
print(level"\n\n"solve())</lang>

{{out}}
<pre>
#######
# #
# #
#. # #
#. $$ #
#.$$ #
#.# @#
#######

ulULLulDDurrrddlULrruLLrrUruLLLulD
</pre>


=={{header|Befunge}}==
=={{header|Befunge}}==
Line 47: Line 152:
########
########
</lang>
</lang>



=={{header|C}}==
=={{header|C}}==