My program is checking for a winner in my blackjack game after the user clicks “stand”.
When I try calling the winner with AJAX, I want to see if there is a winner. If yes, then alert the winner. Else, allow the dealer to make his move. Any help would be greatly appreciated.
Sample result/output after stand: User hand = dQ, hJ; val = 20 – alerts ” wins! in stand”
Expected result after this hand: – go to ajaxDealer(user);
php code:
($checkPlayerHandValue works fine, i.e. if User has C5,HQ, will return 15)
function checkWinner($u, $d, $uValue) {
if($uValue > 21)
return $d;
}
if($checkPlayerHandValue > 0) {
$checkPlayerHandValue = checkPlayerHandValue($user);
$winner = checkWinner($user, $dealer , $checkPlayerHandValue);
}
$row = mysql_fetch_array($checkNextTurn);
$playerTurn = $row['user'];
echo($playerTurn."|");
echo($winner);
js code:
xmlhttpB.onreadystatechange=function() {
if (xmlhttpB.readyState==4 && xmlhttpB.status==200) {
elements = xmlhttpB.responseText.split("|");
user = elements[0];
winner = elements[1];
//alert(winner);
if(winner != "")
ajaxDealer(user);
else
alert(winner + " wins! in stand");
}
clickButton = true;
}
Do yourself a favor and start using the jQuery library with it’s AJAX module.
As far as your question goes, can you tell us what the current problem with your code is (what is not happening that should be happening, or vice versa).
Also, I think this logic is incorrect:
And should be flipped: