Click here to Skip to main content
15,888,803 members
Home / Discussions / C#
   

C#

 
AnswerRe: Better to save locally? Pin
Colin Angus Mackay9-Apr-07 3:40
Colin Angus Mackay9-Apr-07 3:40 
GeneralRe: Better to save locally? Pin
Aaron VanWieren9-Apr-07 5:47
Aaron VanWieren9-Apr-07 5:47 
GeneralRe: Better to save locally? Pin
Colin Angus Mackay9-Apr-07 6:22
Colin Angus Mackay9-Apr-07 6:22 
GeneralRe: Better to save locally? Pin
Aaron VanWieren9-Apr-07 6:33
Aaron VanWieren9-Apr-07 6:33 
QuestionHow to invoke web service method in C#.Net Pin
shanthivasan9-Apr-07 2:25
shanthivasan9-Apr-07 2:25 
AnswerRe: How to invoke web service method in C#.Net Pin
WillemM9-Apr-07 7:01
WillemM9-Apr-07 7:01 
QuestionReport Viewer Pin
vinayak baddi9-Apr-07 2:07
vinayak baddi9-Apr-07 2:07 
QuestionHow to create Poker Game!!! Pin
DukeKing9-Apr-07 1:41
DukeKing9-Apr-07 1:41 
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 />
        //initialize hand with unique cards<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 />
        //The function gets hand and retrieves only the pictures of the hand <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 />
        //fuction asks player how many cards to replace and replaces with unique cards<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 />
            //random draw of the number of cards that player wants to replace and validate uniqueness<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 />
            //replace the cards with the new random cards<br />
            for (int i = 0; i < idxCardToReplace.Length; i++)            <br />
                hand[idxCardToReplace[i] - 1] = cardToReplace[i];<br />
        }<br />
<br />
        //fuction prints hand rank for player and returns to point multiplier of the hand<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 />
        //function calculates and return the total points after game and prints win/lose message<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 />
        //function prints total points, get bet and returns it for calculation<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 />
}

AnswerRe: How to create Poker Game!!! Pin
Colin Angus Mackay9-Apr-07 1:50
Colin Angus Mackay9-Apr-07 1:50 
GeneralRe: How to create Poker Game!!! Pin
Leslie Sanford9-Apr-07 5:35
Leslie Sanford9-Apr-07 5:35 
GeneralRe: How to create Poker Game!!! Pin
Dan Neely9-Apr-07 5:48
Dan Neely9-Apr-07 5:48 
GeneralRe: How to create Poker Game!!! Pin
DukeKing9-Apr-07 5:39
DukeKing9-Apr-07 5:39 
AnswerRe: How to create Poker Game!!! Pin
Sean Michael Murphy9-Apr-07 2:39
Sean Michael Murphy9-Apr-07 2:39 
GeneralRe: How to create Poker Game!!! Pin
DukeKing9-Apr-07 5:44
DukeKing9-Apr-07 5:44 
QuestionHow to display customize value in ComboBox Pin
indiaone9-Apr-07 1:32
indiaone9-Apr-07 1:32 
AnswerRe: How to display customize value in ComboBox Pin
SABhatti9-Apr-07 3:29
SABhatti9-Apr-07 3:29 
AnswerRe: How to display customize value in ComboBox Pin
PS@Codeproj9-Apr-07 19:35
PS@Codeproj9-Apr-07 19:35 
QuestionDataGridView Pin
719-Apr-07 1:11
719-Apr-07 1:11 
QuestionSelect Operation in ComboBox Pin
AL3600B9-Apr-07 0:20
AL3600B9-Apr-07 0:20 
AnswerRe: Select Operation in ComboBox Pin
Colin Angus Mackay9-Apr-07 1:11
Colin Angus Mackay9-Apr-07 1:11 
GeneralRe: Select Operation in ComboBox Pin
AL3600B10-Apr-07 18:38
AL3600B10-Apr-07 18:38 
Questionhow to embedding any fileType (also exe) to resources by code (at runTime) ? Pin
hdv2129-Apr-07 0:17
hdv2129-Apr-07 0:17 
AnswerRe: how to embedding any fileType (also exe) to resources by code (at runTime) ? Pin
Colin Angus Mackay9-Apr-07 0:19
Colin Angus Mackay9-Apr-07 0:19 
QuestionLudo board game in c# Pin
khalidelmeknesi8-Apr-07 23:39
khalidelmeknesi8-Apr-07 23:39 
AnswerRe: Ludo board game in c# Pin
netJP12L9-Apr-07 4:31
netJP12L9-Apr-07 4:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.