12 Replies - 9783 Views - Last Post: 16 March 2013 - 07:57 PM Rate Topic: -----

#1 Harbinger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-December 12

Coding deck of cards for "War Card Game" problem

Posted 30 December 2012 - 10:16 AM

I'm basically making the classic "War Card Game", and I need help with certain pieces of the code along the way. So far i'm still coding the Deck of cards, this is taking me a while because i'm using newly learned things. Here's what I have so far as of now.

Public Class frmWar
Private cv As Integer 'Card Value
Private sv As Integer 'Suit Value
Private Sub frmWar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Suits() As String = {"H", "D", "C", "S"}
Dim Faces() As String = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K"}

End Sub
End Class


The goal here is there will be a deck of 52 cards randomly shuffled and evenly distributed between two players. Player one will click a button to draw card then followed by player 2 doing the same and whoevers card is a greater value wins the point. I'm sure most of you guys could bang this out in an hour but im a noob at this still :P/> So I basically don't know where to go from here exactly...

Also this is my first time joining this forum :D/> I'm trying my best to follow the rules properly but if I slip up please let me know! I really am liking this little community

Is This A Good Question/Topic? 0
  • +

Replies To: Coding deck of cards for "War Card Game" problem

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: Coding deck of cards for "War Card Game" problem

Posted 30 December 2012 - 11:16 AM

Your assignment is to do this in VB6? You're deliberately going to school to learn a language that's been dead for a decade? Should this be moved to VB.NET?

Please read: This is not the VB.NET forum
Please read: Is learning VB6 now a complete waste of time?


Next: Have you learned loops. This would be a good place for two (nested) loops.

for ... card suits
{
   for ... card values
   {
       ' add card to deck
   }
}


But you at least need to give it a try. We won't just give you working code. We will help you on code you create but are having trouble with.
Was This Post Helpful? 0
  • +
  • -

#3 Harbinger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-December 12

Re: Coding deck of cards for "War Card Game" problem

Posted 31 December 2012 - 10:16 AM

View PosttlhIn`toq, on 30 December 2012 - 11:16 AM, said:

Your assignment is to do this in VB6? You're deliberately going to school to learn a language that's been dead for a decade? Should this be moved to VB.NET?

Please read: This is not the VB.NET forum
Please read: Is learning VB6 now a complete waste of time?


Next: Have you learned loops. This would be a good place for two (nested) loops.

for ... card suits
{
   for ... card values
   {
       ' add card to deck
   }
}


But you at least need to give it a try. We won't just give you working code. We will help you on code you create but are having trouble with.


Thanks for showing me the direction I need to head in :) But I'm not learning VB6 in school, it's actually VB2010 but seeing as there are no concurrent forums for assistance on that I resort to VB6 and VB2008 as they are quite similar....any syntax differences are usually easily fixable. But I will try to get my program to at least deal the cards evenly between player 1 and 2 and if i need more help i shall return :)
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9422
  • View blog
  • Posts: 35,406
  • Joined: 12-June 08

Re: Coding deck of cards for "War Card Game" problem

Posted 31 December 2012 - 10:20 AM

Quote

it's actually VB2010 but seeing as there are no concurrent forums for assistance on that

Yes.. it's is the "VB.NET" forum. I am moving this there.
Was This Post Helpful? 0
  • +
  • -

#5 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Coding deck of cards for "War Card Game" problem

Posted 31 December 2012 - 01:02 PM

I didn't look at it, but I know AdamSpeight2008 knows his stuff. Here is his VB.net Black Jack tutorial. I'm sure you can use it as a model for your game of war.
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Coding deck of cards for "War Card Game" problem

Posted 31 December 2012 - 06:56 PM

I would use the more upto date implementation on NuGet Cards. As it's more abstract and interface based implementation, allows you to define any type of card. For example Playing Cards or Tarot Cards.
Was This Post Helpful? 0
  • +
  • -

#7 Harbinger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-December 12

Re: Coding deck of cards for "War Card Game" problem

Posted 01 January 2013 - 10:53 AM

Okay people thanks for the helpful links! I just made the game now, however there are a few issues I don't know how to solve. 1. It only goes from 1 - 10 2. There aren't any "suit" types 3. I have one button drawing for both players and need a button for each player to draw. Here's my code!

Public Class frmWar
    Private intP1num As Integer
    Private intP2num As Integer
    Private intP1score As Integer
    Private intP2score As Integer
    Private Sub frmWar_Load() Handles MyBase.Load
        Randomize()
        lblPlayer1.Text = InputBox("Enter a name for Player 1", "Player 1 Name")
        lblPlayer2.Text = InputBox("Enter a name for Player 2", "Player 2 Name")
    End Sub

    Private Sub btnDraw_Click() Handles btnDraw.Click
        intP1num = Int(10 * Rnd() + 1)
        intP2num = Int(10 * Rnd() + 1)
        lblP1num.Text = intP1num
        lblP2num.Text = intP2num


        If intP1num > intP2num Then
            MsgBox(lblPlayer1.Text & " wins")
            intP1score = intP1score + 1
        ElseIf intP2num > intP1num Then
            MsgBox(lblPlayer2.Text & " wins")
            intP2score = intP2score + 1
        Else
            MsgBox("Tie")
        End If


        lblP1score.Text = "Score: " & intP1score
        lblP2score.Text = "Score: " & intP2score
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Coding deck of cards for "War Card Game" problem

Posted 01 January 2013 - 12:48 PM

Why are people having difficulty comprehending that my Cards NuGet Library can handle (I reckon) all (or nearly all) card, not just playing cards. The example used is BlackJack as that the one used in the tutorial.
Was This Post Helpful? 0
  • +
  • -

#9 Harbinger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-December 12

Re: Coding deck of cards for "War Card Game" problem

Posted 01 January 2013 - 04:13 PM

Okay. I have only been using VB for about a month and honestly don't know what about 90% of that is I'm seeing in that link. I know this is achievable using very simple single dimensional integer arrays and math. I'm not aiming for what's "best" or most "efficient". I just want it to work then I can build from there. I can't rush into these complicated pieces of code when I'm still in the very very early learning process. I literally don't know more than about have a dozen functions, arrays, If statements, and some loops. I also know that I shouldn't even need 100 lines of code to get this to work. So is everyone here just SO experienced they can't drop down to my level? I mean it's simple, player1 click draw, player2 click draw, whichever is bigger gets points, repeat. Yet I'm getting links to stuff on every forum that's up to 1500 lines of code and makes no sense to me. Im sorry If i seem like a jerk, it's just frustrating when I ask for help and I get pointed in the direction of some code that looks like an alien language.

Here's my plan on how it works exactly.
1. The computer generates a deck of 52 cards and randomly deals half the deck to Player 1 and the other half to Player
2. Player 1 clicks "Draw" button and Player 2 clicks "Draw" button
2a. Player with greater card is awarded other players card (For example, if on first draw Player one had King and Player two had 4 then Player one would have 27 cards and Player two would have 25) else if "Tie" both cards return to respective players pile.
3. Repeat until a Player has no cards left.
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9422
  • View blog
  • Posts: 35,406
  • Joined: 12-June 08

Re: Coding deck of cards for "War Card Game" problem

Posted 01 January 2013 - 04:43 PM

Curious question - if given this:

Quote

I literally don't know more than about have a dozen functions, arrays, If statements, and some loops.

... how did you deduce this?

Quote

I also know that I shouldn't even need 100 lines of code to get this to work.



Quote

So is everyone here just SO experienced they can't drop down to my level?

Really? Don't be "that guy".

Quote

Im sorry If i seem like a jerk, it's just frustrating when I ask for help and I get pointed in the direction of some code that looks like an alien language.

Perhaps you should spend more time going over base programming concepts then.


So what exactly do you need help with?

I guess I'll trudge up to your post #7.

Quote

1. It only goes from 1 - 10

What only goes 1-10? The cards, scoring? My annoyance on your vague questions?

Quote

2. There aren't any "suit" types

Then make some. Do you really think there are only ten cards in a suit? No.. there's 13. Just because a suit isn't a number doesn't mean you are not allowed to treat it as such. Okay.. so you count up to number card ten.. then the jack is.. ding-ding-ding, card #11. Queen? Card #12. King? Card #13. Think a bit more abstractly.

Quote

3. I have one button drawing for both players and need a button for each player to draw.

THen.. don't do that? Take out the piece of code that deals with the other player, put it into another button event and move on. This isn't rocket surgery... if you are doing two things in one event method... and you want those two things to be in two different event methods.. THEN SPLIT THEM UP.
Was This Post Helpful? 0
  • +
  • -

#11 Harbinger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-December 12

Re: Coding deck of cards for "War Card Game" problem

Posted 01 January 2013 - 05:02 PM

Obviously we're having bad communication. 1. I don't know how to create the suits. 2. The order of cards goes Ace-King therefor the Ace is a lower integer than any other. How can I make it so someone with a 4 can't beat an Ace. 3. I want to use an array for the cards but don't know how to properly apply the array to the rest of the code to work. 0 represents Ace of Hearts, 27 represents the 2 of clubs, 51 the King of Spades, ect. Here is my Code.

Public Class frmWar
    Private intDeck(51) As Integer
    Private intP1Hand(25) As Integer
    Private intP2Hand(25) As Integer
    Private intP1num As Integer
    Private intP2num As Integer
    Private intP1score As Integer
    Private intP2score As Integer
    Private Sub frmWar_Load() Handles MyBase.Load
        Randomize()
        lblPlayer1.Text = InputBox("Enter a name for Player 1", "Player 1 Name")
        lblPlayer2.Text = InputBox("Enter a name for Player 2", "Player 2 Name")
    End Sub

    Private Sub btnDraw_Click() Handles btnDraw.Click
        intP1num = Int(13 * Rnd() + 1)
        intP2num = Int(13 * Rnd() + 1)

        If intP1num = 1 Then
            lblP1num.Text = "Ace"
        ElseIf intP1num = 11 Then
            lblP1num.Text = "Jack"
        ElseIf intP1num = 12 Then
            lblP1num.Text = "Queen"
        ElseIf intP1num = 13 Then
            lblP1num.Text = "King"
        Else
            lblP1num.Text = intP1num
        End If

        If intP2num = 1 Then
            lblP2num.Text = "Ace"
        ElseIf intP2num = 11 Then
            lblP2num.Text = "Jack"
        ElseIf intP2num = 12 Then
            lblP2num.Text = "Queen"
        ElseIf intP2num = 13 Then
            lblP2num.Text = "King"
        Else
            lblP2num.Text = intP2num
        End If


        If intP1num And intP2num = 1 Then
            MsgBox("Tie")
        ElseIf intP1num = 1 And intP2num <= 2 Then
            MsgBox(lblPlayer1.Text & " wins")
            intP1score = intP1score + 1
        ElseIf intP2num = 1 And intP2num <= 2 Then
            MsgBox(lblPlayer2.Text & " wins")
            intP2score = intP2score + 1
        ElseIf intP1num > intP2num Then
            MsgBox(lblPlayer1.Text & " wins")
            intP1score = intP1score + 1
        ElseIf intP2num > intP1num Then
            MsgBox(lblPlayer2.Text & " wins")
            intP2score = intP2score + 1
        Else
            MsgBox("Tie")
        End If

        lblP1score.Text = "Score: " & intP1score
        lblP2score.Text = "Score: " & intP2score
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: Coding deck of cards for "War Card Game" problem

Posted 01 January 2013 - 07:47 PM

Quote

Obviously we're having bad communication.

Do actually look at the tutorials suggested?

It is also hard to figure out what you are attempting to do from your code.



Begin with a pencil and paper.
So far what we(i) know you need
- Something that encapsulates the concept of a Playing Card
- Something that encapsulates the concept of a deck (of cards)
- A Hands of cards (Which you need two of)
- A Game Controller
This control the state and playing of the game.



It easier to begin Learning VB.net with Console applications, as it focus you on the design, language, solving the problem.



So let's design a class that encapsulates what a (playing) card is.
Class PlayingCard
  Implements IComparable(Of PlayingCard)

  Public ReadOnly Suit As String
  Public ReadOnly Face As String
  Public ReadOnly Value As Integer
  
  Public Sub New (Face As String, Suit As String, Value As Integer )
    Me.Face=Face : Me.Suit = Suit : Me.Value = Value
  End Sub

  Public Function CompareTo(other As PlayingCard ) As Integer Implements IComparable(Of PlayingCard).CompareTo
    If other Is Null Then Return 1
    Return Me.Value - other.Value
  End Function

  Public Override Function ToString() As String
    Return String.Format("{0} of {1} ({2})",Face,Suit,Value)
  End Function
End Class


See how this class only knows things about a single card and how to compare one card with another (to tell which is higher value).



On to the Deck.

Class Deck
  Protected _Cards As New List(Of Card)
  Public Sub New()
    Dim suits() = {"Spades", "Clubs", "Hearts", "Diamonds"}
    Dim faces() = {"Ace", "2","3","4","5","6","7","8","9","10","Jack","Queen","King"}
    For Each s in suits
      For i = 0 To face.Length-1
        _CardsInDeck.Add(New Card(face(i),s,i)
      Next
    Next
    Shuffle()
  End Sub
  
  Public Sub Shuffle()
    _CardsInDeck.Shuffle.ToList(()
  End Sub
  
  Public Function TakeCard() As Card
    If _CardsInDeck.Count>0 Then
      Dim c = _CardsInDeck.First()
      _CardsInDeck.RemoveAt(0)
      Return c
  End Function

  Public Sub ReturnCard(c As Card)
    _CardsInDeck.Add(c)
  End Sub
End Class



Now you can see when I create an instance of the class Deck it have 52 Card(s) objects created for it. These are stored in a generic collection type a list of card. List(Of Card)




I think you can figure out how to do an hand of cards, is it very similar to a Deck.

Task: Define a Class the encapsulates a Hand (of Card)




Shuffling is a little complex (based off the snippet by lucky3
Spoiler

This post has been edited by AdamSpeight2008: 01 January 2013 - 08:00 PM

Was This Post Helpful? 1
  • +
  • -

#13 Jondeate  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 03-February 13

Re: Coding deck of cards for "War Card Game" problem

Posted 16 March 2013 - 07:57 PM

AdamSpeight2008
Jeez, its not even me who needs help, nor im not interested on creating any application related to this one, but i appreciate this kind of effort put on a response, it helps alot to starters like me. bravo.

This post has been edited by Jondeate: 16 March 2013 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1