Jump to content

PHP Game


zppblood

Recommended Posts

I'll describe a project that we all can work on (including me), and the code has to follow what the user says. Once you have made the code, post it in the codebox along with what our next project will be. This is for to gain knowledge in PHP (try to compare yours with the code that is posted), and the project included can be something you are trying to work on now.Project:Pascal's Triangle* User submits INTEGER on number of rows to show via GET method.* Minimum value 1, Maximum value 13.* Must be centered using CSS.* Page only needs what is stated above (not have to be valid).* You do not have to do this, but can help you in the learning process: On each row you can place "(x+y)2 = 1x2 + 2xy + 1y2" Ones may be omittedThe result of it must look at least look like this:http://en.wikipedia.org/wiki/Pascal's_...le#The_triangle

Link to comment
Share on other sites

The project above might be too tough for some of the beginners of php, so I will think of another one for you guys. So I hope this one is a little easier.Project:You have a store on your site* You have a list of items on your front page in a 3x2 table.* To view an item the user must click on the thumbnail of that item, and has a bigger picture of the item, with a description, a textbox/drop down list for the user to enter how many they want, the price, and a link to direct them to the items they have in their cart.* To retrieve the information above, the URL has to be similar to http://www.example.com/index.php?id=10* On the cart page, the user will have all of the items displayed for them, plus the total.* You do not have to have this, but is recommended Click on a submit button, and get a "tracking id" that is made from a hash of their IP Address. A user gets a page with a form where they can enter their tracking id and see all their items in it.

Link to comment
Share on other sites

I like the Pascal's triangle problem. What's really interesting is how you can make fractals from it. The fractals are made by coloring factors, so if you colored every factor of 2 it would produce this pattern:http://manchine.net/w3/pascal.php?i=150&n=2Oddly enough, this doesn't look so great in Opera, it looks like crap in Firefox, and it's perfect in IE6. Opera doesn't even display the right color for some squares.So every factor of 3, 4, 5, etc produces a different pattern:http://manchine.net/w3/pascal.php?i=150&n=3http://manchine.net/w3/pascal.php?i=150&n=4http://manchine.net/w3/pascal.php?i=150&n=5http://manchine.net/w3/pascal.php?i=150&n=6etc. In that script, i is the number of rows, and n is the factor to color. If you leave out n or set it to 0, instead of coloring it will display the numbers in the triangle. The triangle with numbers gets pretty wide though, so values of i greater then 28 or 29, depending on your resolution, may squish the triangle. Here's an example with the numbers:http://manchine.net/w3/pascal.php?i=20If anyone wants to see the code or make changes, here it is:

<?php// max number of rows$i = intval($_GET['i']);// optional factors to highlight$n = intval($_GET['n']);?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html><head><title>Pascal's Triangle</title></head><?php #' ?><style type="text/css">table { display: inline; border: none;}.num { display: inline; margin: 0; padding: 0; <?phpif ($n == 0){  //adjust the cell width to make sure every cell is the same size  if ($i > 0 && $i <= 5)  //1 digit numbers    echo "width: 10px; font-size: 8px;";  elseif ($i > 5 && $i <= 9)  //2 digit numbers    echo "width: 15px; font-size: 8px;";  elseif ($i > 9 && $i <= 13) //3 digit numbers    echo "width: 20px; font-size: 8px;";  elseif ($i > 13 && $i <= 16) //4 digit numbers    echo "width: 25px; font-size: 8px;";  elseif ($i > 16 && $i <= 20) //5 digit numbers    echo "width: 30px; font-size: 8px;";  elseif ($i > 20 && $i <= 23) //6 digit numbers    echo "width: 35px; font-size: 8px;";  else //more then 6 digits    echo "width: 40px; font-size: 8px;";}else  echo "width: 2px; font-size: 1px; line-height: 2px; height: 2px;";?>}.norm { color: #000000; background: #E0E0E0; }.lite { color: #808080; background: #000000; }</style><body><div style="text-align: center;"><?php$last = array();for ($r = 0; $r < $i; $r++){  if (count($last) == 0)  {    echo "<table><tr><td class=\"num" . ($n > 0 ? " norm" : "") . "\">" . ($n > 0 ? " " : "1") . "</td></tr></table><br />";    $last[0] = 1;  }  else  {    $row = array();    for ($j = 0; $j < count($last); $j++)    {      if (!$j)        $row[$j] = 1;      else        $row[$j] = $last[$j] + $last[$j - 1];    }    $row[$j] = 1;    $last = $row;    echo "<table><tr>";    for ($j = 0; $j < count($row); $j++)    {      echo "<td class=\"num";      if ($n > 0)        echo ($row[$j] % $n ? " norm" : " lite");      echo "\">" . ($n > 0 ? " " : $row[$j]) . "</td>";    }    echo "</tr></table><br />";  }}?></div></body></html>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...