12
\$\begingroup\$

This is a start towards implementing a poker-like game called Jacks Or Better.

Screencast

Before proceeding to improve the way cards are shuffled and dealt, I would really appreciate help cleaning up my code.

using Sandbox.Properties;
using System;
using System.Windows.Forms;

namespace Sandbox
{

    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        string[] Cards = { "AClubs", "_2Clubs", "_3Clubs", "_4Clubs", "_5Clubs", "_6Clubs", "_7Clubs", "_8Clubs", "_9Clubs", "_10Clubs", "JClubs", "QClubs", "KClubs", "ADiamonds", "_2Diamonds", "_3Diamonds", "_4Diamonds", "_5Diamonds", "_6Diamonds", "_7Diamonds", "_8Diamonds", "_9Diamonds", "_10Diamonds", "JDiamonds", "QDiamonds", "KDiamonds", "AHearts", "_2Hearts", "_3Hearts", "_4Hearts", "_5Hearts", "_6Hearts", "_7Hearts", "_8Hearts", "_9Hearts", "_10Hearts", "JHearts", "QHearts", "KHearts", "ASpades", "_2Spades", "_3Spades", "_4Spades", "_5Spades", "_6Spades", "_7Spades", "_8Spades", "_9Spades", "_10Spades", "JSpades", "QSpades", "KSpades" };
        private Random R = new Random();
        string Card1;
        string Card2;
        string Card3;
        string Card4;
        string Card5;

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "DEAL")
            {
                Card1 = Cards[R.Next(0, Cards.Length)];
                Card2 = Cards[R.Next(0, Cards.Length)];
                Card3 = Cards[R.Next(0, Cards.Length)];
                Card4 = Cards[R.Next(0, Cards.Length)];
                Card5 = Cards[R.Next(0, Cards.Length)];

                DealCard1();
                DealCard2();
                DealCard3();
                DealCard4();
                DealCard5();

                button1.Text = "DRAW";
            }

            else
            {
                if(label1.Visible == false)
                {
                    Card1 = Cards[R.Next(0, Cards.Length)];
                    DealCard1();
                }

                if (label2.Visible == false)
                {
                    Card2 = Cards[R.Next(0, Cards.Length)];
                    DealCard2();
                }

                if (label3.Visible == false)
                {
                    Card3 = Cards[R.Next(0, Cards.Length)];
                    DealCard3();
                }

                if (label4.Visible == false)
                {
                    Card4 = Cards[R.Next(0, Cards.Length)];
                    DealCard4();
                }

                if (label5.Visible == false)
                {
                    Card5 = Cards[R.Next(0, Cards.Length)];
                    DealCard5();
                }

                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                label4.Visible = false;
                label5.Visible = false;

                button1.Text = "DEAL";
            }
        }

        private void DealCard1()
        {
            if (Card1 == "AClubs")
            {
                pictureBox1.Image = Resources.AClubs;
            }

            if (Card1 == "_2Clubs")
            {
                pictureBox1.Image = Resources._2Clubs;
            }

            if (Card1 == "_3Clubs")
            {
                pictureBox1.Image = Resources._3Clubs;
            }

            if (Card1 == "_4Clubs")
            {
                pictureBox1.Image = Resources._4Clubs;
            }

            if (Card1 == "_5Clubs")
            {
                pictureBox1.Image = Resources._5Clubs;
            }

            if (Card1 == "_6Clubs")
            {
                pictureBox1.Image = Resources._6Clubs;
            }

            if (Card1 == "_7Clubs")
            {
                pictureBox1.Image = Resources._7Clubs;
            }

            if (Card1 == "_8Clubs")
            {
                pictureBox1.Image = Resources._8Clubs;
            }

            if (Card1 == "_9Clubs")
            {
                pictureBox1.Image = Resources._9Clubs;
            }

            if (Card1 == "_10Clubs")
            {
                pictureBox1.Image = Resources._10Clubs;
            }

            if (Card1 == "JClubs")
            {
                pictureBox1.Image = Resources.JClubs;
            }

            if (Card1 == "QClubs")
            {
                pictureBox1.Image = Resources.QClubs;
            }

            if (Card1 == "KClubs")
            {
                pictureBox1.Image = Resources.KClubs;
            }

            if (Card1 == "ADiamonds")
            {
                pictureBox1.Image = Resources.ADiamonds;
            }

            if (Card1 == "_2Diamonds")
            {
                pictureBox1.Image = Resources._2Diamonds;
            }

            if (Card1 == "_3Diamonds")
            {
                pictureBox1.Image = Resources._3Diamonds;
            }

            if (Card1 == "_4Diamonds")
            {
                pictureBox1.Image = Resources._4Diamonds;
            }

            if (Card1 == "_5Diamonds")
            {
                pictureBox1.Image = Resources._5Diamonds;
            }

            if (Card1 == "_6Diamonds")
            {
                pictureBox1.Image = Resources._6Diamonds;
            }

            if (Card1 == "_7Diamonds")
            {
                pictureBox1.Image = Resources._7Diamonds;
            }

            if (Card1 == "_8Diamonds")
            {
                pictureBox1.Image = Resources._8Diamonds;
            }

            if (Card1 == "_9Diamonds")
            {
                pictureBox1.Image = Resources._9Diamonds;
            }

            if (Card1 == "_10Diamonds")
            {
                pictureBox1.Image = Resources._10Diamonds;
            }

            if (Card1 == "JDiamonds")
            {
                pictureBox1.Image = Resources.JDiamonds;
            }

            if (Card1 == "QDiamonds")
            {
                pictureBox1.Image = Resources.QDiamonds;
            }

            if (Card1 == "KDiamonds")
            {
                pictureBox1.Image = Resources.KDiamonds;
            }

            if (Card1 == "AHearts")
            {
                pictureBox1.Image = Resources.AHearts;
            }

            if (Card1 == "_2Hearts")
            {
                pictureBox1.Image = Resources._2Hearts;
            }

            if (Card1 == "_3Hearts")
            {
                pictureBox1.Image = Resources._3Hearts;
            }

            if (Card1 == "_4Hearts")
            {
                pictureBox1.Image = Resources._4Hearts;
            }

            if (Card1 == "_5Hearts")
            {
                pictureBox1.Image = Resources._5Hearts;
            }

            if (Card1 == "_6Hearts")
            {
                pictureBox1.Image = Resources._6Hearts;
            }

            if (Card1 == "_7Hearts")
            {
                pictureBox1.Image = Resources._7Hearts;
            }

            if (Card1 == "_8Hearts")
            {
                pictureBox1.Image = Resources._8Hearts;
            }

            if (Card1 == "_9Hearts")
            {
                pictureBox1.Image = Resources._9Hearts;
            }

            if (Card1 == "_10Hearts")
            {
                pictureBox1.Image = Resources._10Hearts;
            }

            if (Card1 == "JHearts")
            {
                pictureBox1.Image = Resources.JHearts;
            }

            if (Card1 == "QHearts")
            {
                pictureBox1.Image = Resources.QHearts;
            }

            if (Card1 == "KHearts")
            {
                pictureBox1.Image = Resources.KHearts;
            }

            if (Card1 == "ASpades")
            {
                pictureBox1.Image = Resources.ASpades;
            }

            if (Card1 == "_2Spades")
            {
                pictureBox1.Image = Resources._2Spades;
            }

            if (Card1 == "_3Spades")
            {
                pictureBox1.Image = Resources._3Spades;
            }

            if (Card1 == "_4Spades")
            {
                pictureBox1.Image = Resources._4Spades;
            }

            if (Card1 == "_5Spades")
            {
                pictureBox1.Image = Resources._5Spades;
            }

            if (Card1 == "_6Spades")
            {
                pictureBox1.Image = Resources._6Spades;
            }

            if (Card1 == "_7Spades")
            {
                pictureBox1.Image = Resources._7Spades;
            }

            if (Card1 == "_8Spades")
            {
                pictureBox1.Image = Resources._8Spades;
            }

            if (Card1 == "_9Spades")
            {
                pictureBox1.Image = Resources._9Spades;
            }

            if (Card1 == "_10Spades")
            {
                pictureBox1.Image = Resources._10Spades;
            }

            if (Card1 == "JSpades")
            {
                pictureBox1.Image = Resources.JSpades;
            }

            if (Card1 == "QSpades")
            {
                pictureBox1.Image = Resources.QSpades;
            }

            if (Card1 == "KSpades")
            {
                pictureBox1.Image = Resources.KSpades;
            }
        }

        private void DealCard2()
        {
            if (Card2 == "AClubs")
            {
                pictureBox2.Image = Resources.AClubs;
            }

            if (Card2 == "_2Clubs")
            {
                pictureBox2.Image = Resources._2Clubs;
            }

            if (Card2 == "_3Clubs")
            {
                pictureBox2.Image = Resources._3Clubs;
            }

            if (Card2 == "_4Clubs")
            {
                pictureBox2.Image = Resources._4Clubs;
            }

            if (Card2 == "_5Clubs")
            {
                pictureBox2.Image = Resources._5Clubs;
            }

            if (Card2 == "_6Clubs")
            {
                pictureBox2.Image = Resources._6Clubs;
            }

            if (Card2 == "_7Clubs")
            {
                pictureBox2.Image = Resources._7Clubs;
            }

            if (Card2 == "_8Clubs")
            {
                pictureBox2.Image = Resources._8Clubs;
            }

            if (Card2 == "_9Clubs")
            {
                pictureBox2.Image = Resources._9Clubs;
            }

            if (Card2 == "_10Clubs")
            {
                pictureBox2.Image = Resources._10Clubs;
            }

            if (Card2 == "JClubs")
            {
                pictureBox2.Image = Resources.JClubs;
            }

            if (Card2 == "QClubs")
            {
                pictureBox2.Image = Resources.QClubs;
            }

            if (Card2 == "KClubs")
            {
                pictureBox2.Image = Resources.KClubs;
            }

            if (Card2 == "ADiamonds")
            {
                pictureBox2.Image = Resources.ADiamonds;
            }

            if (Card2 == "_2Diamonds")
            {
                pictureBox2.Image = Resources._2Diamonds;
            }

            if (Card2 == "_3Diamonds")
            {
                pictureBox2.Image = Resources._3Diamonds;
            }

            if (Card2 == "_4Diamonds")
            {
                pictureBox2.Image = Resources._4Diamonds;
            }

            if (Card2 == "_5Diamonds")
            {
                pictureBox2.Image = Resources._5Diamonds;
            }

            if (Card2 == "_6Diamonds")
            {
                pictureBox2.Image = Resources._6Diamonds;
            }

            if (Card2 == "_7Diamonds")
            {
                pictureBox2.Image = Resources._7Diamonds;
            }

            if (Card2 == "_8Diamonds")
            {
                pictureBox2.Image = Resources._8Diamonds;
            }

            if (Card2 == "_9Diamonds")
            {
                pictureBox2.Image = Resources._9Diamonds;
            }

            if (Card2 == "_10Diamonds")
            {
                pictureBox2.Image = Resources._10Diamonds;
            }

            if (Card2 == "JDiamonds")
            {
                pictureBox2.Image = Resources.JDiamonds;
            }

            if (Card2 == "QDiamonds")
            {
                pictureBox2.Image = Resources.QDiamonds;
            }

            if (Card2 == "KDiamonds")
            {
                pictureBox2.Image = Resources.KDiamonds;
            }

            if (Card2 == "AHearts")
            {
                pictureBox2.Image = Resources.AHearts;
            }

            if (Card2 == "_2Hearts")
            {
                pictureBox2.Image = Resources._2Hearts;
            }

            if (Card2 == "_3Hearts")
            {
                pictureBox2.Image = Resources._3Hearts;
            }

            if (Card2 == "_4Hearts")
            {
                pictureBox2.Image = Resources._4Hearts;
            }

            if (Card2 == "_5Hearts")
            {
                pictureBox2.Image = Resources._5Hearts;
            }

            if (Card2 == "_6Hearts")
            {
                pictureBox2.Image = Resources._6Hearts;
            }

            if (Card2 == "_7Hearts")
            {
                pictureBox2.Image = Resources._7Hearts;
            }

            if (Card2 == "_8Hearts")
            {
                pictureBox2.Image = Resources._8Hearts;
            }

            if (Card2 == "_9Hearts")
            {
                pictureBox2.Image = Resources._9Hearts;
            }

            if (Card2 == "_10Hearts")
            {
                pictureBox2.Image = Resources._10Hearts;
            }

            if (Card2 == "JHearts")
            {
                pictureBox2.Image = Resources.JHearts;
            }

            if (Card2 == "QHearts")
            {
                pictureBox2.Image = Resources.QHearts;
            }

            if (Card2 == "KHearts")
            {
                pictureBox2.Image = Resources.KHearts;
            }

            if (Card2 == "ASpades")
            {
                pictureBox2.Image = Resources.ASpades;
            }

            if (Card2 == "_2Spades")
            {
                pictureBox2.Image = Resources._2Spades;
            }

            if (Card2 == "_3Spades")
            {
                pictureBox2.Image = Resources._3Spades;
            }

            if (Card2 == "_4Spades")
            {
                pictureBox2.Image = Resources._4Spades;
            }

            if (Card2 == "_5Spades")
            {
                pictureBox2.Image = Resources._5Spades;
            }

            if (Card2 == "_6Spades")
            {
                pictureBox2.Image = Resources._6Spades;
            }

            if (Card2 == "_7Spades")
            {
                pictureBox2.Image = Resources._7Spades;
            }

            if (Card2 == "_8Spades")
            {
                pictureBox2.Image = Resources._8Spades;
            }

            if (Card2 == "_9Spades")
            {
                pictureBox2.Image = Resources._9Spades;
            }

            if (Card2 == "_10Spades")
            {
                pictureBox2.Image = Resources._10Spades;
            }

            if (Card2 == "JSpades")
            {
                pictureBox2.Image = Resources.JSpades;
            }

            if (Card2 == "QSpades")
            {
                pictureBox2.Image = Resources.QSpades;
            }

            if (Card2 == "KSpades")
            {
                pictureBox2.Image = Resources.KSpades;
            }
        }

        private void DealCard3()
        {
            if (Card3 == "AClubs")
            {
                pictureBox3.Image = Resources.AClubs;
            }

            if (Card3 == "_2Clubs")
            {
                pictureBox3.Image = Resources._2Clubs;
            }

            if (Card3 == "_3Clubs")
            {
                pictureBox3.Image = Resources._3Clubs;
            }

            if (Card3 == "_4Clubs")
            {
                pictureBox3.Image = Resources._4Clubs;
            }

            if (Card3 == "_5Clubs")
            {
                pictureBox3.Image = Resources._5Clubs;
            }

            if (Card3 == "_6Clubs")
            {
                pictureBox3.Image = Resources._6Clubs;
            }

            if (Card3 == "_7Clubs")
            {
                pictureBox3.Image = Resources._7Clubs;
            }

            if (Card3 == "_8Clubs")
            {
                pictureBox3.Image = Resources._8Clubs;
            }

            if (Card3 == "_9Clubs")
            {
                pictureBox3.Image = Resources._9Clubs;
            }

            if (Card3 == "_10Clubs")
            {
                pictureBox3.Image = Resources._10Clubs;
            }

            if (Card3 == "JClubs")
            {
                pictureBox3.Image = Resources.JClubs;
            }

            if (Card3 == "QClubs")
            {
                pictureBox3.Image = Resources.QClubs;
            }

            if (Card3 == "KClubs")
            {
                pictureBox3.Image = Resources.KClubs;
            }

            if (Card3 == "ADiamonds")
            {
                pictureBox3.Image = Resources.ADiamonds;
            }

            if (Card3 == "_2Diamonds")
            {
                pictureBox3.Image = Resources._2Diamonds;
            }

            if (Card3 == "_3Diamonds")
            {
                pictureBox3.Image = Resources._3Diamonds;
            }

            if (Card3 == "_4Diamonds")
            {
                pictureBox3.Image = Resources._4Diamonds;
            }

            if (Card3 == "_5Diamonds")
            {
                pictureBox3.Image = Resources._5Diamonds;
            }

            if (Card3 == "_6Diamonds")
            {
                pictureBox3.Image = Resources._6Diamonds;
            }

            if (Card3 == "_7Diamonds")
            {
                pictureBox3.Image = Resources._7Diamonds;
            }

            if (Card3 == "_8Diamonds")
            {
                pictureBox3.Image = Resources._8Diamonds;
            }

            if (Card3 == "_9Diamonds")
            {
                pictureBox3.Image = Resources._9Diamonds;
            }

            if (Card3 == "_10Diamonds")
            {
                pictureBox3.Image = Resources._10Diamonds;
            }

            if (Card3 == "JDiamonds")
            {
                pictureBox3.Image = Resources.JDiamonds;
            }

            if (Card3 == "QDiamonds")
            {
                pictureBox3.Image = Resources.QDiamonds;
            }

            if (Card3 == "KDiamonds")
            {
                pictureBox3.Image = Resources.KDiamonds;
            }

            if (Card3 == "AHearts")
            {
                pictureBox3.Image = Resources.AHearts;
            }

            if (Card3 == "_2Hearts")
            {
                pictureBox3.Image = Resources._2Hearts;
            }

            if (Card3 == "_3Hearts")
            {
                pictureBox3.Image = Resources._3Hearts;
            }

            if (Card3 == "_4Hearts")
            {
                pictureBox3.Image = Resources._4Hearts;
            }

            if (Card3 == "_5Hearts")
            {
                pictureBox3.Image = Resources._5Hearts;
            }

            if (Card3 == "_6Hearts")
            {
                pictureBox3.Image = Resources._6Hearts;
            }

            if (Card3 == "_7Hearts")
            {
                pictureBox3.Image = Resources._7Hearts;
            }

            if (Card3 == "_8Hearts")
            {
                pictureBox3.Image = Resources._8Hearts;
            }

            if (Card3 == "_9Hearts")
            {
                pictureBox3.Image = Resources._9Hearts;
            }

            if (Card3 == "_10Hearts")
            {
                pictureBox3.Image = Resources._10Hearts;
            }

            if (Card3 == "JHearts")
            {
                pictureBox3.Image = Resources.JHearts;
            }

            if (Card3 == "QHearts")
            {
                pictureBox3.Image = Resources.QHearts;
            }

            if (Card3 == "KHearts")
            {
                pictureBox3.Image = Resources.KHearts;
            }

            if (Card3 == "ASpades")
            {
                pictureBox3.Image = Resources.ASpades;
            }

            if (Card3 == "_2Spades")
            {
                pictureBox3.Image = Resources._2Spades;
            }

            if (Card3 == "_3Spades")
            {
                pictureBox3.Image = Resources._3Spades;
            }

            if (Card3 == "_4Spades")
            {
                pictureBox3.Image = Resources._4Spades;
            }

            if (Card3 == "_5Spades")
            {
                pictureBox3.Image = Resources._5Spades;
            }

            if (Card3 == "_6Spades")
            {
                pictureBox3.Image = Resources._6Spades;
            }

            if (Card3 == "_7Spades")
            {
                pictureBox3.Image = Resources._7Spades;
            }

            if (Card3 == "_8Spades")
            {
                pictureBox3.Image = Resources._8Spades;
            }

            if (Card3 == "_9Spades")
            {
                pictureBox3.Image = Resources._9Spades;
            }

            if (Card3 == "_10Spades")
            {
                pictureBox3.Image = Resources._10Spades;
            }

            if (Card3 == "JSpades")
            {
                pictureBox3.Image = Resources.JSpades;
            }

            if (Card3 == "QSpades")
            {
                pictureBox3.Image = Resources.QSpades;
            }

            if (Card3 == "KSpades")
            {
                pictureBox3.Image = Resources.KSpades;
            }
        }

        private void DealCard4()
        {
            if (Card4 == "AClubs")
            {
                pictureBox4.Image = Resources.AClubs;
            }

            if (Card4 == "_2Clubs")
            {
                pictureBox4.Image = Resources._2Clubs;
            }

            if (Card4 == "_3Clubs")
            {
                pictureBox4.Image = Resources._3Clubs;
            }

            if (Card4 == "_4Clubs")
            {
                pictureBox4.Image = Resources._4Clubs;
            }

            if (Card4 == "_5Clubs")
            {
                pictureBox4.Image = Resources._5Clubs;
            }

            if (Card4 == "_6Clubs")
            {
                pictureBox4.Image = Resources._6Clubs;
            }

            if (Card4 == "_7Clubs")
            {
                pictureBox4.Image = Resources._7Clubs;
            }

            if (Card4 == "_8Clubs")
            {
                pictureBox4.Image = Resources._8Clubs;
            }

            if (Card4 == "_9Clubs")
            {
                pictureBox4.Image = Resources._9Clubs;
            }

            if (Card4 == "_10Clubs")
            {
                pictureBox4.Image = Resources._10Clubs;
            }

            if (Card4 == "JClubs")
            {
                pictureBox4.Image = Resources.JClubs;
            }

            if (Card4 == "QClubs")
            {
                pictureBox4.Image = Resources.QClubs;
            }

            if (Card4 == "KClubs")
            {
                pictureBox4.Image = Resources.KClubs;
            }

            if (Card4 == "ADiamonds")
            {
                pictureBox4.Image = Resources.ADiamonds;
            }

            if (Card4 == "_2Diamonds")
            {
                pictureBox4.Image = Resources._2Diamonds;
            }

            if (Card4 == "_3Diamonds")
            {
                pictureBox4.Image = Resources._3Diamonds;
            }

            if (Card4 == "_4Diamonds")
            {
                pictureBox4.Image = Resources._4Diamonds;
            }

            if (Card4 == "_5Diamonds")
            {
                pictureBox4.Image = Resources._5Diamonds;
            }

            if (Card4 == "_6Diamonds")
            {
                pictureBox4.Image = Resources._6Diamonds;
            }

            if (Card4 == "_7Diamonds")
            {
                pictureBox4.Image = Resources._7Diamonds;
            }

            if (Card4 == "_8Diamonds")
            {
                pictureBox4.Image = Resources._8Diamonds;
            }

            if (Card4 == "_9Diamonds")
            {
                pictureBox4.Image = Resources._9Diamonds;
            }

            if (Card4 == "_10Diamonds")
            {
                pictureBox4.Image = Resources._10Diamonds;
            }

            if (Card4 == "JDiamonds")
            {
                pictureBox4.Image = Resources.JDiamonds;
            }

            if (Card4 == "QDiamonds")
            {
                pictureBox4.Image = Resources.QDiamonds;
            }

            if (Card4 == "KDiamonds")
            {
                pictureBox4.Image = Resources.KDiamonds;
            }

            if (Card4 == "AHearts")
            {
                pictureBox4.Image = Resources.AHearts;
            }

            if (Card4 == "_2Hearts")
            {
                pictureBox4.Image = Resources._2Hearts;
            }

            if (Card4 == "_3Hearts")
            {
                pictureBox4.Image = Resources._3Hearts;
            }

            if (Card4 == "_4Hearts")
            {
                pictureBox4.Image = Resources._4Hearts;
            }

            if (Card4 == "_5Hearts")
            {
                pictureBox4.Image = Resources._5Hearts;
            }

            if (Card4 == "_6Hearts")
            {
                pictureBox4.Image = Resources._6Hearts;
            }

            if (Card4 == "_7Hearts")
            {
                pictureBox4.Image = Resources._7Hearts;
            }

            if (Card4 == "_8Hearts")
            {
                pictureBox4.Image = Resources._8Hearts;
            }

            if (Card4 == "_9Hearts")
            {
                pictureBox4.Image = Resources._9Hearts;
            }

            if (Card4 == "_10Hearts")
            {
                pictureBox4.Image = Resources._10Hearts;
            }

            if (Card4 == "JHearts")
            {
                pictureBox4.Image = Resources.JHearts;
            }

            if (Card4 == "QHearts")
            {
                pictureBox4.Image = Resources.QHearts;
            }

            if (Card4 == "KHearts")
            {
                pictureBox4.Image = Resources.KHearts;
            }

            if (Card4 == "ASpades")
            {
                pictureBox4.Image = Resources.ASpades;
            }

            if (Card4 == "_2Spades")
            {
                pictureBox4.Image = Resources._2Spades;
            }

            if (Card4 == "_3Spades")
            {
                pictureBox4.Image = Resources._3Spades;
            }

            if (Card4 == "_4Spades")
            {
                pictureBox4.Image = Resources._4Spades;
            }

            if (Card4 == "_5Spades")
            {
                pictureBox4.Image = Resources._5Spades;
            }

            if (Card4 == "_6Spades")
            {
                pictureBox4.Image = Resources._6Spades;
            }

            if (Card4 == "_7Spades")
            {
                pictureBox4.Image = Resources._7Spades;
            }

            if (Card4 == "_8Spades")
            {
                pictureBox4.Image = Resources._8Spades;
            }

            if (Card4 == "_9Spades")
            {
                pictureBox4.Image = Resources._9Spades;
            }

            if (Card4 == "_10Spades")
            {
                pictureBox4.Image = Resources._10Spades;
            }

            if (Card4 == "JSpades")
            {
                pictureBox4.Image = Resources.JSpades;
            }

            if (Card4 == "QSpades")
            {
                pictureBox4.Image = Resources.QSpades;
            }

            if (Card4 == "KSpades")
            {
                pictureBox4.Image = Resources.KSpades;
            }
        }

        private void DealCard5()
        {
            if (Card5 == "AClubs")
            {
                pictureBox5.Image = Resources.AClubs;
            }

            if (Card5 == "_2Clubs")
            {
                pictureBox5.Image = Resources._2Clubs;
            }

            if (Card5 == "_3Clubs")
            {
                pictureBox5.Image = Resources._3Clubs;
            }

            if (Card5 == "_4Clubs")
            {
                pictureBox5.Image = Resources._4Clubs;
            }

            if (Card5 == "_5Clubs")
            {
                pictureBox5.Image = Resources._5Clubs;
            }

            if (Card5 == "_6Clubs")
            {
                pictureBox5.Image = Resources._6Clubs;
            }

            if (Card5 == "_7Clubs")
            {
                pictureBox5.Image = Resources._7Clubs;
            }

            if (Card5 == "_8Clubs")
            {
                pictureBox5.Image = Resources._8Clubs;
            }

            if (Card5 == "_9Clubs")
            {
                pictureBox5.Image = Resources._9Clubs;
            }

            if (Card5 == "_10Clubs")
            {
                pictureBox5.Image = Resources._10Clubs;
            }

            if (Card5 == "JClubs")
            {
                pictureBox5.Image = Resources.JClubs;
            }

            if (Card5 == "QClubs")
            {
                pictureBox5.Image = Resources.QClubs;
            }

            if (Card5 == "KClubs")
            {
                pictureBox5.Image = Resources.KClubs;
            }

            if (Card5 == "ADiamonds")
            {
                pictureBox5.Image = Resources.ADiamonds;
            }

            if (Card5 == "_2Diamonds")
            {
                pictureBox5.Image = Resources._2Diamonds;
            }

            if (Card5 == "_3Diamonds")
            {
                pictureBox5.Image = Resources._3Diamonds;
            }

            if (Card5 == "_4Diamonds")
            {
                pictureBox5.Image = Resources._4Diamonds;
            }

            if (Card5 == "_5Diamonds")
            {
                pictureBox5.Image = Resources._5Diamonds;
            }

            if (Card5 == "_6Diamonds")
            {
                pictureBox5.Image = Resources._6Diamonds;
            }

            if (Card5 == "_7Diamonds")
            {
                pictureBox5.Image = Resources._7Diamonds;
            }

            if (Card5 == "_8Diamonds")
            {
                pictureBox5.Image = Resources._8Diamonds;
            }

            if (Card5 == "_9Diamonds")
            {
                pictureBox5.Image = Resources._9Diamonds;
            }

            if (Card5 == "_10Diamonds")
            {
                pictureBox5.Image = Resources._10Diamonds;
            }

            if (Card5 == "JDiamonds")
            {
                pictureBox5.Image = Resources.JDiamonds;
            }

            if (Card5 == "QDiamonds")
            {
                pictureBox5.Image = Resources.QDiamonds;
            }

            if (Card5 == "KDiamonds")
            {
                pictureBox5.Image = Resources.KDiamonds;
            }

            if (Card5 == "AHearts")
            {
                pictureBox5.Image = Resources.AHearts;
            }

            if (Card5 == "_2Hearts")
            {
                pictureBox5.Image = Resources._2Hearts;
            }

            if (Card5 == "_3Hearts")
            {
                pictureBox5.Image = Resources._3Hearts;
            }

            if (Card5 == "_4Hearts")
            {
                pictureBox5.Image = Resources._4Hearts;
            }

            if (Card5 == "_5Hearts")
            {
                pictureBox5.Image = Resources._5Hearts;
            }

            if (Card5 == "_6Hearts")
            {
                pictureBox5.Image = Resources._6Hearts;
            }

            if (Card5 == "_7Hearts")
            {
                pictureBox5.Image = Resources._7Hearts;
            }

            if (Card5 == "_8Hearts")
            {
                pictureBox5.Image = Resources._8Hearts;
            }

            if (Card5 == "_9Hearts")
            {
                pictureBox5.Image = Resources._9Hearts;
            }

            if (Card5 == "_10Hearts")
            {
                pictureBox5.Image = Resources._10Hearts;
            }

            if (Card5 == "JHearts")
            {
                pictureBox5.Image = Resources.JHearts;
            }

            if (Card5 == "QHearts")
            {
                pictureBox5.Image = Resources.QHearts;
            }

            if (Card5 == "KHearts")
            {
                pictureBox5.Image = Resources.KHearts;
            }

            if (Card5 == "ASpades")
            {
                pictureBox5.Image = Resources.ASpades;
            }

            if (Card5 == "_2Spades")
            {
                pictureBox5.Image = Resources._2Spades;
            }

            if (Card5 == "_3Spades")
            {
                pictureBox5.Image = Resources._3Spades;
            }

            if (Card5 == "_4Spades")
            {
                pictureBox5.Image = Resources._4Spades;
            }

            if (Card5 == "_5Spades")
            {
                pictureBox5.Image = Resources._5Spades;
            }

            if (Card5 == "_6Spades")
            {
                pictureBox5.Image = Resources._6Spades;
            }

            if (Card5 == "_7Spades")
            {
                pictureBox5.Image = Resources._7Spades;
            }

            if (Card5 == "_8Spades")
            {
                pictureBox5.Image = Resources._8Spades;
            }

            if (Card5 == "_9Spades")
            {
                pictureBox5.Image = Resources._9Spades;
            }

            if (Card5 == "_10Spades")
            {
                pictureBox5.Image = Resources._10Spades;
            }

            if (Card5 == "JSpades")
            {
                pictureBox5.Image = Resources.JSpades;
            }

            if (Card5 == "QSpades")
            {
                pictureBox5.Image = Resources.QSpades;
            }

            if (Card5 == "KSpades")
            {
                pictureBox5.Image = Resources.KSpades;
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "DRAW")
            {
                if (label1.Visible == false)
                {
                    label1.Visible = true;
                }
                else
                {
                    label1.Visible = false;
                }
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (button1.Text == "DRAW")
            {
                if (label2.Visible == false)
                {
                    label2.Visible = true;
                }
                else
                {
                    label2.Visible = false;
                }
            }
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (button1.Text == "DRAW")
            {
                if (label3.Visible == false)
                {
                    label3.Visible = true;
                }
                else
                {
                    label3.Visible = false;
                }
            }
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            if (button1.Text == "DRAW")
            {
                if (label4.Visible == false)
                {
                    label4.Visible = true;
                }
                else
                {
                    label4.Visible = false;
                }
            }
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            if (button1.Text == "DRAW")
            {
                if (label5.Visible == false)
                {
                    label5.Visible = true;
                }
                else
                {
                    label5.Visible = false;
                }
            }
        }
    }

}
\$\endgroup\$
15
\$\begingroup\$

I really can't keep my poker face on this one; This code is bad.

You have so many copy-pasted blocks absolutely everywhere, that I'm getting high from all the paste (glue).

Generic jokes and bad puns aside, this does need refactoring.

How can I refactor this?

These would be good, for a start:

  • Cards would be better be called Deck as that's what they're called.
  • Cards could be better assigned by looping over the suits (Diamonds, Hearts, Spades, Clubs) and assigning all thirteen cards for each suit.
  • Why are you attaching _ to all the numbered cards? Just swap 2 with two and so on.
  • string Card1;: declaring each card as a string could be improved by using a an List of cards, and storing the properties inside that, and, you could call it Hand, because that's also what it is.
  • the DealCard blocks: first, it would be better to pass in the card you're swapping values with, and the picture block you're swapping with as parameters, which would reduce these blocks down to one, but, there's an even better way:
ResourceManager rm = Resources.ResourceManager;
public void DealCard(PictureBox pictureBox, string value){ 
     pictureBox.image = rm.GetObject(value);
}
DealCard(pictureBox1, Hand[0]);
DealCard(pictureBox2, Hand[1]);
DealCard(pictureBox3, Hand[2]);
SetImage(pictureBox4, Hand[3]);
SetImage(pictureBox5, Hand[4]);

pictureBox_click:

  • You can return or set as depending on boolean condition, so instead of the if loop, do this labelX.Visible = !label.
Dictionary<PictureBox, Label> labels = new Dictionary<PictureBox, Label>();
openWith.Add(pictureBox1, label1);
openWith.Add(pictureBox2, label2);
openWith.Add(pictureBox3, label4);
openWith.Add(pictureBox4, label4);
openWith.Add(pictureBox5, label5);

private void pictureBox_click(object sender, EventArgs e)
{
    if (button1.Text == "DRAW")
    {
        lables[sender].Visible = !lables[sender].Visible;
    }
}

and then just assign pictureBox_click to all the picture boxes.


Leaving you with:

using Sandbox.Properties;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Sandbox {

    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }

        string[] Suits = {"Hearts", "Diamonds", "Clubs", "Spades"};
        string[] Cards = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        List<string> Deck = new List<string>();
        foreach (string Suit in Suits) {
            foreach (string Card in Cards){
                Deck.add(Card + Suit);
            }
        }

        List<PictureBox> pictureBoxes = new List<PictureBox>();
        pictureBoxes.add(pictureBox1);
        pictureBoxes.add(pictureBox2);
        pictureBoxes.add(pictureBox3);
        pictureBoxes.add(pictureBox4);
        pictureBoxes.add(pictureBox5);

        Dictionary<PictureBox, Label> labelsLinkedToPictureBoxes = new Dictionary<PictureBox, Label>();
        lablesLinkedToPictureBoxes.Add(pictureBox1, label1);
        lablesLinkedToPictureBoxes.Add(pictureBox2, label2);
        lablesLinkedToPictureBoxes.Add(pictureBox3, label4);
        lablesLinkedToPictureBoxes.Add(pictureBox4, label4);
        lablesLinkedToPictureBoxes.Add(pictureBox5, label5);

        private Random R = new Random();
        ResourceManager rm = Resources.ResourceManager;
        List<string> Hand = new List<string>();
        for (int i = 0; i < 5; i++) {
            Hand.add(string.Empty);
        }
        private void DealCard(PictureBox pb, string resource) {
            pb.image = rm.GetObject(resource);
        }
        private void AssignCard(int handPosition) {
            Hand[handPosition] = Deck[R.Next(0, Deck.Length)];
        }
        private void button1_Click(object sender, EventArgs e) {
            if (button1.Text == "DEAL") {
                for(int i = 0; i < Hand.length; i++) {
                    AssignCard(i);
                    DealCard(pictureBoxes(i), Hand[i]);
                }
                button1.Text = "DRAW";
            } else {
                Label[] labels = [label1, label2, label3, label4, label5];
                for (int i = 0; i < Labels.length; i++) {
                    if (!Labels[i].Visible) {
                        AssignCard(i);
                        DealCard(pictureBoxes[i], Hand[i]);
                    }
                    Labels[i].Visible = false;
                }
                button1.Text = "DEAL";
            }
        }
        private void pictureBox_click(object sender, EventArgs e) {
            if (button1.Text == "DRAW") {
                labelsLinkedToPictureBoxes[sender].Visible = !labelsLinkedToPictureBoxes[sender].Visible;
            }
        }
    }
}
\$\endgroup\$
6
\$\begingroup\$

I suggest a dictionary (or hashset) to avoid so many ifs:

 nameToPicture = {..., "JClubs": Resources.JClubs, ...}

You can then just use

 image = nameToPicture[card_name]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.