Click here to Skip to main content
15,896,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get this War Card game to work for an experiment. I am trying to make two pic boxes show the card the player and computer got as well, but I don't know how.

I borrowed a bit of code from several people and mixed it with my own. But now I'm a bit lost. I'm getting these errors and I don't know why.

I'm a beginner please help.

What I have tried:

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 WarCardGame
{
    public partial class WarCardGame : Form
    {

        static string[] cards = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
        static string[] suits = { "clubs", "diamonds", "hearts", "spades" };
        int[] shuffledDeck = new int[52];
        static Random random = new Random();
        static int counter = 0;
        public int eraseOutput = 0;
        public int playerScore = 0;
        public int compScore = 0;
        public int selectNum;
        String playerCard, computerCard, playerSuit, computerSuit;

        public WarCardGame()
        {
            InitializeComponent();
            picCompCard.Image = Properties.Resources.card;
            picPlayerCard.Image = Properties.Resources.card;
            btnDeal.Visible = true;
            btnPlay.Visible = false;
        }


        public void FillDeck(object sender, EventArgs e)
        {
            bool foundCard = false;
            int card;

            btnDeal.Visible = false;
            btnPlay.Visible = true;

            for (int i = 0; i < shuffledDeck.Length; i++)
            {
                foundCard = false;

                while (foundCard == false)
                {
                    card = random.Next(1, 53);

                    if (Array.IndexOf(shuffledDeck, card) == -1)
                    {
                        shuffledDeck[i] = card;
                        foundCard = true;
                    }
                }
            }
        }

        public int SelectCard()
        {

            if (counter < 26)
            {
                int card = shuffledDeck[counter];
                return card;
            }
            else
            {
                return result();
            }

            counter++;
        }


        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }



        private void button1_Click(object sender, EventArgs e)
        {

            int eraseOutput;
            int selectNum, playerNum, compNum, playerTotal, compTotal;
            String playerCard, compCard, playerSuit, compSuit;
            //int playerScore = 0;
            //int compScore = 0; 

            playerTotal = compTotal = eraseOutput = 0;

            if (eraseOutput % 5 == 0)
            {
                outputTextBox.Clear();
            }

            selectNum = SelectCard() - 1;
            compNum = (selectNum - 1) / 4;
            compCard = cards[compNum];

            if (compNum % 4 == 1)
            {
                compSuit = suits[0];
            }
            else if (compNum % 4 == 2)
            {
                compSuit = suits[1];
            }
            else if (compNum % 4 == 3)
            {
                compSuit = suits[2];
            }
            else
            {
                compSuit = suits[3];
            }

            selectNum = SelectCard() - 1;
            playerNum = (selectNum - 1) / 4;
            playerCard = cards[playerNum];

            if (playerNum % 4 == 1)
            {
                playerSuit = suits[0];
            }
            else if (playerNum == 2)
            {
                playerSuit = suits[1];
            }
            else if (playerNum % 4 == 2)
            {
                playerSuit = suits[2];
            }
            else
            {
                playerSuit = suits[3];
            }

            switch (compNum)
            {
                case 1:
                    compCard = cards[compNum];
                    break;
                case 2:
                    compCard = cards[compNum];
                    break;
                case 3:
                    compCard = cards[compNum];
                    break;
                case 4:
                    computerCard = cards[compNum];
                    break;
            }

            if (playerNum > compNum)
            {
                playerTotal += 2;
                txtPlayerTtlScore.Text = Convert.ToString(playerTotal);
            }
            else if (playerNum < compNum)
            {
                compTotal += 2;
                txtCompTtlScore.Text = Convert.ToString(compScore);
            }
            else
            {
                playerTotal++;
                compTotal++;
                txtPlayerTtlScore.Text = Convert.ToString(playerTotal);
                txtCompTtlScore.Text = Convert.ToString(compScore);
            }

            outputTextBox.AppendText("Deal no." + (counter + 1) + " Player has " + cards[playerNum] + " of " + playerSuit + " Computer has " + cards[compNum] + " of " + compSuit + "\n Player score is " + playerTotal + " Computer score is " + compTotal + ".\n");

            result();

            eraseOutput++;
        }

        private void btnDeal_Click(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 1-May-22 20:22pm
Comments
[no name] 1-May-22 23:31pm    
You could use a Unicode character code instead of an image (if you don't already have images). You can treat them as text (box) strings; change the font size; foreground color.

https://www.alt-codes.net/playing-cards-symbols.php

1 solution

I don't know where you found that code, but it's pretty poor.
this code for example:
C#
for (int i = 0; i < shuffledDeck.Length; i++)
{
    foundCard = false;

    while (foundCard == false)
    {
        card = random.Next(1, 53);

        if (Array.IndexOf(shuffledDeck, card) == -1)
        {
            shuffledDeck[i] = card;
            foundCard = true;
        }
    }
}
How long does it take to "shuffle" the whole deck when you do it like that?
There are 52 "cards" in the deck: so by the half way point (26 cards "shuffled") the odds of a duplication using a random number are 50:50, and they get worse as you put more cards into the pack. So for the last card, there is one possibility in 52 that your card will be unique. Since random numbers are just that, it potentially could take an infinite amount of time to get that unique number!

Don't shuffle cards by generating new values: Write a method that returns a new deck, and then shuffle that:
C#
List<int> NewDeckPlease()
   {
   return Enumerable.Range(1, 52).ToList();
   }
C#
private Random random = new Random();
int[] ShuffleDeck(List<int> deck)
    {
    int[] result = new int[deck.Count];
    for (int i = 0; i < result.Length; i++)
        {
        int index = random.Next(deck.Count);
        result[i] = deck[index];
        deck.RemoveAt(index);
        }
    return result;
    }

That takes a fixed amount of time and can easily be extended later if you decide to do your cards in a better way.

For example ... a better way would be to use the OOPs principles that C# is built on: instead of holding cards as an integer between 1 and 52 inclusive, why not create a Card class: that way, the card could have a suit, a colour, a value, and even a picture which would make your code a lot simpler:
C#
public class PlayingCard
    {
    public enum CardSuit
        {
        Spade = 0,
        Diamond = 1,
        Club = 2,
        Heart = 3,
        }
    private static Color[] Colours = new Color[] { Color.Black, Color.Red, Color.Black, Color.Red };
    private static string[] Names = new string[] { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
                                                   "Nine", "Ten", "Jack", "Queen", "King"};
    public int Value { get; set; }
    public CardSuit Suit { get; set; }
    public Color Color { get { return Colours[(int)Suit]; } }
    public Image Picture { get; set; }
    public string Name { get { return Names[Value - 1]; } }
    public override string ToString()
        {
        return $"{Name} of {Suit}";
        }
    }
Now when you want to compare two cards when looking for a pair it's obvious:
C#
if (cardA.Value == cardB.Value)
   ...

How were you going to compare values with your system?

I would strongly suggest that you throw away the mixture of code you have assembled from I don't know how many poor students homework, sit down and plan some code for yourself using classes. And you will probably find it's a lot easier and quicker than trying to hammer different bits of code into a working solution!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900