1.    using System;
2.    using System.Drawing;
3.    using System.Collections;
4.    using System.Windows.Forms;
5.    using System.Data; 
6.
7.    namespace PocketDice 
8.    { 
9.        public class frmMain:System.Windows.Forms.Form 
10.       { 
11.        private System.Windows.Forms.MainMenu mainMenu1; 
12.
13.        private int _rollCount = 0; 
14.        private ScoringBox[] _scoringBoxes; 
15.        private SingleDie[] _dice; 
16.        private Button _btnRoll; 
17.        private Label _lblRollCount; 
18.        private Data _data; 
19.        private int _totalScore = 0; 
20.
21.        private DiceImages _diceImages = new DiceImages(); 
22.        private System.Windows.Forms.Label lblScore; 
23.        private System.Windows.Forms.MenuItem miNewGame; 
24.        private System.Windows.Forms.MenuItem menuItem1; 
25.        private System.Windows.Forms.MenuItem miHighScores; 
26.        private System.Windows.Forms.MenuItem miHelp; 
27.        private System.Windows.Forms.MenuItem menuItem2; 
28.        private System.Windows.Forms.MenuItem miAbout; 
29.
30.        private const int kDICE_BONUS = 6; 
31.
32.        public frmMain() 
33.        { 
34.            // 
35.            // Ҫ֧Windows Form Designer 
36.            // 

37.            InitializeComponent(); 
38.
39.            // 
40.            // TODO:InitializeComponentúй 
41.            // 
42.        } 
43.
44.        protected override void Dispose( bool disposing ) 
45.        { 
46.            base.Dispose( disposing ); 
47.        } 
48.        /// <summary> 
49.        ///  
50.        /// </summary> 
51.
52.        static void Main() 
53.        { 
54.              Application.Run(new frmMain()); 
55.        } 
56.
57.        /********************************************** 
58.        *ϵĿؼ 
59.        * ********************************************/ 
60.
61.        private const int kNUM_SCORINGBOXES = 17; 
62.
63.        private void SetupScoringBoxes() 
64.        { 
65.            const int kWIDTH = 100; 
66.            const int kHEIGHT = 20; 
67.            int kTOP = lblScore.Bottom + 4; 
68.            const int kLEFT = 10; 
69.            const int kLEFT_COL2 = 114; 
70.            const int kWIDTH_COL2 = 120; 
71.            const int kSEP = 2; 
72.
73.            // Ϊõboxes 
74.            _scoringBoxes = new ScoringBox[kNUM_SCORINGBOXES]; 
75.
76.            _scoringBoxes[0] = new ScoringBox("Aces", kLEFT, kTOP + ((kHEIGHT + kSEP)* 0),kWIDTH, kHEIGHT, new ScoringHandler(Scoring.ScoreAces)); 
77.          _scoringBoxes[1] = new ScoringBox("Twos",  kLEFT, kTOP + ((kHEIGHT + kSEP)* 1), kWIDTH, kHEIGHT, new ScoringHandler(Scoring.ScoreTwos)); 
78.            _scoringBoxes[2] = new ScoringBox("Threes", kLEFT, kTOP + ((kHEIGHT + kSEP)* 2), kWIDTH, kHEIGHT, new ScoringHandler
(Scoring.ScoreThrees)); 
79.            _scoringBoxes[3] = new ScoringBox("Fours", kLEFT, kTOP + ((kHEIGHT + kSEP)* 3), kWIDTH, kHEIGHT, new ScoringHandler(Scoring.ScoreFours)); 
80.            _scoringBoxes[4] = new ScoringBox("Fives",  kLEFT, kTOP + ((kHEIGHT + kSEP)* 4), kWIDTH, kHEIGHT, new ScoringHandler(Scoring.ScoreFives)); 
81.        _scoringBoxes[5] = new ScoringBox("Sixes", kLEFT, kTOP + ((kHEIGHT + kSEP)* 5), kWIDTH, kHEIGHT, new ScoringHandler(Scoring.ScoreSixes)); 
82.        _scoringBoxes[kDICE_BONUS] = new ScoringBox("Bonus",  kLEFT, kTOP + ((kHEIGHT + kSEP)* 6), kWIDTH, kHEIGHT, null); 
83.        _scoringBoxes[7] = new ScoringBox("Reds",  kLEFT, kTOP + ((kHEIGHT + kSEP)* 7), kWIDTH, kHEIGHT, new ScoringHandler(Scoring.ScoreReds)); 
84.        _scoringBoxes[8] = new ScoringBox("Blacks", kLEFT, kTOP + ((kHEIGHT + kSEP)* 8), kWIDTH, kHEIGHT, new ScoringHandler(Scoring.ScoreBlacks)); 
85.
86.        _scoringBoxes[9] = new ScoringBox("3 of a Kind", kLEFT_COL2, kTOP + ((kHEIGHT + kSEP)* 0),kWIDTH_COL2, kHEIGHT, new ScoringHandler(Scoring.Score3OfAKind)); 
87.        _scoringBoxes[10] = new ScoringBox("4 of a Kind", kLEFT_COL2, kTOP + ((kHEIGHT + kSEP)* 1),kWIDTH_COL2, kHEIGHT, new ScoringHandler(Scoring.Score4OfAKind)); 
88.        _scoringBoxes[11] = new ScoringBox("Full House", kLEFT_COL2, kTOP + ((kHEIGHT + kSEP)* 2),kWIDTH_COL2, kHEIGHT, new ScoringHandler(Scoring.ScoreFullHouse)); 
89.        _scoringBoxes[12] = new ScoringBox("Flush", kLEFT_COL2, kTOP + ((kHEIGHT + kSEP)* 3),kWIDTH_COL2, kHEIGHT, new ScoringHandler(Scoring.ScoreFlush)); 
90.        _scoringBoxes[13] = new ScoringBox("Straight", kLEFT_COL2, kTOP + ((kHEIGHT + kSEP)* 4),kWIDTH_COL2, kHEIGHT, new ScoringHandler(Scoring.ScoreStraight)); 
91.        _scoringBoxes[14] = new ScoringBox("Chance",  kLEFT_COL2, kTOP + ((kHEIGHT + kSEP)* 5),kWIDTH_COL2, kHEIGHT, new ScoringHandler(Scoring.ScoreChance)); 
92.        _scoringBoxes[15] = new ScoringBox("5 of a Kind", kLEFT_COL2, kTOP + ((kHEIGHT + kSEP)* 7),kWIDTH_COL2, kHEIGHT, new ScoringHandler(Scoring.Score5OfAKind)); 
93.        _scoringBoxes[16] = new ScoringBox("Str. Flush", kLEFT_COL2, kTOP + ((kHEIGHT + kSEP)* 8),kWIDTH_COL2, kHEIGHT, new ScoringHandler(Scoring.ScoreStraightFlush)); 
94.
95.        // ӵ 
96.        for (int i=0; i <_scoringBoxes.Length; i++) 
97.        { 
98.         _scoringBoxes[i].ScoreClick += new 
PocketDiceEventHandler(HandleScoreClick); 
99.        this.Controls.Add(_scoringBoxes[i]); 
100.        } 
101.
102.    } 
103.
104.    private void SetupDice() 
105.    { 
106.        const int kWIDTH = 32; 
107.        const int kHEIGHT = 32; 
108.        const int kTOP = 230; 
109.        const int kLEFT = 20; 
110.        const int kSEP = 4; 
111.
112.        // ť 
113.        _btnRoll = new Button(); 
114.        _btnRoll.Text = "Roll"; 
115.        _btnRoll.Width = 30; 
116.        _btnRoll.Height = 16; 
117.        _btnRoll.Left = kLEFT + (kWIDTH + kSEP ) * 5; 
118.        _btnRoll.Top = kTOP; 
119.        _btnRoll.Click += new EventHandler(btnRoll_Click); 
120.        this.Controls.Add(_btnRoll); 
121.
122.         // ǩ 
123.         _lblRollCount = new Label(); 
124.         _lblRollCount.Text = ""; 
125.         _lblRollCount.Width=44; 
126.         _lblRollCount.Height = 16; 
127.         _lblRollCount.Left = _btnRoll.Left + 4; 
128.         _lblRollCount.Top = _btnRoll.Bottom + 4; 
129.         this.Controls.Add(_lblRollCount); 
130.
131.
132.         // diceб 
133.         _dice = new SingleDie[5]; 
134.
135.         for (int i=0; i<_dice.Length; i++) 
136.         { 
137.              _dice[i] = new SingleDie( _diceImages, kLEFT + (kWIDTH + kSEP ) * i, kTOP, kWIDTH, kHEIGHT); 
138.             this.Controls.Add(_dice[i]); 
139.         } 
140.
141.    } 
142.
143.    /*************************************************** 
144.     *ʼͼƬͷ 
145.     * *************************************************/ 
146.
147.    private void InitializeDice() 
148.    { 
149.        for (int i=0; i<_dice.Length; i++) 
150.        {
151.            _dice[i].SetBlank();
152.        }
153.    }
154.
155.    private void InitializeScoringBoxes() 
156.    { 
157.        // ؽ
158.        _scoringBoxes[kDICE_BONUS].Enabled = false; 
159.
160.        for (int i=0; i <_scoringBoxes.Length; i++) 
161.        { 
162.            _scoringBoxes[i].State = ScoringBoxState.NotScoring; 
163.            _scoringBoxes[i].ScoreValue = 0; 
164.            _scoringBoxes[i].Invalidate(); 
165.        } 
166.    } 
167.
168.    /*********************************************** 
169.     *ͶһΣ÷ 
170.     * *********************************************/ 
171.
172.
173.    /****************************************************** 
174.     * form & control¼ 
175.     * ****************************************************/ 
176.
177.    private void frmMain_Load(object sender, System.EventArgs e)  
178.    {  
179.        // ڸ߷  
180.        _data = new Data();  
181.     
182.        // õ÷box & diceؼ
183.        SetupScoringBoxes();  
184.        SetupDice();  
185.     
186.        StartNewGame();  
187.    }  
188.     
189.    private void frmMain_Closing(object sender, 
System.ComponentModel.CancelEventArgs e)
190.    { 
191.        // رǰ߷ 
192.         _data.Save(); 
193.    } 
194.
195.
196.    private void btnRoll_Click(object sender, System.EventArgs e) 
197.    { 
198.        // Rollťִһ 
199.
200.        // ӹʾ 
201.        _rollCount++; 
202.        if (_rollCount >=3) _btnRoll.Enabled = false; 
203.        string s=""; 
204.        switch (_rollCount) { case 1: s="*"; break; case 2: s="**"; break; case 3: s="***"; break; } 
205.        _lblRollCount.Text = s; 
206.
207.        // ÿdie 
208.        for (int i=0; i<_dice.Length; i++) 
209.        { 
210.            _dice[i].Roll(); 
211.        } 
212.        this.Refresh(); //´Ӷʹdice 
213.
214.        // ѡܵõĵ÷ַ 
215.        // ʾǵǱڵ÷ 
216.        for (int i=0; i<_scoringBoxes.Length; i++) 
217.        {
218.            _scoringBoxes[i].SetScoring(_dice); 
219.         }  
220.     }  
221.     
222.    private void UpdateTotalScore() 
223.    { 
224.        // ܽеǰķ 
225.        // ʾ 
226.        int score = 0; 
227.        for (int i=0; i<_scoringBoxes.Length; i++) 
228.        { 
229.            if (_scoringBoxes[i].State == ScoringBoxState.Locked) 
230.            {  
231.                score += _scoringBoxes[i].ScoreValue;  
232.            }  
233.        }  
234.      
235.        lblScore.Text = string.Format("Score:   {0}", score);  
236.        _totalScore = score;  
237.     }  
238.      
239.     private void HandleScoreClick(object sender, 
PocketDiceEventArgs e) 
240.     { 

241.         for (int i=0; i<_scoringBoxes.Length; i++) 
242.         { 
243.              _scoringBoxes[i].SetNotScoring(); 
244.         } 
245.     
246.         for (int i=0; i<_dice.Length; i++) 
247.         { 
248.              _dice[i].SetBlank(); 
249.     
250.         } 
251.     
252.         // еõ㹻ķ֣һ 
253.         int p = 0; 
254.         for (int i=0; i<kDICE_BONUS; i++) 
255.         { 
256.             p _scoringBoxes[i].ScoreValue; += 
257.         } 
258.         if (p >= 63) 
259.         { 
260.             _scoringBoxes[kDICE_BONUS].ScoreValue = 35; 
261.             _scoringBoxes[kDICE_BONUS].State = 
ScoringBoxState.Locked; 
262.             _scoringBoxes[kDICE_BONUS].Enabled = true; 
263.         } 
264.
265.        // ܷ 
266.        UpdateTotalScore(); 
267.
268.        // ¹ť 
269.        _rollCount=0; 
270.        _btnRoll.Enabled = true; 
271.        _lblRollCount.Text = ""; 
272.
273.        // Ϸ ? 
274.        if (CheckForEndGame()) EndGame(); 
275.
276.
277.        } 
278.
279.        /******************************************* 
280.         *ͽϷ 
281.         * *****************************************/ 
282.
283.        private void StartNewGame() 
284.        { 
285.            InitializeScoringBoxes(); 
286.            InitializeDice(); 
287.            _btnRoll.Enabled = true; 
288.            _rollCount = 0; 
289.            _lblRollCount.Text = ""; 
290.            UpdateTotalScore(); 
291.        } 
292.
293.        private bool CheckForEndGame() 
294.        { 
295.           for (int i=0; i<_scoringBoxes.Length; i++) 
296.            { 
297.              if (i != kDICE_BONUS) 
298.              { 
299.                  if (_scoringBoxes[i].State != 
ScoringBoxState.Locked) 
300.                        return false; 
301.              } 
302.          } 
303.
304.          return true; 
305.    } 
306.
307.          private void EndGame() 
308.         { 
309.             lblScore.Text += " - - Game Over"; 
310.             InitializeDice(); 
311.             _btnRoll.Enabled = false; 
312.
313.             // ߷ 
314.             if ( _data.IsNewHighScore(_totalScore)) 
315.             { 
316.                 // õû 
317.                 string prompt = "Congratulations - you have a new high score. Enter your name:"; 
318.                 frmInputBox frm = new frmInputBox(); 
319.                 frm.lblPrompt.Text = prompt; 
320.
321.                 frm.Location.Offset(0,50); 
322.
323.                 frm.ShowDialog(); 
324.
325.                 string name = frm.txtValue.Text; 
326.
327.                 // ӵHighScoresб 
328.                 int index = _data.StoreHighScore(_totalScore, name); 
329.
330.                 // ʾHighScoresԻ
331.                 frmHighScores frmHs = new frmHighScores
(_data, index); 
332.                 frmHs.ShowDialog(); 
333.             }  
334.
335.         } 
336.
337.         /*********************************************** 
338.          *˵¼ 
339.          * *********************************************/ 
340.
341.         private void miNewGame_Click(object sender, 
System.EventArgs e) 
342.         { 
343.             // New Game˵дһ 
344.             StartNewGame(); 
345.         } 
346.
347.         private void miHighScores_Click(object sender, System.
EventArgs e) 
348.         { 
349.             // High Scores˵дһ 
350.             frmHighScores frm = new frmHighScores(_data); 
351.             frm.ShowDialog(); 
352.         } 
353.
354.         private void miHelp_Click(object sender, System.EventArgs e) 
355.        { 
356.             // һHelp˵дһ; ߰ 
357.            try 
358.            { 
359.                // ԴInternet Explorerҳ
360.                Launcher.OpenWebPage(@"\Program Files\
PocketDice\help.html"); 
361.            } 
362.            catch (Exception ex) 
363.            { 
364.                MessageBox.Show( 
365.                    string.Format("There has been an error displaying 
online help.\n\n{0}" 
366.    , ex.ToString()) 
367.                    , "Help Error" 
368.                   , MessageBoxButtons.OK 
369.                   , MessageBoxIcon.Hand 
370.                   , MessageBoxDefaultButton.Button1); 
371.            } 
372.        } 
373.
374.        private void miAbout_Click(object sender, System.EventArgs e)
375.        { 
376.            // About˵дһ;ʾйط
377.            frmAbout frm = new frmAbout(); 
378.            frm.ShowDialog(); 
379.        } 
380.    } 
381.  } 
