problem with array sum

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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

problem with array sum

Post 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;
}
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

nope ill try that
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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++;
}
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

what did the code i give you return?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

nothing
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

thats why i think the lop setting $pcv is broken
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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!!!";
}
Post Reply