Search code examples
c#poker

Poker Hand Evaluator Array of Structures


For a task I have been asked the following:

Write a program that evaluates a single poker hand. Display the hand and print out what type it is. The ranks of hands are listed below from highest ranking to lowest ranking. Use a structure for the card, and an array of structures for the hand. You should write a method for each type of hand, returning true if the hand fits that criterion, and false if it doesn’t.
The structure you will use should look something like the following:

public struct card 
{    
    public char suit; // 'C', 'D', 'H', 'S' - for clubs, diamonds, hearts, and spades    
    public int value; //2-14 – for 2-10,Jack, Queen, King, Ace 
};   

struct card hand[5];

External txt files have been assigned for us to use that appear like this :

C 13 H 13 D 13 C 10 H 10

I have managed to read the text files into my program, but I am unsure as what to do with this. The array of structures would be like:

(C 13)(H 13)(D 13)(C 10)(H 10)

Not quite sure how to transfer the line of text from the file into this type of format, any help is appreciated, cheers :)


Solution

  • Try this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace ConsoleApplication45
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<Card> cards = new List<Card>();
                string input = "C 13 H 13 D 13 C 10 H 10";
                string[] inputArray = input.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
    
                for(int i = 0; i < 10; i += 2)
                {
                    Card newCard = new Card();
                    newCard.suit = inputArray[i][0];
                    newCard.value = int.Parse(inputArray[i + 1]);
                    cards.Add(newCard);
                }
    
                foreach (Card card in cards)
                {
                    Console.WriteLine("Suit {0}, Rank {1}", card.suit, card.value.ToString());
                }
                Console.ReadLine();
    
    
            }
            public struct Card
            {
                public char suit; // 'C', 'D', 'H', 'S' - for clubs, diamonds, hearts, and spades    
                public int value; //2-14 – for 2-10,Jack, Queen, King, Ace };
            }
        }
    
    }