3 Replies - 2078 Views - Last Post: 27 April 2012 - 01:23 PM Rate Topic: -----

#1 erikdeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-April 12

Compare two results from a poker dice game

Posted 27 April 2012 - 08:25 AM

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 code the compare part.
  
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
    }
} 


Is This A Good Question/Topic? 0
  • +

Replies To: Compare two results from a poker dice game

#2 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1181
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: Compare two results from a poker dice game

Posted 27 April 2012 - 08:40 AM

You could store all of the possible outcomes in an array (in order), and then compare the comp's outcome and the user's outcome to that array. Whichever outcome ends up with the higher array index wins, if the indices are the same it's a tie, etc.

This post has been edited by h4nnib4l: 27 April 2012 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 erikdeboy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-April 12

Re: Compare two results from a poker dice game

Posted 27 April 2012 - 09:48 AM

Thanks h4nnib4l. Seem like my brain stop function right after 12 hrs of logic thinking. Can you please give me some example base on my code? I got the result of user dice and computer dice from the methods already. For example, I got user hand: is 2 pair and comp hand is fullhouse. My question is how can I compare these 2 values just came out from the methods?
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Compare two results from a poker dice game

Posted 27 April 2012 - 01:23 PM

You'd do something like this
enum HandValue {
    HighCard,
    OnePair,
    TwoPair,
    ThreeOfAKind,
    etc.
}

public HandValue EvaluateHand(int[] values) {
    HandValue result = HandValue.HighCard;

    Array.Sort(values);

    if ((values[0] == 1 && values[1] == 2 && values[2] == 3 && values[3] == 4 && values[4] == 5) ||
        (values[0] == 2 && values[1] == 3 && values[2] == 4 && values[3] == 5 && values[4] == 6)) {
        result = HandValue.Straight;
    } else if (values[1] == values[4] || values[0] == values[3]) {
        result = HandValue.FourOfAKind; // think about why this works :)/>
    } else if ( ... etc ...
    }

    return result;
}

// Now you just need to get the hand values and compare them
// this is not a full method, just part of one
    HandValue player = EvaluateHand(playerHand);
    HandValue comp = EvaluateHand(compHand);

    if (player > comp) {
        // player hand is better
    } else if (comp > player) {
        // comp hand is better
    } else {
        // they have the same hand type, but one may be better than the other, you'll
        // have to determine which (for example, player may have 12222 and comp 23333.
        // both are 4 of a kind, but comp is better than player. You could expand the enum
        // to indicate exactly which type of four-of-a-kind, full-house, etc., but i'd
        // do it a different way, personally :)/>
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1