Card Game

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bamatick
Forum Newbie
Posts: 4
Joined: Fri Dec 05, 2003 5:42 am

Card Game

Post by Bamatick »

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am writing a card game.  I can generate a random card, but I can't make it ignore duplicates.  I am stumped.  Any hints?  Here is my code:

Code: Select all

<?
$host = "localhost"; 
$username = "username"; 
$password = "password"; 
$connection= mysql_connect($host, $username, $password) or die(mysql_error());
$db_name="database";
$table_name="doorCardsPile";
$db=@mysql_select_db($db_name, $connection) or die(mysql_error());
$columnName="cardUsage";
$n="1";

function randomizer ()
{
$ranNum = (rand()%101)+1;

$host = "localhost"; 
$username = "username"; 
$password = "password"; 
$connection= mysql_connect($host, $username, $password) or die(mysql_error());
$sql="SELECT doorNumb, cardUsage FROM doorCardsPile WHERE doorNumb=$ranNum";
$result=@mysql_query($sql, $connection) or die(mysql_error());

While($row=mysql_fetch_array($result))
{
$cardUsage=$row['cardUsage'];
if ($cardUsage != 0)
{
randomizer ();
}
}

return $ranNum;
}

$sql2 = ("SELECT SUM(cardUsage) FROM $table_name");
$result1 = mysql_query($sql2);
$sumCards= mysql_result($result1,0,0);

if ($sumCards < 102)
{
$ranNum1=randomizer();
$sql="SELECT doorNumb, imageName FROM $table_name WHERE doorNumb=$ranNum1";
$result=@mysql_query($sql, $connection) or die(mysql_error());

	While($row=mysql_fetch_array($result))
		{
		$doorNumb=$row['doorNumb'];
		$imageName=$row['imageName'];

                $display_block .= "
		<TABLE>
		<TR>
		<TD>
		<IMG SRC=images/Munchkin/$imageName HEIGHT='275' WIDTH='220'>
		</TD>
		</TR>
		<TR>
		<TD>
		<input type='submit' NAME='drawDoor' VALUE='Draw Door Card' />
		</TD>
		</TR>
		</TABLE>";
		}

	$sql3="UPDATE $table_name SET $columnName='$n' WHERE doorNumb=$ranNum1";
	$result3 = mysql_query($sql3); 	
}

else 
{
	print("Redeal");
	$display_block .= "
	<TABLE>
	<TR>
	<TD>
	<IMG SRC=images/Munchkin/doorCard000.jpg HEIGHT='275' WIDTH='220'>
	</TD>
	</TR>
	<TR>
	<TD>
	<input type='submit' NAME='drawDoor' VALUE='Draw Door Card' />
	</TD>
	</TR>
	</TABLE>";
}

?>
<html>
<body>
<FORM METHOD="POST" ACTION="munchkin.php">
<? echo "$display_block"; ?>
</FORM>
<FORM METHOD="POST" ACTION="newmunchkin.php">
<TABLE>
<TR>
<TD>
<input type="submit" NAME="newGame" VALUE="New Game" />
</TD>
</TR>
</TABLE>
</FORM>
</body>
</html>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


[color=#006600][b]feyd[/b] | added a missing quote to fix highlighting.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

when I've done this in the past, I stored the specific card I added into an array. When selecting a new one, I would search through the already used array.. Alternately, you could [re]move the newly random card from the available list, and drop it into your used list so you can't choose the used card again..
Bamatick
Forum Newbie
Posts: 4
Joined: Fri Dec 05, 2003 5:42 am

Hmm

Post by Bamatick »

It knows the card is already used...it just doesn't care.

What my code currently does is:
1. randomly pick a card (unless max # of cards in deck has been reached)
2. check to see if it's being used (then use it anyway)
3. mark it as used (again)
4. display it

What I want it to do is:
1. randomly pick a card
2. check to see if it's being used, if so, go to 1
3. mark it as used
4. display it

The table has these fields:
1. card #
2. card image
3. card usage (0 for not used, 1 for used)
Post Reply