Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help with 21 card game
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bao_luk




PostPosted: Thu May 15, 2008 8:56 pm   Post subject: Help with 21 card game

Here is the question:
Create a Game 21 application to simulate a simplified version of the game '21'. A deck of cards numbered 1 through 10 is used and any number can be repeated. The computer starts by dealing you (the user) two randomly picked cards, and deals itself three randomly picked cards that are not revealed until the Check Scores button is clicked. You may then draw as many cards as you want, one by one. If both scores are over 21, or if both are equal but under 21, the game is declared a draw. Otherwise, the winner is the one with highest score less than or equal to 21. If one score is over 21 and the other is 21 or less, the player with 21 or less is declared the winner. The result should be displayed in a message box.

So what I did is this:
VisualBASIC:

Option Explicit

Private intPlayer As Integer
Private intComputer As Integer

Private Sub Form_Load()
Randomize
lblPlayerDealt.Caption = Int(10 * Rnd + 1)
lblPlayerDealt2.Caption = Int(10 * Rnd + 1)
End Sub

Private Sub cmdDraw_Click()  [color=#ff0088]<--This is what happens when I click the command button "Draw Card"[/color]
lblPlayerDrew.Caption = Int(10 * Rnd + 1)
End Sub

Private Sub cmdCheck_Click()  [color=#ff0088]<-- Command button "Check Scores"[/color]
lblCompDealt.Caption = Int(10 * Rnd + 1)
lblCompDealt2.Caption = Int(10 * Rnd + 1)
lblCompDealt3.Caption = Int(10 * Rnd + 1)

intPlayer = lblPlayerDealt + lblPlayerDealt2 + lblPlayerDrew
intComputer = lblCompDealt + lblCompDealt2 + lblCompDealt3

If intPlayer > 21 And intComputer > 21 Or intPlayer = intComputer Then
    MsgBox "That's a draw."
ElseIf intPlayer = 21 Or intPlayer > intComputer And intPlayer < 21 Then
    MsgBox "Player wins."
ElseIf intComputer = 21 Or intComputer > intPlayer And intComputer < 21 Then
    MsgBox "Computer wins."
ElseIf intPlayer > 21 And intComputer < 21 Then
    MsgBox "Computer wins."
ElseIf intPlayer < 21 And intComputer > 21 Then
    MsgBox "Player wins."
Else
End If

End Sub

Private Sub cmdPlay_Click()  [color=#ff0088]<-- Command button "Play again"[/color]
lblPlayerDealt.Caption = Int(10 * Rnd + 1)
lblPlayerDealt2.Caption = Int(10 * Rnd + 1)
lblCompDealt.Caption = ""
lblCompDealt2.Caption = ""
lblCompDealt3.Caption = ""
lblPlayerDrew.Caption = ""
End Sub

Private Sub cmdDone_Click()   [color=#ff0088]<-- Command button "Done"[/color]
Unload frmGame21
End Sub


But there are some errors...
The Message Box is always displaying "That's a Draw." no matter what,
even when the player or the computer is supposed to win.
So is there any problem with my If statements?
and one more question is that it says "You may then draw as many cards as you want, one by one. "
So how do I let the program display one card each time when I click the button "Draw Card"??

Thanks a lot,,hope I can get a solution to that.
and please reply ASAP =)
Sponsor
Sponsor
Sponsor
sponsor
Steven Toews




PostPosted: Fri May 16, 2008 1:02 pm   Post subject: Re: URGENT!!!------>> Can someone help me??

Well I'm not at all fluent with Visual Basic, but I would recommend trying just if statements inside of a function instead of have a bunch of else ifs. Else if, has been known to cause errors or not work most of the time with alot of languages I've encountered, such as flash actionscript.
bao_luk




PostPosted: Sat May 17, 2008 6:13 am   Post subject: Re: URGENT!!!------>> Can someone help me??

Steven Toews @ Fri May 16, 2008 1:02 pm wrote:
Well I'm not at all fluent with Visual Basic, but I would recommend trying just if statements inside of a function instead of have a bunch of else ifs. Else if, has been known to cause errors or not work most of the time with alot of languages I've encountered, such as flash actionscript.


What will be that function??
McKenzie




PostPosted: Sat May 17, 2008 7:06 am   Post subject: Re: URGENT!!!------>> Can someone help me??

First, I'll warn you that putting URGENT and ASAP in your post make most people not want to help you. If you are asking for a favour it is rude to DEMAND results quickly.

That said, you have a number of errors in your program. I think the one that is causing the tie is:
code:
intPlayer = lblPlayerDealt + lblPlayerDealt2 + lblPlayerDrew
intComputer = lblCompDealt + lblCompDealt2 + lblCompDealt3


Types in VB are kinda funky. The values in the labels are strings. If you assign a string to an integer variable VB converts the string to an int if it can, if you try to add a string to an int it will convert the string. What you are doing here is adding the strings then assigning the value to an integer. So if the card values are "4", "10", "7" you get an integer 4107. You want to convert each of the labels to integer BEFORE you add them (I believe it is the Int() function.)
bao_luk




PostPosted: Sat May 17, 2008 12:33 pm   Post subject: Re: URGENT!!!------>> Can someone help me??

McKenzie @ Sat May 17, 2008 7:06 am wrote:
First, I'll warn you that putting URGENT and ASAP in your post make most people not want to help you. If you are asking for a favour it is rude to DEMAND results quickly.

That said, you have a number of errors in your program. I think the one that is causing the tie is:
code:
intPlayer = lblPlayerDealt + lblPlayerDealt2 + lblPlayerDrew
intComputer = lblCompDealt + lblCompDealt2 + lblCompDealt3


Types in VB are kinda funky. The values in the labels are strings. If you assign a string to an integer variable VB converts the string to an int if it can, if you try to add a string to an int it will convert the string. What you are doing here is adding the strings then assigning the value to an integer. So if the card values are "4", "10", "7" you get an integer 4107. You want to convert each of the labels to integer BEFORE you add them (I believe it is the Int() function.)


Sorry and thanks but I still have one question,,
how do I let the program add up the numbers displayed automatically everytime I click the button "Draw Card"??
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: