Creation of a puzzle game with levels generated by the computer

Read all posts about "" game

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers