This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Incognito's profile picture

Turtle Sokoban

Started by Incognito, 31 May 2013 - 06:44 PM
Incognito #1
Posted 31 May 2013 - 08:44 PM
Hey everyone

I just found out about CC recently after learning about lua through another game and looking around for other things with lua integrated similarly. After fooling around a bit with computers and turtles, I saw that nobody had created anything like this yet (or at least didn't share it), so in an effort to learn lua and CC, I created this program.

Basically, this program allows a turtle to create a sokoban level. It reads the level from a file, parses it, and builds it. After it's done being built, the turtle returns to the start position of the level, waiting for a wireless connection from a computer to begin playing the level. The turtle can build the levels vertically or horizontally, and can play them either way as well. Everything described works as it's supposed to, but it's not optimized completely. Building horizontally is faster than vertically, and playing the game isn't exactly fast-paced, because the turtle itself actually moves around to determine valid moves. It might be worth mentioning that the levels can be built with whatever materials you wish, you just need to put them in the correct columns in the turtle. The first inventory column is for the walls, the second is for boxes, the third is for goals, and the fourth is for boxes that are on top of goals. It is probably a good idea to make sure the inventory has at least one block of each type in the first row before beginning playing, as it continues use of these blocks to determine valid moves and when the game has been won.

The computer connecting to the turtle is expected to have 5 buttons connected to it, 4 for directions (up, down, left, and right), and a reset button (allows the computer to stop waiting for commands, and tells the turtle to stop listening to commands if it's connected). At this point in time, the inputs to the computer are hard coded, but I'm planning on making it able to be customized. Where each input is expected to be is described in the corresponding file (described below). EDIT: You can now control the turtle through the computer by using the arrow keys instead of buttons (see below)

There are 85 levels included, the format is condensed, each line is considered a different level. Vertical pipes ( | ) separate rows of the level, and different symbols correspond to the objects in the game: (-) = space; (#) = wall; ($) = box; (.) = goal; (@) = player start; (*) = box on goal; (+) = player start on goal (i think that's all). Numbers preceding a symbol tell how many of the next symbol to repeat.

There are 3 files: sokoban, sokoban_controller, and default.sok. sokoban is the main program, run this on the turtle. sokoban_controller is for the computer. default.sok is the file with the 85 levels. I made a new folder in the 'rom/programs/' directory called 'sokoban', and the path in sokoban near the top of the file reflects that. If you put the files in a different location, just change it here, or it won't be able to find the default levels if it can't read a file you specify. More detailed descriptions of each file and how to set up the computer for connecting with the turtle are in comments at the beginning of the files. If you need help getting the computer set up right, I can post a screenshot of how I did it. But anyway, here are the files:

http://pastebin.com/LQLuXKk5 - sokoban
http://pastebin.com/naxCyVLC - sokoban_controller
http://pastebin.com/V9detkFJ - default.sok
http://imgur.com/a/Fux7b - screenshots, including levels and button setup for the computer

EDIT: I forgot to mention, to use the program, simply cd to the folder with the file, and call it with the syntax 'sokoban <level> [path] [-v]', where level is a positive integer, path is an optional file path to the file you want to load the level from (instead of the default), and -v is the option to build the level vertically. For the computer, simply call 'sokoban_controller <port> <turtleID> [-d/b]', where port is the position you put the wireless modem, turtleID is the ID of the turtle you want to connect to, and -d or -b is the option to use the arrow keys instead of buttons or both methods, respectively. Currently, the button inputs are hard coded, so the only position you can put the wireless modem is 'top', unless you use -d, then you can put it wherever you want.

The main attraction here is the turtle building and allowing playthrough of the level, but if the main sokoban program is run on a computer (not sokoban_controller), it prints out the level to the console. This can help with debugging if you designed your own levels, but eventually I may make it possible to play through the console. My main focus was getting the turtle to build and be able to play the levels, and this is what I present to you today.

Try it out, find some bugs for me to fix, ask me some questions, suggestions, complaints, or whatever, and I'll do my best to respond.

Have fun!
unobtanium #2
Posted 01 June 2013 - 06:42 AM
Well, this is really new and very well done as well. I really would like to see ingame screenshots, even i know what Sokoban is, but some may dont ;D
What about one more program which would remove the levels after i played them and want to get rid of them?
Incognito #3
Posted 01 June 2013 - 04:05 PM
Sure thing, here's some screenshots: http://imgur.com/a/Fux7b

I was planning on doing something with destroying levels, either to remove it completely, or to build a new level in the same place, that way instead of having to rebuild it all, it can re-use blocks already in the right places. I'm not sure how I'd make that work for the vertical levels, but off the top of my head, one way I could do it would be really slow, and it'd probably be faster to destroy and rebuild in that case.

And for those who don't know what sokoban is: http://en.wikipedia.org/wiki/Sokoban

Thanks for your input :)/>
unobtanium #4
Posted 01 June 2013 - 04:30 PM
Would it be easier to let the player insert where the turtle should go in the computer itself? The wiring seems to be a bit wrong, because the computer allready gives you the possibility of doing this.
Incognito #5
Posted 01 June 2013 - 04:42 PM
It is possible to do that, but then they'd have to go into the computer and type in the command or whatever to move the turtle. I thought it'd just be easier to just let a button do the work, and then they wouldn't have to constantly be going into the computer and back to see the level after each move.
unobtanium #6
Posted 01 June 2013 - 04:53 PM
Yeah, i know :D/>
What about let the player choose from these possible input ways? One version with the redstone controlled thingy and one with the keypress. Or if you get it to work with pulling Events in one version it would be very neat as well :P/>
Incognito #7
Posted 01 June 2013 - 05:37 PM
I'll see what I can come up with. I'll modify the sokoban_controller and add an optional -d (direct) parameter to use arrow keys in the computer instead of buttons. When I finish I'll update the OP with the updated file.

EDIT: http://pastebin.com/naxCyVLC, updated the OP, use -d to enable keys instead of buttons, arrow keys to move, enter key to disconnect. Use -b to use both buttons, or the arrow keys.
unobtanium #8
Posted 01 June 2013 - 06:52 PM
There might be a way, but i didnt tested it yet.
You are allready using os.pullEvent("redstone") and os.pullEven("key").
If you just leave it like this it might work as well:

local event, code = os.pullEvent()
if event == "redstone" then
-- do rednet stuff
elseif event == "key" then
-- do key stuff
end

Might work with your setup allready or you have to change how to detect on which side the redstone changed.
Incognito #9
Posted 01 June 2013 - 07:01 PM
Yep, that's probably a better way than I did it, I used parallel, I just captured the key from pullEvent("key") in the method that I created to use in waitForAny. I updated the OP with the new code
unobtanium #10
Posted 02 June 2013 - 05:32 AM
using pullEvent for any action is usefull in this case, but i experimented with rednet the last couple days and a keypress while receiving a rednet message messed the whole program up. I think in this case it is going to be fine :D/>
Great that i could helped you!