I am taking a class on web design and I must get three projects
finished before the term ends. The problem with that is I have to go
and have heart sugery next week. I am goinf to post a source code
and the questions. Please see if you can help me any at all. God
Bless you. Here is the code:
<html>
<head>
<title>Battleship</title>
<style type="text/css">
table {color: black; font-weight: bold}
</style>
</head>
<body bgcolor="#CCCCCC">
<img src="battleshipgray.gif" /><br><br>
<b>Objective of the Game:</b> Sink other players ships. (1-carrier
[5], 1-battleship[4], 2-subs[3] & 2-pt[2])<br>
<b>How to Play:</b> Select a radio button and click fire. A hit
will be shown as a red star; a miss will be shown as a blue square.
Each player will alternate turns.<br>
<b>How to Win:</b> First player to hit(sink) all ships belonging to
other player wins game.
<p>
<form action="battleship4.php" method="post">
<?
// ******** first time through
if (!$fire) $fire = "Play Again?";
// ******** first time through or Play Again button clicked
if ($fire == "Play Again?") {
global $fn1, $fn2, $guessArray1, $guessArray2;
// ******* select 1 of 8 ship placement text files using random
number - rand(1,8)
$rand1 = rand(1,8);
$rand2 = rand(1,8);
// $fn1 = "ship".$rand1.".txt"; //****** example: ship4.txt
// $fn2 = "ship".$rand2.".txt";
$fn1 = "ship1.txt";
$fn2 = "ship2.txt";
// ********* load guess arrays with 9's
for ($i=0;$i<100;$i++)
{
$guessArray1[$i] = 9;
$guessArray2[$i] = 9;
}
// ********* load guess arrays to guess text file
$fp = fopen("play1.txt", 'w');
for ($i=0;$i<100;$i++){$ip .= "$guessArray1[$i]" ;}
fwrite($fp, $ip);
fclose($fp);
$fp = fopen("play2.txt", 'w');
for ($i=0;$i<100;$i++){$ip .= "$guessArray2[$i]" ;}
fwrite($fp, $ip);
fclose($fp);
initialize(); // ******* call initialize function
loadScreen(); // ******* call loadScreen function
}
// ************ fire button clicked
if ($fire == "FIRE") {
initialize(); // ******* call initialize function
checkloc(); // ******* call checkloc function
loadScreen(); // ******* call loadScreen function
}
//*******************************************************************
***********************************
function checkloc() {
//*******************************************************************
***********************************
global $rb, $gridArray1, $gridArray2, $guessArray1, $guessArray2;
//******** player 1 radio button selected
if ($rb < 200)
{
$guessArray1[$rb] = $gridArray1[$rb];
$fp = fopen("play1.txt", "w");
for ($i=0;$i<100;$i++){$ip .= "$guessArray1[$i]" ;}
fwrite($fp, $ip);
fclose($fp);
}
else //******** player 2 radio button selected
{
$m = $rb - 200;
$guessArray2[$m] = $gridArray2[$m];
$fp = fopen("play2.txt", "w");
for ($i=0;$i<100;$i++){$ip .= "$guessArray2[$i]" ;}
fwrite($fp, $ip);
fclose($fp);
}
} // end checkloc
//*******************************************************************
*************************************
function initialize() {
//*******************************************************************
*************************************
global $gridArray1, $gridArray2, $guessArray1, $guessArray2,
$fn1, $fn2;
// ******** read text files into arrays - ship locations and
guess locations
// ******** read player 1 ship location textfile into array
$gridArray1
$fp = fopen($fn1, 'r');
$c=0;
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
$gridArray1[$c] = $char;
$c++;
}
fclose($fp);
// ****************** read player 2 ship location text file into
array $gridArray2
$fp = fopen($fn2, 'r');
$c=0;
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
$gridArray2[$c] = $char;
$c++;
}
fclose($fp);
//****************** read player 1 guesses text file into array
$guessArray1
$fp = fopen("play1.txt", 'r');
$c=0;
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
$guessArray1[$c] = $char;
$c++;
}
fclose($fp);
//***************** read player 2 guesses text file into array
$guessArray2
$fp = fopen("play2.txt", 'r');
$c=0;
if (!$fp) {
echo 'Could not open file somefile.txt';
}
while (false !== ($char = fgetc($fp))) {
$guessArray2[$c] = $char;
$c++;
}
fclose($fp);
//***************** hidden text boxes to hold text file names
print ' <input type = "hidden" name = "fn1" value = "';
print$fn1;
print '">';
print ' <input type = "hidden" name = "fn2" value = "';
print$fn2;
print '">';
} //end initialize
//*******************************************************************
**************************************
function loadScreen() {
//*******************************************************************
**************************************
global $gridArray1, $gridArray2, $guessArray1, $guessArray2;
print "<table align=center><tr><td>";
print "<table border=1 bgcolor=white>";
print "<th align=center colspan=10>PLAYER 1</th>";
$g=0;
$hits1=0;
for ($i = 0; $i < 10; $i++) {
print "<tr>";
for ($j = 0; $j < 10; $j++) {
if ($guessArray1[$g] == 9) {
print '<td><input type="radio" name="rb" value="';print
$g; print '"/></td>';}
else if ($guessArray1[$g] > 0) {
print '<td><img src="hit2.gif" /></td>'; $hits1++;}
else if ($guessArray1[$g] == 0) {
print '<td><img src="batt100a.gif" /></td>';}
$g++;
} //end for $j
print "</tr>";} //end for $i
print '</table></td><td><input type="submit" name="fire"
value="FIRE" /></td><td>';
print "<table border=1 bgcolor=white>";
print "<th align=center colspan=10>PLAYER 2</th>";
$g=0;
$hits2=0;
$h=200;
for ($i = 0; $i < 10; $i++) {
print "<tr>";
for ($j = 0; $j < 10; $j++) {
if ($guessArray2[$g] == 9) {
print '<td><input type="radio" name="rb" value="';print
$h; print '"/></td>';}
else if ($guessArray2[$g] > 0) {
print '<td><img src="hit2.gif" /></td>'; $hits2++;}
else if ($guessArray2[$g] == 0) {
print '<td><img src="batt100a.gif" /></td>';}
$g++;$h++;
} //end for $j
print "</tr>";} //end for $i
print "</table></td></tr>";
//$hits1 = 19;
if ($hits1 == 19) {
print '<tr><td><h2> </h2></td><td></td><td
style="color:red;font-weight:bold"
align=center><h2>WINNER!!! </h2></td></tr>';
}
if ($hits2 == 19) {
print '<tr><td style="color:red;font-weight:bold"
align=center><h2>WINNER!!!</h2>
</td><td></td><td><h2> </h2></td></tr>';
}
if ($hits1 > 18 || $hits2 > 18) {
print '<tr><td colspan=3 align=center><input type="submit"
name="fire" value="Play Again?" height = /></td></tr>';}
print "</table>";
} //end LoadScreen
?>
</form>
</body>
</html>
HERE IS THE QUESTIONS:
1. How does the program know where the ships are located?
2. Why are these 2 lines comment out?
$fn1 = "ship".$rand1.".txt"; //****** example: ship4.txt
$fn2 = "ship".$rand2.".txt";
3. How does the program know which player's radio button is
selected? (Hint: print $g; & print $h;)
4. How does the program know which buttons have already been
selected by the players?
5. What image file is displayed if a player scores a hit?
6. What is the pages background color?
7. How many arrays are used in this program?
8. What do the file attributes (FileZilla) need to be for text files
play1.txt and play2.txt? Why?
9. What do the file attributes need to be for text files ship1.txt
and ship2.txt? Why?
10.Why are texts files used in this program anyway?
------------------------ Yahoo! Groups Sponsor --------------------~-->
You can search right from your browser? It's easy and it's free. See how.
http://us.click.yahoo.com/_7bhrC/NGxNAA/yQLSAA/67folB/TM
--------------------------------------------------------------------~->
If you have any questions or problems with any aspect of this site, please feel
free to contact me directly [EMAIL PROTECTED] Please do not post personal
issues directly to the group.
To unsubscribe from this list, send an email to [EMAIL PROTECTED]
Thank you for using A-1 Computer Tech
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/A-1-Computer_Tech/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/