Jump to content

blackjack


pepper_roni

Recommended Posts

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.

Link to comment
Share on other sites

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! :P 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.

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.