Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100 off


PHP Tutorials and Scripts   




Title: Minesweeper PHP Class    Marked Cool    (Review this resource)
Author: kaklz
Posted On: 2004-12-14
Category: Home > PHP Classes

Popularity: 1 points out of 10    

Description: Ever wanted to create your own Minesweeper game on the web? Using this class you will be able to create one within minutes! Demo included and available on the web.

Total Hits: 1100     Total Votes: 2     Total Points: 10 (3 reviews)    

Page Navigation:  [1]


Minesweeper PHP class What is minesweeper?

The goal of the game is to find where are all the mines within a game field. There are no rules that limit the size of mine field, however there are some typical sizes, like 10x10 fields, with 10 mines hidden, 16x16 with 40 mines hidden and so on. To help you, the game shows a number in a square which tells you how many mines there are adjacent to that square. For instance, supose the following 4x4 field with 2 mines (which are represented by an * character):

*...
....
.*..
....

If we would represent the same field placing the hint numbers described above, we would end up with:

*100
2210
1*10
1110

As you may have already noticed, each square may have at most 8 adjacent squares.

What this class offers?

This class offers you a really simple way for creating your own Minesweeper games with a game field size of your choice.

How does it work?

I've created a sample minesweeper game for you:


<?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="lv" lang="lv">
<head>
    <title>MineSweeper PHP Class demonstration</title>
    <style type="text/css">
        body{font-family: verdana; font-size: 16px;}
        td{text-align:center;width: 20px;}
        td.unchecked{background-color: #A7A7A7;}
        td.unchecked a{font-weight: bold;color: black;}
        td.checked{background-color: silver;}
        td.bomb{background-color:black;color: white;}
        td.found_bomb{background-color: green;color: white;}

        table{border-collapse: collapse;}
        h1{font-size: 18px;}
    </style>
</head>
<body>
<h1>Minesweeper PHP Class demonstration</h1>
<p><a href="?newgame=1">New game</a></p>

<?php
    require_once('minesweeper.php');

    // create minesweeper gamefield object
    $ms = new Minesweeper(8, 8, 10);


    // let's check if we have a new game or continiue existing
    if (!isset($_REQUEST['newgame'])){
        $ms -> Restore($_SESSION['minesweep']);
    }else{
        $ms -> InitField();

        $ms -> BuildField();
    }

    // is player checking a field for mines?
    if (!empty($_REQUEST['x']) && !empty($_REQUEST['y'])){

        $ms -> CheckField($_REQUEST['x'], $_REQUEST['y']);
    }

    // print the game field
    echo $ms -> BuildXHTML();


    // save game field variables into session for later use. You can also
    // use database or any other type of storage there.
    $_SESSION['minesweep'] = $ms -> DumpVars();

    // Let's print number of fields left
    if ($ms -> FieldsOpen - $ms -> MineCount != 0){

        echo '<p>'. ($ms -> FieldsOpen - $ms -> MineCount) . ' fields left</p>';

    }else{
        echo '<p>No fields left!</p>';
    }
?>
</body>
</html>

See minesweeper class in action

I've set up this class demonstration on my homepage: http://3a3-interactive.net/games/php_contest_minesweeper/index.php?newgame=1

What's inside download.zip?

minesweeper.php - Minesweeper PHP class.
index.php - Demonstration of minesweeper PHP class (the same as you can see above).

About author

Ingus Rukis, http://www.wisecms.com, ingus.rukis@gmail.com. I've been working as a PHP programmer for a while (some 3 years, I guess) and on my spare time I enjoy writing some simple and easy tutorials for beginners. I mostly write in Latvian, that's my mother language, so if you notice some spelling or grammar mistakes, please excuse me for that.

P.S. if you like the class of find it useful, feel free to vote, or leave comments.



Page Navigation:  [1]



© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.