Question

Create black jack game using C#, windows forms. Tester file shows what needs to be done....

Create black jack game using C#, windows forms. Tester file shows what needs to be done. Other file is what I have so far.

https://drive.google.com/file/d/1BtwuERUsdmCxeaU40SQP-GnvlUQAXCTc/view?usp=sharing

https://drive.google.com/file/d/1AsfYguryuKkso5sUF253pu_fwIO8p6fK/view?usp=sharing

0 0
Add a comment Improve this question Transcribed image text
Answer #1

NOTE: CANT FIND ANYTHING ON THE GIVEN LINK

P.S. THIS IS HOW I TRIED TO DO IT.

I have 3 main classes: Card, Deck and Hand. Of course the form has its own Form1 class.

I also have 2 enums: CardValue and CardSuit. Both enum indexing starts from 1. The CardValue contains the type of the cards (Ace, King, 9, 7, 2, etc.). The CardSuit contains the colors (hearts, spades, etc.).

Create a new class: Card.cs

Under the class place the 2 enums:

public enum CardValue
{
Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13
}

public enum CardSuit
{
Hearts = 1,
Spades = 2,
Clubs = 3,
Diamonds = 4
}
The Card class will have 3 properties, 1 constructor, 1 new method and 1 overridden method. What properties does a card have? Of course its value, its color and its picture. So create the private properties with their public encapsulated fields. The Image property won’t have setter, the value and color setter will have the image loader method:

Image image;
CardValue cardValue;
CardSuit suit;

public Image Image
{
get
{
return this.image;
}
}

public CardValue Value
{
get
{
return this.cardValue;
}
set
{
this.cardValue = value;
GetImage();
}
}

public CardSuit Suit
{
get
{
return this.suit;
}
set
{
this.suit = value;
GetImage();
}
}


With the constructor let’s create a dummy card with no color and value:

public Card()
{
cardValue = 0;
suit = 0;
image = null;
}
The attached image has the following properties: it’s 392 pixel high and 950 wide. So 1 card is about 97 high and 73 wide. The method has to slice the image depending on the following color from the deck (one color in each row on the image) and depending on the value (the image has the values in ascending order).

private void GetImage()
{
if (this.Suit != 0 && this.Value != 0)//so it must be a valid card (see the Enums)
{
int x = 0;//starting point from the left
int y = 0;//starting point from the top. Can be 0, 98, 196 and 294
int height = 97;
int width = 73;

switch (this.Suit)
{
case CardSuit.Hearts:
y = 196;
break;
case CardSuit.Spades:
y = 98;
break;
case CardSuit.Clubs:
y = 0;
break;
case CardSuit.Diamonds:
y = 294;
break;
}

x = width * ((int)this.Value - 1);//the Ace has the value of 1 (see the Enum), so the X coordinate will be the starting (first one), that's why we have to subtract 1. The card 6 has the total width of the first 6 cards (6*73=438) minus the total width of the first 5 cards (5*73=365). Of course it is 73. The starting X coordinate is at the end of the 5th card (or the start of the left side of the 6th card). Hope you understand. :)

Bitmap source = Resources.cards;//the original cards.png image
Bitmap img = new Bitmap(width, height);//this will be the created one for each card
Graphics g = Graphics.FromImage(img);
g.DrawImage(source, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);//here we slice the original into pieces
g.Dispose();
this.image = img;
}
}


And the overridden ToString method, just for fun (not used later in the code):

public override string ToString()
{
string realValue = "";
switch (cardValue)
{
case CardValue.Two:
case CardValue.Three:
case CardValue.Four:
case CardValue.Five:
case CardValue.Six:
case CardValue.Seven:
case CardValue.Eight:
case CardValue.Nine:
case CardValue.Ten:
realValue = ((int)cardValue).ToString();
break;
case CardValue.Jack:
case CardValue.Queen:
case CardValue.King:
case CardValue.Ace:
realValue = cardValue.ToString();
break;
}
return realValue + " of " + suit.ToString();
}
}
The previous part has the class for the card, in this part we create the class for the deck. The deck contains the cards, 1 from each. Of course we have to shuffle the deck before every new game and we have to draw a card from the shuffled deck. Now we now what properties and methods should have this class:

card list
shuffle method
draw method
The list property with private and public fields:

private List cards;

public List Cards
{
get { return this.cards; }
set { this.cards = value; }
}
The constructor:



public Deck()
{
Cards = new List();
ShuffleNewDeck();
}
As you can see, every time you create a new deck it will be shuffled. So let’s see the shuffle method:

public void ShuffleNewDeck()
{
cards.Clear();
for (int i = 1; i < 5; i++)//CardSuits
{
for (int j = 1; j < 14; j++)//CardValues: 2...10,J,Q,K,A = 13 different values { Card card = new Card(); card.Value = (CardValue)j; card.Suit = (CardSuit)i; cards.Add(card); } } Random r = new Random(); cards = cards.OrderBy(x => r.Next()).ToList();
}
This method walks through on every card, add them to a list (deck). The OrderBy Linq will randomize (shuffle) the deck.

Of course, we have to be able to draw a card, so:

public Card DrawCard(Hand hand)
{
Card drawn = cards[cards.Count - 1];
cards.Remove(drawn);
hand.Cards.Add(drawn);
return drawn;
}
///The hand where the drawn card goes to public Card DrawCard(Hand hand) { Card drawn = cards[cards.Count – 1]; cards.Remove(drawn); hand.Cards.Add(drawn); return drawn; }

Every time you draw a card, you draw it from the top of your deck (from the end of your list). It’s simple.

I have also created an ’empty’ exception class for the deck, called DeckException. You can also write this either in this Deck.cs file or in a new file:



[Serializable]
internal class DeckException : Exception
{
public DeckException()
{
}

public DeckException(string message) : base(message)
{
}

public DeckException(string message, Exception innerException) : base(message, innerException)
{
}

protected DeckException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}


This custom exception only shows you how to create the basics of your exception and nothing more. From now you can throw (then catch) your own exception.

Now we need a ‘hand’ that holds the drawn cards, so let’s create its class. It will have 1 property (the list of the cards), 1 constructor with our new custom exception and 1 method that counts our and computer’s total points.

using System.Collections.Generic;

namespace BlackJack
{
public class Hand
{
private List cards;

public List Cards
{
get { return cards; }
}

public Hand(int startingHand, Deck deck)
{
if (deck == null) throw new DeckException("No decks available to draw from!");
else if (deck.Cards.Count == 0) throw new DeckException("No more cards to draw!");
else
{
cards = new List();
for (int i = 0; i < startingHand; i++)
{
deck.DrawCard(this);
}
}
}

public void AddValue(Card drawn, ref int currentSum)
{
if (drawn.Value == CardValue.Ace)
{
if (currentSum <= 10)
{
currentSum += 11;
}
else
{
currentSum += 1;
}
}
else if (drawn.Value == CardValue.Jack || drawn.Value == CardValue.Queen || drawn.Value == CardValue.King)
{
currentSum += 10;
}
else
{
currentSum += (int)drawn.Value;
}
}
}
}


As you can see, the constructor waits for a startinghand number that is the starting number of the cards in hand (in this case it is 2) and a Deck type parameter that is the deck that we are going to draw from.
The AddValue method has no return value (void) that’s why we use the currentSum input parameter as ref. We pass this parameter here as a reference, so any changes on this parameter here will be visible from outside of this class (for example on our main form). You’ll see later. The Ace could have 2 possible values: 1 or 11. If our score is not more than 10 it is 11. If our score is at least 11 the Ace is going to be only 1. All the figure cards have the value of 10.
In the final part we create the form and use our classes to be able to play.


Create a new form with the size of 1000*400. Its FormBorderStyle should be FixedSingle. Place a MenuStrip on it (only for further developments, for example: new game, score table, etc.) and a SplitContainer with 2 horizontal panels on it. You also need 2 buttons (hit and stay) and 3 labels to show scores and computer action if it STAYs.

The form should look like something like this:


Let’s create some class-level variables (so you can reach them from every method):

private readonly int STARTING_HAND = 2;
private readonly int MAX_CARDS_ON_TABLE = 11;//worst case: you could have maximum 11 cards in hand (four 2s + four 3s + four 1s = 21)
PictureBox p;
PictureBox q;
Deck deck;
Hand player;
Hand computer;
int computerSum;
int playerSum;
The first 2 variables are readonly one. It could be const, if you prefer that one, doesn’t matter. PictureBox p and q are for the 11-11 pictureboxes that will be placed onto the table for each gamblers: p for computer, q for player. We also have a deck and 2 hands and of course the 2 total points in computerSum and playerSum.
We will have only 6 methods on our form:

NewGame – resets the form, clears the table, initializes the card places, resets the scores
ClearTable
InitializeCardPlacesOnTable
btnHit_Click
btnStay_Click
computerHit

Add a comment
Know the answer?
Add Answer to:
Create black jack game using C#, windows forms. Tester file shows what needs to be done....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded...

    In this assignment you will practice using Data Structures and Object Oriented concepts in Java. Your implementation should target the most efficient algorithms and data structures. You will be graded based on the efficiency of your implementation. You will not be awarded any points if you use simple nested loops to implement the below tasks. You should use one or more of the below data structures: - ArrayList : - JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html - Tutorial: http://docs.oracle.com/javase/tutorial/collections/interfaces/list.html Question You are provided with...

  • This is done in C++ Create an Employee class using a separate header file and implementation...

    This is done in C++ Create an Employee class using a separate header file and implementation file. Review your UML class diagram for the attributes and behaviors The calculatePay() method should return 0.0f. The toString() method should return the attribute values ("state of the object"). Create an Hourly class using a separate header file and implementation file. The Hourly class needs to inherit from the Employee class The calculatePay() method should return the pay based on the number of hours...

  • I want to have a C++ program that simulates Black jack game with 2 players using vectors along wi...

    I want to have a C++ program that simulates Black jack game with 2 players using vectors along with the betting amount. The out put must clearly show the suit and value of cards on the console which was dealt to each player and dealer. and then calculate to check who is the winner. repeat the dealing 5 times. Code should be clearly sperated into header files, .cpp files and the main.cpp that calls the functions of the respective class....

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • C++ Project - Create a memory game in c++ using structs and pointers. For this exercise,...

    C++ Project - Create a memory game in c++ using structs and pointers. For this exercise, you will create a simple version of the Memory Game. You will again be working with multiple functions and arrays. You will be using pointers for your arrays and you must use a struct to store the move and pass it to functions as needed. Program Design You may want to create two different 4x4 arrays. One to store the symbols the player is...

  • Using Java - Your job is to create an animal guessing game that will work like...

    Using Java - Your job is to create an animal guessing game that will work like the following example: Computer: Does it have 4 legs? User: No Computer: Then it is a chicken? User: No Computer: What was your animal? User: Snake Computer: Enter in a question that differentiates your animal from the other animal, and is a yes answer to your animal. User: Does it have no legs? Then the next time the game is played it will run...

  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game...

    Using python 3.7.3 Challenge: Rock, Paper, Scissors GamePDF Description: Create a menu-driven rock, paper, scissors game in Python 3 that a user plays against the computer with the ability to save and load a game and its associated play statistics. Purpose: The purpose of this challenge is to assess the developer’s ability to create an interactive application with data persistence in Python 3. Requirements: Create a Rock, Paper, Scissors game in Python named rps.py according to the requirements specified in...

  • Please Help me! Recursive approximate median Consider an array of integers. You wish to find an...

    Please Help me! Recursive approximate median Consider an array of integers. You wish to find an approximate median. You have an idea: split the array (or a range of the array) into three pieces, find the approximate median of each piece, and then return the actual median of the three approximate medians (if you put the three approximate medians in sorted order, the one in the middle). There are a few details to consider. If we are trying to find...

  • Write a program in C++ that uses a class template to create a set of items....

    Write a program in C++ that uses a class template to create a set of items. . . The Problem Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 Hint: Use vectors and vector functions to store the set of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT