question

SamukeloSiyanda-0448 avatar image
0 Votes"
SamukeloSiyanda-0448 asked SamukeloSiyanda-0448 answered

Windows application in C# using visual studio

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-serverdotnet-csharpwindows-11windows-apiwindows-forms
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@amukeloSiyanda-0448, Welcome to Microsoft Q&A, What is your current project type? Is it windows forms?Also, If possible, Please provide the related code you have done about your problem.

1 Vote 1 ·

I'm doing windows application. I want the game to display two different cards in the picture box and determine the card that have the highest value , my current code is like this



0 Votes 0 ·

Option 1 Set breakpoints before the problem and step through code, attempt to fix, if this fails move to option 2

Option 2 Best to create a GitHub repository for the Visual Studio solution and post the link here along with a detailed description of the problem and exactly where the problem resides in the source code.

Comments: If you research what is needed to get started there is a decent amount of code needed as shown below which is why it's better to if needed use option 2 if option 1 does not work at your level of coding.

218181-cards.png


0 Votes 0 ·
cards.png (24.3 KiB)
OlafHelper-2800 avatar image
0 Votes"
OlafHelper-2800 answered

but it's not working

"Not working" means what in detail? How does your existing code look like?
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SamukeloSiyanda-0448 avatar image
0 Votes"
SamukeloSiyanda-0448 answered

218172-image.png


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SamukeloSiyanda-0448 avatar image
0 Votes"
SamukeloSiyanda-0448 answered

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


     }
 }

}

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.