Posted  by  admin

Poker Visual Basic

Poker celebrity Daniel Negreanu is made up of signed upon toward be an ambassador for the manufacturer, as stated through a short article upon Twitter yesterday. Even though Negreanu’s posting captivated a good deal of congratulations towards his friends, there was moreover a.

  1. Poker Visual Basics
  2. Poker Virtual Background

Visual Basic Poker Game Codes and Scripts Downloads Free. The Visual Basic Game Template is a Visual Basic 2010 template for making games. Compiling Components in Visual Basic for ASP is a tutorial which elaborates about methods involved in generating a compiling componets for MTS and IIS. Visual Basic / VB.NET: Jeu de poker - CodeS SourceS - Guide Visual Basic / VB.NET: Analyse d'une main dans un jeu de poker( avec toutes les cartes en.bmp ) -Guide. Poker playing Mac users breathed a sigh of relief when PokerCruncher arrived on the scene, since it put them on a level playing with their Windows counterparts. The product now features a plethora of purchasing options from a basic iPhone app at $3.99 to the full monty for a Mac desktop at $49.99, as well as an Android version.

Home > Articles > Programming > Visual Basic

  1. Blackjack, Anyone?
< BackPage 5 of 8Next >

Blackjack, Anyone?

The sample program shows how to call many of the clsDeck methods, but it doesn't show them in action in a real game. In the next chapter you'll design a complete card game called Poker Squares, but for now, something a little simpler is in order. It's time to create a bare-bones version of blackjack.

Creating Blackjack's User Interface

The first step is to create the game's user interface. Perform the following steps:

  1. Start a new Standard EXE Visual Basic project.

  2. Set the form's properties to the values listed here:

    Name = CardForm

    AutoRedraw = True

    BackColor = Black

    Caption = 'Blackjack'

    Height = 6015

    ScaleMode = Pixel

    Width = 8250

  3. Add three CommandButton controls to the form, giving them the property values listed here:

    CommandButton #1

    Name = cmdStartGame

    Caption = '&Start Game'

    Height = 33

    Left = 19

    Top = 320

    Width = 89

    CommandButton #2

    Name = cmdHit

    Caption = '&Hit'

    Height = 33

    Left = 341

    Top = 320

    Width = 89

    CommandButton #3

    Name = cmdStay

    Caption = 'S&tay'

    Height = 33

    Left = 443

    Top = 320

    Width = 89

  4. Add a Timer control to the form.

Poker Visual Basic

You've now completed blackjack's user interface. Figure 8.6 shows what your main form will look like at this point. In the next section, you'll add handlers for the program's various controls.

Poker

Figure 8.6
The completed user interface.

Adding the Object Handlers

Next, you need to associate code with the various objects—the form, buttons, and timer—that make up the user interface. To accomplish this task, perform the following steps:

Visual
  1. Double-click the form to bring up the code window, and add the following form handlers to it. You can either type the code or copy it from the BlackJack1.txt file, which you can find in the Chap08Code directory of this book's CD-ROM.

Listing 8.8 The Form Handlers

Analysis - The Form_Load subroutine, which Visual Basic calls when the user starts the program, disables the Hit and Stay buttons (Lines 2 and 4) and the starts a new game by simulating a click on the Start Game button (Line 4). Line 8 in the Form_Unload subroutine removes the frmCards form from memory at the same time the main form closes.
Basic
  1. Add to the code window the CommandButton handlers in Listing 8.9. You can either type the code or copy it from the BlackJack2.txt file, which you can find in the Chap08Code directory of this book's CD-ROM.

Listing 8.9 The CommandButton Handlers

Analysis - The cmdHit_Click subroutine responds to the Hit button. Line 2 increases the player's card count, and Line 3 deals another card into the player's hand. A call to GetCardTotal (Line 5) gets the player's current score, and if the total is over 21 (Line 6), the game is over (Lines 7 to 9).
Poker virtual background
Analysis - The cmdStartGame_Click subroutine responds to the Start Game button. Line 14 clears the screen, and Lines 16 and 17 enable the Hit and Stay buttons. Then, Lines 18 and 19 create a new Deck object and shuffle it. Lines 20 and 21 initialize the card counts, and Lines 22 to 24 print the 'DEALER'S HAND' label. Lines 25 and 26 deal two cards to the dealer, one of them face down, while Line 30 does the same thing for the player's hand, except this time both cards are dealt face-up.
Analysis - The cmdStay_Click subroutine responds to the Stay button. Lines 34 and 35 disable the Hit and Stay buttons in preparation for the dealer's turn. Line 36 turns on the timer, which gets the dealer's turn going.
  1. Add to the code window the Timer handler shown in Listing 8.10. You can either type the code or copy it from the BlackJack3.txt file, which you can find in the Chap08Code directory of this book's CD-ROM.

Listing 8.10 The Timer Handler

Analysis - The Timer1_Timer subroutine implements the computer player and gets called for each timer event. Line 2 gets the dealer's current card total. If the total is greater than 21, Line 4 notifies the player that the dealer has busted and Line 5 turns off the timer. If the dealer's total is greater than 16, the dealer stays (Lines 8 and 9) and the current game ends (Line 10). Finally, if the dealer's total is less than or equal to 16, Lines 12 to 14 add a card to the dealer's hand.

Completing the Game

Almost there! After you add the general game subroutines and the required modules, you'll be ready to play blackjack. Here are the final steps:

  1. Add to the code window the general game subroutines shown in Listing 8.11. You can either type the code or copy it from the BlackJack4.txt file, which you can find in the Chap08Code directory of this book's CD-ROM.

Listing 8.11 The General Subroutines

Analysis - The GetCardTotal function calculates the card total for the player or dealer, depending upon the value of the plyer parameter. You'll study this function in detail later in this chapter. The EndGame subroutine shows the dealer's hand (Line 30), gets the player's and dealer's card totals (Lines 31 and 32), deletes the deck (Line 33), sets the game's buttons (Lines 34 to 36), and displays a message telling the player who won (Lines 37 to 45).
  1. Add to the top of the code window the variable declarations and enumerations in Listing 8.12. You can either type the code or copy it from the BlackJack5.txt file, which you can find in the Chap08Code directory of this book's CD-ROM.

Listing 8.12 The Declarations

  1. Add the Cards.frm form and the clsCard.cls, clsDeck.cls, and Cards.bas modules to the project, just as you did with the previous demo program.

  2. Save the game's main form as CardForm.frm and the project file as BlackJack.vbp.

You've now completed the blackjack program.

Playing Blackjack

When you run the program, you see the screen shown in Figure 8.7. The dealer's hand is at the top of the screen, and the player's hand is at the bottom. The objective of the game is to get as close to 21 as you can without going over. (The cards 2 through 10 are worth 2–10 points. All face cards count as 10 points, and an ace can count as either 1 or 11 points.)

Poker Visual Basics

Figure 8.7
The main blackjack screen.

To draw a card, press Enter or click the Hit button. Continue to draw until you're ready to stop, and then click the Stay button. If you haven't gone over 21, the dealer then begins to draw cards. The dealer must continue to draw until it reaches 17 or better. The winning hand is the one that's closest to 21 without going over (see Figure 8.8).

Figure 8.8
Winning at blackjack.

Programming Blackjack

Obviously, this program isn't a complete blackjack game. Many of the game's details are ignored (like doubling-down and insurance), there's no betting, and each game is only a single hand. However, the program does demonstrate how you can use the clsDeck and clsCard classes when programming an actual game. Much of the code in the program needs no explanation. However, one function, GetCardTotal, is the heart of the game and worthy of close examination.

GetCardTotal analyzes a blackjack hand and comes up with a total. This might seem like a trivial task until you recall that an ace can count as either 1 or 11 points. Moreover, a hand might have as many as four aces, further complicating the point-counting process.

To keep this task simple, GetCardTotal assumes that it will count all aces in a hand as the same value. The point value that the program chooses depends on the hand's point total. (Obviously, the program will never use 11 as an ace point value if the hand has more than one ace, because two 11-point aces will bring the hand to over 21.)

First, the program determines how many cards are in the hand by calling NumCardsInHand:

This clsDeck method takes as its single parameter the number of the hand to check. The program uses the value returned from NumCardsInHand to set up a For loop that looks at each card in the hand. In the loop, the program first calculates the value of the current card:

This calculation results in a value from 0 to 12 (ace to king). If the card's value is greater than 10, indicating a face card (jack, queen, or king), the program sets the card's value to Ten:

(The constants range from Ace, which equals 0, to King, which equals 12. Therefore, Ten is actually the integer value 9, not 10 as you might think.)

If the card turns out to be an ace, the program increments the number of aces in the hand and sets value to 10:

The program first assumes that it will treat the ace as a high card that is worth one point more than the face cards.

Next, the program adds the value of the current card to the total so far:

Because the card values range from 0 to 12, the added point value is actually value+1.

After totaling the values of all cards in the hand, the program checks whether the hand is over 21. If it is, and it contains aces, the program subtracts 10 for each ace in the hand so that the values of the aces all change to 1:

The function then returns the total to the calling method:

That's all there is to analyzing a blackjack hand (although this is a simplified version of the game). Now you're ready to move on to more challenging card games.

Related Resources

There are currently no related titles. Please check back later.

Home > Articles > Programming > Visual Basic

  1. Programming Card Games
Page 1 of 8Next >
In this sample chapter, author Clayton Walnum teaches you how to create a class for manipulating a deck of cards. Learn how to write a class to represent a single card or a deck of cards, and how to use classes to write card programs such as blackjack.
This chapter was selected from Sams Teach Yourself Game Programming with Visual Basic in 21 Days.

Few types of games are more popular than card games. Most households have a deck of cards, and probably more fortunes are won and lost over a card table than on a roulette wheel or a slot machine. That popularity crosses over to computer card games as well. The bottom line is that if you're going to be a Visual Basic game programmer, you need to know how to use VB to handle a deck of virtual cards.

Fortunately, Lady Luck is smiling upon you. In this chapter, not only will you create a class for manipulating a deck of cards, but you'll also get a full set of graphical images for your cards. If there's one thing that discourages most programmers from creating card games, it's the daunting challenge of drawing images for 52 cards—especially the face cards, which are the most graphically complex cards in the deck.

Specifically, today you'll learn the following:

  • How to write a class to represent a single card

  • How to write a class to represent a deck of cards

  • How to write card programs using classes

  • How to write a simple blackjack game

Deck-Handling Functions

Poker Virtual Background

In the following sections, you'll create a class that you can use to program card games. Before creating a class, however, you have to consider carefully the different ways that you must manipulate the data encapsulated in the class. After you've analyzed your game's needs, you can then write the class's functions. Unfortunately for programmers, there are more card games than craters on the moon. This makes creating a complete card class a nearly impossible task. You can never predict all the different ways that you might need to manipulate cards in your programs.

Therefore, the best you can do is to write the functions that every card game needs—such as shuffling a deck and dealing hands—and then add more specific functions as you need them. That's the approach this chapter will take with the clsCard class, which will be used in the next few chapters. After you understand how the clsCard class works, you'll be able to add any other functions that you need to create specific card games.

Related Resources

There are currently no related titles. Please check back later.