Jump to content

something stupid


Caitlin-havener

Recommended Posts

My matrix array isn't being assigned a value correctly. I feel like this is right under my nose but I can't tell what the ###### it is I'm doing. $Value[$x][$y]=$shipnumber should just store the number of "this" ship into the coordinates of that array, but it is storing the value of $y in the array not $shipnumber. Can you see it?

class Ship{	private $shipnumber, $xloc, $yloc, $orientation, $type;		function __construct($shipnumber, $xin,$yin, $orientation, $type)	{		$this->shipnumber=$shipnumber;		$this->xloc=$xin;		$this->yloc=$yin;		$this->orientation=$orientation;		$this->type=$type;	}function drawship()	{		global $Grid;		$ship_color=$this->type;		$orientation=$this->orientation;		$shipnumber=$this->shipnumber;		if ($orientation=="horizontal"){			$y=$this->yloc;			for ($x=$this->xloc;$x<=$this->xloc+4; $x++)			{				$Grid[$x][$y]=$ship_color;				$Value[$x][$y]=$shipnumber;				print "Boat1: $Value[$x][$y]";			}		}		else if ($orientation=="vertical"){			$x=$this->xloc;			for ($y=$this->yloc;$y<=$this->yloc+4; $y++)			{				$Grid[$x][$y]=$ship_color;				$Value[$x][$y]=$shipnumber;					print "shipnumber: $shipnumber";				print "Boat2: $Value[$x][$y]";			}		}	}}

print "$shipnumber" prints the correct value but print "$Value[$x][$y]" does not. Please help!You shouldnt have to look at the whole thing but here it is:

<?php	session_start();	// STARTER KIT for Battleship - DIG 3134 - Spring 2011 - Moshell// Derived from the Wall War prototype$Testnumber=3; // use 3 to see the Ship's methods at work.$Gridsize=16;$Imagewidth=300; // pixels$Cellwidth=$Imagewidth/$Gridsize;define('BLACK','#000000');define('GOLD','#AAAA33');define('BLUE','#0000FF');define('WHITE','#FFFFFF');define('YELLOW','#FFF600');define('GREY','#888888');define ('RED','#FF0000');#makeheader:function makeheader(){ global $Cellwidth;	print '	  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">	<html xmlns="http://www.w3.org/1999/xhtml">	<head>	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />	<title>Battleship - Starter Kit</title>';print "	<style type='text/css'>			.mtable td 			{				width:$Cellwidth"."px; height:$Cellwidth"."px;				color:RED			}		</style>";print '</head><body><form method="post">';} # makeheaderfunction preparegrid(){global $Grid, $Gridsize, $Visible;	for ($y=0; $y<=$Gridsize+1; $y++)	{		for ($x=0; $x<=$Gridsize+1; $x++)		{			if ($x==0) // left margin			{				$Grid[$x][$y]=BLUE;				//$Visible[$x][$y]=1;			}			else if ($y==0) // top margin			{				$Grid[$x][$y]=BLUE;				//$Visible[$x][$y]=1;			}			else if (($x==$Gridsize+1)||($y==$Gridsize+1))			{	// right margin and bottom margin				$Grid[$x][$y]=BLUE;				//$Visible[$x][$y]=1;			}			else	// fill the dude with white!			{				$Grid[$x][$y]=WHITE;				//$Visible[$x][$y]=0;			}						} # x loop	} # y loop} # preparegridfunction drawgrid(){global $Grid, $Gridsize;	$result='<table class="mtable" border=1>';	for ($y=0; $y<=$Gridsize+1; $y++)	{		$result.="<tr>";		for ($x=0; $x<=$Gridsize+1; $x++)		{			if ($x==0) $char=$y;			else if ($y==0){ 				if($x<27)            		$char= strtoupper(chr($x+96)); 			}			//$char=$x; //printing x here columns			else if ($Grid[$x][$y]==BLUE)				$char=" ";			else $char="<input type='radio' name='fillhere' value='$x.$y'>";			$color=$Grid[$x][$y];			$result.="<td align='center' style='background-color:$color'>$char</td>";		} # x loop		$result.="</tr>";	} # y loop	$result.="</table>";	print $result;} #drawgrid#drawinputs:function drawinputs(){	print "		<input type='submit' name='action' value='BOMB' />		<input type='submit' name='action' value='CLEAR' />				";}////////// CLASS DEFINITIONSclass Ship{	private $shipnumber, $xloc, $yloc, $orientation, $type;		function __construct($shipnumber, $xin,$yin, $orientation, $type)	{		$this->shipnumber=$shipnumber;		$this->xloc=$xin;		$this->yloc=$yin;		$this->orientation=$orientation;		$this->type=$type;	}		function describe()	{		print "shipnumber:$this->shipnumber <br />				x, y: $this->xloc $this->yloc <br />				orientation: $this->orientation <br />				type: $this->type <br />";	} # describe		function isfloating()	{		global $Grid;		$orientation=$this->orientation;		if ($orientation=="horizontal"){			$y=$this->yloc;			for ($x=$this->xloc;$x<=$this->xloc+4; $x++)			{				if ($Grid[$x][$y]==$this->type)					$count++;			}		}		else if ($orientation=="vertical"){			$x=$this->xloc;			for ($y=$this->yloc;$y<=$this->yloc+4; $y++)			{				if ($Grid[$x][$y]==$this->type)					$count++;			}		}		if ($count>=3)			return 1;		else			return 0;	} # isfloating		#mycolorcount:	function mycolorcount()	{		global $Grid;		$orientation=$this->orientation;		if ($orientation=="horizontal"){			$y=$this->yloc;			for ($x=$this->xloc;$x<=$this->xloc+4; $x++)			{				if ($Grid[$x][$y]==$this->type)					$count++;			}		}		else if ($orientation=="vertical"){			$x=$this->xloc;			for ($y=$this->yloc;$y<=$this->yloc+4; $y++)			{				if ($Grid[$x][$y]==$this->type)					$count++;			}		}				return $count;	} # mycolorcount		#myspectrum:	function myspectrum()	{		global $Grid;		$orientation=$this->orientation;		if ($orientation=="horizontal"){			$y=$this->yloc;			for ($x=$this->xloc;$x<=$this->xloc+4; $x++)			{				$spectrum[$Grid[$x][$y]]++;			}		}		else if ($orientation=="vertical"){			$x=$this->xloc;			for ($y=$this->yloc;$y<=$this->yloc+4; $y++)			{				$spectrum[$Grid[$x][$y]]++;			}		}					return $spectrum;	} # myspectrum;		function drawship()	{		global $Grid;		$ship_color=$this->type;		$orientation=$this->orientation;		$shipnumber=$this->shipnumber;		if ($orientation=="horizontal"){			$y=$this->yloc;			for ($x=$this->xloc;$x<=$this->xloc+4; $x++)			{				$Grid[$x][$y]=$ship_color;				$Value[$x][$y]=$shipnumber;				print "Boat1: $Value[$x][$y]";			}		}		else if ($orientation=="vertical"){			$x=$this->xloc;			for ($y=$this->yloc;$y<=$this->yloc+4; $y++)			{				$Grid[$x][$y]=$ship_color;				$Value[$x][$y]=$shipnumber;					print "shipnumber: $shipnumber";				print "Boat2: $Value[$x][$y]";			}		}	}	function isfloating2()	{		$spectrum=$this->myspectrum();		if ($spectrum[$this->type]>=3)			return 1;		else			return 0;		// you can also use:  		//		return ($spectrum[$this->type]>=3);		//		because the (expression) is either true (1) or false (0)	} #isfloating2	} # Ship/////// MAIN PROGRAM ///////	makeheader();		$action=$_POST['action'];	$Grid=$_SESSION['Grid'];	$ship=$_SESSION['ship'];	$Value=$_SESSION['Value'];		print "<center><h3>Damage Control</h3>";	if ((!$action)||($action=='CLEAR'))	{				preparegrid();		//$Shipone=new Ship(1,2,2,"horizontal","#000000");		$ship[1]=new Ship(1,1,1,'horizontal',BLACK);		$ship[2]=new Ship(2,3,3,'vertical',GOLD);		$ship[1]->drawship();		$ship[2]->drawship();	}			else // these commands need the X, Y coordinates	{					$f=$_POST['fillhere'];		if ($f)		{			$fparts=explode('.',$f);			$x=$fparts[0];			$y=$fparts[1];		}				if ($action=='BOMB'){			if ($Grid[$x][$y]==WHITE){				$Grid[$x][$y]=BLUE;				print "Sploosh!";			}			else if (($Grid[$x][$y]==GREY) || ($Grid[$x][$y]==YELLOW)){				print "Boom Again!";			}			else if ($Grid[$x][$y]==BLACK){				$Grid[$x][$y]=GREY;				$i=$Value[$x][$y];				$floating=$ship[$i]->isfloating();				if ($floating)					print "Boom!";				else if (!$floating)					print "Arrggh! The ship sank!";			}			else if ($Grid[$x][$y]==GOLD){				$Grid[$x][$y]=YELLOW;				$i=$Value[$x][$y];				$floating=$ship[$i]->isfloating();				if ($floating)					print "Boom!";				else if (!$floating)					print "Arrggh! The ship sank!";			}		}				/*else if ($action=='SHOW SHIP')		{			print "/////// Describe the Ship: ////////<br />";			$ship[1]->describe();			print "/////// the 'isfloating' method says: ////////<br />";			print $ship[1]->isfloating();			print "<br /> /////// the 'mycolorcount' method says: ////////<br />";			print $ship[1]->mycolorcount();			print "<br />/////// the 'isfloating2' method says: ////////<br />";			print $ship[1]->isfloating2();				}*/		} # 		drawgrid();			drawinputs();		$_SESSION['ship']=$ship;	$_SESSION['Value']=$Value;	$_SESSION['Grid']=$Grid; // Store the grid and the visibility for the next cycle?></form></body></html>

Link to comment
Share on other sites

It might just be a printing issue with the multidimensional array. Take those out of the string:print "shipnumber: " . $shipnumber;print "Boat2: " . $Value[$x][$y];or:print "shipnumber: $shipnumber";print "Boat2: {$Value[$x][$y]}";

Link to comment
Share on other sites

It might just be a printing issue with the multidimensional array. Take those out of the string:print "shipnumber: " . $shipnumber;print "Boat2: " . $Value[$x][$y];or:print "shipnumber: $shipnumber";print "Boat2: {$Value[$x][$y]}";
should it affect the value in initialization?$i=$Value[$x][$y] is giving me the $y value
Link to comment
Share on other sites

No, it won't affect assignment. You're saying that after doing this:$Value[$x][$y]=$shipnumberthose two lines print different numbers?
yes you got it. This is how it should be : $Value[$x][$y]=$shipnumberThis is what im getting: $Value[$x][$y]=$y
Link to comment
Share on other sites

if you change below to

function drawship(){global $Grid;$ship_color=$this->type;$orientation=$this->orientation;$shipnumber=$this->shipnumber;if ($orientation=="horizontal"){$y=$this->yloc;for ($x=$this->xloc;$x<=$this->xloc+4; $x++){$Grid[$x][$y]=$ship_color;$Value[$x][$y]=$shipnumber;print "shipnumber: $shipnumber";print " : Boat1:".$Value[$x][$y];echo ' x value: '.$x.';';echo ' Y value: '.$y.';<br />';}}else if ($orientation=="vertical"){$x=$this->xloc;for ($y=$this->yloc;$y<=$this->yloc+4; $y++){$Grid[$x][$y]=$ship_color;$Value[$x][$y]=$shipnumber;print "shipnumber: $shipnumber";print " : Boat2:".$Value[$x][$y];echo ' x value: '.$x.';';echo ' Y value: '.$y.';<br />';}}}

i got

shipnumber: 1 : Boat1:1 x value: 1; Y value: 1;shipnumber: 1 : Boat1:1 x value: 2; Y value: 1;shipnumber: 1 : Boat1:1 x value: 3; Y value: 1;shipnumber: 1 : Boat1:1 x value: 4; Y value: 1;shipnumber: 1 : Boat1:1 x value: 5; Y value: 1;shipnumber: 2 : Boat2:2 x value: 3; Y value: 3;shipnumber: 2 : Boat2:2 x value: 3; Y value: 4;shipnumber: 2 : Boat2:2 x value: 3; Y value: 5;shipnumber: 2 : Boat2:2 x value: 3; Y value: 6;shipnumber: 2 : Boat2:2 x value: 3; Y value: 7;

which as far as i can tell is correct

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...