Last active
March 5, 2022 11:56
-
-
Save xewl/459ad599c3964e8b64368f3756da2f18 to your computer and use it in GitHub Desktop.
Stripped down Discord Minesweeper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// http://thisismywww.com/blog/2018/09/minesweeper-in-php/ | |
// edited the above for some Discord fun. | |
// Note: 9x9 is prolly maximum grid | |
// 10x10 will leave off the last emoji for some reason | |
class MineSweeper { | |
// private $spacer = '🟥'; | |
private $empty = '🟦'; | |
private $bomb = '💣'; | |
private $caps = ['0️⃣','1️⃣','2️⃣','3️⃣','4️⃣','5️⃣','6️⃣','7️⃣','8️⃣','9️⃣','🔟']; | |
public $grid_reference = []; | |
public $cell_values = []; | |
public $mine_cells = []; | |
public $num_rows; | |
public $num_cols; | |
public $num_mines; | |
public function __construct( | |
$num_rows = 10, | |
$num_cols = 10, | |
$num_mines = 5 | |
) | |
{ | |
$this->num_rows = $num_rows; | |
$this->num_cols = $num_cols; | |
$this->num_mines = $num_mines; | |
$this->generate_grid(); | |
$this->generate_values(); | |
} | |
function generate_grid(){ | |
for ($x=10;$x<($this->num_rows+10);$x++){ | |
for ($y=10;$y<($this->num_cols+10);$y++){ | |
array_push($this->grid_reference,$x.$y); | |
} | |
} | |
} | |
function generate_values(){ | |
// Generate mine_cells and cell_values - need to know mine_cells before cell values | |
// Mine Cells are created by using the grid references, minus clicked cell then trimmed | |
$this->mine_cells = $this->grid_reference; | |
// $key = array_search($this->submitted_block, $this->mine_cells); | |
// unset($this->mine_cells[$key]); | |
shuffle($this->mine_cells); | |
$this->mine_cells = array_values(array_slice($this->mine_cells, 0, $this->num_mines)); | |
// Calculate how many mines are surrounding each cell if cell isn't a bomb | |
// If none, don't set value | |
foreach($this->grid_reference as $cell){ | |
if (!in_array($cell,$this->mine_cells)){ | |
$cells_to_check = []; | |
$cells_to_check = $this->get_surrounding_cells($cell); | |
$number = count(array_intersect($cells_to_check,$this->mine_cells)); | |
if ($number>0){ | |
$this->cell_values[$cell]=$number; | |
} | |
} | |
} | |
// On first round, make the submitted block visible | |
// $this->process_cell($this->submitted_block); | |
} | |
function get_surrounding_cells($cell){ | |
// Sloppy, but generates array of all cells surrounding the submitted cell, making sure | |
// the values are only those one in the grid reference array. | |
// also removes marked cells so it's not included in a cell which is checked. | |
$cells_to_check = []; | |
array_push($cells_to_check, substr($cell,0,2)-1 .substr($cell,2,2)-1); | |
array_push($cells_to_check, substr($cell,0,2)-1 .substr($cell,2,2)); | |
array_push($cells_to_check, substr($cell,0,2)-1 .substr($cell,2,2)+1); | |
array_push($cells_to_check, substr($cell,0,2) .substr($cell,2,2)-1); | |
array_push($cells_to_check, substr($cell,0,2) .substr($cell,2,2)+1); | |
array_push($cells_to_check, substr($cell,0,2)+1 .substr($cell,2,2)-1); | |
array_push($cells_to_check, substr($cell,0,2)+1 .substr($cell,2,2)); | |
array_push($cells_to_check, substr($cell,0,2)+1 .substr($cell,2,2)+1); | |
return array_intersect($cells_to_check,$this->grid_reference); | |
} | |
/** @test */ | |
public function generate_field() | |
{ | |
$lines = ""; | |
for ($x=10;$x<($this->num_rows+10);$x++){ | |
$lines .= ""; | |
for ($y=10;$y<($this->num_cols+10);$y++){ | |
$block = $x.$y; | |
// $lines .= $this->cell_content($block); | |
$lines .= '||'.$this->cell_content($block).'||'; | |
} | |
$lines .= "\n"; | |
} | |
return $lines; | |
} | |
function cell_content($block){ | |
$line = ''; | |
if (array_key_exists($block,$this->cell_values)){ | |
$line .= $this->color_number($this->cell_values[$block]); | |
}else{ | |
if (in_array($block,$this->mine_cells)){ | |
$line .= $this->bomb; | |
}else{ | |
$line .= $this->empty; | |
} | |
} | |
return $line; | |
} | |
function color_number($number){ | |
return $this->caps[$number]; | |
} | |
} | |
echo '<pre>', (new MineSweeper(9,9,9))->generate_field(), '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stripped down, because I used the following blog post xD:
http://thisismywww.com/blog/2018/09/minesweeper-in-php/