Talking about Nodes game, Game development and Php.
One of the things I love of (some) puzzle games is the capability of generating random possible levels.
When I mean “puzzle game” this time I am not thinking about Bejeweled or Minesweepes, which are just a bunch of random items in an array, but games with actual levels like Sokoban or, like in this post, Chain Reaction, a very old game made by a Greek developed now disappeared whose games are now hosted on JayIsGames.
Normally when we create a puzzle game level, we start from the end and step back until we have a complete set of moves (normally called walkthrough) required to solve the level.
Here’s an attempt to create random Chain Reaction levels with php:
<?php $field = array(); $width=3; $height=3; $shapes=2; $colors=2; $start_x = rand(0,$width-1); $start_y = rand(0,$height-1); $start_shape = rand(0,$shapes-1); $start_color = rand(0,$colors-1); $field[$start_y][$start_x]=array("shape"=>$start_shape,"color"=>$start_color); for($i=1;$i<$width*$height;$i++){ echo "\nstart on $start_y,$start_x with shape = $start_shape and color $start_color"; echo "\nchecking for next possible cells:"; $possible_moves = array(); for($x=0;$x<$width;$x++){ if(!$field[$start_y][$x]){ echo "\n$start_y,$x is possible"; $possible_moves[]=array("x"=>$x,"y"=>$start_y); } } for($y=0;$y<$width;$y++){ if(!$field[$y][$start_x]){ echo "\n$y,$start_x is possible"; $possible_moves[]=array("x"=>$start_x,"y"=>$y); } } if(!count($possible_moves)){ echo "\nno more cells"; break; } $next_step = rand(0,count($possible_moves)-1); echo "\nwill place on ".$possible_moves[$next_step][y].",".$possible_moves[$next_step][x]; $start_y = $possible_moves[$next_step][y]; $start_x = $possible_moves[$next_step][x]; $what_to_change = rand(0,1); switch($what_to_change){ case 0: echo "\nchanging the shape"; $start_shape = rand(0,$shapes-1); break; case 1: echo "\nchanging the color"; $start_color = rand(0,$colors-1); break; } $field[$start_y][$start_x]=array("shape"=>$start_shape,"color"=>$start_color); } for($y=0;$y<$height;$y++){ for($x=0;$x<$width;$x++){ if($x==0){ echo "\n+"; } echo "--+"; } for($x=0;$x<$width;$x++){ if($x==0){ echo "\n|"; } if($field[$y][$x]){ echo $field[$y][$x][shape].$field[$y][$x][color]."|"; } else{ echo " |"; } } } for($x=0;$x<$width;$x++){ if($x==0){ echo "\n+"; } echo "--+"; } ?>
If you run the script, you’ll generate something like that:
start on 2,1 with shape = 0 and color 0 checking for next possible cells: 2,0 is possible 2,2 is possible 0,1 is possible 1,1 is possible will place on 2,0 changing the shape start on 2,0 with shape = 1 and color 0 checking for next possible cells: 2,2 is possible 0,0 is possible 1,0 is possible will place on 0,0 changing the color start on 0,0 with shape = 1 and color 0 checking for next possible cells: 0,1 is possible 0,2 is possible 1,0 is possible will place on 0,1 changing the shape start on 0,1 with shape = 0 and color 0 checking for next possible cells: 0,2 is possible 1,1 is possible will place on 0,2 changing the color start on 0,2 with shape = 0 and color 0 checking for next possible cells: 1,2 is possible 2,2 is possible will place on 2,2 changing the shape start on 2,2 with shape = 1 and color 0 checking for next possible cells: 1,2 is possible will place on 1,2 changing the shape start on 1,2 with shape = 0 and color 0 checking for next possible cells: 1,0 is possible 1,1 is possible will place on 1,1 changing the color start on 1,1 with shape = 0 and color 0 checking for next possible cells: 1,0 is possible will place on 1,0 changing the color +--+--+--+ |10|00|00| +--+--+--+ |01|00|00| +--+--+--+ |10|00|10| +--+--+--+
Which is a playable and solveable Chain Reaction level, the first number in the cell should be the shape and the second number is the color.
If you get some practice with the original game you should be able to play these randomly generated levels too.
I am going to create a little browser game out of it, so you will see the concept in a real world example.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.