课程的理解
现在对类的理解更深刻了一些,应该.......知道类图的画法,这次作业是通过先构思类图再写代码。
设计实现
业务类:
- Dice(滚轮的一个面)
- DiceVessel(滚轮,存储6个滚轮面)
- Award(单个奖品)
- AwardVessel(奖品池)
UI类:
- 界面1:Form1
- 界面2:Form1、Form2
代码展示
-业务类代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DiceGame_3_dll 8 { 9 public class Dice 10 { 11 //1--苹果 2--香蕉 3--梨子 4--草莓 5--西瓜 6--头奖 12 private int type; 13 private string picturefile; 14 15 public Dice(int type, string picturefile) 16 { 17 this.type = type; 18 this.picturefile = picturefile; 19 } 20 21 public string PictureFile() 22 { 23 return picturefile; 24 } 25 } 26 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Threading; 7 8 namespace DiceGame_3_dll 9 { 10 public class DiceVessel 11 { 12 Dice[] d = new Dice[6]; 13 14 private int awardjudge; 15 16 public DiceVessel() 17 { 18 d[0] = new Dice(1, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\1.png"); 19 d[1] = new Dice(2, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\2.png"); 20 d[2] = new Dice(3, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\3.png"); 21 d[3] = new Dice(4, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\4.png"); 22 d[4] = new Dice(5, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\5.png"); 23 d[5] = new Dice(6, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\6.png"); 24 } 25 26 public int Awardjudge() 27 { 28 return awardjudge; 29 } 30 31 public string DicePictureReturn(bool judge) 32 { 33 if (judge) 34 return RollingStart(); 35 else 36 return RollingStop(); 37 } 38 39 private string RollingStart() 40 { 41 return "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\Rolling.gif"; 42 } 43 44 private string RollingStop() 45 { 46 awardjudge = GetRandom(); 47 return d[awardjudge - 1].PictureFile(); 48 } 49 50 private int GetRandom() 51 { 52 Thread.Sleep(10); 53 Random ran = new Random(unchecked((int)DateTime.Now.Ticks)); 54 return ran.Next(1, 6); 55 } 56 } 57 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DiceGame_3_dll 8 { 9 public class Award 10 { 11 private string picturefile; 12 private int type; 13 14 public Award(int type, string picturefile) 15 { 16 this.type = type; 17 this.picturefile = picturefile; 18 } 19 20 public string GetPicturefile() 21 { 22 return picturefile; 23 } 24 25 public int GetType() 26 { 27 return type; 28 } 29 } 30 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace DiceGame_3_dll 8 { 9 public class AwardVessel 10 { 11 Award[] a = new Award[6]; 12 13 public AwardVessel() 14 { 15 a[0] = new Award(1, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\一等奖.jpg"); 16 a[1] = new Award(2, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\二等奖.jpg"); 17 a[2] = new Award(3, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\三等奖.jpg"); 18 a[3] = new Award(4, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\四等奖.jpg"); 19 a[4] = new Award(5, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\鼓励奖.jpg"); 20 a[5] = new Award(6, "C:\\Users\\Benjamin\\Desktop\\DiceGame_3\\DiceGame_3\\DiceGame_3\\Resources\\安慰奖.jpg"); 21 } 22 23 public string AwardPictureReturn(int t1, int t2, int t3) 24 { 25 if (t1 == 1 && t2 == 1 && t3 == 1) 26 return a[0].GetPicturefile(); 27 else if (t1 == 2 && t2 == 2 && t3 == 2) 28 return a[1].GetPicturefile(); 29 else if (t1 == 3 && t2 == 3 && t3 == 3) 30 return a[2].GetPicturefile(); 31 else if (t1 == 4 && t2 == 4 && t3 == 4) 32 return a[3].GetPicturefile(); 33 else if (t1 == 5 && t2 == 5 && t3 == 5) 34 return a[4].GetPicturefile(); 35 else 36 return a[5].GetPicturefile(); 37 } 38 39 public string AwardTextReturn(int t1, int t2, int t3) 40 { 41 if (t1 == 1 && t2 == 1 && t3 == 1) 42 return a[0].GetType() + "等奖"; 43 else if (t1 == 2 && t2 == 2 && t3 == 2) 44 return a[1].GetType() + "等奖"; 45 else if (t1 == 3 && t2 == 3 && t3 == 3) 46 return a[2].GetType() + "等奖"; 47 else if (t1 == 4 && t2 == 4 && t3 == 4) 48 return a[3].GetType() + "等奖"; 49 else if (t1 == 5 && t2 == 5 && t3 == 5) 50 return "鼓励奖"; 51 else 52 return "安慰奖"; 53 } 54 } 55 }
-UI类代码:
界面1:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using System.Threading; 11 using DiceGame_3_dll; 12 13 namespace DiceGame_3 14 { 15 public partial class Form1 : Form 16 { 17 18 DiceGame_3_dll.DiceVessel v1 = new DiceGame_3_dll.DiceVessel(); 19 DiceGame_3_dll.DiceVessel v2 = new DiceGame_3_dll.DiceVessel(); 20 DiceGame_3_dll.DiceVessel v3 = new DiceGame_3_dll.DiceVessel(); 21 DiceGame_3_dll.AwardVessel a = new DiceGame_3_dll.AwardVessel(); 22 23 public Form1() 24 { 25 InitializeComponent(); 26 } 27 28 private void Start_MouseClick(object sender, MouseEventArgs e) 29 { 30 pictureBox1.Image=Image.FromFile(v1.DicePictureReturn(true)); 31 pictureBox2.Image = Image.FromFile(v2.DicePictureReturn(true)); 32 pictureBox3.Image = Image.FromFile(v3.DicePictureReturn(true)); 33 } 34 35 private void End_MouseClick(object sender, MouseEventArgs e) 36 { 37 pictureBox1.Image = Image.FromFile(v1.DicePictureReturn(false)); 38 pictureBox2.Image = Image.FromFile(v2.DicePictureReturn(false)); 39 pictureBox3.Image = Image.FromFile(v3.DicePictureReturn(false)); 40 textBox1.Text = a.AwardTextReturn(v1.Awardjudge(),v2.Awardjudge(),v3.Awardjudge()); 41 } 42 43 private void Form1_Load(object sender, EventArgs e) 44 { 45 46 } 47 } 48 }
界面2:
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using DiceGame_3_dll; 11 12 namespace DiceGame_3_2 13 { 14 public partial class Form1 : Form 15 { 16 DiceGame_3_dll.DiceVessel v1 = new DiceGame_3_dll.DiceVessel(); 17 DiceGame_3_dll.DiceVessel v2 = new DiceGame_3_dll.DiceVessel(); 18 DiceGame_3_dll.DiceVessel v3 = new DiceGame_3_dll.DiceVessel(); 19 DiceGame_3_dll.AwardVessel a = new DiceGame_3_dll.AwardVessel(); 20 21 public Form1() 22 { 23 InitializeComponent(); 24 } 25 26 private void Form1_Load(object sender, EventArgs e) 27 { 28 29 } 30 31 private void button1_MouseClick(object sender, MouseEventArgs e) 32 { 33 pictureBox1.Image = Image.FromFile(v1.DicePictureReturn(false)); 34 pictureBox2.Image = Image.FromFile(v2.DicePictureReturn(false)); 35 pictureBox3.Image = Image.FromFile(v3.DicePictureReturn(false)); 36 37 Form2 f=new Form2(a,v1.Awardjudge(),v2.Awardjudge(),v3.Awardjudge()); 38 f.Show(); 39 } 40 } 41 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 using DiceGame_3_dll; 11 12 namespace DiceGame_3_2 13 { 14 public partial class Form2 : Form 15 { 16 public Form2(AwardVessel a,int v1,int v2,int v3) 17 { 18 InitializeComponent(); 19 pictureBox1.Image = Image.FromFile(a.AwardPictureReturn(v1,v2,v3)); 20 } 21 22 private void Form2_Load(object sender, EventArgs e) 23 { 24 25 } 26 } 27 }
测试结果
界面1:
界面2:
小结
上次作业理解错老师的意思,这次经过仔细思考类图的构建,对类的理解更深刻了,这次在编写代码阶段轻松了很多,相比上次一边写一边思考要省事很多。但是构思上面还欠缺经验,有时候还需要后面想到了再添加进去。
这次在随机数的产生上面出现一个问题:因为三个类同时返回随机数的时间间隔太短,随机数又是以当前时间返回伪随机数的,所以变成每次三个数都相同。后面查了其他人的博客(比如CSDN之类的),重写了随机数的方法(添加种子数和使用Threed.Sleep()方法)。