|
All dear friends of this forum i'm asking you help.
I have to create simple poker game in console application.
I created already but this via functions. I need to create with structs this game. Plz help me. I can't succeed almost 2 weeks....
I understand that is hard question but this code is ready and it works
just how to create this in structs and not in functions
And how can i upload file to this forum.
arr[j + 1])<br />
{<br />
temp = arr[j];<br />
arr[j] = arr[j + 1];<br />
arr[j + 1] = temp;<br />
} <br />
}<br />
<br />
static void InitializeHand(int[] hand)<br />
{<br />
Random rnd = new Random();<br />
hand[0] = rnd.Next(0, 52);<br />
for (int i = 1; i < hand.Length; i++)<br />
{<br />
hand[i] = rnd.Next(0, 52);<br />
for (int j = i - 1; j >= 0; j--)<br />
if (hand[i] == hand[j])<br />
{<br />
i--;<br />
break;<br />
}<br />
} <br />
} <br />
<br />
static void PrintHand(int[] hand)<br />
{<br />
char sign = ' ';<br />
string card = "";<br />
int val;<br />
Console.WriteLine("Your hand:"); <br />
<br />
for (int i = 0; i < hand.Length; i++)<br />
{<br />
val = hand[i] / 13;<br />
switch (val)<br />
{<br />
case 0:<br />
sign = '♠';<br />
break;<br />
case 1:<br />
sign = '♣';<br />
break;<br />
case 2:<br />
sign = '♥';<br />
break;<br />
case 3:<br />
sign = '♦';<br />
break;<br />
}<br />
<br />
val = hand[i] % 13;<br />
switch (val)<br />
{<br />
case 0:<br />
card = "A";<br />
break;<br />
case 1:<br />
case 2:<br />
case 3:<br />
case 4:<br />
case 5:<br />
case 6:<br />
case 7:<br />
case 8:<br />
case 9:<br />
card = (val + 1).ToString();<br />
break;<br />
case 10:<br />
card = "J";<br />
break;<br />
case 11:<br />
card = "Q";<br />
break;<br />
case 12:<br />
card = "K";<br />
break;<br />
}<br />
<br />
Console.WriteLine("{0}.{1,3} {2}", i + 1, card, sign);<br />
}<br />
}<br />
<br />
static int[] GetPics(int[] hand)<br />
{<br />
int[] arr = new int[hand.Length];<br />
for (int i = 0; i < arr.Length; i++)<br />
arr[i] = hand[i] % 13;<br />
return arr;<br />
}<br />
<br />
static bool Flush(int[] hand)<br />
{<br />
if (hand[0] / 13 == hand[1]/13 && hand[1]/13 == hand[2] / 13 && hand[2] / 13 == hand[3] / 13 && hand [3] / 13 == hand[4] / 13)<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
static bool Straight(int[] hand)<br />
{<br />
int[] pic = GetPics(hand);<br />
<br />
BubbleSort(pic); <br />
<br />
if (((pic[0] + 1 == pic[1]) || (pic[0] + 12 == pic[4])) && pic[1] + 1 == pic[2] && pic[2] + 1 == pic[3] && pic[3] + 1 == pic[4])<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
static bool FourOfAKind(int[] hand)<br />
{<br />
int[] pic = GetPics(hand);<br />
<br />
BubbleSort(pic);<br />
<br />
if (((pic[0] == pic[1]) || (pic[3] == pic[4])) && pic[1] == pic[2] && pic[2] == pic[3])<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
static bool FullHouse(int[] hand)<br />
{<br />
int[] pic = GetPics(hand);<br />
<br />
BubbleSort(pic);<br />
<br />
if ((pic[0] == pic[1] && pic[1] == pic[2] && pic[3] == pic[4]) || (pic[0] == pic[1] && pic[2] == pic[3] && pic[3] == pic[4]))<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
static bool ThreeOfAKind(int[] hand)<br />
{<br />
int[] pic = GetPics(hand);<br />
<br />
BubbleSort(pic);<br />
<br />
if ((pic[0] == pic[1] && pic[1] == pic[2]) || (pic[1] == pic[2] && pic[2] == pic[3]) || (pic[2] == pic[3] && pic[3] == pic[4]))<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
static bool TwoPairs(int[] hand)<br />
{<br />
int[] pic = GetPics(hand);<br />
<br />
BubbleSort(pic);<br />
<br />
if ((pic[0] == pic[1] && pic[2] == pic[3]) || (pic[1] == pic[2] && pic[3] == pic[4]) || (pic[0] == pic[1] && pic[3] == pic[4]))<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
static bool Pair(int[] hand)<br />
{<br />
int[] pic = GetPics(hand);<br />
<br />
BubbleSort(pic);<br />
<br />
if ((pic[0] == pic[1] || pic[1] == pic[2]) || (pic[2] == pic[3] || pic[3] == pic[4]))<br />
return true;<br />
else<br />
return false;<br />
}<br />
<br />
static void CardReplace(int[] hand)<br />
{<br />
string s;<br />
do<br />
{<br />
Console.Write("How many cards would you like to replace (0-3): ");<br />
s = Console.ReadLine();<br />
} while (s != "0" && s != "1" && s != "2" && s != "3");<br />
<br />
int[] idxCardToReplace = new int[int.Parse(s)];<br />
for (int i = 0; i < idxCardToReplace.Length; i++)<br />
{<br />
do<br />
{<br />
Console.Write("Replace card ({0}/{1}): ", i + 1, idxCardToReplace.Length);<br />
s = Console.ReadLine();<br />
} while (s != "1" && s != "2" && s != "3" && s != "4" && s != "5");<br />
idxCardToReplace[i] = int.Parse(s);<br />
for (int j = i - 1; j >= 0; j--)<br />
if (idxCardToReplace[i] == idxCardToReplace[j])<br />
{<br />
Console.WriteLine("This card was chosen already");<br />
i--;<br />
break;<br />
}<br />
} <br />
<br />
int[] cardToReplace = new int[int.Parse(s)];<br />
Random rnd = new Random(); <br />
for (int i = 0; i < cardToReplace.Length; i++)<br />
{<br />
cardToReplace[i] = rnd.Next(0, 52);<br />
for (int j = 0; j < hand.Length; j++)<br />
if (cardToReplace[i] == hand[j])<br />
{<br />
i--;<br />
break;<br />
}<br />
for (int j = 0; j < i; j++)<br />
if (cardToReplace[i] == cardToReplace[j])<br />
{<br />
i--;<br />
break;<br />
}<br />
}<br />
<br />
for (int i = 0; i < idxCardToReplace.Length; i++) <br />
hand[idxCardToReplace[i] - 1] = cardToReplace[i];<br />
}<br />
<br />
static int PrintHandRank(int[] hand)<br />
{ <br />
int[] tmpHand = new int[hand.Length];<br />
for (int i = 0; i < hand.Length; i++)<br />
tmpHand[i] = hand[i];<br />
BubbleSort(tmpHand);<br />
<br />
Console.Write("Rank: ");<br />
if (Flush(tmpHand) && Straight(tmpHand))<br />
{<br />
Console.WriteLine("Straight flush");<br />
return 10;<br />
}<br />
else if (FourOfAKind(tmpHand))<br />
{<br />
Console.WriteLine("Four of a kind");<br />
return 7;<br />
}<br />
else if (FullHouse(tmpHand))<br />
{<br />
Console.WriteLine("Full house");<br />
return 6;<br />
}<br />
else if (Flush(tmpHand))<br />
{<br />
Console.WriteLine("Flush");<br />
return 5;<br />
}<br />
else if (Straight(tmpHand))<br />
{<br />
Console.WriteLine("Straight");<br />
return 4;<br />
}<br />
else if (ThreeOfAKind(tmpHand))<br />
{<br />
Console.WriteLine("Three of a kind");<br />
return 3;<br />
}<br />
else if (TwoPairs(tmpHand))<br />
{<br />
Console.WriteLine("Two Pairs");<br />
return 2;<br />
}<br />
else if (Pair(tmpHand))<br />
{<br />
Console.WriteLine("Pair");<br />
return 1;<br />
}<br />
else <br />
Console.WriteLine("Highest Card");<br />
return 0;<br />
}<br />
<br />
static int CalcPoints(int bet, int mult, int total)<br />
{<br />
switch (mult)<br />
{<br />
case 0:<br />
Console.WriteLine("You lost {0} points.", bet);<br />
break;<br />
case 1:<br />
Console.WriteLine("No win no lose.");<br />
break;<br />
default:<br />
Console.WriteLine("You won {0} points.", mult * bet);<br />
break;<br />
}<br />
total += bet * mult;<br />
Console.WriteLine("Total points: {0}", total);<br />
return total;<br />
}<br />
<br />
static int GetBet(int total)<br />
{<br />
int bet;<br />
Console.WriteLine("Total points: {0}", total);<br />
do<br />
{<br />
Console.Write("Enter bet (1-{0}): ", total);<br />
bet = int.Parse(Console.ReadLine());<br />
} while (bet < 1 || bet > total);<br />
return bet;<br />
}<br />
<br />
static void PrintMenu()<br />
{<br />
Console.WriteLine("Poker Project:");<br />
Console.WriteLine("-------------------");<br />
Console.WriteLine("(1) Play");<br />
Console.WriteLine("(2) Exit");<br />
Console.Write("Enter your choice: ");<br />
}<br />
<br />
static void HandleUser()<br />
{<br />
int[] hand = new int[5];<br />
<br />
string choice;<br />
int total = 100;<br />
int bet;<br />
int mult;<br />
<br />
do<br />
{<br />
Console.Clear();<br />
PrintMenu();<br />
choice = Console.ReadLine();<br />
Console.WriteLine();<br />
<br />
switch (choice)<br />
{<br />
case "1": <br />
bet = GetBet(total);<br />
total -= bet; <br />
Console.Clear();<br />
InitializeHand(hand);<br />
PrintHand(hand); <br />
mult = PrintHandRank(hand);<br />
CardReplace(hand);<br />
PrintHand(hand); <br />
mult = PrintHandRank(hand);<br />
total = CalcPoints(bet, mult, total);<br />
if (total == 0)<br />
{<br />
Console.WriteLine("Game Over.");<br />
goto case "2";<br />
}<br />
break;<br />
<br />
case "2":<br />
Console.WriteLine("Thank you for playing poker.");<br />
choice = "2";<br />
break; <br />
<br />
default:<br />
Console.WriteLine("Choice {0} is invalid, please try again..", choice);<br />
break;<br />
}<br />
Console.Write("Press Enter to continue..");<br />
Console.ReadLine();<br />
} while (choice != "2");<br />
<br />
}<br />
<br />
static void Main(string[] args)<br />
{<br />
HandleUser();<br />
}<br />
}<br />
}
|
|
|
|
|
When you posted this and you got the warning that your message was very long and you should consider cutting it down, did you actually pay any attention?
I'd love to help, but I'm not going to read through all that. If you have a specific question then we'd love to help.
One quick comment. Your first method is a bubble sort. What was wrong with the built in sorting mechanisms in .NET?
|
|
|
|
|
Colin Angus Mackay wrote: I'd love to help, but I'm not going to read through all that. If you have a specific question then we'd love to help.
I've started helping out here on somewhat of a regular basis. I've noticed that posters rarely surround their code with pre tags. Instead, they use the code tag or no tag at all. This makes it hard to read their code.
|
|
|
|
|
Leslie Sanford wrote: I've noticed that posters rarely surround their code with pre tags. Instead, they use the code tag or no tag at all.
The problem, and it's something you should take up with Chris if you want anything done is that the pre tag doesn't appear to be intended for code snippets. I'm not sure what to rename it as to make what it and the code tag are intended to do more clear, otherwise I'd've posted to the suggestions board a while ago myself.
--
CleaKO The sad part about this instance is that none of the users ever said anything [about the problem].
Pete O`Hanlon Doesn't that just tell you everything you need to know about users?
|
|
|
|
|
Yes Colin i saw this warning about long code and i appoligize for that.
May be you are right about bubble sort and it's not be included in code.
My question how to rebuilt this code in "structs". I began programm 4 months
ago and i don't have a lot of skills. But i have to rebuilt this to struct.
i want to do 3 or 4 structs : " Card", "CardDeck", "Hand" and may be "Game".
Card and CardDeck i think allright, but Hand that initializes 5 cards prints
and replaces old card to new not working, i don't know what to do.
May be somebody know source code or links to this.
Tnanks...
|
|
|
|
|
You could have a look at my article [^]about scoring cribbage hands.
Cribbage is a fundamentally different card game from poker, but I show you how to find pairs, flushes and straights...
Share and enjoy.
Sean
|
|
|
|
|
I did it already and i know how to find pairs but i've got confused
how to rebuilt it structs my brother
|
|
|
|
|
Hi,
From database i m getting value like 1,2,3,4,5,6,7,8,9,10,11 upto 9999.
these value i m displaying in combobox
but i want to display like this
if it's 1 to 9 then i have to display 0001.
if it's 10 to 99 then i have to display 0010 upto 0099.
if it's 100 to 999 then i have to display 0100 upto 0999.
if it's 1000 to 9999 then i have to display 1000 upto 9999.
help me it's too urgent.
thanks
asif
|
|
|
|
|
Well I haven't worked with comboboxes but I believe they have two properties, datatextfield and datavaluefield. If so you can do this
combobox.datatextfield = datatable["field"].ToString().PadLeft(4,'0');
-----
|
|
|
|
|
comboBox1.Text = comboBox1.Text.ToString().PadLeft(4, '0');
|
|
|
|
|
hi,
i am using Datagrid view in my project..
and i have got 3 columns..
but in one of the 3 columns i want formated data in one of the column like..
000.000.000.000
user should enetr the number only..
how can do it..
pls help me
With Regards
prasad
|
|
|
|
|
Gents,
I have a combobox that contains operations (Range, >, < and =). The user should select needed opertion to process some text fields. How can I get the operation selected to put in the if statment. For example, here is a '>' is selected:
if (DoubleA > DoubleB)
I want to do it in efficient way because i have many comparisons based on user selection.
Thanks for your assistance in advance.
|
|
|
|
|
A little OO thinking helps.
abstract class FilterBase
{
public abstract bool PerformOperator(int a, int b);
}
public class EqualsFilter : FilterBase
{
public bool PerformOperator(int a, int b);
{
return (a == b);
}
}
The above is an example of the strategy pattern.
Each item in the combo box is assosiated with a specific instance of the Filter class (or rather one of its derived varients). Because the FilterBase class cotains the methods needed, that is all you ever need to reference. Once set up the rest of the code does not need to know anything about each of the specialised classes.
|
|
|
|
|
Thank you thats was really helpfull to solve my issue
|
|
|
|
|
hi
i want to embedding any exe file(s) to resource at runTime (by code) and extract it to drive and run it, but how to do ?
thanks
|
|
|
|
|
You cannot. Resources are built at compile time.
|
|
|
|
|
Im looking for the board game ludo in c#. Or another board game like it with source files. If somebody knows it let me know..Thx
C#C#C#C#C#C#C#C#C#C#C#C#
|
|
|
|
|
I doubt there is one but you are welcome to build your own and ask here if you have any questions
|
|
|
|
|
www.techipost.com/opensource-ludo-game-c-csharp/
|
|
|
|
|
|
for: break
do: break
while: break
if: no keyword to exit
read a book on C# !
|
|
|
|
|
i guess there is a return;
for example,
if(x<3)
return;
try it
|
|
|
|
|
break to exit from for.
There is nothing to break out of if since it is not a loop but a conditional directive.
|
|
|
|
|
Hi,
What is the best way to plot graphs in C# ?
Can crystal reports in VS 2005 be used for the same ?
Thanks in Advance
|
|
|
|
|
hi all,
i'm using hash table which has one key and one value, but i need to get two keys and one values now,
is there is any collection available for this, suggest me
thnks in advance
prashanth,
s/w Engineer,
Syfnosys.
|
|
|
|