6 Replies - 4371 Views - Last Post: 17 January 2012 - 07:44 PM Rate Topic: -----

#1 NONO123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-January 12

Game of 21 Card Game, code for checking score is not working?

Posted 13 January 2012 - 07:35 PM

Check Score:
The winner is the one with the highest score less than or equal to 21
If both scores(player total and computer total) are over 21, or if both are equal but under 21, the game is declared a draw.
There is something wrong with this line:
If intPlayerCardTotal < intScore And intComputerTotal < intScore Then
MsgBox "Draw"
ElseIf intPlayerCardTotal < intComputerTotal And intComputerTotal < intScore Then
MsgBox " Player Wins"
ElseIf intComputerTotal < intPlayerCardTotal And intPlayerCardTotal < intScore Then
MsgBox "Computer Wins"
End If


Option Explicit
Private intPlayerCard1 As Integer, intPlayerCard2 As Integer
Private intComputerCard1 As Integer, intComputerCard2 As Integer, intComputerCard3 As Integer
Private intComputerTotal As Integer, intPlayerCardTotal As Integer
Private intRandomNumber7 As Integer, intRandomNumber8 As Integer
Private intRandomNumber9 As Integer, intRandomNumber10 As Integer
Private intCounter As Integer
Private intScore As Integer


Private Sub Form_Load()
    Randomize
    intCounter = 0
    
   
End Sub

Private Sub cmdPlay_Click()

    intCounter = 0
    ' Player is dealt two random cards
    intPlayerCard1 = Int(10 * Rnd) + 1
    lblRandomNumber1.Caption = intPlayerCard1
    intPlayerCard2 = Int(10 * Rnd) + 1
    lblRandomNumber2.Caption = intPlayerCard2
    
    ' Computer is dealt three random cards but is not revealed
    intComputerCard1 = Int(10 * Rnd) + 1
    lblComputerCard1.Caption = "*"
    intComputerCard2 = Int(10 * Rnd) + 1
    lblComputerCard2.Caption = "*"
    intComputerCard3 = Int(10 * Rnd) + 1
    lblComputerCard3.Caption = "*"
    
     lblRandomNumber7.Caption = ""
    lblRandomNumber8.Caption = ""
    lblRandomNumber9.Caption = ""
    lblRandomNumber10.Caption = ""
    
   
End Sub
Private Sub cmdCheckScore_Click()

    ' Computer is dealt three random cards
    lblComputerCard1.Caption = intComputerCard1
    lblComputerCard2.Caption = intComputerCard2
    lblComputerCard3.Caption = intComputerCard3
    
    ' Adds total of the players cards and the computers cards
    intComputerTotal = intComputerCard1 + intComputerCard2 + intComputerCard3
    intPlayerCardTotal = intPlayerCard1 + intPlayerCard2
    intPlayerCardTotal = intPlayerCardTotal + intRandomNumber7 + intRandomNumber8 + intRandomNumber9 + intRandomNumber10
    intScore = 21
    
    If intPlayerCardTotal < intScore And intComputerTotal < intScore Then
        MsgBox "Draw"
    ElseIf intPlayerCardTotal < intComputerTotal And intComputerTotal < intScore Then
        MsgBox " Player Wins"
    ElseIf intComputerTotal < intPlayerCardTotal And intPlayerCardTotal < intScore Then
     MsgBox "Computer Wins"
    End If
End Sub

Private Sub cmdDrawCard_Click()

    
    Dim intDrawCard As Integer
    
    intCounter = intCounter + 1

    If intCounter = 1 Then
    intRandomNumber7 = Int(10 * Rnd) + 1
    lblRandomNumber7.Caption = intRandomNumber7
    intPlayerCardTotal = intPlayerCardTotal + intRandomNumber7
    End If
    
    If intCounter = 2 Then
    intRandomNumber8 = Int(10 * Rnd) + 1
    lblRandomNumber8.Caption = intRandomNumber8
    intPlayerCardTotal = intPlayerCardTotal + intRandomNumber7 + intRandomNumber8
    End If

    If intCounter = 3 Then
    intRandomNumber9 = Int(10 * Rnd) + 1
    lblRandomNumber9.Caption = intRandomNumber9
    intPlayerCardTotal = intPlayerCardTotal + intRandomNumber7 + intRandomNumber8 + intRandomNumber9
    End If
 
    If intCounter = 4 Then
    intRandomNumber10 = Int(10 * Rnd) + 1
    lblRandomNumber10.Caption = intRandomNumber10
    intPlayerCardTotal = intPlayerCardTotal + intRandomNumber7 + intRandomNumber8 + intRandomNumber9 + intRandomNumber10
    End If
    
End Sub



Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: Game of 21 Card Game, code for checking score is not working?

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Game of 21 Card Game, code for checking score is not working?

Posted 13 January 2012 - 07:50 PM

Quote

There is something wrong with this line

What is wrong? what it gives which is not what you expect?

Quote

The winner is the one with the highest score less than or equal to 21

You said the winner is the one with the highest score less than 21, then here you say:
ElseIf intPlayerCardTotal < intComputerTotal And intComputerTotal < intScore Then
     MsgBox " Player Wins"

which means if player has less than computer and below 21, then he wins!!!

Also think of having the default else part an use AndAlso instead of AND
Was This Post Helpful? 0
  • +
  • -

#3 NONO123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-January 12

Re: Game of 21 Card Game, code for checking score is not working?

Posted 13 January 2012 - 08:02 PM

How about this?
If intPlayerCardTotal > intComputerTotal And intPlayerCardTotal >= intScore Then
        MsgBox "Player wins"
    ElseIf intComputerTotal > intPlayerCardTotal And intComputerTotal >= intScore Then
        MsgBox "Computer Wins"
    ElseIf intComputerTotal >= intScore And intPlayerCardTotal >= intScore Then
         MsgBox "Draw"
    End If

This post has been edited by smohd: 13 January 2012 - 08:06 PM
Reason for edit:: removed duplicate quotes

Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Game of 21 Card Game, code for checking score is not working?

Posted 13 January 2012 - 08:08 PM

Did you test it? what it prints? As I told you before, you are the one to tell us what it should and what it doesnt!!! Do you expect us to test your code for you?
Test and tell us if it gives what you wanted or not, and if not then what it gives
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Game of 21 Card Game, code for checking score is not working?

Posted 15 January 2012 - 02:57 PM

I assume that you are attempting to implement a blackjack game.

Based on your if and elseif statements, you have implemented the following logic:

1. If neither player busts or hits 21, the hand is drawn.
2. If player is less than computer, and neither player has 21 or more, player wins.
3. If computer is less than player, and neither player has 21 or more, computer wins.
4. Any other condition is not evaluated.

Not the game of blackjack I'm familiar with...

So, your second attempt then implements this logic:

1. If player busts or hits 21, player wins so long as computer doesn't bust bigger than he does.
2. If computer busts or hits 21, computer wins so long as player doesn't bust bigger than computer.
3. If both computer and player bust the same amount, or both hit 21, that's a draw.
4. Any other possible condition is not evaluated.

Looks like you need to reread your professor's logic statement until you understand it fully. Then try again.
Was This Post Helpful? 0
  • +
  • -

#6 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,675
  • Joined: 24-August 11

Re: Game of 21 Card Game, code for checking score is not working?

Posted 16 January 2012 - 03:39 PM

what BobRodes is getting at is that before you worry about anything else, you need to take a good long look at your basic mathematical logic. you are consistently reversing the inequality evaluators, which kills any chance of correct output. once the mathematical logic is straightened out, then you can start posting errors/outputs so we can see what's going wrong.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: Game of 21 Card Game, code for checking score is not working?

Posted 17 January 2012 - 07:44 PM

Yup. So, I would suggest this logic:

1. If both scores are over 21, draw.
2. ElseIf one score is over 21, other wins.
3. ElseIf scores are equal, draw.
4. Else, higher score wins.

Get all your +21s out of the way first, and you'll narrow down the options that you have to deal with. It's like having a set of strainers, each one finer than the one before. If it helps to think of it that way, that is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1