検索

用php做扫雷游戏

Jun 06, 2016 pm 07:55 PM
php適切ではありませんしかし地雷除去ゲーム単純

php其实不适合做游戏,但是简单的游戏还是可以实现的,下面用php 实现简单的扫雷游戏,包含注释,代码只有167行 ?php$init = $_POST[init];//game restart$clickvalue = $_POST[clickvalue];//minesweeping$checkflag = 0;//Victory or defeat$click_count =

php其实不适合做游戏,但是简单的游戏还是可以实现的,下面用php 实现简单的扫雷游戏,包含注释,代码只有167行

<?php $init = $_POST["init"];//game restart
$clickvalue = $_POST["clickvalue"];//minesweeping
$checkflag = 0;//Victory or defeat
$click_count = 0;//clicks count
if($init == null && $clickvalue == null){//initialization
	$_POST = array();//set POST with a array
	$_POST["rows"] = 9;//set rows
	$_POST["cols"] = 9;//set cols
	$_POST["num"] = 10;//set num
	$_POST["timeshow"] = "00:00"; //set starttime
	$init = true;//set initialization
}
$rows = $_POST["rows"];//get rows
$cols = $_POST["cols"];//get cols
$num = $_POST["num"];//get num
$starttime = $_POST["starttime"];//get starttime
if($init){// is initialization
	$timeshow = "00:00";//set starttime
	$data = array();//data initialization
	for($i=0;$i<$rows;$i++){//all the rows
		for($j=0;$j<$cols;$j++){//all the cols
			$data["data".$i."_".$j] = 0;//set mine with null
			$data["open".$i."_".$j] = 0;//set node with close
		}
	}
	$i=0;//reset the index,and set the mines(Random setting)
	while($i < $num){//number of mine
		$r = rand(0,$rows - 1);//row's index
		$c = rand(0,$cols - 1);//col's index
		if($data["data".$r."_".$c] == 0){//if not a mine
			$data["data".$r."_".$c] = 100;//set the node with a mine
			$i++;
		}
	}
	for($i=0;$i<$rows;$i++){//all the rows
		for($j=0;$j<$cols;$j++){//all the cols
			if($data["data".$i."_".$j] == 100)continue;//is not a mine , set number of adjacent mines 
			$cnt = 0;
			if($i - 1 >= 0 && $j - 1 >= 0 && $data["data".($i - 1)."_".($j - 1)] == 100)$cnt++;//upper left
			if($i - 1 >= 0 && $data["data".($i - 1)."_".$j] == 100)$cnt++;//left
			if($i - 1 >= 0 && $j + 1 = 0 && $data["data".$i."_".($j - 1)] == 100)$cnt++;//upper
			if($j + 1 = 0 && $data["data".($i + 1)."_".($j - 1)] == 100)$cnt++;//upper right
			if($i + 1 9?$min:"0".$min).":".($sec>9?$sec:"0".$sec);
}else{
	$timeshow = "00:00";//if game is stop , time stop
}
function openNode($i,$j){//set nodes to open,if it is can open
	global $rows;//get the rows
	global $cols;//get the cols
	global $data;//get the data
	if($i = $rows || $j = $cols || $data["open".$i."_".$j])return;//it is not a node,or it has been opened
	$data["open".$i."_".$j] = 1;//open the node
	if($data["data".$i."_".$j] > 0)return;//need to continue?
	openNode($i - 1,$j - 1);
	openNode($i - 1,$j);
	openNode($i - 1,$j + 1);
	openNode($i,$j - 1);
	openNode($i,$j + 1);
	openNode($i + 1,$j - 1);
	openNode($i + 1,$j);
	openNode($i + 1,$j + 1);
}
?>


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>minesweeper</title>


rows:
cols
num:
"; else if($checkflag == 2)echo "game over!
"; ?>
" value=""> " value=""> ')" style="width:20px;height:20px;">



演示地址

http://www.fsanguo.comoj.com/public/minesweeper/index.php

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

SublimeText3 英語版

SublimeText3 英語版

推奨: Win バージョン、コードプロンプトをサポート!

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

SecLists

SecLists

SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

VSCode Windows 64 ビットのダウンロード

VSCode Windows 64 ビットのダウンロード

Microsoft によって発売された無料で強力な IDE エディター