1 Replies - 486 Views - Last Post: 12 December 2010 - 05:24 PM Rate Topic: -----

#1 Guest_tangerine-rain*


Reputation:

I think I have a bug in my program...

Posted 12 December 2010 - 01:09 AM

I'm using BASIC to write a blackjack program for school. I followed the directions exactly, yet there still seems to be a problem with the program. It's supposed to stop looping and move on to the next line of code if the player indicates that they don't want another card, but instead it keeps asking "Would you like another card?" over and over, and I'm not sure why.

For clarity's sake,the instructions I was given are as follows:

The program is basically 4 blocks of code. Here is a skeleton of what you will have to do:

•Set a variable like AGAIN to 1 (this variable will control your loops, 1 means it will loop, 0 means it will terminate)
•Use INPUT with a string variable that asks for Yes or No to the question, do you want to play black jack.
•Use an IF statement to check is the user said Yes or NO, if they said Yes, you need to use the INT statement that was given to you in the directions and set it to a variable like PlayerHand and then print out the random value with a statement like “You start with:”;PlayerHand. If the answer is NO, you have to Print a goodbye statement and then tack on :END to that to make the program terminate (be sure to use the colons between the print statement and the End statement)

Now, set up a DO WHILE AGAIN=1 block that contain the following:

•Ask if they want another card with a string variable like Card$
•If Card$ is Yes, then set a numeric variable like Card to the Random INT statement given to you in the program instructions, print out the result and add it to PlayerHand
•Then check PlayerHand to see if it is greater than 21 (with an IF statement) if it is, tell them they are busted and the computer wins, and end the program with :END
• If Card$ is not Yes, then set Again=0
•Print “your Total is”;PlayerHand
•Loop

Set AGAIN to 1 and then Set up another DO WHILE AGAIN=1 block for the dealers hand that looks like this:

•Set a variable like Card to the Random INT statement given in the instructions
•Set a variable like DealerHand to DealerHand + Card
•Check to see if DealerHand is greater than or equal to 16 and Less than or equal to 21, if it is set Again to 0 (so the loop ends)
•Check to see if DealerHand is greater than 21, if it is print a message that the dealer is bust and you win and use :END to terminate the program
•Print “Dealers Hand is:”; DealerHand
•Loop

The last part is to compare PlayerHand and DealerHand:

•If PlayerHand is less than DealerHand, then Print Dealer wins
•If PlayerHand is greater than DealerHand, then Print You win
•If PlayerHand is equal to DealerHand, Print It is a draw.

And this is my program:

again=1
print "Do you want to play blackjack? Enter yes or no.";
input answer$
if answer$ = "yes" then
let playerhand = int(rnd(1)*10)+1
print "You start with "; playerhand
else
if answer$ = "no" then
print "Goodbye!":end
end if
end if
do while again=1
print "Do you want another card? Enter yes or no."
input card$
if card$ = "yes" then
let card = int(rnd(1)*10)+1
let playerhand = card+playerhand
print "You now have "; playerhand; " in your hand."
if playerhand > 21 then
print "Busted! You loose. :(/>":end
else
if card$ = "no" then
again=0
print "your total is "; playerhand
end if
end if
end if
loop
again=1
do while again=1
let card = int(rnd(1)*10)+1
let dealerhand = dealerhand+card
if dealerhand >= 16 and dealerhand <= 21 then
again=0
if dealerhand > 21 then
print "Dealer is bust. You win! :)/>":end
else
print "Dealer's hand is "; dealerhand
end if
end if
loop
if playerhand < dealerhand then
print "Dealer wins."
else
if playerhand > dealerhand then
print "You win."
else
if playerhand = dealerhand then
print "It's a draw."
end if
end if
end if
end


Is This A Good Question/Topic? 0

Replies To: I think I have a bug in my program...

#2 calvinthedestroyer  Icon User is offline

  • D.I.C Lover

Reputation: 166
  • View blog
  • Posts: 1,904
  • Joined: 13-October 07

Re: I think I have a bug in my program...

Posted 12 December 2010 - 05:24 PM

You have "again=0" nested in another IF statment, If the Player is over 21 it will print that they loose, but then would keep on playing since you did not reset the again variable to "no"
if playerhand > 21 then
        print "Busted! You loose. :(/>":end
else
	if card$ = "no" then
                 again=0
                 print "your total is "; playerhand
        end if
end if



Add another again=0 to your code like this:
if playerhand > 21 then
        print "Busted! You loose. :(/>":end
        again=0
else
	if card$ = "no" then
                 again=0
                 print "your total is "; playerhand
        end if
end if



Oh, and please use tab spacing to make it easier to follow all of your IF statements :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1