1

im pretty new to C# and i want to make a cardgame. What i have is a list of cards (strings) with names like c6, s3, h11, d13 where the character represents the colour and the number represents the value. When pressing a button the program takes a random string from the list and displays it in a textbox. From there the point of the game is to guess if the next random card will have a higher value or a lower value then the previous card.

What i want to do is to take the string from the textbox and turn it into a int so that i can compare the value of the previous card with the new one. Only problem is how do i get rid of the c in c6 so i can convert it by using parse.

This is my code.

public partial class MainWindow : Window
{
    static Random rndmlist = new Random();
    Random rndm = new Random();
    List<string> deck = new List<string>();
    int score = 0;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void test_Click(object sender, RoutedEventArgs e)
    {
        //disregard this
        foreach (string j in deck)
        {
            testbox.Text += j + ", ";
        }
    }

    private void btnstart_Click(object sender, RoutedEventArgs e)
    {
        //this is where i add all the cards to the list
        for (int i = 1; i <= 13;)
        {
            deck.Add("c" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("s" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("h" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("d" + i);
            i++;
        }
    }

    private void btnbegin_Click(object sender, RoutedEventArgs e)
    {
        //this is where i take a random card from the list and display it in textBox2
        int r = rndmlist.Next(deck.Count);
        textBox2.Text = ((string)deck[r]);

        //disregard this
        testbox.Text += ((string)deck[r]) + ", ";
        deck.Remove((string)deck[r]);
    }

    private void btnhigh_Click(object sender, RoutedEventArgs e)
    {
        //this is where i want to compare the cards.
    }
}

Thank you in advance for reading this. (:

4
  • 2
    int.Parse(str.Substring(1)) Commented May 28, 2013 at 11:02
  • 11
    It would probably be better if you modeled cards with a struct Card having Suit and Value fields that map to enums instead of dumb strings. Commented May 28, 2013 at 11:02
  • 1
    use mystring.Substring(1); Commented May 28, 2013 at 11:02
  • If you structure your database in the right way you'll find that it will do half the work for you. Using Code values within strings and then having to extract those values every time you want to do something isn't very efficient. Commented Oct 22, 2024 at 16:24

5 Answers 5

5

I'd better create a class Card, which represents a card, with 2 properties: Color and Number and implemented method Card.ParseFromString()

Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

 string SubString = MyString.Substring(1);

But take care if the string is empty, it is an error case.

1 Comment

I decided to go with this one since it's simple. Thanks for all the help :D
1

Assuming there will always be one (and only one) character before the number, you can simply do this:

string numberAsString = "c2".Substring(1);

And to make that an int:

int number = Int32.Parse(numberAsString);

Comments

0

You can use Regex to replace all alpha characters eg

string result = Regex.Replace(myString, @"[a-zA-Z\s]+", string.Empty);

Comments

0
string str = "c12";
var noChars = str.SubString(1); // take a new string from index 1
var number = Int32.Parse(noChars);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.