Windows application in C# using visual studio

Samukelo Siyanda Zungu 21 Reputation points
2022-07-06T01:08:00.017+00:00

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

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
11,933 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,387 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,099 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
7,925 questions
{count} votes

2 additional answers

Sort by: Most helpful
  1. Samukelo Siyanda Zungu 21 Reputation points
    2022-07-06T11:20:05.55+00:00

    218172-image.png

    0 comments No comments

  2. Samukelo Siyanda Zungu 21 Reputation points
    2022-07-06T11:20:34.777+00:00

    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;  
            // }  
    
    
        }  
    }  
    

    }

    0 comments No comments