but it's not working
"Not working" means what in detail? How does your existing code look like?
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
How can I create card game that will randomly select 2 cards from deck of 52 cards, one for player and one for the computer then display the cards selected and determine who has won or if there is a tie by comaping the card values. The card that have higher value wins and the game must have 26 rounds dealing a full deck with no reputation cards.
I tried to use list box and picture box but it's not working
but it's not working
"Not working" means what in detail? How does your existing code look like?
218172-image.png
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CardGame
{
public partial class CardGame : Form
{
private PictureBox[] pictures;
public const string imagePath = @"Cards/";
public CardGame()
{
InitializeComponent();
pictures = new PictureBox[52];
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void CardGame_Load(object sender, EventArgs e)
{
}
private void btnShuffle_Click(object sender, EventArgs e)
{
CreateControls();
DisplayControls();
}
private void CreateControls()
{
for(var i=0;i<52;i++)
{
var newPictureBox = new PictureBox();
newPictureBox.Width = 1000;
newPictureBox.Height = 2000;
newPictureBox.BorderStyle = BorderStyle.FixedSingle;
pictures[i] = SizeImage(newPictureBox, i + 1);
}
}
private PictureBox SizeImage(PictureBox pb, int i)
{
Image img = Image.FromFile(imagePath + i.ToString()+ ".jpg");
pb.Image = img;
pb.SizeMode = PictureBoxSizeMode.CenterImage;
return pb;
}
private void DisplayControls()
{
for(var i=0;i<2;i++)
{
pictures[i].Left = (i * 2000) + 100;
this.Controls.Add(pictures[i]);
}
}
int imageNumber = 0;
int imageComp = 0;
private void btnPlay_Click(object sender, EventArgs e)
{
int[] imageArray = new int[26];
Random random = new Random();
imageNumber = random.Next(imageListCards.Images.Count);
imageComp = random.Next(imageListCards.Images.Count);
for (int i = 0; i < 26; i++)
{
picBoxPlayer.Image = imageListCards.Images[imageNumber];
imageNumber++;
picBoxComputer.Image = imageListCards.Images[imageComp];
imageNumber++;
if (imageNumber == imageListCards.Images.Count)
{
imageNumber = 0;
}
else
{
imageNumber++;
}
}
var bmp = new Bitmap(1, 1);
bmp.Tag = "uniqueTag";
picBoxPlayer.Image.Tag = bmp;
if (picBoxPlayer.Image.Tag == picBoxComputer.Image.Tag)
{
}
if(picBoxPlayer.Image==Properties.Resources.ace_of_clubs1)
{
MessageBox.Show ("Player one lose the game");
}
//private void timer1_Tick(object sender, EventArgs e)
// {
//picBoxPlayer.Image = Properties.Resources.cardBackGreen;
// }
}
}
}