I have been tasked with creating BlackJack in C# for my final project. C# is fairly foreign to me so I'm having trouble with assigning images to my cards. Right now I have a cards class:
public class Card
{
private string face;
private string suit;
public Card(string cardFace, string cardSuit)
{
face = cardFace;
suit = cardSuit;
}
public override string ToString()
{
return face + " of " + suit;
}
}
And I have Deck class:
public class Deck
{
private Card[] deck;
private int currentCard;
private const int NUMBER_OF_CARDS = 52;
private Random ranNum;
public Deck()
{
string[] faces = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
string[] suits = { "Hearts", "Clubs", "Diamonds", "Spades" };
deck = new Card[NUMBER_OF_CARDS];
currentCard = 0;
ranNum = new Random();
for (int count = 0; count < deck.Length; count++)
deck[count] = new Card(faces[count % 13], suits[count / 13]);
}
public void Shuffle()
{
currentCard = 0;
for (int first = 0; first < deck.Length; first++)
{
int second = ranNum.Next(NUMBER_OF_CARDS);
Card temp = deck[first];
deck[first] = deck[second];
deck[second] = temp;
}
}
public Card DealCard()
{
if (currentCard < deck.Length)
return deck[currentCard++];
else
return null;
}
}
Finally, I have a simple Windows Form that has two buttons that simply deals a card and shuffles the deck with a label that shows the string of the current card that was dealt:
public partial class Form1 : Form
{
Deck deck = new Deck();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void buttonDeal_Click(object sender, EventArgs e)
{
Card card = deck.DealCard();
labelOutput.Text = card.ToString();
}
private void buttonShuffle_Click(object sender, EventArgs e)
{
deck.Shuffle();
}
}
Now, I just need some way to assign images to my cards, and I'm not sure on the how.. Any help would be greatly appreciated.
EDIT: Here's what I've been trying to in my Card class.
public static Image FromFile()
{
fileName = face + "_" + "of" + "_" +suit+".png";
Image image = Image.FromFile(fileName);
return image;
}
I have an error saying that Resources does not have a definition for fileName.
I've added all the images as resources to the project and named all the files with the convention simliar to "Eight_of_Clubs".
EDIT: I've changed this in my Windows form:
private void buttonDeal_Click(object sender, EventArgs e)
{
Card card = deck.DealCard();
string fileName = card.getFace() + "_" + "of" + "_" + card.getSuit() + ".png";
Image image = Image.FromFile(fileName);
pictureBox1.Image = image;
labelOutput.Text = card.ToString();
}
But when I run the program and click the deal button i get the error: File does not exist exception
Which is false because there is a file loaded in with the resources for this project that is named King_of_Spades.png