mojstermiha Posted May 26, 2014 Share Posted May 26, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/288773-simple-php-game-earth-wind-fire/ Share on other sites More sharing options...
QuickOldCar Posted May 26, 2014 Share Posted May 26, 2014 <?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> Quote Link to comment https://forums.phpfreaks.com/topic/288773-simple-php-game-earth-wind-fire/#findComment-1480855 Share on other sites More sharing options...
mojstermiha Posted May 26, 2014 Author Share Posted May 26, 2014 Thanks for your reply i will try this out. Quote Link to comment https://forums.phpfreaks.com/topic/288773-simple-php-game-earth-wind-fire/#findComment-1480890 Share on other sites More sharing options...
mojstermiha Posted May 26, 2014 Author Share Posted May 26, 2014 <?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? Quote Link to comment https://forums.phpfreaks.com/topic/288773-simple-php-game-earth-wind-fire/#findComment-1480900 Share on other sites More sharing options...
MDCode Posted May 26, 2014 Share Posted May 26, 2014 You should perhaps turn error reporting on. I see a syntax error in your first array. Quote Link to comment https://forums.phpfreaks.com/topic/288773-simple-php-game-earth-wind-fire/#findComment-1480929 Share on other sites More sharing options...
QuickOldCar Posted May 27, 2014 Share Posted May 27, 2014 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> Quote Link to comment https://forums.phpfreaks.com/topic/288773-simple-php-game-earth-wind-fire/#findComment-1480996 Share on other sites More sharing options...
mojstermiha Posted May 27, 2014 Author Share Posted May 27, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/288773-simple-php-game-earth-wind-fire/#findComment-1481074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.