コード例 #1
0
        // Try버튼을 클릭하였을 때 랜덤 뽑기를 진행
        private void TryButton_Click(object sender, EventArgs e)
        {
            decimal sum = 0;

            // 리스트에 아이템이 없을 경우 처리
            if (itemList.Count == 0)
            {
                TryButtonMessageBox(0);
                return;
            }

            foreach (LootItem item in itemList)
            {
                sum += item.Probability;
            }
            if (sum > 100)
            {
                TryButtonMessageBox(1);
                return;
            }

            Random r            = new Random(DateTime.Now.Millisecond);
            int    randomResult = r.Next(0, 100000);

            decimal min = 0;
            decimal max = itemList[0].Probability;

            // 100 미만일 시 나머지를 꽝으로 처리
            if (sum < 100)
            {
                string[] lang = new string[3] {
                    "lose", "꽝", "空っぽ"
                };

                if (loseItem == null)
                {
                    loseItem = new ResultLootItem(lang[language_comboList.SelectedIndex], 100.0m - sum);
                    countResultList.Add(loseItem);
                    countResult_dataGridView.Rows.Add(loseItem.ItemImage, loseItem.Name, loseItem.Count);
                }
                loseItem.Probability = 100.0m - sum;
                itemList.Add(loseItem);
            }

            for (int i = 0; i < itemList.Count; i++)
            {
                Debug.WriteLine("min : {0}, prob : {1}", min * 1000, itemList[i].Probability * 1000);
                // 성공
                if (randomResult >= (min * 1000) && randomResult < (max * 1000))
                {
                    Debug.WriteLine("당첨!");
                    if (itemList[i].ImgFIlePath.Equals("NULL"))
                    {
                        orderResultList.Add(new ResultLootItem(itemList[i].Name, itemList[i].Probability));
                    }
                    else
                    {
                        orderResultList.Add(new ResultLootItem(itemList[i].Name, itemList[i].Probability, itemList[i].ItemImage, itemList[i].OriginalImage, itemList[i].ImgFIlePath));
                    }
                    orderResult_dataGridView.Rows.Add(itemList[i].ItemImage, itemList[i].Name);
                    countResultList[i].Count++;
                    Debug.WriteLine(countResult_dataGridView.Rows[i].Cells[2].Value);
                    Debug.WriteLine(countResultList[i].Count);

                    countResult_dataGridView.Rows[i].Cells[2].Value = countResultList[i].Count;

                    ResultPage resultPage = new ResultPage(itemList[i], language_comboList.SelectedIndex);

                    resultPage.ShowDialog();

                    for (int k = 0; k < itemList.Count; k++)
                    {
                        if (loseItem == null)
                        {
                            break;
                        }
                        if (itemList[k].GetHashCode() == loseItem.GetHashCode())
                        {
                            // 성공
                            itemList.RemoveAt(k);
                            break;
                        }
                    }
                    return;
                }
                // 실패
                else
                {
                    Debug.WriteLine("실패");
                    min = itemList[i].Probability;
                    if (itemList.Count == i + 1)
                    {
                        break;
                    }
                    max += itemList[i + 1].Probability;
                }
            }
            MessageBox.Show("꽝!");
        }