Jump to content

Minesweeper Project Problems.


Sn1p34

Recommended Posts

I've been haveing some problems with one of my php projects bear with me because I'm still learning and don't have a very braud knowleage on php.

 

I'm trying to get the areas that are marked as a mine with a flag not to spread, but the only way I can prevent it is by using a die command (which is not an ideal solution) for some reason it dosen't spread when the die command is there but when I don't include it the selected area spreads.

 

<?php

session_start();

if (isset($_SESSION[mine_guess])) $mine_guess = $_SESSION[mine_guess];
if (isset($_SESSION[empty_guess])) $empty_guess = $_SESSION[empty_guess];

function html_head($name) {
echo "<html><head><title>$name</title></head><body>";
}

// Sets up the board
function make_board()	{
$_SESSION = array();
session_destroy();
session_start();
$_SESSION[playing] = TRUE;
if($_POST[level]==easy)	{
	$_SESSION[board_size] = 7;
	$amount_mines = 0;
}
if($_POST[level]==medium)	{
	$_SESSION[board_size] = 12;
	$amount_mines = 25;
}
if($_POST[level]==hard)	{
	$_SESSION[board_size] = 13;
	$amount_mines = 30;
}
// Loop through board and create mines
for ($x = 1; $x <= $_SESSION[board_size]; ++$x)	{
	for ($y = 1; $y <= $_SESSION[board_size]; ++$y)	{
		$rand = rand(0,100);
		if ($rand<$amount_mines) $board[$x][$y] = "M";
	}	
}
$_SESSION[board] = $board;
}
if (isset($_POST[level])) {
# Make board
make_board();
#Delete variables
unset($mine_guess,$empty_guess);
}
html_head("Minesweeper");

// If the user is playing hide this menu
if (!$_SESSION[playing])	{
echo "<form name=levelchooser method=post action=mine.php>";
echo "Please choose a level!";
echo "<select name=level>";
echo "<option value=\"easy\">Easy</option>";
echo "<option value=\"medium\">Medium</option>";
echo "<option value=\"hard\">Hard</option>";
echo "</select>";
echo "<input type=submit value=\"New Game!\">";
echo "</form>";
}
$newgame = $_GET[level];


// Counts the players guesses
function count_guesses($empty_guess,$board_size)	{
for ($x = 1; $x <= $board_size; ++$x)	{
	for ($y = 1; $y <= $board_size; ++$y)	{
		if (isset($empty_guess[$x][$y])) $num_squares++;
		}
}
return $num_squares;
}

// Counts the players mine guesses
function mine_guesses($mine_guess,$board_size)	{
for ($x = 1; $x <= $board_size; ++$x)	{
	for ($y = 1; $y <= $board_size; ++$y)	{
		if (isset($mine_guess[$x][$y])) $mine_squares++;
		}
}
return $mine_squares;
}


// Counts the amount of mines on the board
function amount_mines($x,$y,$board_size)	{
for ($x = 1; $x <= $board_size; ++$x)	{
	for ($y = 1; $y <= $board_size; ++$y)	{
		if (is_mine($x,$y)) $num_mines++;
	}	
}
return $num_mines;
}

// Determines if the selected spot is a mine
function is_mine($x,$y) {
if ($_SESSION[board][$x][$y] == "M") return TRUE;
else return FALSE;
}

// If the player loses this function will be executed
function you_lose() {
echo "<form name=levelchooser method=post action=mine.php>";
echo "Please choose a level!";
echo "<select name=level>";
echo "<option value=\"easy\">Easy</option>";
echo "<option value=\"medium\">Medium</option>";
echo "<option value=\"hard\">Hard</option>";
echo "</select>";
echo "<input type=submit value=\"New Game!\">";
echo "</form>";
echo "<h1>You Lose!</h1>";
$square_colour[$xcord][$ycord] = "red";
$lost = TRUE;
}

// Counts the amount of mines around the box
function count_mines($x,$y)	{
// Check the surrounding squares (
if (is_mine($x-1,$y+1)) ++$number_mines;
if (is_mine($x-1,$y)) ++$number_mines;
if (is_mine($x-1,$y-1)) ++$number_mines;
if (is_mine($x,$y+1)) ++$number_mines;
if (is_mine($x,$y-1)) ++$number_mines;
if (is_mine($x+1,$y-1)) ++$number_mines;
if (is_mine($x+1,$y)) ++$number_mines;
if (is_mine($x+1,$y+1)) ++$number_mines;
	return $number_mines;
}

function spread($empty_guess,$x,$y)	{
  $empty_guess[$x][$y] = TRUE;
if (count_mines($x,$y)== 0)	{
	$empty_guess[$x-1][$y+1] = TRUE;
	#unset($mine_guess[$x-1][$y+1]);
	$empty_guess[$x-1][$y] = TRUE;
	#unset($mine_guess[$x-1][$y]);
	$empty_guess[$x-1][$y-1] = TRUE;
	#unset($mine_guess[$x-1][$y-1]);
	$empty_guess[$x][$y+1] = TRUE;
	#unset($mine_guess[$x][$y+1]);
	$empty_guess[$x][$y-1] = TRUE;
	#unset($mine_guess[$x][$y-1]);
	$empty_guess[$x+1][$y-1] = TRUE;
	#unset($mine_guess[$x+1][$y-1]);
	$empty_guess[$x+1][$y] = TRUE;
	#unset($mine_guess[$x+1][$y]);
	$empty_guess[$x+1][$y+1] = TRUE;
	#unset($mine_guess[$x+1][$y+1]);
}
#unset($mine_guess[$x][$y]);
return $empty_guess;
}

// Width and height
$width = "35px";
$height = "35px";
$x_cord = 50;
$y_cord = 50;

// If the user loses change the square to red else make it yellow
if(isset($_GET[guess])) {
$xcord = $_GET[xcord];
$ycord = $_GET[ycord];
if (is_mine($xcord,$ycord))	{
		you_lose();
		$_SESSION[game_die] = TRUE;
  }
  else {
    $empty_guess = spread($empty_guess,$xcord,$ycord);
    echo "hello";
    die;
  }
}
# Process guess
if (isset($_GET[mine])) {
$xcord = $_GET[xcord];
$ycord = $_GET[ycord];
$mine_guess[$xcord][$ycord] = TRUE;
unset($_SESSION[game_die]);
}
$yellow_squares = count_guesses($empty_guess,$_SESSION[board_size]);
$number = amount_mines($x,$y,$_SESSION[board_size]);

// Set the session board size as a variable
$board_size = $_SESSION[board_size];

// Determine if the player has won
$size_board = $board_size*$board_size;
if($_POST[level]==easy)	{
$_SESSION[boards] = $size_board-$number;
}
if($_POST[level]==medium)	{
$_SESSION[boards] = $size_board-$number;
}
if($_POST[level]==hard)	{
$_SESSION[boards] = $size_board-$number;
}
if ($_SESSION[boards] == $yellow_squares)	{
	if ($_SESSION[boards] > 0)	{
	echo "<form name=levelchooser method=post action=mine.php>";
	echo "Please choose a level!";
	echo "<select name=level>";
	echo "<option value=\"easy\">Easy</option>";
	echo "<option value=\"medium\">Medium</option>";
	echo "<option value=\"hard\">Hard</option>";
	echo "</select>";
	echo "<input type=submit value=\"New Game!\">";
	echo "</form>";
	echo "<h1>You Win!</h1>";
	$finish = TRUE;
	}
}
$spaces = $_SESSION[boards];
// echo "Empty Spaces: $spaces";

$y_cord = 220;

// If the square is set as mine, unset it if 
if ($_GET["unset"] == yes)	{
$x = $_GET["xcord"];
$y = $_GET["ycord"];
unset($mine_guess[$x][$y]);
}

// Sets up the board
for ($y = 1; $y <= $_SESSION[board_size]; ++$y)	{
$y_cord += $height;
$x_cord = 20;
for ($x = 1; $x <= $_SESSION[board_size]; ++$x)	{
	$image = "bak.png";
	$x_cord += $width;
		$mines = "";
		if ($empty_guess[$x][$y]) {
			$image = "bak_down.png";
			$mines = count_mines($x,$y);
		}
	if ($mine_guess[$x][$y]) $image = "smallflag.png";

	// find out if square is mine and $_SESSION[die] is set, if so, make colour = red
	if ($cheat==enter) {
		if (is_mine($x,$y)) $image = "mine.png";
	}
		if ($_SESSION[game_die]==TRUE)	{
			if (is_mine($x,$y)) $image = "mine.png";
		}
	// If the player loses disable the bord
		if (!$_SESSION[game_die]) {
			# if not flagged
			if (!$mine_guess[$x][$y]) {
			echo "<div onclick=\"javascript:document.location.href='mine.php?xcord=$x&ycord=$y&guess=yes';\" style=\"color: black; border: 0px solid darkgrey; background-image: url(images/$image); position: absolute; left: $x_cord; top: $y_cord; width: $width; height: $height\">";
		}
		else {
			echo "<div onclick=\"javascript:document.location.href='mine.php?xcord=$x&ycord=$y&unset=yes';\" style=\"color: black; border: 0px solid darkgrey; background-image: url(images/$image); position: absolute; left: $x_cord; top: $y_cord; width: $width; height: $height; \">";
		}
	}
	else echo "<div style=\"color: black; border: 1px solid darkgrey; background-image: url(images/bak.png); position: absolute; left: $x_cord; top: $y_cord; width: $width; height: $height\">";

	// If die equals TRUE place a image on every square with a mine
	if ($_SESSION[game_die])	{	
		if (is_mine($x,$y))	{
			echo "<div style=\"position: absolute; left: 7px; top: 7px;\"><img src=images/mine.png></div>";
		}
	}
		if (!$_SESSION[game_die])	{
			if ($mines == 5) $colour = "darkred";
			if ($mines == 4) $colour = "darkgreen";
			if ($mines == 3) $colour = "darkblue";
			if ($mines == 2) $colour = "green";
			if ($mines == 1) $colour = "blue";
			if ($empty_guess[$x][$y] AND !$mine_guess[$x][$y])
			echo "<div align=center style=\"color: $colour; position: absolute; left: 12px; top: 9px;\"><b>$mines</b></div>";
		else if (!$mine_guess[$x][$y]) echo "<a href='mine.php?xcord=$x&ycord=$y&mine=yes' align=center><img src=images/flag_corner.gif border='0'></a></div>";
		}
	echo "</div>";
}	
}


$squares_left = $size_board-$yellow_squares-$number;
$guess_mine = mine_guesses($mine_guess,$_SESSION[board_size]);

// Unsets a mine guess if possible
if ($guess_mine == FALSE)	{
$guess_mine = 0;
}
if ($yellow_squares == FALSE)	{
$yellow_squares = 0;
}

// Set up the statistics table
echo "<div style=\"position: absolute; left: 27px; top: 100px;\">";
echo "<table border=0 cellpadding=1 cellspaceing=1 bgcolor=red width=300>";
echo "<tr>";
echo "<td bgcolor=orange width=40% colspan=3>";
echo "<center><b><h2>Statistics</h2></b>";
echo "</td></tr>";
echo "<td bgcolor=red colspan=1 width=15%>";
echo "Mines: $number";
echo "</td>";
echo "<td bgcolor=orange colspan=1 width=15%>";
echo "Guessed: $yellow_squares";
echo "</td></tr>";
echo "<td bgcolor=orange colspan=1 width=15%>";
echo "Mines Gussed: $guess_mine";
echo "</td>";
echo "<td bgcolor=red colspan=1 width=15%>";
echo "Squares Left: $squares_left";
echo "</center>";
echo "</table>";
echo "</div>";

// Instructions
function instructions()  {
    echo "<div style=\"position: absolute; left: 427px; top: 35px;\">";
    echo "<h1><b>Instructions</h1></b>";
    echo "1)	Select a level from the top left menu and press new game.<br>";
    echo "2)	Click on a square if your sure there is no mine there.<br>";
    echo "3)	Click on the small flag in the top left of the square to mark it.<br>";
    echo "4)	To win the game you must uncover all the squares with no mines.";
    echo "</div>";
}

echo "<div style=\"position: absolute; left: 40px; top: 75px;\">";
if (!$finish == TRUE) {
  if (!$_SESSION[game_die]) {
    if (!$ins)  {
    $ins = "yes";
      if ($ins == yes) {
      echo "<a href=mine.php?instructions=$ins>Show Instructions</a>";
          }
        }
    }
}

$ins = $_GET[instructions];

if (!$finish == TRUE) {
  if (!$_SESSION[game_die]) {
    if ($ins==yes)  {
      $ins = "no";
      echo " <a href=mine.php?instructions=$ins>Hide Instructions</a>";
      echo "</div>";
      instructions();
        }
    }
}

if (isset($mine_guess)) $_SESSION[mine_guess] = $mine_guess;
if (isset($empty_guess)) $_SESSION[empty_guess] = $empty_guess;
if ($lost) {
$_SESSION = array();
unset($board);
session_destroy;
session_start;
make_board();
}

?>

 

 

The script can be tested here http://www.enertechdbi.com/MineUpdated/mine.php (without the debuging)

 

any help would be greatly appreciated.

 

Thanks,

 

Regards,

 

 

Cameron McCuish

Link to comment
Share on other sites

um, i havent got time to read the code, but i think that this project wuld be best done n javascript...

 

 

the only suggestion to your php version, is that you specify each button with a unique id, then random assing each id with either pressed, mine, or num.

 

then write to a database or file each time a button is pressed, to specify which ones which.

 

after thinking about it, simply write the file to wha tht e button is, butthe actuly php assingment.....

 

 

that way it keeps updateing, and you could use a die() to stop if a mine appears

 

 

in terms of the buttons being auto pressed, i think that paybe your accidently telling it to set as defult pressed, when a button is there

 

 

 

good luck

 

PC Nerd

 

 

soz if ive just repeated you code.... :-\

Link to comment
Share on other sites

I understand that the project would best be done in Java script, but I'm takeing a PHP coarse hense the reason it is coded in php  ;). I've already spoke to my teacher about this, and he was unable to figure out what was cuaseing the problem, and sugested I post it on a forum to see if anyone else would know what is wrong.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.