Results 1 to 39 of 39

Thread: Card game Help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Card game Help

    Im kinda new to visual basic, so im making a card war game that is with the user vs the computer has buttons called NewGame, Nextcard, and stop. So basically when the user chooses next card another card will display on both sides of the user and the computer and whoever has the highest authority of the card gets 2 points and if its a draw they each get 1 point and i cant repeat cards.

    How do i display the 52 deck of cards in a picture box, keep in mine i will
    1

    have 2 picture boxes as one is for the user and the other is for the computer?

    How would the user be able to stop the game using the stop button?


    Heres My code so far:
    Code:
    Public Class Form1
    02
        Dim card(52) As Integer
    03
        Dim shuffle As New Random
    04
        Dim cardnumber As Integer = 1
    05
        Dim cardpick As Integer = 2
    06
        Dim cardpickcomputer As Integer = 1
    07
        Dim Y, X As Integer
    08
     
    09
     
    10
        Private Sub BtnNewgame_Click(sender As Object, e As EventArgs) Handles BtnNewgame.Click
    11
            BtnNewgame.Hide()
    12
            For shuffleCard = 1 To 52
    13
    StartPosition: X = shuffle.Next(53) + 1
    14
                For Y = 1 To 52
    15
                    If card(Y) = X Then
    16
                        Do While StartPosition
    17
     
    18
                        Loop
    19
                    End If
    20
                Next Y
    21
                card(cardnumber) = X
    22
                cardnumber = cardnumber + 1
    23
            Next shuffleCard
    24
     
    25
        End Sub
    26
     
    27
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    28
     
    29
        End Sub
    30
     
    31
        Private Sub BtnNextcard_Click(sender As Object, e As EventArgs) Handles BtnNextcard.Click
    32
     
    33
        End Sub
    34
     
    35
        Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    36
     
    37
        End Sub
    38
     
    39
        Private Sub BtnStop_Click(sender As Object, e As EventArgs) Handles BtnStop.Click
    40
     
    41
        End Sub
    42
     
    43
        Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
    44
     
    45
        End Sub
    46
    End Class
    Last edited by dday9; Jul 24th, 2017 at 01:57 PM. Reason: Added Code Tags

  2. #2

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    2/ Use a Form level boolean:

    Code:
    Dim cancelled as boolean
    Then in your loop:

    Code:
    If cancelled Then Exit For
    You can shuffle your deck like so:

    Code:
    card = card.OrderBy(Function(c) shuffle.NextDouble).ToArray

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    in your code the handviewer code always runs an error and says
    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    blackJackGame.Form1.HandViewer1.get returned Nothing.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    You downloaded my code and it won't run? Did you have to upgrade and chose make backup?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    no i didnt have to do that

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    Give me two minutes. I'll check it. What version of vb are you using?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    like it runs only to the point where you enter your name then its takes me to an error

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    im using visual studio

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    Just to be sure... It's my project that won't run, or you've used part in your own project. I haven't looked at the project for a long time, but I'm fairly sure the card images it uses are all contained in My.Resources

    Project-->Properties-->Resources - images (menuitem)

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    your project wont run

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    that might also be the case since you havent looked at the project in years. Doesnt vb change every year

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    Ok. I just checked it. It's written in Visual Studio 2015. If you're using an earlier version it won't run as it is.
    Can you open Help-->About in your visual studio? It'll tell you which version you're using...

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    cause i noticed that nothing else was giving an error just the handviewer part in form 1 so i dont know what is wrong with that part

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    its visual basic 2017

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    Its finally working paul. Thank you

  17. #17
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    OK. I just created a new zip archive for you, but now you have it working. No problem...

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    one more question paul so if i wanted to create a score board using label boxes how would i do that. like if it shows who won, how would i display 2 points for the winner using the two labels and yes im talking about your code

  19. #19
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    Can you draw a quick diagram of your score board?

    To keep a count of the score:

    Dim scores() As Integer ={0, 0}

    Where there are 2 elements in the array.
    If the player wins:

    Scores(0) +=1

    Or if the computer wins:

    Scores(1) += 1

    And after:

    Label1.Text = $"Player: {scores(0)}{environment.newline}Computer: {scores(1)}"

    Where Label1 is the name of your score board label

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    Don't worry about the diagram. The instructions will work. The array should be declared at form level

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    sorry how do i draw a quick diagaram

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    but then in your code when it says who wins how would you relate that to the label boxes

  23. #23
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    My game is blackjack. Are you writing a new game or just modifying mine? I haven't looked at the code recently, or memorized it line for line. Post the code snippet you're referring to...

  24. #24
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    Quote Originally Posted by lisamorgan1 View Post
    but then in your code when it says who wins how would you relate that to the label boxes
    The directions i gave you in post #19 are fairly clear

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    This is your code
    Code:
    Dim w As Integer = Count.getWinner(Count.getSum(getPlayer, True), Count.getSum(getOpponent, True))
                    RaiseEvent displayText(If(w = 1, getPlayer().getName() + " won", If(w = 2, getOpponent().getName() + " won", "No winner")))
                End If
            End If
    so when it says who won i want to display 2 point to the winner each time they win in these label boxes

    Code:
    Private Sub Lblscoreyou_Click(sender As Object, e As EventArgs) Handles Lblscoreyou.Click
    
    
    
    
        End Sub
    
        Private Sub Lblscorecomputer_Click(sender As Object, e As EventArgs) Handles Lblscorecomputer.Click
    
        End Sub
    End Class
    Code:
    Public Class Form1
    
        Private WithEvents game As BlackJack
        Dim scores() As Integer = {0, 0}
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
            Dim d As New InputDialog
            'this shows a modal Dialog with a prompt, and a  title, with no DefaultResponse
            If d.ShowDialog("Enter your name", "OOP BlackJack") = DialogResult.OK Then
                Dim playerName As String = d.returnedValue
                HandViewer1.setName(playerName)
                HandViewer2.setName("Computer")
                game = New BlackJack("Computer", playerName)
                BtnNextcard.PerformClick()
            Else
                Application.Exit()
            End If
        End Sub
    
        Private Sub btnNewGame_Click(sender As Object, e As EventArgs) Handles BtnNextcard.Click
            HandViewer1.setImage()
            HandViewer2.setImage()
            btnHit.Enabled = True
            btnStick.Enabled = True
            BtnNextcard.Enabled = True
            game.NewGame()
        End Sub
    
        Private Sub btnStick_Click(sender As Object, e As EventArgs) Handles btnStick.Click
            game.stick()
        End Sub
    
        Private Sub btnHit_Click(sender As Object, e As EventArgs) Handles btnHit.Click
            game.hit()
        End Sub
    
        Private Sub game_displayHand(hand As List(Of Card), player As Integer) Handles game.displayHand
            If player = 1 Then
                HandViewer1.setImage(hand)
            Else
                HandViewer2.setImage(hand)
            End If
        End Sub
    
        Private Sub game_displayText(text As String) Handles game.displayText
            Label1.Text &= text & Environment.NewLine & Environment.NewLine
        End Sub
    
        Private Sub game_clearText() Handles game.clearText
            Label1.Text = ""
        End Sub
    
        Private Sub game_isOver() Handles game.isOver
            btnHit.Enabled = False
            btnStick.Enabled = False
            BtnNextcard.Enabled = True
        End Sub
    
        Private Sub HandViewer1_Resize(sender As Object, e As EventArgs) Handles HandViewer1.Resize
            HandViewer2.Left = HandViewer1.Right + 6
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            End
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
        End Sub
    
        Private Sub Lblscoreyou_Click(sender As Object, e As EventArgs) Handles Lblscoreyou.Click
    
    Code:
    Public Class BlackJack
    
        Public Event displayHand(hand As List(Of Card), player As Integer)
        Public Event displayText(text As String)
        Public Event clearText()
        Public Event isOver()
    
        Private deck As Deck
    
    
    
    
        Public Sub New(player1 As String, player2 As String)
            getOpponent = New Player(player1)
            getPlayer = New Player(player2)
            deck = New Deck
            playerStarts = True
        End Sub
    
        Public Sub NewGame()
            deck.createDeck()
            gameOver = False
            moveCounter = If(playerStarts, 0, 1)
            playerStarts = Not playerStarts
    
            getPlayer.getHand.Clear()
            getOpponent.getHand.Clear()
    
            deck.deal(1, getOpponent)
            deck.deal(1, getPlayer)
            deck.deal(1, getOpponent)
            deck.deal(1, getPlayer)
    
            getPlayer.hasStuck = False
            getOpponent.hasStuck = False
    
            display = True
    
            RaiseEvent clearText()
            If moveCounter = 0 Then
                RaiseEvent displayText("Your hand: " + getPlayer.getHandAsString + Count.getScoreString(Count.getSum(getPlayer, True), getPlayer))
                RaiseEvent displayHand(getPlayer.getHand, 1)
                play()
            Else
                hit()
            End If
    
        End Sub
    
    #Region "     Properties"
    
        Private _playerStarts As Boolean
        Private Property playerStarts() As Boolean
            Get
                Return _playerStarts
            End Get
            Set(ByVal value As Boolean)
                _playerStarts = value
            End Set
        End Property
    
        Private _gameOver As Boolean
        Private Property gameOver() As Boolean
            Get
                Return _gameOver
            End Get
            Set(ByVal value As Boolean)
                _gameOver = value
            End Set
        End Property
    
        Private _moveCounter As Integer
        Private Property moveCounter() As Integer
            Get
                Return _moveCounter
            End Get
            Set(ByVal value As Integer)
                _moveCounter = value
            End Set
        End Property
    
        Private _player As Player
        Private Property getPlayer() As Player
            Get
                Return _player
            End Get
            Set(ByVal value As Player)
                _player = value
            End Set
        End Property
    
        Private _opponent As Player
        Private Property getOpponent() As Player
            Get
                Return _opponent
            End Get
            Set(ByVal value As Player)
                _opponent = value
            End Set
        End Property
    
        Private _display As Boolean
        Private Property display() As Boolean
            Get
                Return _display
            End Get
            Set(ByVal value As Boolean)
                _display = value
            End Set
        End Property
    
        Public Property [RaiseEvent] As String
    
    #End Region
    
        Public Sub hit()
            If Not getPlayer.hasStuck OrElse Not getOpponent.hasStuck Then
                If moveCounter Mod 2 = 0 Then
                    deck.deal(1, getPlayer)
                    RaiseEvent displayText("Your hand: " + getPlayer.getHandAsString + Count.getScoreString(Count.getSum(getPlayer, True), getPlayer))
                    RaiseEvent displayHand(getPlayer.getHand, 1)
                    Dim c As Integer = Count.getHighestBelow(getPlayer)
                    If c = -1 Then
                        'bust
                        stick()
                    Else
                        If c <= 21 Then
                            RaiseEvent displayText("Hit, stick?")
                        Else
                            stick()
                        End If
                    End If
                Else
                    Dim c As Integer = Count.getHighestBelow(getPlayer)
                    If c = -1 Then
                        'bust
                        stick(False)
                    Else
                        c = Count.getHighestBelow(getOpponent)
                        If c > -1 AndAlso c < 17 Then
                            deck.deal(1, getOpponent)
                        End If
                        c = Count.getHighestBelow(getOpponent)
                        If c = -1 Then
                            'bust
                            stick(False)
                        Else
                            If c < 17 Then
                                hit()
                            Else
                                stick(False)
                            End If
                        End If
                    End If
                End If
            End If
        End Sub
    
        Public Sub stick(Optional incrementMove As Boolean = True)
            If Not getPlayer.hasStuck OrElse Not getOpponent.hasStuck Then
                If moveCounter Mod 2 = 0 Then
                    getPlayer.hasStuck = True
                Else
                    getOpponent.hasStuck = True
                End If
                moveCounter += If(incrementMove, 1, 0)
                play()
            End If
        End Sub
    
        Private Sub play()
            If Not gameOver Then
                If Not getPlayer.hasStuck AndAlso moveCounter Mod 2 = 0 Then
                    If getOpponent.hasStuck Then
                        RaiseEvent displayText("Your hand: " + getPlayer.getHandAsString + Count.getScoreString(Count.getSum(getPlayer, True), getPlayer))
                        RaiseEvent displayHand(getPlayer.getHand, 1)
                    End If
                    display = False
                    RaiseEvent displayText("Hit, stick?")
                ElseIf Not getOpponent.hasStuck AndAlso moveCounter Mod 2 = 1 Then
                    Dim c As Integer = Count.getHighestBelow(getOpponent)
                    If c > -1 AndAlso c < 17 Then
                        hit()
                        'Return
                    Else
                        stick()
                        'Stop
                    End If
                ElseIf Not gameOver AndAlso Not getPlayer.hasStuck AndAlso getOpponent.hasStuck AndAlso moveCounter Mod 2 = 1 Then
                    moveCounter += 1
                    Dim c As Integer = Count.getHighestBelow(getOpponent)
                    If c = -1 Then
                        stick()
                    Else
                        play()
                    End If
                End If
    
                If Not gameOver AndAlso getPlayer.hasStuck AndAlso getOpponent.hasStuck Then
                    gameOver = True
                    RaiseEvent isOver()
                    If playerStarts Then
                        If display Then RaiseEvent displayText("Your hand: " + getPlayer.getHandAsString + Count.getScoreString(Count.getSum(getPlayer, True), getPlayer))
                        RaiseEvent displayHand(getPlayer.getHand, 1)
                    End If
                    RaiseEvent isOver()
                    RaiseEvent displayText(getOpponent.getName + "'s hand: " + getOpponent.getHandAsString + Count.getScoreString(Count.getSum(getOpponent, True), getOpponent))
                    RaiseEvent displayHand(getOpponent.getHand, 2)
                    Dim w As Integer = Count.getWinner(Count.getSum(getPlayer, True), Count.getSum(getOpponent, True))
                    RaiseEvent displayText(If(w = 1, getPlayer().getName() + " won", If(w = 2, getOpponent().getName() + " won", "No winner")))
                End If
            End If
    
    
    
    
    
        End Sub
    
    End Class
    End Sub Private Sub Lblscorecomputer_Click(sender As Object, e As EventArgs) Handles Lblscorecomputer.Click End Sub End Class
    Last edited by lisamorgan1; Jul 24th, 2017 at 06:36 PM.

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    At form level:

    Code:
    Dim scores() As Integer ={0, 0}
    After the RaiseEvent line:

    Code:
    Scores(w-1) += 2
    Label1.Text = $"Player: {scores(0)}{environment.newline}Computer: {scores(1)}"

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    Thats all i have to do

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    But, I just remembered that won't work.
    But I can tell you it's the w variable that tells you who won. 1 is the player, 2 is the computer.
    My example isn't a simple beginners example. I only intended to show you how I drew the cards in the pictureboxes...

  29. #29

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    Thank you atleast you tried

  30. #30
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    I could try more. I'm fairly confident I can write your program from scratch in less than an hour, but that kinda defeats the object really. The point is to write your own program and learn from it... ��

  31. #31

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    ive already written my own program looking at yours but i really dont know what to do about the label buttons like if been sitting here and i search it up aswell but still nothing and my assignment is soon due

  32. #32
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    Ok here's a simplified example showing how to keep score...

    WindowsApplication1.zip

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    Ok this really helps but for if i wanted to place 52 cards from my resource folder how would i do that. Like i have 2 picture boxes one for the computer and one for the user. So i want the cards to shuffle and not just stay on 1 card like your card program except i just want 1 card to show each time they shuffle cause for yours you have multiple cards in 1 picture box

  34. #34

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help


  35. #35
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    You only need to shuffle them once. Each card drawn is then removed from the deck. The deck is still shuffled and cards aren't repeated...

  36. #36

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    If anyone can help me that would be real appreciated,PLzzz!!!. I would actually learn a lot.
    If anyone knows a code or has a code or a file like this can they please share it. My assignment is soon due, so if you have a lot of background in vb can you please help me
    https://www.youtube.com/watch?v=-m2EGPoUDpM
    Last edited by lisamorgan1; Jul 25th, 2017 at 09:56 PM.

  37. #37

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    So no one wants to help me?It would probably help other people to since there are not alot of tutorials on doing this type of game. So please help
    Last edited by lisamorgan1; Jul 25th, 2017 at 10:11 PM.

  38. #38

    Thread Starter
    Junior Member
    Join Date
    Jul 2017
    Posts
    23

    Re: Card game Help

    If anyone wants the zip i would be glad to help since none of you didnt

  39. #39
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Card game Help

    If you could've explained clearly what you wanted at the start, you might have got more relevant help. If you've completed your assignment by yourself now, well done but it's not appropriate for me, you, or anyone to give someone a complete assignment solution. Why bother going to class if you have no intention of doing your homework?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width