I'm trying to create a poker dice game. There are comp player and user player. Each player will roll dice 5 times. Basically, it likes create 2 random array, with 5 elements in each array. I'm trying to compare the outcome of these 2 arrays. For example; 2 pairs beat 1 pair/ 3 of a kind beat 2 pair/Fullhouse beat 3 of a kind and 2 pair...
I'm stuck at the comparing part. Don't know how to do it in the shortest way.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiceRoller
{
class Program
{
//Declare and initilize variables
static int[] userDiceCount = { 1, 1, 1, 1, 1, 0 };
static int[] userDice = { 1, 2, 3, 4, 5 };
static int[] compDiceCount = { 1, 1, 1, 1, 1, 0 };
static int[] compDice = { 1, 2, 3, 4, 5 };
//main Method runs first and contains main loop
static void Main(string[] args)
{
Console.WriteLine("Let's play some poker dice game"); //Output to inform user
userRollDice();
System.Threading.Thread.Sleep(20);
Console.WriteLine();
compRollDice();
}
//Rolls the vitual dice
static void userRollDice()
{
Random rand = new Random();//Declare and initilize random number
Array.Clear(userDiceCount, 0, 6);//Clear dice count array
Console.WriteLine("You rolled:");//Inform user what they rolled
for (int i = 0; i < userDice.Length; i++)
{
userDice[i] = rand.Next(6) + 1;//Gets a random number from 1 to 6
Console.Write("{0} ", userDice[i]); //output each dice
userDiceCount[userDice[i] - 1]++; //Increment that dice's number counter
}
Console.WriteLine();
Console.WriteLine("You have:");//Inform user
Console.WriteLine(checkHandUser());//Output results of checkHand Function
}
static void compRollDice()
{
Random rand = new Random();//Declare and initilize random number
Array.Clear(compDiceCount, 0, 6);//Clear dice count array
Console.WriteLine("Computer rolled:");//Inform user what they rolled
for (int i = 0; i < compDice.Length; i++)
{
compDice[i] = rand.Next(6) + 1;//Gets a random number from 1 to 6
Console.Write("{0} ", compDice[i]); //output each dice
compDiceCount[compDice[i] - 1]++; //Increment that dice's number counter
}
Console.WriteLine();
Console.WriteLine("Computer have:");//Inform user
Console.WriteLine(checkHandComp());//Output results of checkHand Function
}
//Determines the poker hand based on the dice the user has
static String checkHandUser()
{
//Declare and initialize local variables
String result = "Nothing";
int numberOfPairs = 0;
Boolean threeOfKind = false;
int highestCount = 0;
//Loop thorugh the counted dice list
for (int i = 0; i < userDiceCount.Length; i++)
{
if (userDiceCount[i] == 2) //If there are 2 of a dice number
{
numberOfPairs++; //Increment the amount of pairs
if (numberOfPairs >= 1 && numberOfPairs <= 2)
{//If one pair or two pairs
result = numberOfPairs + " Pair(s)"; //output the number of pairs
}
}
else if (userDiceCount[i] >= 3) //If 3 or more of a dice number
{
result = userDiceCount[i] + " of a kind"; //Output the number of a kind
if (userDiceCount[i] == 3) //If its a 3 of a kind,
{
threeOfKind = true;//set a flag to true, used to check for full house
}
}
//Check for highest number of a single dice number, used to check for straight
if (userDiceCount[i] > highestCount)
{
highestCount = userDiceCount[i];
}
}
if (threeOfKind && numberOfPairs >= 1)//if a three of a kind and a pair is found,
{
result = "Full House"; //it must be a full house
}
//if the highest amount of a dice number is 1, and there are no ones or no sixs,
if (highestCount == 1 && (userDiceCount[0] == 0 || userDiceCount[5] == 0))
{
result = "Straight"; //It must be a straight
}
return result;
}//Return the determined hand
static String checkHandComp()
{
//Declare and initialize local variables
String result = "Nothing";
int numberOfPairs = 0;
Boolean threeOfKind = false;
int highestCount = 0;
//Loop thorugh the counted dice list
for (int i = 0; i < compDiceCount.Length; i++)
{
if (compDiceCount[i] == 2) //If there are 2 of a dice number
{
numberOfPairs++; //Increment the amount of pairs
if (numberOfPairs >= 1 && numberOfPairs <= 2)
{//If one pair or two pairs
result = numberOfPairs + " Pair(s)"; //output the number of pairs
}
}
else if (compDiceCount[i] >= 3) //If 3 or more of a dice number
{
result = compDiceCount[i] + " of a kind"; //Output the number of a kind
if (compDiceCount[i] == 3) //If its a 3 of a kind,
{
threeOfKind = true;//set a flag to true, used to check for full house
}
}
//Check for highest number of a single dice number, used to check for straight
if (compDiceCount[i] > highestCount)
{
highestCount = compDiceCount[i];
}
}
if (threeOfKind && numberOfPairs >= 1)//if a three of a kind and a pair is found,
{
result = "Full House"; //it must be a full house
}
//if the highest amount of a dice number is 1, and there are no ones or no sixs,
if (highestCount == 1 && (compDiceCount[0] == 0 || compDiceCount[5] == 0))
{
result = "Straight"; //It must be a straight
}
return result;
}//Return the determined hand
}
}
Why don't you:
1) make something that is a straight, plain list of the rankings of the outcomes (like an enum, a list or another array).
2) make a function that evaluates a single hand that gives you the value or name of its particular outcome.
3) compare player1's outcome to player2's outcome.
The critical piece #1 is creating something that tells you which outcome is greater than another outcome.
Also, in your evaluation (returning the outcome), you would need to start with the highest rank and work down.
Otherwise 4-of-a-kind could accidentally look like a pair (and so on).
Thanks to you thines01. I tried your suggestion and it worked. But my version is really long. I will post my solution as soon as I got home today. I'm so curious about your version. If you can, please post it up :)
Well, I'll post my Main() and you can determine if you want to see the rest of the project.
I also made a batch file to run it 100 times so I could see all of the outcomes (usually).
I found some really interesting ways of doing this -- most of which would be considered very complex, but they are to-the-point.
To do this justice, you would need to see the entire project (because of the way I separate functions and modules).
The actual Dice class is a separate DLL I wrote for another DaniWeb question a while back, but this Dice Poker Game uses it.
using System;
using System.Collections.Generic;
using System.Linq;
namespace DW_421870_CS_CON
{
using DICE_HAND = IEnumerable<int>;
using KVP_E2S = KeyValuePair<DicePoker.E_DICE_POKER_HAND_VAL, string>;
using KVP_PRD2FN = KeyValuePair<Predicate<int>, Func<List<KeyValuePair<DicePoker.E_DICE_POKER_HAND_VAL, string>>, string>>;
public partial class DicePoker
{
public enum E_DICE_POKER_HAND_VAL
{
HIGH_ROLL, ONE_PAIR, TWO_PAIR, THREE_OF_A_KIND,
FULL_HOUSE, FOUR_OF_A_KIND, STRAIGHT_FLUSH, FIVE_CARD_CHARLIE
};
//////////////////////////////////////////////////////////////////////////
// Since the rolling is simulated anyway, let the computer roll for both.
private static DICE_HAND dhPlayerOne = Roll(1);
private static DICE_HAND dhPlayerTwo = Roll(2);
//////////////////////////////////////////////////////////////////////////
// Magical construction to determine the winner of a given hand Key/Value.
private static KVP_PRD2FN[] lst_kvpPrd2fn =
{
new KVP_PRD2FN(new Predicate<int>(i => i.Equals(0)),
PlayerTie),//first tie
new KVP_PRD2FN(new Predicate<int>(i => i > 0),
(m => string.Format("Player One wins\n1={0}({1})\n2={2}({3})",
m[0].Key, m[0].Value, m[1].Key, m[1].Value))),
new KVP_PRD2FN(new Predicate<int>(i => i < 0),
(m => string.Format("Player Two wins\n2={2}({3})\n1={0}({1})",
m[0].Key, m[0].Value, m[1].Key, m[1].Value))),
new KVP_PRD2FN(new Predicate<int>(i => i.Equals(0)),
(m => string.Format("Tie({0}) \n1={1}\n2={2}",
m[0].Key, m[0].Value, m[1].Value))),
};
static void Main(string[] args)
{
List<KVP_E2S> lstDiceRollResults = new List<KVP_E2S>
{
new KVP_E2S(GetHandEval(dhPlayerOne), RenderPokerOrder(dhPlayerOne)),
new KVP_E2S(GetHandEval(dhPlayerTwo), RenderPokerOrder(dhPlayerTwo))
};
//
Console.WriteLine(lst_kvpPrd2fn.Take(3)
.Where(x => x.Key.Invoke(
lstDiceRollResults[0].Key.CompareTo(lstDiceRollResults[1].Key))
.Equals(true))
.Select(s => s.Value).FirstOrDefault().Invoke(lstDiceRollResults));
}
}
}
LOL
It becomes more readable if you spread it out beyond 80 columns.
:)
The Dice.DLL inside the zip might need to be moved, but you'll get the gist.
Here is my version. SOrry thines01 for the late reply :)
namespace DiceRoller
{
class Program
{
//Declare and initilize variables
static int[] userDiceCount = { 1, 1, 1, 1, 1, 0 };
static int[] userDice = { 1, 2, 3, 4, 5 };
static int[] compDiceCount = { 1, 1, 1, 1, 1, 0 };
static int[] compDice = { 1, 2, 3, 4, 5 };
static Boolean playing = true;
//main Method runs first and contains main loop
static void Main(string[] args)
{
int count = 0;
int match = 0;
int points = 0;
int x = 0;
const int SIZE = 6;
const int DICE = 5;
int compHandValue = 0;
int userHandValue = 0;
int[] compCountSum = new int[SIZE];
int[] userCountSum = new int[SIZE];
Console.WriteLine("Let's play some poker dice game");
Console.WriteLine();
compRollDice();
System.Threading.Thread.Sleep(20);
Console.WriteLine();
userRollDice();
CalcCountSum(x, count, SIZE, DICE, compDice, compCountSum);
CalcCountSum(x, count, SIZE, DICE, userDice, userCountSum);
compHandValue = CalcHandValue(x, match, points, SIZE, DICE, compCountSum, compHandValue);
userHandValue = CalcHandValue(x, match, points, SIZE, DICE, userCountSum, userHandValue);
DecideWinner(userHandValue, compHandValue);
}
//Rolls the vitual dice
static void compRollDice()
{
Random rand = new Random();//Declare and initilize random number
Array.Clear(compDiceCount, 0, 6);//Clear dice count array
Console.WriteLine("Computer rolled:");//Inform user what they rolled
for (int i = 0; i < compDice.Length; i++)
{
compDice[i] = rand.Next(6) + 1;//Gets a random number from 1 to 6
Console.Write("{0} ", compDice[i]); //output each dice
compDiceCount[compDice[i] - 1]++; //Increment that dice's number counter
}
Console.WriteLine();
Console.WriteLine("Computer have:");//Inform user
Console.WriteLine(checkHandComp());//Output results of checkHand Function
}
//Determines the poker hand based on the dice the user has
static void userRollDice()
{
Random rand = new Random();//Declare and initilize random number
Array.Clear(userDiceCount, 0, 6);//Clear dice count array
Console.WriteLine("You rolled:");//Inform user what they rolled
for (int i = 0; i < userDice.Length; i++)
{
userDice[i] = rand.Next(6) + 1;//Gets a random number from 1 to 6
Console.Write("{0} ", userDice[i]); //output each dice
userDiceCount[userDice[i] - 1]++; //Increment that dice's number counter
}
Console.WriteLine();
Console.WriteLine("You have:");//Inform user
Console.WriteLine(checkHandUser());//Output results of checkHand Function
}
static String checkHandUser()
{
//Declare and initialize local variables
String result = "Nothing";
int numberOfPairs = 0;
Boolean threeOfKind = false;
int highestCount = 0;
//Loop thorugh the counted dice list
for (int i = 0; i < userDiceCount.Length; i++)
{
if (userDiceCount[i] == 2) //If there are 2 of a dice number
{
numberOfPairs++; //Increment the amount of pairs
if (numberOfPairs >= 1 && numberOfPairs <= 2)
{//If one pair or two pairs
result = numberOfPairs + " Pair(s)"; //output the number of pairs
}
}
else if (userDiceCount[i] >= 3) //If 3 or more of a dice number
{
result = userDiceCount[i] + " of a kind"; //Output the number of a kind
if (userDiceCount[i] == 3) //If its a 3 of a kind,
{
threeOfKind = true;//set a flag to true, used to check for full house
}
}
//Check for highest number of a single dice number, used to check for straight
if (userDiceCount[i] > highestCount)
{
highestCount = userDiceCount[i];
}
}
if (threeOfKind && numberOfPairs >= 1)//if a three of a kind and a pair is found,
{
result = "Full House"; //it must be a full house
}
//if the highest amount of a dice number is 1, and there are no ones or no sixs,
if (highestCount == 1 && (userDiceCount[0] == 0 || userDiceCount[5] == 0))
{
result = "Straight"; //It must be a straight
}
return result;
}//Return the determined hand
static String checkHandComp()
{
//Declare and initialize local variables
String result = "Nothing";
int numberOfPairs = 0;
Boolean threeOfKind = false;
int highestCount = 0;
//Loop thorugh the counted dice list
for (int i = 0; i < compDiceCount.Length; i++)
{
if (compDiceCount[i] == 2) //If there are 2 of a dice number
{
numberOfPairs++; //Increment the amount of pairs
if (numberOfPairs >= 1 && numberOfPairs <= 2)
{//If one pair or two pairs
result = numberOfPairs + " Pair(s)"; //output the number of pairs
}
}
else if (compDiceCount[i] >= 3) //If 3 or more of a dice number
{
result = compDiceCount[i] + " of a kind"; //Output the number of a kind
if (compDiceCount[i] == 3) //If its a 3 of a kind,
{
threeOfKind = true;//set a flag to true, used to check for full house
}
}
//Check for highest number of a single dice number, used to check for straight
if (compDiceCount[i] > highestCount)
{
highestCount = compDiceCount[i];
}
}
if (threeOfKind && numberOfPairs >= 1)//if a three of a kind and a pair is found,
{
result = "Full House"; //it must be a full house
}
//if the highest amount of a dice number is 1, and there are no ones or no sixs,
if (highestCount == 1 && (compDiceCount[0] == 0 || compDiceCount[5] == 0))
{
result = "Straight"; //It must be a straight
}
return result;
}//Return the determined hand
static void CalcCountSum(int x, int count, int SIZE,
int DICE, int[] diceRoll, int[] countSum)
{
count = 1;
while (count <= SIZE)
{
for (x = 0; x < DICE; x++)
{
if (diceRoll[x] == count)
{
countSum[count - 1] = countSum[count - 1] + 1;
}
}
count++;
}
}
static int CalcHandValue(int x, int match, int points, int SIZE,
int DICE, int[] countSum, int handValue)
{
handValue = 0;
match = 2;
while (match <= DICE)
{
for (x = 0; x < SIZE; x++)
{
points++;
if (countSum[x] == match)
{
handValue = points;
}
}
match++;
}
return handValue;
}
static void DecideWinner(int userHandValue, int compHandValue)
{
if (userHandValue > compHandValue)
{
Console.WriteLine("\n\n{0}", "You win!");
}
if (compHandValue > userHandValue)
{
Console.WriteLine("\n\n{0}", "Computer wins.");
}
if (compHandValue == userHandValue)
{
Console.WriteLine("\n\n{0}", "Game is a tie.");
}
}
}
}
Pretty neat.
What are you going to do about this case?:
Let's play some poker dice game
Computer rolled:
5 4 1 6 6
Computer have:
1 Pair(s)You rolled:
6 4 5 4 6
You have:
2 Pair(s)Game is a tie.
...where I have two-pair, but the program says it ties with one-pair.
...and here is my final updated version (in case you wanted to see it) All if-statements and for-loops removed.