Jump to content

Simple PHP game - Earth & Wind & Fire


mojstermiha

Recommended Posts

Hello!

 

I was assigned to create a simple php game as a part of my grade. I'm not really a php expert and this isnt really working. I copied some of the code from this website, but this isn't really working for me. I don't really know how to solve the problem and connect that two files. 

Part 1:

<html> 
<head> 
<title>PHP based example Game - Earth & Wind & Fire (aka Paper-Scissors-Rock)</title> 
</head> 
<body> 
<center> 
<div id="game"> 
	<a href="?item=earth">Earth<br /><img src="images/sand.png" width="135" height="135" alt="Earth"></a><br /><a href="?item=wind">Wind<br /><img src="images/wind.png" width="135" height="135" alt="Wind"></a><br /><a href="?item=fire">Fire<br /><img src="images/fire.png" width="135" height="135" alt="Fire"></a><br /></div> 
</center> 
</body> 
</html>

Part 2:

<?php
 
function showComponents($items = null)
{
$pictures = 
array(
 
"earth" => '<a href="?item=earth">Earth<br /><img src="images/sand.png" width="135" height="135" alt="Earth"></a><br />',
"wind" => '<a href="?item=wind">Wind<br /><img src="images/wind.png" width="135" height="135" alt="Wind"></a><br />',
"fire" => '<a href="?item=fire">Fire<br /><img src="images/fire.png" width="135" height="135" alt="Fire"></a><br />',
 
);
 
if ($items == null) :
	foreach( $pictures as $items => $value ):
	echo $value;
	endforeach;
   else:
	echo str_replace("?item={$items}", "#", $pictures[$items]);
 
	endif;
 
}
 
function game() {
 
	if ( isset($_GET['item']) == TRUE ) :
 
		$pictures = array('earth','wind','fire');
		$playerPic = strtolower($_GET['item']);
		$computerPic = $pictures[rand(0, 2)]; 
 
		echo '<div><a href="http://mapswidgets.com/game.php">New game</a></div>';
 
		if (in_array($playerPic, $pictures) == FALSE):
			echo "Play as either Earth, Wind or Fire.";
			die;
		endif;
 
		if  ( $playerPic == 'fire' && $computerPic == 'wind' OR 
   		      $playerPic == 'earth' && $computerPic == 'fire' OR
		      $playerPic == 'wind' && $computerPic == 'earth' ):
		      echo '<h2>You Win!</h2>';
 
		endif;
 
		if  ( $computerPic == 'fire' && $playePic == 'wind' OR 
	 	      $computerPic == 'earth' && $playerPic == 'fire' OR
	              $computerPic == 'wind' && $playerPic == 'earth' ):
 
                      echo '<h2>Computer wins!</h2>';
 
		endif;
 
		if ($playerPic == $computerPic) :
  		      echo '<h2>House wins! =)</h2>';
 
		endif;
 
		showComponents($playerPic);
		showComponents($computerPic);
 
		else :
 
			showComponents();
 
		endif;
}
 
?>

Thanks for your time and help!

Link to comment
https://forums.phpfreaks.com/topic/288773-simple-php-game-earth-wind-fire/
Share on other sites

 

<?php

function showComponents($items = null)
{
$pictures =
array(

"earth" => '<a href="?item=earth">Earth<br /><img src="images/sand.png" width="135" height="135" alt="Earth"></a><br />',
"wind" => '<a href="?item=wind">Wind<br /><img src="images/wind.png" width="135" height="135" alt="Wind"></a><br />',
"fire" => '<a href="?item=fire">Fire<br /><img src="images/fire.png" width="135" height="135" alt="Fire"></a><br />',

);

if ($items == null) :
foreach( $pictures as $items => $value ):
echo $value;
endforeach;
   else:
echo str_replace("?item={$items}", "#", $pictures[$items]);

endif;

}

function game() {

if ( isset($_GET['item']) == TRUE ) :

  $pictures = array('earth','wind','fire');
  $playerPic = strtolower($_GET['item']);
  $computerPic = $pictures[rand(0, 2)];

  echo '<div><a href="http://mapswidgets.com/game.php">New game</a></div>';

  if (in_array($playerPic, $pictures) == FALSE):
   echo "Play as either Earth, Wind or Fire.";
   die;
  endif;

  if  ( $playerPic == 'fire' && $computerPic == 'wind' OR
           $playerPic == 'earth' && $computerPic == 'fire' OR
        $playerPic == 'wind' && $computerPic == 'earth' ):
        echo '<h2>You Win!</h2>';

  endif;

  if  ( $computerPic == 'fire' && $playePic == 'wind' OR
         $computerPic == 'earth' && $playerPic == 'fire' OR
               $computerPic == 'wind' && $playerPic == 'earth' ):

                      echo '<h2>Computer wins!</h2>';

  endif;

  if ($playerPic == $computerPic) :
          echo '<h2>House wins! =)</h2>';

  endif;

  showComponents($playerPic);
  showComponents($computerPic);

  else :

   showComponents();

  endif;
}

?>
<html>
<head>
<title>PHP based example Game - Earth & Wind & Fire (aka Paper-Scissors-Rock)</title>
</head>
<body>
<center>
<?php game();?>
<div id="game">
<a href="?item=earth">Earth<br /><img src="images/sand.png" width="135" height="135" alt="Earth"></a><br /><a href="?item=wind">Wind<br /><img src="images/wind.png" width="135" height="135" alt="Wind"></a><br /><a href="?item=fire">Fire<br /><img src="images/fire.png" width="135" height="135" alt="Fire"></a><br />
</div>

</center>
</body>
</html>
<?php

function showComponents($items = null)
{
$pictures =
array(

"earth" => '<a href="?item=earth">Earth<br /><img src="images/sand.png" width="135" height="135" alt="Earth"></a><br />',
"wind" => '<a href="?item=wind">Wind<br /><img src="images/wind.png" width="135" height="135" alt="Wind"></a><br />',
"fire" => '<a href="?item=fire">Fire<br /><img src="images/fire.png" width="135" height="135" alt="Fire"></a><br />',

);

if ($items == null) :
foreach( $pictures as $items => $value ):
echo $value;
endforeach;
   else:
echo str_replace("?item={$items}", "#", $pictures[$items]);

endif;

}

function game() {

if ( isset($_GET['item']) == TRUE ) :

  $pictures = array('earth','wind','fire');
  $playerPic = strtolower($_GET['item']);
  $computerPic = $pictures[rand(0, 2)];

  echo '<div><a href="http://mapswidgets.com/game.php">New game</a></div>';

  if (in_array($playerPic, $pictures) == FALSE):
   echo "Play as either Earth, Wind or Fire.";
   die;
  endif;

  if  ( $playerPic == 'fire' && $computerPic == 'wind' OR
           $playerPic == 'earth' && $computerPic == 'fire' OR
        $playerPic == 'wind' && $computerPic == 'earth' ):
        echo '<h2>You Win!</h2>';

  endif;

  if  ( $computerPic == 'fire' && $playePic == 'wind' OR
         $computerPic == 'earth' && $playerPic == 'fire' OR
               $computerPic == 'wind' && $playerPic == 'earth' ):

                      echo '<h2>Computer wins!</h2>';

  endif;

  if ($playerPic == $computerPic) :
          echo '<h2>House wins! =)</h2>';

  endif;

  showComponents($playerPic);
  showComponents($computerPic);

  else :

   showComponents();

  endif;
}

?>
<html>
<head>
<title>PHP based example Game - Earth & Wind & Fire (aka Paper-Scissors-Rock)</title>
</head>
<body>
<center>
<?php game();?>
<div id="game">
<a href="?item=earth">Earth<br /><img src="images/sand.png" width="135" height="135" alt="Earth"></a><br /><a href="?item=wind">Wind<br /><img src="images/wind.png" width="135" height="135" alt="Wind"></a><br /><a href="?item=fire">Fire<br /><img src="images/fire.png" width="135" height="135" alt="Fire"></a><br />
</div>

</center>
</body>
</html>

Thanks. How could I make it in two files: game.php and maingame.php?

You can include the file and use it the same.

 

 

<?php include('game.php');?>
<html>
<head>
<title>PHP based example Game - Earth & Wind & Fire (aka Paper-Scissors-Rock)</title>
</head>
<body>
<center>
<?php game();?>
<div id="game">
<a href="?item=earth">Earth<br /><img src="images/sand.png" width="135" height="135" alt="Earth"></a><br /><a href="?item=wind">Wind<br /><img src="images/wind.png" width="135" height="135" alt="Wind"></a><br /><a href="?item=fire">Fire<br /><img src="images/fire.png" width="135" height="135" alt="Fire"></a><br /></div>
</center>
</body>
</html>

 

You can include the file and use it the same.

<?php include('game.php');?>
<html>
<head>
<title>PHP based example Game - Earth & Wind & Fire (aka Paper-Scissors-Rock)</title>
</head>
<body>
<center>
<?php game();?>
<div id="game">
<a href="?item=earth">Earth<br /><img src="images/sand.png" width="135" height="135" alt="Earth"></a><br /><a href="?item=wind">Wind<br /><img src="images/wind.png" width="135" height="135" alt="Wind"></a><br /><a href="?item=fire">Fire<br /><img src="images/fire.png" width="135" height="135" alt="Fire"></a><br /></div>
</center>
</body>
</html>

This isn't working for me. If i use the first method i get both php and html displayed so i need to remove the html but than i cant style it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.