In my intro to computer science class at McNeese State, the final was
to make a card game similar to a three-card monty in which the user
guesses where the Ace of Spades is. I noticed during the time we
spent coding that most of the students in the class required help from
either myself or our teacher to understand how to make the cards work
correctly. I had to do quite a bit of googling to find the images I
wanted, but being a hacker/programmer before I stepped foot into
college meant that the assignment wasn't that hard for me. I neary
wasn't able to turn it in, however, because my classmates constantly
needed help, and, being the good semaritan that I am, I couldn't turn
them down. So, for all the other programmers here on the interweb, I
am now going to post the code to make a simple gui, and deal four
cards from a real shuffled deck at random onto the screen. I also
tried to comment it as well as I could to ensure that students and
autodidects alike can understand what I did here. Enjoy.
[code]
/*note, the cards I used were obtained from http://www.jfitz.com/cards/
They can be downloaded directly from the website and come named 1-54
for ease of assignment while coding. The owner of the images gives
them away free, with all needed information. The main knowledge
needed for this programming example is that of arrays. Arrays are
strings of integers or some other datatype. They can be amazing
resources for things like cards and other multi-piece objects, like a
puzzle. Feel free to use this code as you wish, as long as you don't
somehow use it to like me to some sort of criminal activity, and I am
not responsible for what you do with this code./*
/* The main point of this program is to show beginning programmers how
to make, shuffle, and deal a real deck of cards for a game. This
program will work for all games programmed in Visual C#.NET 2003 and
any years that it may be further compatibe. Please enjoy and feel
free to use it in any of your programming needs.*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CardDraw2Jokers
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class uiCardGameForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button uiDealButton;
private System.Windows.Forms.PictureBox uiCard0PictureBox;
private System.Windows.Forms.PictureBox uiCard1PictureBox;
private System.Windows.Forms.PictureBox uICard2PictureBox;
private System.Windows.Forms.PictureBox uiCard3PictureBox;
private System.Windows.Forms.Button uiInfoButton;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public uiCardGameForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(uiCardGameForm));
this.uiCard0PictureBox = new System.Windows.Forms.PictureBox();
this.uiCard1PictureBox = new System.Windows.Forms.PictureBox();
this.uICard2PictureBox = new System.Windows.Forms.PictureBox();
this.uiCard3PictureBox = new System.Windows.Forms.PictureBox();
this.uiDealButton = new System.Windows.Forms.Button();
this.uiInfoButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// uiCard0PictureBox
//
this.uiCard0PictureBox.Image = ((System.Drawing.Image)
(resources.GetObject("uiCard0PictureBox.Image")));
this.uiCard0PictureBox.Location = new System.Drawing.Point(24, 24);
this.uiCard0PictureBox.Name = "uiCard0PictureBox";
this.uiCard0PictureBox.Size = new System.Drawing.Size(72, 96);
this.uiCard0PictureBox.TabIndex = 0;
this.uiCard0PictureBox.TabStop = false;
//
// uiCard1PictureBox
//
this.uiCard1PictureBox.Image = ((System.Drawing.Image)
(resources.GetObject("uiCard1PictureBox.Image")));
this.uiCard1PictureBox.Location = new System.Drawing.Point(128,
24);
this.uiCard1PictureBox.Name = "uiCard1PictureBox";
this.uiCard1PictureBox.Size = new System.Drawing.Size(72, 96);
this.uiCard1PictureBox.TabIndex = 1;
this.uiCard1PictureBox.TabStop = false;
//
// uICard2PictureBox
//
this.uICard2PictureBox.Image = ((System.Drawing.Image)
(resources.GetObject("uICard2PictureBox.Image")));
this.uICard2PictureBox.Location = new System.Drawing.Point(232,
24);
this.uICard2PictureBox.Name = "uICard2PictureBox";
this.uICard2PictureBox.Size = new System.Drawing.Size(72, 96);
this.uICard2PictureBox.TabIndex = 2;
this.uICard2PictureBox.TabStop = false;
//
// uiCard3PictureBox
//
this.uiCard3PictureBox.Image = ((System.Drawing.Image)
(resources.GetObject("uiCard3PictureBox.Image")));
this.uiCard3PictureBox.Location = new System.Drawing.Point(336,
24);
this.uiCard3PictureBox.Name = "uiCard3PictureBox";
this.uiCard3PictureBox.Size = new System.Drawing.Size(72, 96);
this.uiCard3PictureBox.TabIndex = 3;
this.uiCard3PictureBox.TabStop = false;
//
// uiDealButton
//
this.uiDealButton.BackColor =
System.Drawing.SystemColors.ActiveBorder;
this.uiDealButton.Font = new System.Drawing.Font("Microsoft Sans
Serif", 10.2F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.uiDealButton.Location = new System.Drawing.Point(52, 136);
this.uiDealButton.Name = "uiDealButton";
this.uiDealButton.Size = new System.Drawing.Size(136, 32);
this.uiDealButton.TabIndex = 4;
this.uiDealButton.Text = "Deal Cards!!";
this.uiDealButton.Click += new
System.EventHandler(this.uiDealButton_Click);
//
// uiInfoButton
//
this.uiInfoButton.BackColor =
System.Drawing.SystemColors.ActiveBorder;
this.uiInfoButton.Font = new System.Drawing.Font("Microsoft Sans
Serif", 10.2F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.uiInfoButton.Location = new System.Drawing.Point(244, 136);
this.uiInfoButton.Name = "uiInfoButton";
this.uiInfoButton.Size = new System.Drawing.Size(136, 32);
this.uiInfoButton.TabIndex = 5;
this.uiInfoButton.Text = "More Info";
this.uiInfoButton.Click += new
System.EventHandler(this.uiInfoButton_Click);
//
// uiCardGameForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.BackColor = System.Drawing.Color.Green;
this.ClientSize = new System.Drawing.Size(432, 224);
this.Controls.Add(this.uiInfoButton);
this.Controls.Add(this.uiDealButton);
this.Controls.Add(this.uiCard3PictureBox);
this.Controls.Add(this.uICard2PictureBox);
this.Controls.Add(this.uiCard1PictureBox);
this.Controls.Add(this.uiCard0PictureBox);
this.Name = "uiCardGameForm";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Card Game Tite";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new uiCardGameForm());
}
/* I wrote everything below here. Everything above
was created by the Windows Form Designer.*/
#region VARS
//cstintEND will determine the number of cards, which is 54, and
could be 13, 15, 26, 28, and so on depending on how many suits/jokers
you are using.
public const int cstintEND = 54;
//intCardArray holds the cards
int[] intCardArray = new int[54];
//this starts the random generator.
Random generateRandom = new Random();
#endregion
/*the cards and buttons are already laid out, set with
the back of a card as the default image. When the button is clicked,
it then shuffles the cards, deals the top four, and stops. It re-
shuffles and deals them when the button is clicked again.*/
private void uiDealButton_Click(object sender, System.EventArgs e)
{
int i;
//shuffle cards
for ( i = 0; i <= 53; i++)
{
intCardArray[i] = generateRandom.Next(1, cstintEND);
}
/* Since intCardArray[0] is the first to be
assigned, it doesn't have to be checked for duplicity, but the others
have to be checked with all that came before them, and the conditions
for these loops get very big when you actually deal ten or twelve
cards out.*/
//be sure there are no duplicates.
while (intCardArray[1] == intCardArray[0])
intCardArray[1] = generateRandom.Next(1, cstintEND);
while ((intCardArray[2] == intCardArray[1]) || (intCardArray[0] ==
intCardArray[2]))
intCardArray[2] = generateRandom.Next(1, cstintEND);
while ((intCardArray[3] == intCardArray[2]) || (intCardArray[3] ==
intCardArray[1]) || (intCardArray[3] == intCardArray[0]))
intCardArray[3]= generateRandom.Next(1, cstintEND);
//
//deal the cards
//
i = 0; //reset i, recycling a variable
/*As you can see, the overloading of the array makes card
placement much easier than with if statements or switches. */
//cards must be named and placed in the project's bin/debug folder
to work this way.
this.uiCard0PictureBox.Image = Image.FromFile(intCardArray[i] +
".png");
this.uiCard1PictureBox.Image = Image.FromFile(intCardArray[i + 1] +
".png");
this.uICard2PictureBox.Image = Image.FromFile(intCardArray[i + 2] +
".png");
this.uiCard3PictureBox.Image
...
read more »