pepper_roni Posted December 14, 2010 Share Posted December 14, 2010 I'm making a blackjack program for my final project in programming fundamentals.... but it is having a problem. here is the code: <?php /* This is were the magic happens */ require("utils.inc"); $player = 0; $card = 0; welcome(); $randomNum = randomNumber(); if($randomNum == 11) { $card = 10; echo "\n\nYou have been dealt a Jack\n\n"; } elseif($randomNum == 12) { $card = 10; echo "\n\nYou have been dealt a Queen\n\n"; } elseif($randomNum == 13) { $card = 10; echo "\n\nYou have been dealt a King\n\n"; } else { $card = $randomNum; echo "\n\n You have been dealt a " . $card; } $player = $card; $dealer = dealer(); nextCard($card); function nextCard($card) { $count = 0; $stop = false; for($count = 0; $stop == true; $count++) { $hit = promptForDecision("Would you like to hit or stand?"); if($hit = true) { $randomNum = randomNumber(); if($randomNum == 11) { $card = 10; echo "\n\nYou have been dealt a Jack\n\n"; } elseif($randomNum == 12) { $card = 10; echo "\n\nYou have been dealt a Queen\n\n"; } elseif($randomNum == 13) { $card = 10; echo "\n\nYou have been dealt a King\n\n"; } else { $card = $randomNum; echo "\n\n You have been dealt a " . $card; } } else { } } return $card; } function randomNumber() { $randomNum = 0; $randomNum = rand(1,13); return $randomNum; } function dealer() { $i = 0; $dealer = 0; $stop = false; for($i = 0; $stop == true; $i++) { $randomNum = randomNumber(); $dealer = $dealer + $randomNum; if($dealer > 17) { $stop = true; } } return $dealer; } function welcome() { echo "\n\n**********Welcome To**************\n\n\n"; echo "*************************************\n"; echo "**************Black Jack***************\n"; echo "*****************************************\n"; } ?> the program gets to the nextCard function, but then just stops, it doesnt give a prompt to hit or stand or anything like that. Hope you can help. Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/ Share on other sites More sharing options...
jdavidbakr Posted December 14, 2010 Share Posted December 14, 2010 Where is promptForDecision() defined? I've never done PHP interactively on the CL so I'm not familiar with getting user input. Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147353 Share on other sites More sharing options...
Maq Posted December 14, 2010 Share Posted December 14, 2010 Please use tags. Fatal error maybe. Add these two lines directly after your opening PHP tag: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147364 Share on other sites More sharing options...
pepper_roni Posted December 14, 2010 Author Share Posted December 14, 2010 No errors reported back, and ill use code tags from now on Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147374 Share on other sites More sharing options...
Maq Posted December 14, 2010 Share Posted December 14, 2010 $stop is false, so your for won't start. $stop = false; for($count = 0; $stop == true; $count++) Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147378 Share on other sites More sharing options...
MMDE Posted December 14, 2010 Share Posted December 14, 2010 I remember I wrote a black jack game... with a dealer bot. Do you want it to be one deck? So people can't get the same card twice in a row, or in the same round that is. You may want to check if they have already been given out. You may also consider just putting the whole deck into an array with name and value. If total value>21 then you should check if one of the cards are an ace, and if so sub 10 from total value! also when you hit 21 with the two first cards = instant win. If it's a tie between player and bot = the least cards is the winner. also when you've drawn i think 6 cards = instant win. Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147385 Share on other sites More sharing options...
pepper_roni Posted December 14, 2010 Author Share Posted December 14, 2010 $stop is false, so your for won't start. $stop = false; for($count = 0; $stop == true; $count++) $stop is assigned to false, and the loop is set to stop when $stop equals true so that isnt it, but it was my first thought at what was wrong. Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147387 Share on other sites More sharing options...
jdavidbakr Posted December 14, 2010 Share Posted December 14, 2010 $stop is false, so your for won't start. $stop = false; for($count = 0; $stop == true; $count++) $stop is assigned to false, and the loop is set to stop when $stop equals true so that isnt it, but it was my first thought at what was wrong. No, the loop will run as long as $stop == true Remember, a normal loop condition would be like this: for($i=0; $i<10; $i++) to loop 10 times, or as long as $i < 10. Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147391 Share on other sites More sharing options...
Maq Posted December 14, 2010 Share Posted December 14, 2010 I don't get the logic of this for loop... But w/e. for($count = 0; $stop == true; $count++) This is always true. if($hit = true) Have you echoed out to see where the code stops? Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147416 Share on other sites More sharing options...
.josh Posted December 14, 2010 Share Posted December 14, 2010 umm..no. Maq is right. You make $stop false and then make the condition to your loop be to run as long as $stop is true. A for loop runs as long as the condition evaluates true. Since true != false, the loop is never executed. And in your example, if for instance the current iteration of $i is 1 and the condition is $i < 10, that evaluates true, because 1 < 10 is true. p.s. - even if you change it to initially be true, you never actually change the value of $stop in the loop, so you will next open yourself up to an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147439 Share on other sites More sharing options...
pepper_roni Posted December 15, 2010 Author Share Posted December 15, 2010 ah, ya thanks for the info guys... only been learning php for the last few months and i keep screwing stuff like that up... illl get it eventually.... hopefully Quote Link to comment https://forums.phpfreaks.com/topic/221676-blackjack/#findComment-1147452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.