石头剪刀
学习C#已经有一段时间了,今天跟着老师学习了石头剪刀布,下面是自己写的石头剪刀布
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _08石头剪刀布 8 { 9 class Player 10 { 11 //定义出拳方式:1--石头 2--剪刀 3--布 12 public int ShowFist(string fist) 13 { 14 int fistType = 0; 15 switch (fist) 16 { 17 case "石头": fistType = 1; break; 18 case "剪刀": fistType = 2; break; 19 case "布": fistType = 3; break; 20 } 21 return fistType; 22 23 } 24 } 25 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _08石头剪刀布 8 { 9 class Computer 10 { 11 Random r = new Random(); 12 public string Fist 13 { 14 get; 15 set; 16 17 } 18 public int ShowFist() 19 { 20 int fistType = r.Next(1,4); 21 switch (fistType) 22 { 23 case 1: this.Fist = "石头"; break; 24 case 2: this.Fist = "剪刀"; break; 25 case 3: this.Fist = "布"; break; 26 27 } 28 return fistType; 29 } 30 } 31 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace _08石头剪刀布 8 { 9 public class Referee 10 { 11 public enum Result 12 { 13 玩家赢, 14 电脑赢, 15 平局 16 17 } 18 public static Result Judge(int playFist,int computerFist) 19 { 20 if ((playFist - computerFist == -1) || (playFist - computerFist == 2)) 21 { 22 return Result.玩家赢; 23 } 24 else if (playFist == computerFist) 25 { 26 return Result.平局; 27 } 28 else 29 { 30 return Result.电脑赢; 31 } 32 33 } 34 } 35 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 11 namespace _08石头剪刀布 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void button1_Click(object sender, EventArgs e) 21 { 22 string str="石头"; 23 show(str); 24 } 25 26 27 private void button2_Click(object sender, EventArgs e) 28 { 29 string str = "剪刀"; 30 show(str); 31 32 } 33 34 private void button3_Click(object sender, EventArgs e) 35 { 36 string str = "布"; 37 show(str); 38 } 39 /// <summary> 40 /// 定义界面角色显示出拳结果,并对其结果反映在界面。 41 /// </summary> 42 /// <param name="strFist"></param> 43 public void show(string strFist) 44 { 45 label3.Text = strFist; 46 Player pfist = new Player(); 47 int playerfist = pfist.ShowFist(strFist); 48 Computer cfist = new Computer(); 49 int cpufist = cfist.ShowFist(); 50 switch (cpufist) 51 { 52 53 case 1: label4.Text = "石头"; break; 54 case 2: label4.Text = "剪刀"; break; 55 case 3: label4.Text = "布"; break; 56 } 57 label6.Text = Referee.Judge(playerfist, cpufist).ToString(); 58 59 } 60 61 62 } 63 }
还是有点小小成就感的,主要利用面对对象的思想还是蛮重要的。
当然对于 出拳方式的处理,让 1--石头,2--剪刀 ,3--布
对于裁判的判断,只进行 减法结果的判断进行这一点还是蛮巧妙的
玩家赢的结果有:1-2=-1,2-3=-1,3-1=2
平局只要减法结果为0
而剩下的便是电脑赢。
以下是我执行结果的一张例图