How to make, shuffle, and deal a deck of cards in C# .NET 2003

已查看 634 次
跳至第一个未读帖子

Hitekrednek

未读,
2007年10月1日 17:21:502007/10/1
收件人 Google Code for Educators
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 = Image.FromFile(intCardArray[i + 3] +
".png");

/* A note on the above: It may be possible,
when assigning large numbers of cards, to loop through the process by
making a variable == X in this.uiCardXPictureBox. I'm not sure right
off hand how it would be done, but it could be looked into.*/
}

private void uiInfoButton_Click(object sender, System.EventArgs e)
{
MessageBox.Show("This program is written to shuffle a deck of cards
and deal the top four. It automatically re-shuffles the deck each
time the deal button is pressed, dealing four re-randomized cards.
The images are named by number and saved in the bin/debug folder of
the project, simplifying the image assignment process.",
"Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
/* And there you have it. Just open up your copy of Visual Studio
(with Visual C#.NET installed, of course), copy and paste this code
into it, save it, and press F5 to run it and see how it actually
works. Happy coding! */
[/code]

Hitekrednek

未读,
2007年10月1日 17:41:472007/10/1
收件人 Google Code for Educators
I did find that when I copy this code into Visual Studio, it does not
run as is. There are several spacing mistakes, a few comments that
need to go from // to /* */ and one capitalization error on the word
"Information" in the MessageBox.Show statement. If you would like me
to post the corrected code, please let me know, but I don't want to
flood the place with code just to make a few small corrections.
again, enjoy the code.

oscarjaime

未读,
2007年10月18日 18:30:022007/10/18
收件人 Google Code for Educators
hello hitkrednek

I did the same and getting the same
can you post the correct code please

Also I'm in a middle of card game project for the church
I'm learning my self VS .NET & Powerbuilder

Can you tell me how can I start these project from the scracth
I'm little disoriented

Thank you - Oscar

Hitekrednek

未读,
2007年11月29日 13:50:422007/11/29
收件人 Google Code for Educators
Firstly, you should open Visual Studio, open a blank solution,
project, and form. The only corrections that must be made with the
code are listed in my short post below it. Paste the code I've posted
into the form. Check all of the comments to be sure they're properly
opened/closed, as I added some of them while posting this, and some of
them are wrong, and fix the capitalization error in the
MessageBox.Show statement. When that's done, you should be able to
just compile and run the program. You can use the break/step feature
to see how the code works line by line as it runs.
You can email me at jdiamo...@gmail.com for any further
assistance, and that goes for any and all who need help with similar
situations. I am not currently able to afford my own internet
connection, and am not on often, but I try to check/respond to
everyone who writes me asking for help, unless they have a close
deadline and I miss it. If that happens, I'm sorry.
I am not familiar with powerbuilder, so you'll have to seek
assistance elsewhere for that, but I'm open to all VS.NET questions.
Also, obviously, this code is just to build the core of the
cards' operation. There are many other features that could be added
to the dealing process and such, and there are also an infinite number
of rules that could be formed to make various different card games
around this code, and would only require slight changes to the form/
number of cards dealt.
I hope that helps, and don't hesitate to ask for help when
needed.

-Hitekrednek,
Moderator - www.HackThisSite.org
回复全部
回复作者
转发
0 个新帖子