Page 1 of 1

problem with array sum

Posted: Wed Mar 10, 2004 8:46 am
by dull1554
I have been working on a black jack game just to pass the time, and i have run into a snag. when a player hits, i want to script to first check if their new total is over 21, if it is it will say you've gone bust, or something like that.

the card values are stored in a array. ($pcv)...and to get the sum i used array_sum(). But no matter what i do array_sum($pcv) returns 0/zero.

here's all my code, the part i'm speaking of is in between ///////////// towards the bottom....

thanks in advances

Code: Select all

<?php
error_reporting(0);
session_start();
if((isset($_COOKIE['bj_rand_num'])) && (!isset($_POST['submit'])))
{
Print <<< EOT
You are seeing this message because you either tried to cheat, or you are trying to play this game to soon after starting the last game.
EOT;
exit;
}
if(!isset($_POST['submit']))
{
/////////cheater prevention
$rand = mt_rand(1,999999999);
$_SESSION['rand'] = $rand;
setcookie("bj_rand_num", $rand,time()+1);
/////////end cheater prevention
$dcs = array();
$dcn = array();
$pcs = array();
$pcn = array();
for($i = 1; $i <= 2; $i++)
{
$suit = mt_rand(1,4);
$card = mt_rand(1,13);
$dcs[$i] = $suit;
$dcn[$i] = $card;
}
while($dcs[1]+$dcn[1] == $dcs[2]+$dcn[2])
{
$suit = mt_rand(1,4);
$card = mt_rand(1,13);
$dcs[2] = $suit;
$dcn[2] = $card;
}

for($i = 1; $i <= 2; $i++)
{
$suit = mt_rand(1,4);
$card = mt_rand(1,13);
$pcs[$i] = $suit;
$pcn[$i] = $card;
}
while(($pcs[1]+$pcn[1] == $pcs[2]+$pcn[2]) || ($pcs[1]+$pcn[1] == $dcs[1]+$dcn[1]) || ($pcs[1]+$pcn[1] == $dcs[2]+$dcn[2]) || ($pcs[2]+$pcn[2] == $dcs[1]+$dcn[1]) || ($pcs[2]+$pcn[2] == $dcs[2]+$dcn[2]))
{
for($i = 1; $i <= 2; $i++)
{
$suit = mt_rand(1,4);
$card = mt_rand(1,13);
$pcs[$i] = $suit;
$pcn[$i] = $card;
}
}
for($i = 1; $i <= 2; $i++)
{
$_SESSION['pcs'.$i] = $pcs[$i];
$_SESSION['pcn'.$i] = $pcn[$i];
}
for($i = 1; $i <= 2; $i++)
{
$_SESSION['dcs'.$i] = $dcs[$i];
$_SESSION['dcn'.$i] = $dcn[$i];
}
$_SESSION['cards'] = 2;
Print <<< EOT
<center>
<table><tr>
<td>Dealers cards</td><td><img src={$dcs[1]}/{$dcn[1]}.gif> <img src={$dcs[2]}/{$dcn[2]}.gif></td>
</tr>
<tr>
<td>Players cards</td><td><img src={$pcs[1]}/{$pcn[1]}.gif> <img src={$pcs[2]}/{$pcn[2]}.gif></td>
</tr>
<td colspan=2>
<center>
<form action=index.php method=post>Players options &nbsp; &nbsp; &nbsp;&nbsp;
<input type='submit' name='submit' value='Hit'>
<input type='submit' name='submit' value='stand'>
<input type='submit' name='submit' value='quit'>
</form></center></td>
<tr>
</center>
EOT;
}

if((isset($_POST['submit'])) && ($_POST['submit'] == "Hit"))
{
$dcs = array();
$dcn = array();
$pcs = array();
$pcn = array();
$cards = $_SESSION['cards'];
$i = 1;
while((isset($_SESSION['pcs'.$i])) && (isset($_SESSION['pcn'.$i])))
{
$pcs[$i] = $_SESSION['pcs'.$i];
$pcn[$i] = $_SESSION['pcn'.$i];
$i++;
}
$i = 1;
while((isset($_SESSION['dcs'.$i])) && (isset($_SESSION['dcn'.$i])))
{
$dcs[$i] = $_SESSION['dcs'.$i];
$dcn[$i] = $_SESSION['dcn'.$i];
$i++;
}

for($i = 1; $i <= 2; $i++)
{
$suit = mt_rand(1,4);
$card = mt_rand(1,13);
$pcs[$cards+1] = $suit;
$pcn[$cards+1] = $card;
}

while(($pcs[$cards+1]+$pcn[$cards+1] == $pcs+$pcn) || ($pcs[$cards+1]+$pcn[$cards+1] == $dcs+$dcn))
{
for($i = 1; $i <= 2; $i++)
{
$suit = mt_rand(1,4);
$card = mt_rand(1,13);
$pcs[$cards+1] = $suit;
$pcn[$cards+1] = $card;
}
}

$_SESSION['cards'] = $_SESSION['cards']+1;
$_SESSION['pcs'.$_SESSION['cards']] = $pcs[$cards+1];
$_SESSION['pcn'.$_SESSION['cards']] = $pcn[$cards+1];
///////////////////////////////////////////////////////////////////////
$i = 1;
$pcv = array();
while((isset($_SESSION['pcs'.$i])) && (isset($_SESSION['pcn'.$i])))
{
$pcv[i] = $pcn[i];
if(($pcn[$i] == 11) || ($pcn[$i] == 12) || ($pcn[$i] == 13))
{
$pcv[i] = 10;
}
$i++;
}

$pcvt = array_sum($pcv);
echo $pcvt."<br>";
if($pctv > 21)
{
echo $pctv." your bust!!!";
}
////////////////////////////////////////////////////////////////////////
Print <<< EOT
<center>
<table><tr>
<td>Dealers cards</td><td><img src={$dcs[1]}/{$dcn[1]}.gif> <img src={$dcs[2]}/{$dcn[2]}.gif></td>
</tr>
<tr>
<td>Players cards</td>
<td>
EOT;
for($i = 1; $i <= $_SESSION['cards']; $i++)
{
Print <<< EOT
<img src={$pcs[$i]}/{$pcn[$i]}.gif>&nbsp;
EOT;
}
Print <<< EOT
</td>
</tr>
<td colspan=2>
<center>
<form action=index.php method=post>Players options &nbsp; &nbsp; &nbsp;&nbsp;
<input type='submit' name='submit' value='Hit'>
<input type='submit' name='submit' value='stand'>
<input type='submit' name='submit' value='quit'>
</form></center></td>
<tr>
</center>
EOT;
}
?>

Posted: Wed Mar 10, 2004 9:00 am
by JayBird
have ya done a quick

Code: Select all

echo "<pre>";
print_r($pvc);
echo "</pre>";
Just to check what is in that array??

Post the result

Mark

Posted: Wed Mar 10, 2004 9:06 am
by dull1554
nope ill try that

Posted: Wed Mar 10, 2004 9:19 am
by dull1554
i can tell that the $pcs and $pcn arrays are set with the proper numbers, but i guess what is not working is this statement

Code: Select all

$i = 1;
$pcv = array();
while((isset($_SESSION['pcs'.$i])) && (isset($_SESSION['pcn'.$i])))
{
$pcv[i] = $pcn[i];
if(($pcn[$i] == 11) || ($pcn[$i] == 12) || ($pcn[$i] == 13))
{
$pcv[i] = 10;
}
$i++;
}

Posted: Wed Mar 10, 2004 9:21 am
by JayBird
what did the code i give you return?

Posted: Wed Mar 10, 2004 9:32 am
by dull1554
nothing

Posted: Wed Mar 10, 2004 9:32 am
by dull1554
thats why i think the lop setting $pcv is broken

Posted: Wed Mar 10, 2004 11:10 am
by dull1554
all i want it to do is look at $_SESSION['pcn'.$i] and if it is set then to set
pcv[$i] to $_SESSION['pcn'.$i], and if $pcv[$i] >= 11 =====>>>>>$pcv[$i] = 10;

i so changed it to this
and got it to work

Code: Select all

$i = 1;
$pcv = array();
while(isset($_SESSION['pcn'.$i]))
{
$pcv[$i] = $_SESSION['pcn'.$i];
if($pcv[$i] >= 11)
{
$pcv[$i] = 10;
}
$i++;
}
echo "<pre>";
print_r($pcv);
echo "</pre>";
$pcvt = array_sum($pcv);
if($pcvt > 21)
{
echo $pcvt." your bust!!!";
}