Advertisement
Guest User

Neopravitelná chyba v kódu Pokeru

a guest
Mar 18th, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.69 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Runtime.Remoting.Metadata;
  9. using System.Runtime.Versioning;
  10. using System.Text;
  11. using System.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.Windows.Forms.VisualStyles;
  15. using Poker.Properties;
  16.  
  17. namespace Poker
  18. {
  19.     public partial class Room : Form
  20.     {
  21.         public Room()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private string[] players = new string[7] { "Dankula", "FoKy", "Fereal", "Reaper", "rokmen", "gogo", "nevim" };
  27.  
  28.  
  29.         //Všechny hodnoty karet
  30.         public int[] cards_values = new int[13] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
  31.         //Všechny barvy karet
  32.         public readonly int[] cards_colors = new int[4] { 1, 2, 3, 4 };
  33.         //public List<string> cards_colors = new List<string>() {    "s", "k", "t", "p" };
  34.  
  35.         public List<int[]> UsedCards = new List<int[]>();
  36.  
  37.         public float pravdepodobnost = 0;
  38.  
  39.         public List<int[]> Cards = new List<int[]>();
  40.  
  41.         private void Room_Load(object sender, EventArgs e)
  42.         {
  43.  
  44.             for (int i = 0; i < cards_values.Count(); i++)
  45.             {
  46.                 for (int j = 0; j < cards_colors.Count(); j++)
  47.                 {
  48.                     Cards.Add(new int[2] { cards_values[i], cards_colors[j] });
  49.                 }
  50.             }
  51.             timer1.Start();
  52.         }
  53.  
  54.         private string getCardVal(int number, int color)
  55.         {
  56.             List<string> colors = new List<string>() { "s", "k", "t", "p" };
  57.             List<string> values = new List<string>() { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
  58.             return values[number - 1] + colors[color - 1];
  59.         }
  60.  
  61.         private void raise_trackbar_Scroll(object sender, EventArgs e)
  62.         {
  63.             int value = raise_trackbar.Value;
  64.             trackbar_value.Text = "$" + Player.convertBalance(Math.Round(Player.balance / 100, 0) * value);
  65.         }
  66.  
  67.  
  68.         public int CountStringOccurrences(string text, string pattern)
  69.         {
  70.             // Loop through all instances of the string 'text'.
  71.             int count = 0;
  72.             int i = 0;
  73.             while ((i = text.IndexOf(pattern, i)) != -1)
  74.             {
  75.                 i += pattern.Length;
  76.                 count++;
  77.             }
  78.             return count;
  79.         }
  80.  
  81.         //Seřadit karty od nejnižší po nejvyšší
  82.         public List<int[]> SerializeCards(List<int[]> input)
  83.         {
  84.             for (int j = 0; j < input.Count(); j++)
  85.             {
  86.                 for (int i = 0; i < input.Count() - 1; i++)
  87.                 {
  88.                     if (input[i][0] > input[i + 1][0])
  89.                     {
  90.                         var pom = input[i][0];
  91.                         var pom2 = input[i][1];
  92.                         input[i][0] = input[i + 1][0];
  93.                         input[i][1] = input[i + 1][1];
  94.                         input[i + 1][0] = pom;
  95.                         input[i + 1][1] = pom2;
  96.                     }
  97.                 }
  98.             }
  99.             return input;
  100.         }
  101.  
  102.         //Vyčistí karty od duplikátů
  103.         public int[] ClearCards(int[] input)
  104.         {
  105.             return input.Distinct().ToArray();
  106.         }
  107.  
  108.         public bool IsStraight(List<int[]> input)
  109.         {
  110.             /////////////////////////////////////////////////////////////////
  111.             //                          Postupka                           //
  112.             int count = 0;
  113.             List<int[]> serializedcards = SerializeCards(input);
  114.  
  115.  
  116.             if (serializedcards.Count >= 5)
  117.             {
  118.                 int firstcardindex = 0;
  119.                 for (int i = 0; i < 2; i++)
  120.                 {
  121.                     if (serializedcards[i][0] == (serializedcards[i + 1][0] - 1))
  122.                     {
  123.                         if (serializedcards[i + 2][0] == (serializedcards[i + 3][0] - 1))
  124.                         {
  125.                             if (serializedcards[i + 3][0] == (serializedcards[i + 4][0] - 1))
  126.                             {
  127.                                 if (serializedcards[i + 4][0] == (serializedcards[i + 5][0] - 1))
  128.                                 {
  129.                                     count++;
  130.                                 }
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.                 if (count >= 1)
  136.                 {
  137.                     return true;
  138.                 }
  139.                 else return false;
  140.             }
  141.             else return false;
  142.             /////////////////////////////////////////////////////////////////
  143.         }
  144.  
  145.         public bool IsFlush(List<int[]> input)
  146.         {
  147.             /////////////////////////////////////////////////////////////////
  148.             //                            Barva                            //
  149.             bool color = false;
  150.             for (int i = 0; i < cards_colors.Count(); i++)
  151.             {
  152.                 int points = 0;
  153.                 if (input[0][1] == cards_colors[i]) points++;
  154.                 if (input[1][1] == cards_colors[i]) points++;
  155.                 if (input[2][1] == cards_colors[i]) points++;
  156.                 if (input[3][1] == cards_colors[i]) points++;
  157.                 if (input[4][1] == cards_colors[i]) points++;
  158.                 if (input[5][1] == cards_colors[i]) points++;
  159.                 if (input[6][1] == cards_colors[i]) points++;
  160.  
  161.                 if (points >= 5) color = true;
  162.             }
  163.             if (color) return true;
  164.             else return false;
  165.             //////////////////////////////////////////////////////////////////
  166.         }
  167.  
  168.         public bool IsPair(List<int[]> input)
  169.         {
  170.             /////////////////////////////////////////////////////////////////
  171.             //                          Pár                                //
  172.             int count = 0;
  173.             List<int[]> serializedcards = SerializeCards(input);
  174.             for (int i = 0; i < serializedcards.Count - 1; i++)
  175.             {
  176.                 if (serializedcards[i][0] == (serializedcards[i + 1][0]))
  177.                 {
  178.                     count++;
  179.                 }
  180.             }
  181.             if (count == 1) return true;
  182.             else return false;
  183.             //////////////////////////////////////////////////////////////////
  184.         }
  185.  
  186.         public bool IsTwoPair(List<int[]> input)
  187.         {
  188.             /////////////////////////////////////////////////////////////////
  189.             //                          Dva páry                           //
  190.             int count = 0;
  191.             List<int[]> serializedcards = SerializeCards(input);
  192.             for (int i = 0; i < serializedcards.Count - 1; i++)
  193.             {
  194.                 if (serializedcards[i][0] == (serializedcards[i + 1][0]))
  195.                 {
  196.                     count++;
  197.                 }
  198.             }
  199.             if (count >= 2) return true;
  200.             else return false;
  201.             //////////////////////////////////////////////////////////////////
  202.         }
  203.  
  204.         public bool IsThreeOfAKind(List<int[]> input)
  205.         {
  206.             /////////////////////////////////////////////////////////////////
  207.             //                         Trojička                            //
  208.             int count = 0;
  209.             List<int[]> serializedcards = SerializeCards(input);
  210.             for (int i = 0; i < serializedcards.Count - 2; i++)
  211.             {
  212.                 if (serializedcards[i][0] == serializedcards[i + 1][0] &&
  213.                     serializedcards[i + 1][0] == serializedcards[i + 2][0])
  214.                 {
  215.                     count++;
  216.                 }
  217.             }
  218.             if (count != 0) return true;
  219.             else return false;
  220.             //////////////////////////////////////////////////////////////////
  221.         }
  222.  
  223.         public bool IsFullHouse(List<int[]> input)
  224.         {
  225.             /////////////////////////////////////////////////////////////////
  226.             ///                         FullHouse                          //
  227.             int count = 0, count2 = 0;
  228.             int toakcard = 0;
  229.             List<int[]> serializedcards = SerializeCards(input);
  230.             for (int i = 0; i < serializedcards.Count - 2; i++)
  231.             {
  232.                 if (serializedcards[i] == serializedcards[i + 1] && serializedcards[i + 1] == serializedcards[i + 2])
  233.                 {
  234.                     toakcard = serializedcards[i][0];
  235.                     count++;
  236.                 }
  237.             }
  238.             if (count != 0)
  239.             {
  240.                 int[] remaining = serializedcards[0].Where(val => val != toakcard).ToArray();
  241.                 for (int i = 0; i < remaining.Length - 1; i++)
  242.                 {
  243.                     if (remaining[i] == remaining[i + 1])
  244.                     {
  245.                         count2++;
  246.                     }
  247.                 }
  248.                 if (count2 != 0) return true;
  249.                 else return false;
  250.             }
  251.             else return false;
  252.             //////////////////////////////////////////////////////////////////
  253.         }
  254.  
  255.         public bool IsFourOfAKind(List<int[]> input)
  256.         {
  257.             /////////////////////////////////////////////////////////////////
  258.             ///                          Čtyřka                            //
  259.             int count = 0;
  260.             List<int[]> serializedcards = SerializeCards(input);
  261.             for (int i = 0; i < serializedcards.Count - 3; i++)
  262.             {
  263.                 if (serializedcards[i][0] == serializedcards[i + 1][0] &&
  264.                     serializedcards[i + 1][0] == serializedcards[i + 2][0] &&
  265.                     serializedcards[i + 2][0] == serializedcards[i + 3][0])
  266.                 {
  267.                     count++;
  268.                 }
  269.             }
  270.             if (count != 0) return true;
  271.             else return false;
  272.             //////////////////////////////////////////////////////////////////
  273.         }
  274.  
  275.         public bool IsStraightFlush(List<int[]> input)
  276.         {
  277.             /////////////////////////////////////////////////////////////////
  278.             //                       Barevná postupka                      //
  279.             int count = 0;
  280.             List<int[]> serializedcards = SerializeCards(input);
  281.             bool first = false;
  282.             int firstcardinrow = 0;
  283.             int firstcardindex = 0;
  284.             for (int i = 0; i < 3; i++)
  285.             {
  286.                 if (serializedcards[i][0] == (serializedcards[i + 1][0] - 1))
  287.                 {
  288.                     if (serializedcards[i + 1][0] == (serializedcards[i + 2][0] - 1))
  289.                     {
  290.                         if (serializedcards[i + 2][0] == (serializedcards[i + 3][0] - 1))
  291.                         {
  292.                             if (serializedcards[i + 3][0] == (serializedcards[i + 4][0] - 1))
  293.                             {
  294.                                 if (!first)
  295.                                 {
  296.                                     firstcardindex = i;
  297.                                     firstcardinrow = serializedcards[i][0];
  298.                                     first = true;
  299.                                 }
  300.                                 count++;
  301.                             }
  302.                         }
  303.                     }
  304.                 }
  305.             }
  306.             if (count >= 1)
  307.             {
  308.                 bool color = false;
  309.                 for (int i = 0; i < cards_colors.Count(); i++)
  310.                 {
  311.                     int points = 0;
  312.  
  313.  
  314.                     if (serializedcards[firstcardindex][1] == cards_colors[i]) points++;
  315.                     if (serializedcards[firstcardindex + 1][1] == cards_colors[i]) points++;
  316.                     if (serializedcards[firstcardindex + 2][1] == cards_colors[i]) points++;
  317.                     if (serializedcards[firstcardindex + 3][1] == cards_colors[i]) points++;
  318.                     if (serializedcards[firstcardindex + 4][1] == cards_colors[i]) points++;
  319.                     if (points == 5) color = true;
  320.                 }
  321.                 if (color) return true;
  322.                 return false;
  323.             }
  324.             return false;
  325.             //////////////////////////////////////////////////////////////////
  326.         }
  327.  
  328.         public int[] UniqeRandom(int size, int Min, int Max)
  329.         {
  330.  
  331.             int[] UniqueArray = new int[size];
  332.             Random rnd = new Random();
  333.  
  334.             for (int i = 0; i < size; i++)
  335.             {
  336.  
  337.                 var random = rnd.Next(Min, Max);
  338.  
  339.                 for (int j = i; j >= 0; j--)
  340.                 {
  341.  
  342.                     if (UniqueArray[j] == random)
  343.                     {
  344.                         random = rnd.Next(Min, Max);
  345.                         j = i;
  346.                     }
  347.  
  348.                 }
  349.  
  350.                 UniqueArray[i] = random;
  351.  
  352.             }
  353.  
  354.             return UniqueArray;
  355.         }
  356.  
  357.         public List<T> Randomize<T>(List<T> list)
  358.         {
  359.             List<T> randomizedList = new List<T>();
  360.             Random rnd = new Random();
  361.             while (list.Count > 0)
  362.             {
  363.                 int index = rnd.Next(0, list.Count); //pick a random item from the master list
  364.                 randomizedList.Add(list[index]); //place it at the end of the randomized list
  365.                 list.RemoveAt(index);
  366.             }
  367.             return randomizedList;
  368.         }
  369.  
  370.         private List<int[]> GenerateCardsForPlayer(List<int[]> usedCards1)
  371.         {
  372.             List<int[]> remainingCards = new List<int[]>(Cards);
  373.             for (int i = 0; i < usedCards1.Count(); i++)
  374.             {
  375.                 remainingCards.Remove(usedCards1[i]);
  376.             }
  377.             remainingCards = Randomize(remainingCards);
  378.             int remainingCardsCount = remainingCards.Count();
  379.             int[] twoCards = UniqeRandom(2, 0, remainingCardsCount);
  380.  
  381.             UsedCards.Add(remainingCards[twoCards[0]]);
  382.             UsedCards.Add(remainingCards[twoCards[1]]);
  383.  
  384.             List<int[]> playerCards = new List<int[]>() { remainingCards[twoCards[0]], remainingCards[twoCards[1]] };
  385.             return playerCards;
  386.         }
  387.  
  388.         private List<int[]> GenerateLandCards(List<int[]> usedCards1)
  389.         {
  390.             List<int[]> remainingCards = new List<int[]>(Cards);
  391.             for (int i = 0; i < usedCards1.Count(); i++)
  392.             {
  393.                 remainingCards.Remove(usedCards1[i]);
  394.             }
  395.             remainingCards = Randomize(remainingCards);
  396.             int remainingCardsCount = remainingCards.Count();
  397.             int[] twoCards = UniqeRandom(5, 0, remainingCardsCount);
  398.  
  399.  
  400.             UsedCards.Add(remainingCards[twoCards[0]]);
  401.             UsedCards.Add(remainingCards[twoCards[1]]);
  402.             UsedCards.Add(remainingCards[twoCards[2]]);
  403.             UsedCards.Add(remainingCards[twoCards[3]]);
  404.             UsedCards.Add(remainingCards[twoCards[4]]);
  405.  
  406.             List<int[]> landCards = new List<int[]>()
  407.             {
  408.                 remainingCards[twoCards[0]],
  409.                 remainingCards[twoCards[1]],
  410.                 remainingCards[twoCards[2]],
  411.                 remainingCards[twoCards[3]],
  412.                 remainingCards[twoCards[4]]
  413.             };
  414.             return landCards;
  415.         }
  416.  
  417.         public int solveCards(string player, List<int[]> cards, List<int[]> lands)
  418.         {
  419.            
  420.             cards.AddRange(lands);
  421.            
  422.  
  423.             /////////////////////////////////////////////////////////////////
  424.             ///                   Zjišťování kombinací                    ///
  425.             /////////////////////////////////////////////////////////////////
  426.  
  427.             int rank = 0;
  428.  
  429.             if (IsPair(cards) && rank == 0 ) rank = 1;
  430.             /*
  431.             if (IsTwoPair(cards1) && rank < 2) rank = 2;
  432.             if (IsThreeOfAKind(cards1) && rank < 3) rank = 3;
  433.             if (IsStraight(cards1) && rank < 4) rank = 4;
  434.             if (IsFlush(cards1) && rank < 5) rank = 5;
  435.             if (IsFullHouse(cards1) && rank < 6) rank = 6;
  436.             if (IsFourOfAKind(cards1) && rank < 7) rank = 7;
  437.             if (IsStraightFlush(cards1) && rank < 8) rank = 8;
  438.              */
  439.              
  440.             return rank;
  441.         }
  442.  
  443.         public void Generating()
  444.         {
  445.  
  446.             List<int[]> playerCards1 = GenerateCardsForPlayer(UsedCards);
  447.             player0Card1.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards1[0][0], playerCards1[0][1]));
  448.             player0Card2.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards1[1][0], playerCards1[1][1]));
  449.             player0Card1l.Text = getCardVal(playerCards1[0][0], playerCards1[0][1]);
  450.             player0Card2l.Text = getCardVal(playerCards1[1][0], playerCards1[1][1]);
  451.  
  452.            
  453.             List<int[]> playerCards2 = GenerateCardsForPlayer(UsedCards);
  454.             player1Card1.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards2[0][0], playerCards2[0][1]));
  455.             player1Card2.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards2[1][0], playerCards2[1][1]));
  456.             //player1Card1l.Text = getCardVal(playerCards2[0][0], playerCards2[0][1]);
  457.             //player1Card2l.Text = getCardVal(playerCards2[1][0], playerCards2[1][1]);
  458.  
  459.             List<int[]> playerCards3 = GenerateCardsForPlayer(UsedCards);
  460.             player2Card1.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards3[0][0], playerCards3[0][1]));
  461.             player2Card2.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards3[1][0], playerCards3[1][1]));
  462.             //player2Card1l.Text = getCardVal(playerCards3[0][0], playerCards3[0][1]);
  463.             //player2Card2l.Text = getCardVal(playerCards3[1][0], playerCards3[1][1]);
  464.  
  465.             List<int[]> playerCards4 = GenerateCardsForPlayer(UsedCards);
  466.             player3Card1.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards4[0][0], playerCards4[0][1]));
  467.             player3Card2.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards4[1][0], playerCards4[1][1]));
  468.  
  469.             List<int[]> playerCards5 = GenerateCardsForPlayer(UsedCards);
  470.             player4Card1.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards5[0][0], playerCards5[0][1]));
  471.             player4Card2.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards5[1][0], playerCards5[1][1]));
  472.  
  473.             List<int[]> playerCards6 = GenerateCardsForPlayer(UsedCards);
  474.             player5Card1.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards6[0][0], playerCards6[0][1]));
  475.             player5Card2.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards6[1][0], playerCards6[1][1]));
  476.  
  477.             List<int[]> playerCards7 = GenerateCardsForPlayer(UsedCards);
  478.             player6Card1.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards7[0][0], playerCards7[0][1]));
  479.             player6Card2.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards7[1][0], playerCards7[1][1]));
  480.  
  481.             List<int[]> playerCards8 = GenerateCardsForPlayer(UsedCards);
  482.             player7Card1.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards8[0][0], playerCards8[0][1]));
  483.             player7Card2.Image = (Image)Resources.ResourceManager.GetObject(getCardVal(playerCards8[1][0], playerCards8[1][1]));
  484.            
  485.             //Vyložit pět karet na stůl
  486.             List<int[]> landCards = GenerateLandCards(UsedCards);
  487.            
  488.             landCard1.Image =
  489.                 (Image)Resources.ResourceManager.GetObject(getCardVal(landCards[0][0], landCards[0][1]));
  490.             landCard2.Image =
  491.                 (Image)Resources.ResourceManager.GetObject(getCardVal(landCards[1][0], landCards[1][1]));
  492.             landCard3.Image =
  493.                 (Image)Resources.ResourceManager.GetObject(getCardVal(landCards[2][0], landCards[2][1]));
  494.             landCard4.Image =
  495.                 (Image)Resources.ResourceManager.GetObject(getCardVal(landCards[3][0], landCards[3][1]));
  496.             landCard5.Image =
  497.                 (Image)Resources.ResourceManager.GetObject(getCardVal(landCards[4][0], landCards[4][1]));
  498.  
  499.  
  500.             List<int[]> landCards1 = new List<int[]>(landCards);
  501.  
  502.             solveCards("player0", playerCards1, landCards);
  503.  
  504.             //solveCards("player1", playerCards2[0], playerCards2[1], landCards[0], landCards[1], landCards[2], landCards[3], landCards[4]);
  505.  
  506.             label3.Text = getCardVal(playerCards1[0][0], playerCards1[0][1]);
  507.             label4.Text = getCardVal(playerCards1[1][0], playerCards1[1][1]);
  508.  
  509.             label5.Text = getCardVal(playerCards2[0][0], playerCards2[0][1]);
  510.             label6.Text = getCardVal(playerCards2[1][0], playerCards2[1][1]);
  511.  
  512.             land_card_1l.Text = getCardVal(landCards1[0][0], landCards1[0][1]);
  513.             land_card_2l.Text = getCardVal(landCards1[1][0], landCards1[1][1]);
  514.             land_card_3l.Text = getCardVal(landCards1[2][0], landCards1[0][1]);
  515.             land_card_4l.Text = getCardVal(landCards1[3][0], landCards1[3][1]);
  516.             land_card_5l.Text = getCardVal(landCards1[4][0], landCards1[4][1]);
  517.  
  518.  
  519.  
  520.  
  521.            
  522.  
  523.             UsedCards.Clear();
  524.         }
  525.  
  526.         private void timer1_Tick(object sender, EventArgs e)
  527.         {
  528.             //Generating();
  529.         }
  530.  
  531.         private void button1_Click(object sender, EventArgs e)
  532.         {
  533.             //timer1.Start();
  534.             Generating();
  535.         }
  536.     }
  537. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement