Jump to content

dynamic checkboxes


Caitlin-havener

Recommended Posts

Before I ask question this is what the game is about:This project will teach concepts of 2d arrays, interactive controls, recursion, and database table construction.The game board consists of an n x n grid (for 3<=n<=25, and n must be an odd number), whose squares can either be white, black or gold. The columns have letters A, B, C... and the rows have numbers 1, 2, 3... There are two teams - Black and Gold. Each team in turn specifies a cell, and that cell is colored with the team's color.The border of the game board consists of a single row of blue cells, except that the middle cell of the left border is black, and the middle cell of the top border is gold.The object of this game is to draw a continuous line in your team's color, from one side of the screen to the other. I need to figure out how to put dynamic radio buttons in the table instead of typing the coordinates in the input fields. How would I go about this?See the game: http://www.havener.me/project3.phpCode:

<?php	//error_reporting(0);	session_start();define('GRIDSIZE',7);define('BLACK','#000000');define('GOLD','#AAAA33');define('BLUE','#0000FF');define('WHITE','#FFFFFF');$Cellwidth=500/GRIDSIZE;$_SESSION['turn'] = "GOLD";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>Wall War</title>';print "	<style type='text/css'>			.mtable td 			{				width:$Cellwidth"."px; height:$Cellwidth"."px;				color:WHITE			}		</style>";print '</head><body><form method="post">';} # makeheaderfunction preparegrid(){global $Grid;	for ($y=0; $y<=GRIDSIZE+1; $y++)	{		for ($x=0; $x<=GRIDSIZE+1; $x++)		{			if ($x==0) // left margin			{				if ($y==(GRIDSIZE+1)/2)					$Grid[$x][$y]=BLACK;				else					$Grid[$x][$y]=BLUE;			}			else if ($y==0) // top margin			{				if ($x==(GRIDSIZE+1)/2)					$Grid[$x][$y]=GOLD;				else					$Grid[$x][$y]=BLUE;			}			else if (($x==GRIDSIZE+1)||($y==GRIDSIZE+1))			{	// right margin and bottom margin				$Grid[$x][$y]=BLUE;			}			else	// fill the dude with white!				$Grid[$x][$y]=WHITE;						} # x loop	} # y loop} # preparegridfunction drawgrid(){global $Grid;	$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 $char=" ";						$color=$Grid[$x][$y];			$result.="<td align='center' style='background-color:$color'>$char</td>";		} # x loop		$result.="</tr>";	} # y loop	$result.="</table>";	print $result;} #drawgridfunction drawinputs(){	$turn_print = $_SESSION['turn'];    print"<div id='team'>It is $turn_print team's turn.</div>";		print "<p>X position:<input type='text' size=2 name='xinput'>A Letter.<br />		Y position:<input type='text' size=2 name='yinput'>A Number.<br />		Color:<input type='text' size=2 name='colorinput'>g or b.<br />		<input type='submit' name='action' value='CLEAR'>		<input type='submit' name='action' value='GO'><br />		<input type='submit' name='action' value='END GAME'>";}/////// MAIN PROGRAM ///////	makeheader();	$action=$_POST['action'];		if ((!$action)||($action=='CLEAR'))		preparegrid();	else if ($action=='END GAME'){            $_SESSION = array();            if (ini_get('session.use_cookies')) {                $params = session_get_cookie_params();                setcookie(session_name(), '', time() - 42000,                        $params['path'], $params['domain'],                        $params['secure'], $params['httponly']                );            }            session_destroy();            header('Location: ' . $_SERVER['REQUEST_URI']);	}	else // we must be playing already. So fetch and modify the Grid.	{		$Grid=$_SESSION['Grid'];		$x=strtolower($_POST['xinput']);   		$x=ord($x)-96;            //Convert to an integer		$y=$_POST['yinput'];		$c=$_POST['colorinput'];		if ($c=='g')			if ($Grid[$x-1][$y]==GOLD || $Grid[$x+1][$y]==GOLD || $Grid[$x][$y+1]==GOLD || $Grid[$x][$y-1]==GOLD)				$color=GOLD;			else print "You can not make a move there.";		else if ($c=='b')			if ($Grid[$x-1][$y]==BLACK || $Grid[$x+1][$y]==BLACK || $Grid[$x][$y+1]==BLACK || $Grid[$x][$y-1]==BLACK)				$color=BLACK;			else print "You can not make a move there.";		else			$color=WHITE;		$Grid[$x][$y]=$color;				//WHOS TURN??        $turn = $_SESSION['turn'];        if ($turn == "GOLD") {            $_SESSION['turn'] = "BLACK";        }else if ($turn == "BLACK") {            $_SESSION['turn'] = "GOLD";        } #End whos turn	} # 		drawgrid();	drawinputs();		$_SESSION['Grid']=$Grid; // Store the grid for the next cycle	?></form></body></html>

Link to comment
Share on other sites

You can use ord() to get the ascii value of A and then you can loop through by increasing by 1 untill it reaches the ascii value of H. then another loop which will itrate through the row numbers..something like...

<?phpfor($col=ord("A");$col<=ord("H");$col++){	for($row=0;$row<=7;$row++)	{		//print out debug		//echo chr($col)."$row";		echo "<input type='radio' name='box' value='".chr($col)."$row' />";	}	//go to new line;	echo '<br/>';}?>

http://php.net/function.ordhttp://php.net/function.chr for more detailswith more modification it should (not tested) send the row as A1 H3. you need to parse it in reciving page to determine the field location

Link to comment
Share on other sites

Hey ya'll have a question now based on prior posts. I made some radio buttons- one for each cell of the table on the game. How can I select a radio button and have that post just by being selected and not have to put a corresponding "GO" button to get it to post??

Link to comment
Share on other sites

Okay In put that function in the radio button, it seems like it is submitting but I am not getting the right result. The following is the part that needs to work when a radio is pushed:

$f=$_POST['fillhere'];    		if ($f)		{			$fparts=explode('.',$f);			$x=$fparts[0];			$y=$fparts[1];		}			$turn=$_SESSION['turn'];			if ($turn=="GOLD")				$color=GOLD;			else if ($turn=="BLACK")				$color=BLACK;$Grid[$x][$y]=$color;

And then it calls the function to draw the whole grid. As seen in the whole code below. The turn session above is supposed to make the cell the color of the team's turn- which is

<?php	//error_reporting(0);	session_start();define('GRIDSIZE',7);define('BLACK','#000000');define('GOLD','#AAAA33');define('BLUE','#0000FF');define('WHITE','#FFFFFF');$Cellwidth=500/GRIDSIZE;$_SESSION['turn'] = "GOLD";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>Wall War</title>';print "	<style type='text/css'>			.mtable td 			{				width:$Cellwidth"."px; height:$Cellwidth"."px;				color:WHITE			}		</style>";print '</head><body><form method="post">';} # makeheaderfunction preparegrid(){global $Grid;	for ($y=0; $y<=GRIDSIZE+1; $y++)	{		for ($x=0; $x<=GRIDSIZE+1; $x++)		{			if ($x==0) // left margin			{				if ($y==(GRIDSIZE+1)/2)					$Grid[$x][$y]=BLACK;				else					$Grid[$x][$y]=BLUE;			}			else if ($y==0) // top margin			{				if ($x==(GRIDSIZE+1)/2)					$Grid[$x][$y]=GOLD;				else					$Grid[$x][$y]=BLUE;			}			else if (($x==GRIDSIZE+1)||($y==GRIDSIZE+1))			{	// right margin and bottom margin				$Grid[$x][$y]=BLUE;			}			else	// fill the dude with white!				$Grid[$x][$y]=WHITE;						} # x loop	} # y loop} # preparegridfunction drawgrid(){global $Grid;	$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' onclick='this.form.submit();'>";						$color=$Grid[$x][$y];			$result.="<td align='center' style='background-color:$color'>$char</td>";		} # x loop		$result.="</tr>";	} # y loop	$result.="</table>";	print $result;} #drawgridfunction drawinputs(){	$turn_print = $_SESSION['turn'];    print"<div id='team'>It is $turn_print team's turn.</div>";		print "<p>Coordinates:<input type='text' size=2 name='xyinput'>A Letter followed by a single digit number, no comma.<br />		Color:<input type='text' size=2 name='colorinput'>g or b.<br />		<input type='submit' name='action' value='CLEAR'>		<input type='submit' name='action' value='GO'><br />		<input type='submit' name='action' value='END GAME'>";}/////// MAIN PROGRAM ///////	makeheader();	$action=$_POST['action'];		switch($action){	case '':		preparegrid();		break;		case 'CLEAR':		preparegrid();		break;			case'END GAME':            $_SESSION = array();            if (ini_get('session.use_cookies')) {                $params = session_get_cookie_params();                setcookie(session_name(), '', time() - 42000,                        $params['path'], $params['domain'],                        $params['secure'], $params['httponly']                );            }            session_destroy();            header('Location: ' . $_SERVER['REQUEST_URI']);			break;		default: 			$Grid=$_SESSION['Grid'];				##### SELECTION BY RADIO BUTTONS		$f=$_POST['fillhere'];    		if ($f)		{			$fparts=explode('.',$f);			$x=$fparts[0];			$y=$fparts[1];		}			$turn=$_SESSION['turn'];			if ($turn=="GOLD")				$color=GOLD;			else if ($turn=="BLACK")				$color=BLACK;		####						#### SELECTION BY INPUT FIELD		$coords=$_POST['xyinput']; 		if (preg_match("/^[a-zA-Z]{1}[1-9]{1}$/", $coords)){			$x=strtolower($coords[0]);   			$x=ord($x)-96;            //Convert to an integer			$y=$coords[1]; 		} 		else 			print"Wrong format. You must enter a letter followed by a single digit number.";					$c=$_POST['colorinput'];		if ($c=='g')			if ($Grid[$x-1][$y]==GOLD || $Grid[$x+1][$y]==GOLD || $Grid[$x][$y+1]==GOLD || $Grid[$x][$y-1]==GOLD)				$color=GOLD;			else print "You can not make a move there.";		else if ($c=='b')			if ($Grid[$x-1][$y]==BLACK || $Grid[$x+1][$y]==BLACK || $Grid[$x][$y+1]==BLACK || $Grid[$x][$y-1]==BLACK)				$color=BLACK;			else print "You can not make a move there.";		else			$color=WHITE;		####						$Grid[$x][$y]=$color;				### WHOS TURN??        $turn = $_SESSION['turn'];        if ($turn == "GOLD") {            $_SESSION['turn'] = "BLACK";        }else if ($turn == "BLACK") {            $_SESSION['turn'] = "GOLD";        } #End whos turn 	break;		}	drawgrid();	drawinputs();		$_SESSION['Grid']=$Grid; // Store the grid for the next cycle	?></form></body></html>

determined elsewhere in the code. Can you see why it does not work?

Link to comment
Share on other sites

Are the names and values correct for the buttons? If you print $f, do you get the expected value?
I'm not getting anything printing $f so apparently that is not working. I'm not all that familiar with javascript events, or it has been a while anyway... should the following submit the radio button and pull the value out with $_POST['fillhere']?
"<input type='radio' name='fillhere' value='$x.$y' onclick='this.form.submit();'>";

$f=$_POST['fillhere'];    		if ($f)		{			$fparts=explode('.',$f);			$x=$fparts[0];			$y=$fparts[1];		}			$turn=$_SESSION['turn'];			if ($turn=="GOLD")				$color=GOLD;			else if ($turn=="BLACK")				$color=BLACK;		####		print $f;

Link to comment
Share on other sites

Thanks guys fixed that... I'm trying to find a way to put all the input submits into the same switch case. There is the radio buttons one

<input type='radio' name='fillhere' value='$x.$y' onclick='this.form.submit();'>

and the others

<p>Coordinates:<input type='text' size=2 name='xyinput'>A Letter followed by a single digit number, no comma.<br />		Color:<input type='text' size=2 name='colorinput'>g or b.<br />		<input type='submit' name='action' value='CLEAR'>		<input type='submit' name='action' value='GO'><br />		<input type='submit' name='action' value='END GAME'>";

I currently have the bottom ones in a switch($action) where $action=post['action'] and the top input is just $f=post['fillhere'] and if($f)...I would like to put the first input above into the switch($action) case but obviously I can't make a case for the value='$x.$y' when it isn't a predetermined value! How could I do this??

Link to comment
Share on other sites

I'm not sure I understand. What condition are you trying to check for in the switch? I understand the actions, but what condition are you trying to check for with the button values?
switch($action)case "CLEAR"case "GO"case "END GAME"defaultThe radio buttons have variable values of $x and $y so I don't know how to put them in the same switch case. Would be nice because I'll have to repeat a lot of functions...
Link to comment
Share on other sites

I'm confused about why the x and y values should go in that switch, I'm not sure specifically what you're trying to accomplish. Does it make sense to have another switch or an if structure inside one of those cases to check the values? Why you need to do a switch on the x and y I guess is what I'm asking.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...