i need to write a program where from a user determined file i need to find which poker hand type is input into the program. i have where i can count the number of each suit in the hand but i cant think of way to find the numbers of the cards in a way it can have double digits. it would be preferable if it was in an array but am focusing on getting it into a string for now. im mostly wanting to count each card value so then i can have if statements of the criteria for each hand to find the inputed hand matches what the hand is supposed to be
the layout for the cards in each hand are like the following file title: Flush.txt file content: C 6 C 9 C 10 C 11 C 12
this is all the code i have for the program right now
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace pokertask
{
class Program
{
static void Main(string[] args)
{
string fileName = "";
string fileContent = "";
int clubSuit = 0;
int heartSuit = 0;
int spadeSuit = 0;
int diamondSuit = 0;
//int[] array = new int[5];
string cardNum = "";
//read text file
Console.WriteLine("type the file name of the hand that you are testing including its file extension.");
fileName = Console.ReadLine();
try
{//open and read selected file
StreamReader inFile = new StreamReader(fileName);
fileContent = inFile.ReadLine();
inFile.Close();
Console.WriteLine("{0}", fileContent);
Console.ReadLine();
}
catch
{//response if failed
Console.WriteLine("File Handling error has occured. Press any button to close the program.");
Console.ReadLine();
}
//display
foreach (char C in fileContent)
{
if ( C == 'C')
{
clubSuit ++;
}
}
foreach (char S in fileContent)
{
if (S == 'S')
{
spadeSuit++;
}
}
foreach (char H in fileContent)
{
if (H == 'H')
{
heartSuit++;
}
}
foreach (char D in fileContent)
{
if (D == 'D')
{
diamondSuit++;
}
}
Console.WriteLine(" number of clubs {0}", clubSuit);
Console.WriteLine(" number of spades {0}", spadeSuit);
Console.WriteLine(" number of hearts {0}", heartSuit);
Console.WriteLine(" number of diamonds {0}", diamondSuit);
Console.ReadLine();
cardNum = fileContent.Trim('C','H','D','S');
Console.WriteLine("{0}", cardNum);
Console.ReadLine();
}
}
}
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = "C 6 C 9 C 10 C 11 C 12";
string[] array = input.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < 10; i += 2)
{
string suit = array[i];
string rank = array[i + 1];
Console.WriteLine("Card {0} , Suit : {1}, Rank : {2}" , i/2, suit, rank);
}
Console.ReadLine();
}
}
}
As already suggested by others, it is much better to use a more flexible format like XML and JSON, but if you are stuck with this format:
You could split the input by spaces and read the data in groups of two:
var split = fileContent.Split(' ');
var cards = new List<KeyValuePair<string, int>>();
for (var i = 0; i < split.Length; i += 2)
{
var suit = split[i];
var digits = int.Parse(split[i + 1]);
cards.Add(new KeyValuePair<string, int>(suit, digits));
}
// TODO: read the input from cards list
The cards list defined up there is made up of KeyValuePairs where the Key is the suit (eg "C") and Value is the digit (eg 9)
First I would recommend using a container for your card suits. List<int>
will do, but you may need something more depending on your needs ( Dict
? class
?). I don't think this solution is ideal and there's no error checking, but it assumes the layout you show in your example file.
var clubs = new List<int>();
var hearts = new List<int>();
var diamonds = new List<int>();
var spades = new List<int>();
Then use a code similar to the snippet shown below to parse your suits.
var parts = fileContent.Split(' ');
for (int i = 0; i < parts.Length; i+=2)
{
if (parts[i] == "C")
{
clubs.Add(Int32.Parse(parts[i+1]));
}
}
Full program below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Poker
{
class Program
{
static void Main(string[] args)
{
var clubs = new List<int>();
var hearts = new List<int>();
var diamonds = new List<int>();
var spades = new List<int>();
var fileContent = "C 6 C 9 C 10 C 11 C 12";
var parts = fileContent.Split(' ');
for (int i = 0; i < parts.Length; i += 2)
{
switch (parts[i])
{
case "C": clubs.Add(Int32.Parse(parts[i + 1])); break;
case "H": hearts.Add(Int32.Parse(parts[i + 1])); break;
case "D": diamonds.Add(Int32.Parse(parts[i + 1])); break;
case "S": spades.Add(Int32.Parse(parts[i + 1])); break;
default:
break;
}
}
Console.WriteLine("Clubs: {0}", string.Join(" ", clubs));
Console.WriteLine("Hearts: {0}", string.Join(" ", hearts));
Console.WriteLine("Diamonds: {0}", string.Join(" ", diamonds));
Console.WriteLine("Spades: {0}", string.Join(" ", spades));
Console.ReadLine();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.