How to resolve the algorithm 2048 step by step in the PHP programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 2048 step by step in the PHP programming language
Table of Contents
Problem Statement
Implement a 2D sliding block puzzle game where blocks with numbers are combined to add their values.
The name comes from the popular open-source implementation of this game mechanic, 2048.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 2048 step by step in the PHP programming language
The provided PHP code implements a console-based game of 2048. Here's how it works:
-
Game Initialization:
- The game starts by initializing a new instance of the
Game
class. - It creates a 4x4 grid (
field
), sets the winning score to 2048, and places a random number (2 or 4) in an empty spot on the grid.
- The game starts by initializing a new instance of the
-
Game Loop:
- The game enters an infinite loop that continues until the game is won or lost.
- In each iteration, the user is prompted to enter a command (
W
,A
,S
,D
, orQ
).
-
Command Processing:
- The user's command is processed:
W
,A
,S
,D
: These commands move the numbers on the grid in the specified direction (up, left, down, right).Q
: Exits the game.
- After processing the command, the game checks if any changes were made to the grid. If yes, it proceeds to the next step.
- The user's command is processed:
-
Adding a New Number:
- After a valid move, a random number (2 or 4) is added to an empty spot on the grid.
-
Game End Conditions:
- Win: If any number on the grid reaches the winning score (2048), the game ends with a "You Win!" message.
- Lose: If there are no valid moves left and the winning score has not been reached, the game ends with a "No options left!, You Lose!!" message.
-
Score and Error Handling:
- The game tracks the score, which is increased whenever two numbers of the same value merge.
- Error messages are displayed if an invalid command is entered or if the game ends.
-
Grid Rendering:
- The grid is rendered to the console, displaying the numbers and their positions.
- Vertical and horizontal separators are used to create a visually appealing representation of the grid.
-
User Input:
- The
readline_callback_handler_install
andreadline_callback_handler_remove
functions are used to read user input without echoing it to the console.
- The
Source code in the php programming language
<?php
$game = new Game();
while(true) {
$game->cycle();
}
class Game {
private $field;
private $fieldSize;
private $command;
private $error;
private $lastIndexX, $lastIndexY;
private $score;
private $finishScore;
function __construct() {
$this->field = array();
$this->fieldSize = 4;
$this->finishScore = 2048;
$this->score = 0;
$this->addNumber();
$this->render();
}
public function cycle() {
$this->command = strtolower($this->readchar('Use WASD, q exits'));
$this->cls();
if($this->processCommand()) {
$this->addNumber();
} else {
if(count($this->getFreeList()) == 0 ) {
$this->error = 'No options left!, You Lose!!';
} else {
$this->error = 'Invalid move, try again!';
}
}
$this->render();
}
private function readchar($prompt) {
readline_callback_handler_install($prompt, function() {});
$char = stream_get_contents(STDIN, 1);
readline_callback_handler_remove();
return $char;
}
/**
* Insert a number in an empty spot on the field
*/
private function addNumber() {
$freeList = $this->getFreeList();
if(count($freeList) == 0) {
return;
}
$index = mt_rand(0, count($freeList)-1);
$nr = (mt_rand(0,9) == 0)? 4 : 2;
$this->field[$freeList[$index]['x']][$freeList[$index]['y']] = $nr;
return;
}
/**
* @return array(array('x' => <x>, 'y' => <y>)) with empty positions in the field
*/
private function getFreeList() {
$freeList = array();
for($y =0; $y< $this->fieldSize;$y++) {
for($x=0; $x < $this->fieldSize; $x++) {
if(!isset($this->field[$x][$y])) {
$freeList[] = array('x' => $x, 'y' => $y);
} elseif($this->field[$x][$y] == $this->finishScore) {
$this->error = 'You Win!!';
}
}
}
return $freeList;
}
/**
* Process a command:
* @return is the command valid (Did it cause a change in the field)
*/
private function processCommand() {
if(!in_array($this->command, array('w','a','s','d','q'))) {
$this->error = 'Invalid Command';
return false;
}
if($this->command == 'q') {
echo PHP_EOL. 'Bye!'. PHP_EOL;
exit;
}
// Determine over which axis and in which direction we move:
$axis = 'x';
$sDir = 1;
switch($this->command) {
case 'w':
$axis = 'y';
$sDir = -1;
break;
case 'a':
$sDir = -1;
break;
case 's':
$axis = 'y';
break;
case 'd':
break;
}
$done = 0;
// shift all numbers in that direction
$done += $this->shift($axis, $sDir);
// merge equal numbers in opposite direction
$done += $this->merge($axis, $sDir * -1);
// shift merged numbers in that direction
$done += $this->shift($axis, $sDir);
return $done >0;
}
private function shift($axis, $dir) {
$totalDone = 0;
for($i = 0; $i <$this->fieldSize; $i++) {
$done = 0;
foreach($this->iterate($axis,$dir) as $xy) {
if($xy['vDest'] === NULL && $xy['vSrc'] !== NULL) {
$this->field[$xy['dX']][$xy['dY']] = $xy['vSrc'];
$this->field[$xy['sX']][$xy['sY']] = NULL;
$done++;
}
}
$totalDone += $done;
if($done == 0) {
// nothing to shift anymore
break;
}
}
return $totalDone;
}
private function merge($axis, $dir) {
$done = 0;
foreach($this->iterate($axis,$dir) as $xy) {
if($xy['vDest'] !== NULL && $xy['vDest'] === $xy['vSrc']) {
$this->field[$xy['sX']][$xy['sY']] += $xy['vDest'];
$this->field[$xy['dX']][$xy['dY']] = NULL;
$this->score += $this->field[$xy['sX']][$xy['sY']];
$done ++;
}
}
return $done;
}
/**
* @return array List of src, dest pairs and their values to iterate over.
*/
private function iterate($axis, $dir) {
$res = array();
for($y = 0; $y < $this->fieldSize; $y++) {
for($x=0; $x < $this->fieldSize; $x++) {
$item = array('sX'=> $x,'sY' => $y, 'dX' => $x, 'dY' => $y, 'vDest' => NULL,'vSrc' => NULL);
if($axis == 'x') {
$item['dX'] += $dir;
} else {
$item['dY'] += $dir;
}
if($item['dX'] >= $this->fieldSize || $item['dY'] >=$this->fieldSize || $item['dX'] < 0 || $item['dY'] < 0) {
continue;
}
$item['vDest'] = (isset($this->field[$item['dX']][$item['dY']]))? $this->field[$item['dX']][$item['dY']] : NULL;
$item['vSrc'] = (isset($this->field[$item['sX']][$item['sY']]))? $this->field[$item['sX']][$item['sY']] : NULL;
$res[] = $item;
}
}
if($dir < 0) {
$res = array_reverse($res);
}
return $res;
}
/// RENDER ///
/**
* Clear terminal screen
*/
private function cls() {
echo chr(27).chr(91).'H'.chr(27).chr(91).'J';
}
private function render() {
echo $this->finishScore . '! Current score: '. $this->score .PHP_EOL;
if(!empty($this->error)) {
echo $this->error . PHP_EOL;
$this->error = NULL;
}
$this->renderField();
}
private function renderField() {
$width = 5;
$this->renderVSeperator($width);
for($y =0; $y < $this->fieldSize; $y ++) {
for($x = 0;$x < $this->fieldSize; $x++) {
echo '|';
if(!isset($this->field[$x][$y])) {
echo str_repeat(' ', $width);
continue;
}
printf('%'.$width.'s', $this->field[$x][$y]);
}
echo '|'. PHP_EOL;
$this->renderVSeperator($width);
}
}
private function renderVSeperator($width) {
echo str_repeat('+'. str_repeat('-', $width), $this->fieldSize) .'+' .PHP_EOL;
}
}
You may also check:How to resolve the algorithm Arrays step by step in the PL/I programming language
You may also check:How to resolve the algorithm Stream merge step by step in the C programming language
You may also check:How to resolve the algorithm Perfect shuffle step by step in the SETL programming language
You may also check:How to resolve the algorithm Empty string step by step in the Scala programming language
You may also check:How to resolve the algorithm OpenGL step by step in the Julia programming language