problem with array sum
Posted: Wed Mar 10, 2004 8:46 am
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
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
<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>
EOT;
}
Print <<< EOT
</td>
</tr>
<td colspan=2>
<center>
<form action=index.php method=post>Players options
<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;
}
?>