6 Replies - 4966 Views - Last Post: 01 May 2011 - 09:09 AM Rate Topic: -----

#1 Blind_Geek81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-February 11

Using images in VB for a Blackjack game

Posted 29 April 2011 - 09:56 AM

My classmates said I was crazy when I told them I was re-making my old blackjack game in VB. It's actually been going a lot easier than it was when I was doing it in C#, though. I'm hitting some snags, which are commented on in the code. The basic logic is mostly right.

My main question is how would I add images to the game? Where would I put them? How would I make sure the suits are randomized, too?

New Code:
Public Class FRM_BlackJack
	Private Sub FRM_BlackJack_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub

	'Global Variables
	Dim PlayerCards(4) As Image
	Dim PlayerTotal As Integer = 0
	Dim DealerCards As Image
	Dim DealerTotal As Integer = 0
	Dim Count As Integer = 1
	'This is supposed to randomize the cards A to K
	'Will this randomize the images as well?
	Dim CardRandomizer As Random
	'End of Global Variables

	'This function names and gives values to the cards
	'Should I put the images in it, too?
	Public Function AddCard()
		Dim Name As String
		'Limiting the randomizer to a minimum of 1 and a maximum of 10
		Dim Value As Integer = CardRandomizer.Next(1, 11)

		Select Case Value
			Case 0
				Name = "Two"
				Value += 2
			Case 1
				Name = "Three"
				Value += 3
			Case 2
				Name = "Four"
				Value += 4
			Case 3
				Name = "Five"
				Value += 5
			Case 4
				Name = "Six"
				Value += 6
			Case 5
				Name = "Seven"
				Value += 7
			Case 6
				Name = "Eight"
				Value += 8
			Case 7
				Name = "Nine"
				Value += 9
			Case 8
				Name = "Ten"
				Value += 10
			Case 9
				Name = "Jack"
				Value += 10
			Case 10
				Name = "Queen"
				Value += 10
			Case 11
				Name = "King"
				Value += 10
			Case 12
				Name = "Ace"
				Value += 11
			Case Else
				Name = "Ace"
				Value += 1
		End Select
		Return Value
	End Function


	Private Sub BTN_Deal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Deal.Click
		'Not sure if these Total variables are right at all; the dealer and the player are supposed to be dealt two cards each
		DealerTotal = CardRandomizer.Next()
		'By default, only the first two pictureboxes are shown
		PB_DCard1.Show()
		PB_DCard2.Show()

		If (DealerTotal < 21) Then
			AddCard()
		End If

		PlayerTotal = CardRandomizer.Next()
		PB_PCard1.Show()
		PB_PCard2.Show()

		If PlayerTotal = 21 Then
			LBL_PTotal.Text = "BLACKJACK!!"
			LBL_PTotal.ForeColor = System.Drawing.Color.Lime
		ElseIf PlayerTotal > 21 Then
			LBL_PTotal.Text = "Busted"
			LBL_PTotal.ForeColor = System.Drawing.Color.Orange
		ElseIf PlayerTotal < 21 Then
			LBL_PTotal.Text = PlayerTotal
			LBL_PTotal.ForeColor = System.Drawing.Color.Lime
		End If
	End Sub

	Private Sub BTN_Hit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Hit.Click
		'This button is supposed to show the next picturebox, add a card, and update the total of the pleyer's cards
		Count += 1
		PB_PCard1.Image = PlayerCards(Count)
		PB_PCard2.Image = PlayerCards(Count)
		PB_PCard3.Image = PlayerCards(Count)

		'A simple message box that explains why there is a limit of 5 cards
		If PlayerCards.Length > 4 Then
			MessageBox.Show("To keep things easier for people with low vision, the number of cards per hand is limited to five")
		End If
	End Sub

	'Simple betting functions
	Private Sub PB_Bet10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PB_Bet10.Click
		Dim Bet10 As Integer = 10

		LBL_BetTotal.Text += 10
	End Sub

	Private Sub PB_Bet50_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PB_Bet50.Click
		Dim Bet50 As Integer = 50

		LBL_BetTotal.Text += 50
	End Sub

	Private Sub PB_Bet250_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PB_Bet250.Click
		Dim Bet250 As Integer = 250

		LBL_BetTotal.Text += 250
	End Sub

	Private Sub BTN_Stand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Stand.Click
		LBL_BetTotal.Text += LBL_BankTotal.Text
		LBL_BetTotal.Refresh()

		'Clear player's hand
		PB_PCard1.Refresh()
		PB_PCard2.Refresh()
		PB_PCard3.Refresh()
		PB_PCard4.Refresh()
		PB_PCard5.Refresh()
		LBL_PTotal.Refresh()

		'Clear dealer's hand
		PB_DCard1.Refresh()
		PB_DCard2.Refresh()
		PB_DCard3.Refresh()
		PB_DCard4.Refresh()
		PB_DCard3.Refresh()
		LBL_DTotal.Refresh()

	End Sub
End Class



Old C# Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Isaak_BlackJack
{
    class Program
    {
        static string[] strPlayerCards = new string[11];
        static string strHitOrStand;
        static int intTotal = 0, intCount = 1, intDealerTotal = 0;
        static Random cardRandomizer = new Random();

        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Blackjack!");
            Start();
        }

        static void Start()
        {
            intDealerTotal = cardRandomizer.Next(15, 22);
            strPlayerCards[0] = Deal();
            strPlayerCards[1] = Deal();
            do
            {
                Console.WriteLine("You were dealt {0} and {1}. \nYour total is {2} .\nWould you like to hit or stand?", strPlayerCards[0], strPlayerCards[1], intTotal);
                strHitOrStand = Console.ReadLine().ToLower();
            }
            while (!strHitOrStand.Equals("hit") && !strHitOrStand.Equals("stand"));
            Game();
        }

        static void Game()
        {
            if (strHitOrStand.Equals("hit"))
            {
                Hit();
            }
            else if (strHitOrStand.Equals("stand"))
            {
                if (intTotal > intDealerTotal && intTotal <= 21)
                {
                    Console.WriteLine("\nWINNER! The dealer's total was {0}.\nWould you like to play again? Press \"Y\" for Yes or \"N\" for No", intDealerTotal);
                    PlayAgain();
                }
                else if (intTotal < intDealerTotal)
                {
                    Console.WriteLine("\nLOOSER! The dealer's total was {0}.\nWould you like to play again? Press \"Y\" for Yes or \"N\" for No", intDealerTotal);
                    PlayAgain();
                }
            }
            Console.ReadKey();
        }

        static string Deal()
        {
            string strCard;
            int intCards = cardRandomizer.Next(1, 14);
            switch (intCards)
            {
                case 1:
                    strCard = "Two"; intTotal += 2;
                    break;
                case 2:
                    strCard = "Three"; intTotal += 3;
                    break;
                case 3:
                    strCard = "Four"; intTotal += 4;
                    break;
                case 4:
                    strCard = "Five"; intTotal += 5;
                    break;
                case 5:
                    strCard = "Six"; intTotal += 6;
                    break;
                case 6:
                    strCard = "Seven"; intTotal += 7;
                    break;
                case 7:
                    strCard = "Eight"; intTotal += 8;
                    break;
                case 8:
                    strCard = "Nine"; intTotal += 9;
                    break;
                case 9:
                    strCard = "Ten"; intTotal += 10;
                    break;
                case 10:
                    strCard = "Jack"; intTotal += 10;
                    break;
                case 11:
                    strCard = "Queen"; intTotal += 10;
                    break;
                case 12:
                    strCard = "King"; intTotal += 10;
                    break;
                case 13:
                        strCard = "Ace"; intTotal += 10;
                    break;
                default:
                    strCard = "Ace"; intTotal += 1;
                    break;
            }
            return strCard;
        }

        static void Hit()
        {
            intCount += 1;
            strPlayerCards[intCount] = Deal();
            Console.WriteLine("\nYou were dealt a(n) {0}.\nYour new total is {1}", strPlayerCards[intCount], intTotal);
            if (intTotal.Equals(21))
            {
                Console.WriteLine("\nBLACKJACK!! The dealer's total was {0}.\nWould you like to play again?", intDealerTotal);
                PlayAgain();
            }
            else if (intTotal > 21)
            {
                Console.WriteLine("\nYOU BUSTED! The dealer's total was {0}. \nWould you like to play again? Press \"Y\" for Yes and \"N\" for No.");
                PlayAgain();
            }
            else if (intTotal < 21)
            {
                do
                {
                    Console.WriteLine("\nWould you like to hit or stand?");
                    strHitOrStand = Console.ReadLine().ToLower();
                }
                while (!strHitOrStand.Equals("hit") && !strHitOrStand.Equals("stand"));
                Game();
            }
        }

        static void PlayAgain()
        {
            string strPlayAgain;
            do
            {
                strPlayAgain = Console.ReadLine().ToLower();
            }
            while (!strPlayAgain.Equals("Y") && !strPlayAgain.Equals("N"));
            if (strPlayAgain.Equals("Y"))
            {
                Console.WriteLine("\nPress enter to restart the game!");
                strPlayAgain = Convert.ToString(Console.ReadKey());
                Console.Clear();

                intDealerTotal = 0;
                intCount = 1;
                intTotal = 0;
                Start();
            }
            else if (strPlayAgain.Equals("N"))
            {
                Console.WriteLine("\nPress enter to close Blackjack.");
                strPlayAgain = Convert.ToString(Console.ReadKey());
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Using images in VB for a Blackjack game

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,293
  • Joined: 12-June 08

Re: Using images in VB for a Blackjack game

Posted 29 April 2011 - 10:06 AM

Quote

My main question is how would I add images to the game?


You would add them by right clicking your project -> add -> add existing. Select the image. Bam.

Alternately you could add an 'imagelist' from the toolbox to your project. Then from there go to the image property and select the image from your computer.

You also could load an image up by the 'bitmap' class.

You also could add them as a resource in the project settings.. then call them with the my.settings!

Quote

Where would I put them?

In a folder in the project, in a resource, in a location with the exe to be referenced in code, and so on. It all depends on how you want them referenced.

Quote

How would I make sure the suits are randomized, too?

Use the random method with a good seed. I like using an aggregate of the current date and time (to the miliseconds).
Was This Post Helpful? 0
  • +
  • -

#3 Blind_Geek81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-February 11

Re: Using images in VB for a Blackjack game

Posted 01 May 2011 - 07:06 AM

Thanks, modi, but your misunderstood my questions :) Adding the images was easy; using them in the game is what I'm having difficulties with.

  • Every time I run the program, I get a "File not found" exception How would I handle it?
  • The "AddCard()" function has all the information the game needs for a small, 13-card deck. The shoe is unlimited, and there are no Double down and split buttons.
  • The program is supposed to draw two random numbers from 1 to 21 and display the card related to the number.
  • Both the player and the dealer have only one hand, which can have a maximum of 5 cards. I have set up the pictureboxes and arrays for them. I am not sure if the arrays are better as integers or images; I would think the INT_DealerTotal and INT_PlayerTotal are supposed to show the player their score as well as the dealer's score; the cards and pictureboxes are for display purposes only.
  • How do I fix the "InvalidOperationException"? I am so confused by that one! I tried putting "System.Drawing..." to my image links; I thought it might fix the "FileNotFound" exception. When I ran the program again, I got the "InvalidOperation" exception. I deleted the System.Drawing... text, but my program still won't work! What did I do wrong??


I have more questions within the code itself. I'd really appreciate all the help I can get! Thanks so much, everyone! :)

I've uploaded two screenshots; the first shows you what my game looks like; the second shows the page Visual Studio shows me that is supposed to help me understand the "InvalidOperationException"

Public Class FRM_BlackJack
	Private Sub FRM_BlackJack_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

	End Sub

	'Global Variables
	Dim PlayerCards(4) As Image
	Dim INT_PlayerTotal As Integer = CStr(LBL_PTotal.Text)
	Dim DealerCards As Image
	Dim INT_DealerTotal As Integer = CStr(LBL_DTotal.Text)
	Dim INT_Count As Integer = 1
	'This is supposed to randomize the cards A to K
	'Will this randomize the images as well?
	Dim CardRandomizer As Random = New Random
	'End of Global Variables

	'This function names and gives values to the cards
	'Should I put the images in it, too?
	Public Function AddCard()
		Dim STR_Name As String
		'Limiting the randomizer to a minimum of 1 and a maximum of 10
		Dim INT_Value As Integer = CardRandomizer.Next(1, 11)
		'Adding the images
		Dim Card_Image As Image

		Select Case INT_Value
			Case 0
				Card_Image = Image.FromFile("Two")
				STR_Name = "Two"
				INT_Value += 2
			Case 1
				Card_Image = Image.FromFile("Three")
				STR_Name = "Three"
				INT_Value += 3
			Case 2
				Card_Image = Image.FromFile("Four")
				STR_Name = "Four"
				INT_Value += 4
			Case 3
				Card_Image = Image.FromFile("Five")
				STR_Name = "Five"
				INT_Value += 5
			Case 4
				Card_Image = Image.FromFile("Six")
				STR_Name = "Six"
				INT_Value += 6
			Case 5
				Card_Image = Image.FromFile("Seven")
				STR_Name = "Seven"
				INT_Value += 7
			Case 6
				Card_Image = Image.FromFile("Eight")
				STR_Name = "Eight"
				INT_Value += 8
			Case 7
				Card_Image = Image.FromFile("Nine")
				STR_Name = "Nine"
				INT_Value += 9
			Case 8
				Card_Image = Image.FromFile("Ten")
				STR_Name = "Ten"
				INT_Value += 10
			Case 9
				Card_Image = Image.FromFile("Jack")
				STR_Name = "Jack"
				INT_Value += 10
			Case 10
				Card_Image = Image.FromFile("Queen")
				STR_Name = "Queen"
				INT_Value += 10
			Case 11
				Card_Image = Image.FromFile("King")
				STR_Name = "King"
				INT_Value += 10
			Case 12
				Card_Image = Image.FromFile("Ace")
				STR_Name = "Ace"
				INT_Value += 11
			Case Else
				Card_Image = Image.FromFile("Ace")
				STR_Name = "Ace"
				INT_Value += 1
		End Select
		Return INT_Value
		Return Card_Image
	End Function

	Private Sub BTN_Deal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Deal.Click
		'Not sure if these Total variables are right at all; the dealer and the player are supposed to be dealt two cards each
		INT_DealerTotal = CardRandomizer.Next(1, 21)
		'By default, only the first two pictureboxes are shown
		PB_DCard1 = AddCard()
		PB_DCard2 = AddCard()

		If (INT_DealerTotal < 21) Then
			AddCard()
		End If

		INT_PlayerTotal = CardRandomizer.Next(1, 21)
		PB_PCard1 = AddCard()
		PB_PCard2 = AddCard()

		If INT_PlayerTotal = 21 Then
			LBL_PTotal.Text = "BLACKJACK!!"
			LBL_PTotal.ForeColor = System.Drawing.Color.Lime
		ElseIf INT_PlayerTotal > 21 Then
			LBL_PTotal.Text = "Busted"
			LBL_PTotal.ForeColor = System.Drawing.Color.Orange
		ElseIf INT_PlayerTotal < 21 Then
			LBL_PTotal.Text = INT_PlayerTotal
			LBL_PTotal.ForeColor = System.Drawing.Color.Lime
		End If
		BTN_Deal.Hide()
	End Sub

	Private Sub BTN_Hit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Hit.Click
		'This button is supposed to show the next picturebox, add a card, and update the total of the pleyer's cards
		INT_Count += 1
		PB_PCard1.Image = PlayerCards(INT_Count)
		PB_PCard2.Image = PlayerCards(INT_Count)
		PB_PCard3.Image = PlayerCards(INT_Count)

		'A simple message box that explains why there is a limit of 5 cards
		If PlayerCards.Length > 4 Then
			MessageBox.Show("To keep things easier for people with low vision, the number of cards per hand is limited to five")
		End If
	End Sub

	'Simple betting functions
	Private Sub PB_Bet10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PB_Bet10.Click
		Dim Bet10 As Integer = 10

		LBL_BetTotal.Text += 10
		LBL_BankTotal.Text -= 10
		'The maximum bet a player can place per hand is 1,000. This message box lets the player know
		If LBL_BetTotal.Text > 1000 Then
			MessageBox.Show("You cannot bet over 1,000 per hand")
			LBL_BetTotal.ResetText()
		End If
	End Sub

	Private Sub PB_Bet50_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PB_Bet50.Click
		Dim Bet50 As Integer = 50

		LBL_BetTotal.Text += 50
		LBL_BankTotal.Text -= 50

		'The maximum bet a player can place per hand is 1,000. This message box lets the player know
		If LBL_BetTotal.Text > 1000 Then
			MessageBox.Show("You cannot bet over 1,000 per hand")
			LBL_BetTotal.ResetText()
		End If
	End Sub

	Private Sub PB_Bet250_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PB_Bet250.Click
		Dim Bet250 As Integer = 250

		LBL_BetTotal.Text += 250
		LBL_BankTotal.Text -= 250

		'The maximum bet a player can place per hand is 1,000. This message box lets the player know
		If LBL_BetTotal.Text > 1000 Then
			MessageBox.Show("You cannot bet over 1,000 per hand")
			LBL_BetTotal.ResetText()
		End If
	End Sub

	Private Sub BTN_Stand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_Stand.Click
		LBL_BetTotal.Text += LBL_BankTotal.Text
		LBL_BetTotal.ResetText()

		'Clear player's hand
		PB_PCard1.Refresh()
		PB_PCard2.Refresh()
		PB_PCard3.Refresh()
		PB_PCard4.Refresh()
		PB_PCard5.Refresh()
		LBL_PTotal.ResetText()

		'Clear dealer's hand
		PB_DCard1.Refresh()
		PB_DCard2.Refresh()
		PB_DCard3.Refresh()
		PB_DCard4.Refresh()
		PB_DCard3.Refresh()
		LBL_DTotal.ResetText()
	End Sub
End Class

Attached image(s)

  • Attached Image
  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: Using images in VB for a Blackjack game

Posted 01 May 2011 - 07:48 AM

Hi,

You should add them into the resources folder:

Rightclick project and open solution Explorer
Select Resources -> select Images -> add resources -> New Images

Then you can call them like this:

Card_Image = My.Resources.Ace

Was This Post Helpful? 1
  • +
  • -

#5 Blind_Geek81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-February 11

Re: Using images in VB for a Blackjack game

Posted 01 May 2011 - 08:55 AM

View PostLuc001, on 01 May 2011 - 07:48 AM, said:

Hi,

You should add them into the resources folder:

Rightclick project and open solution Explorer
Select Resources -> select Images -> add resources -> New Images

Then you can call them like this:

Card_Image = My.Resources.Ace


Hey thanks so much! I'm originally studying C# in school, but my teacher is letting us use VB if we want. Personally, I find the syntax of VB a lot easier! I thought there must be something similar to C#'s <program name>.Properties.Resources.<image name>, but I thought the VB version was Image.FromFile :)
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,176
  • Joined: 02-July 08

Re: Using images in VB for a Blackjack game

Posted 01 May 2011 - 09:01 AM

VB does have the Image.FromFile(...), but like Luc001 suggested this is more portable when you deploy the program - otherwise you would have to worry about adding these file to deploy.
Was This Post Helpful? 0
  • +
  • -

#7 Blind_Geek81  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-February 11

Re: Using images in VB for a Blackjack game

Posted 01 May 2011 - 09:09 AM

I like this way better, anyway :) I can actually see the filenames while I'm typing, plus I don't have to worry about the file extension :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1