system("stty -icanon");//this line will cause php to recive every single character typed into terminal without//pressing enter, it is not the best way of doing this and limits usage of this program//to linux but it works

include"sokoban.php";//this file contains class handling the game logicinclude"cliClass.php";//this file contains class extending sokoban class and adding draw functioninclude"mapFromBase.php";//definition of a single function that converts maps from string format to one that//sokoban class uses

$game=new cliClass();//declare a new sokoban object (cliClass is child of sokoban)$game->loadMap(mapFromBase("BggEBgYGCQkGCQkQAAAAAKiqqip4VVUtWFVVJVhVVSVYVVUlWFVVJViVViVYlVYlWFVVJVhVVSVYVVUlWFVVJXhVVS2oqqoqAAAAAA=="));

while($c=fread(STDIN,1)){//iterate through all keys pressed by the userif("w"==$c||"a"==$c||"s"==$c||"d"==$c){//filter out anything but WASD$game->move($moves[$c]);//invoke move method with proper paramecho$game->draw();//and print next frameif($game->check())break;}}

?>

It starts with a game initialization ("new cliClass()") and map loading ("$game->loadMap()"). Then we have a simple loop over upcoming datas, that processes them and passes to game object ("$game->move()"). The whole file is just to make the actual code running.

The sokoban.php contains sokoban class that has following public methods:

The move method invokes isFree, which checks if the player is trying to stand on a wall (which is forbidden) or if is trying to push two stones at once (forbidden as well). Both move and isFree methods invokes translateMoveDirection which is doing simple calculations to determine position of processed elements.

My first concerns concern the algorithm of move method and those invoked by it. The move method saves its parameter in a class field instead of passing it to invoked methods. This is due to the fact that I was trying to save memory (every invoked method uses this parameter so passing it all the time would cause the interpreter to copy the parameter a few times for no reason, in my way it is stored in a field once). Is it worth it or should the invoked methods (isFree, translateMoveDirection) takes the data as a parameter?

Another thing is it worth to make translateMoveDirection static, since it would return the value based only on the parameter not on an object internal state? My general question (at least on that file) is: is it possible to make it more object oriented, to simplify the whole? I was wondering if it would be possible to somehow break it to separated classes representing game, map and stones?

There is no much to say, it's basically a class extending sokoban class and adding draw method, that is returning a string that printed to a terminal represents a game "frame". It is more for debugging purposes but I would like to ask if this way of adding functionalities to existing classes is a good way to do OOP?

It stores a single function mapFromBase that is used to convert from one (string) format to another (array) datas about map (shape of the walls, stones, X's) and I have no idea how to do it properly. I would like my code to be prepared to create more formats and also keep it independent from the cliClass. I guess I have to use some pattern like factory or builder, but I am really confused by the choice so I ask for any advice on that as well.

In general I ask for any advices and comments on what can be done to make my code being better quality. I'll be very grateful for any help!

Last edited by requinix on Mon Mar 02, 2015 4:32 pm, edited 1 time in total.

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum