2017-04-10 57 views
0

我最近創建了一個可以正常工作的輪盤。唯一的問題是它會一直旋轉很長時間(通常10分鐘以上)。顯然這不是很好,我想知道如何設置它將旋轉的最大值。如何阻止我的輪盤旋轉幾年?

public partial class Roulette : Form 
{ 
    #region Variables 

    int[] x = {213, 231, 250, 268, 285, 297, 310, 319, 324, 325, 326, 323, 315, 304, 291, 277, 260, 242, 223, 204, 183, 165, 147, 132, 120, 110, 102, 97, 98, 99, 104, 115, 126, 142, 158, 176, 194}; 
    int[] y = {152, 153, 158, 165, 174, 185, 202, 219, 236, 254, 270, 293, 307, 322, 333, 348, 355, 361, 367, 364, 362, 357, 347, 338, 321, 307, 288, 269, 254, 234, 217, 201, 185, 174, 164, 160, 155}; 
    string[] Number = { "0", "32", "15", "19", "4", "21", "2", "25", "17", "34", "6", "27", "13", "36", "11", "30", "8", "23", "10", "5", "24", "16", "33", "1", "20", "14", "31", "9", "22", "18", "29", "7", "28", "12", "35", "3", "26" }; 

    int position = 0; 
    Random rnd = new Random(); 
    int loopTimes = 0; 
    int loopCount = 0; 
    int ballPos = 0; 
    int totalGrapes = 30; 
    int currentBet = 0; 
    int addEarnings; 


    #endregion 

    public Roulette() 
    { 
     InitializeComponent(); 
    } 

    private void Roulette_Load(object sender, EventArgs e) 
    { 
     #region cboNum 

     cboNum.Items.Add("0");    
     cboNum.Items.Add("32"); 
     cboNum.Items.Add("15"); 
     cboNum.Items.Add("19"); 
     cboNum.Items.Add("4"); 
     cboNum.Items.Add("21"); 
     cboNum.Items.Add("2"); 
     cboNum.Items.Add("25"); 
     cboNum.Items.Add("17"); 
     cboNum.Items.Add("34"); 
     cboNum.Items.Add("6"); 
     cboNum.Items.Add("27"); 
     cboNum.Items.Add("13"); 
     cboNum.Items.Add("36"); 
     cboNum.Items.Add("11"); 
     cboNum.Items.Add("30"); 
     cboNum.Items.Add("8"); 
     cboNum.Items.Add("23"); 
     cboNum.Items.Add("10"); 
     cboNum.Items.Add("5"); 
     cboNum.Items.Add("24"); 
     cboNum.Items.Add("16"); 
     cboNum.Items.Add("33"); 
     cboNum.Items.Add("1"); 
     cboNum.Items.Add("20"); 
     cboNum.Items.Add("14"); 
     cboNum.Items.Add("31"); 
     cboNum.Items.Add("9"); 
     cboNum.Items.Add("22"); 
     cboNum.Items.Add("18"); 
     cboNum.Items.Add("29"); 
     cboNum.Items.Add("7"); 
     cboNum.Items.Add("28"); 
     cboNum.Items.Add("12"); 
     cboNum.Items.Add("35"); 
     cboNum.Items.Add("3"); 
     cboNum.Items.Add("26"); 

     #endregion 


    } 

    private void picBall_Click(object sender, EventArgs e) 
    { 

    } 

    private void btnTest_Click(object sender, EventArgs e) 
    { 
     // Used to test the coordinates 
     #region Number Input 

     if (txtNumber.Text == "0")       
     { 
      picBall.Location = new Point(213, 152); 
     } 
     if (txtNumber.Text == "32") 
     { 
      picBall.Location = new Point(231, 153); 
     } 
     if (txtNumber.Text == "15") 
     { 
      picBall.Location = new Point(250, 158); 
     } 
     if (txtNumber.Text == "19") 
     { 
      picBall.Location = new Point(268, 165); 
     } 
     if (txtNumber.Text == "4") 
     { 
      picBall.Location = new Point(285, 174); 
     } 
     if (txtNumber.Text == "21") 
     { 
      picBall.Location = new Point(297, 185); 
     } 
     if (txtNumber.Text == "2") 
     { 
      picBall.Location = new Point(310, 202); 
     } 
     if (txtNumber.Text == "25") 
     { 
      picBall.Location = new Point(319, 219); 
     } 
     if (txtNumber.Text == "17") 
     { 
      picBall.Location = new Point(324, 236); 
     } 
     if (txtNumber.Text == "34") 
     { 
      picBall.Location = new Point(325, 254); 
     } 
     if (txtNumber.Text == "6") 
     { 
      picBall.Location = new Point(326, 270); 
     } 
     if (txtNumber.Text == "27") 
     { 
      picBall.Location = new Point(323, 293); 
     } 
     if (txtNumber.Text == "13") 
     { 
      picBall.Location = new Point(315, 307); 
     } 
     if (txtNumber.Text == "36") 
     { 
      picBall.Location = new Point(304, 322); 
     } 
     if (txtNumber.Text == "11") 
     { 
      picBall.Location = new Point(291, 333); 
     } 
     if (txtNumber.Text == "30") 
     { 
      picBall.Location = new Point(277, 348); 
     } 
     if (txtNumber.Text == "8") 
     { 
      picBall.Location = new Point(260, 355); 
     } 
     if (txtNumber.Text == "23") 
     { 
      picBall.Location = new Point(242, 361); 
     } 
     if (txtNumber.Text == "10") 
     { 
      picBall.Location = new Point(223, 367); 
     } 
     if (txtNumber.Text == "5") 
     { 
      picBall.Location = new Point(204, 364); 
     } 
     if (txtNumber.Text == "24") 
     { 
      picBall.Location = new Point(183, 362); 
     } 
     if (txtNumber.Text == "16") 
     { 
      picBall.Location = new Point(165, 357); 
     } 
     if (txtNumber.Text == "33") 
     { 
      picBall.Location = new Point(147, 347); 
     } 
     if (txtNumber.Text == "1") 
     { 
      picBall.Location = new Point(132, 338); 
     } 
     if (txtNumber.Text == "20") 
     { 
      picBall.Location = new Point(120, 321); 
     } 
     if (txtNumber.Text == "14") 
     { 
      picBall.Location = new Point(110, 307); 
     } 
     if (txtNumber.Text == "31") 
     { 
      picBall.Location = new Point(102, 288); 
     } 
     if (txtNumber.Text == "9") 
     { 
      picBall.Location = new Point(97, 269); 
     } 
     if (txtNumber.Text == "22") 
     { 
      picBall.Location = new Point(98, 254); 
     } 
     if (txtNumber.Text == "18") 
     { 
      picBall.Location = new Point(99, 234); 
     } 
     if (txtNumber.Text == "29") 
     { 
      picBall.Location = new Point(104, 217); 
     } 
     if (txtNumber.Text == "7") 
     { 
      picBall.Location = new Point(115, 201); 
     } 
     if (txtNumber.Text == "28") 
     { 
      picBall.Location = new Point(126, 185); 
     } 
     if (txtNumber.Text == "12") 
     { 
      picBall.Location = new Point(142, 175); 
     } 
     if (txtNumber.Text == "35") 
     { 
      picBall.Location = new Point(158, 164); 
     } 
     if (txtNumber.Text == "3") 
     { 
      picBall.Location = new Point(176, 160); 
     } 
     if (txtNumber.Text == "26") 
     { 
      picBall.Location = new Point(194, 155); 
     } 

     #endregion 
    } 

    private void btnRoll_Click(object sender, EventArgs e) 
    { 
     position = 0; 
     loopCount = 0; 

     tmrRoll.Interval = 90; 

     tmrRoll.Enabled = true; 

     loopTimes = rnd.Next(); 
     ballPos = rnd.Next(5); 

     // Used to place grapes 
     #region Grapes placed 

     if (txtPlaced.Text == "1") 
     { 
      totalGrapes--; 
      currentBet = 1; 
     } 
     if (txtPlaced.Text == "2") 
     { 
      totalGrapes--; 
      totalGrapes--; 
      currentBet = 2; 
     } 
     if (txtPlaced.Text == "3") 
     { 
      totalGrapes--; 
      totalGrapes--; 
      totalGrapes--; 
      currentBet = 3; 
     } 
     if (txtPlaced.Text == "4") 
     { 
      totalGrapes--; 
      totalGrapes--; 
      totalGrapes--; 
      totalGrapes--; 
      currentBet = 4; 
     } 
     if (txtPlaced.Text == "5") 
     { 
      totalGrapes--; 
      totalGrapes--; 
      totalGrapes--; 
      totalGrapes--; 
      totalGrapes--; 
      currentBet = 5; 
     } 
     else 
     { 
      txtPlaced.Text = "Error (Bet 1-5)"; 
     } 
     #endregion 

     if (totalGrapes <= 0) 
     { 
      MessageBox.Show("You have run out of grapes!"); 
      Application.Exit(); 
     } 


    } 

    private void tmrRoll_Tick(object sender, EventArgs e) 
    { 

     string guess = cboNum.Text; 

     lbltotalGrapes.Text = totalGrapes.ToString(); 
     txtPlaced.Text = currentBet.ToString(); 

     picBall.Location = new Point(x[position], y[position]); 


     if (position == ballPos && loopTimes == loopCount)    
     { 
      tmrRoll.Enabled = false; 

      int arraypos = Array.IndexOf(Number, guess); 

      if (ballPos == arraypos)         
      { 
       MessageBox.Show("You guessed correctly!"); 





      } 
      else 
      { 
       MessageBox.Show("Try again"); 
       addEarnings = currentBet * 3; 
       totalGrapes = totalGrapes + addEarnings; 

      } 
     } 

     position++; 

     if (position > x.Length - 1) 
     { 
      position = 0; 
      loopCount++; 
      tmrRoll.Interval += 30; 
     } 
    } 
+4

可以縮短,只粘貼相關的代碼? – niksofteng

回答

0

loopTimes = rnd.Next()返回一個32位有符號整數,它是大於或等於0且小於MaxValue,這是一個龐大的數字。

要限制環路的最大數量,這樣的:

int loopLimit = 100; // set your limit 
loopTimes = rnd.Next(loopLimit); 
+0

另外,請查看[loops](https://msdn.microsoft.com/en-us/library/32dbftby.aspx),它會讓你的生活變得更輕鬆。 –