用c#控制台玩石头剪刀布游戏

代码
  1  
  2 
  3 using System;
  4 
  5 using System.Collections.Generic;
  6 
  7 using System.Linq;
  8 
  9 using System.Text;
 10 
 11 using System.Threading;//线程命名空间必需引用
 12 
 13 using System.Collections;//集全命名空间必需引用
 14 
 15 using System.Collections.Generic;
 16 
 17 using System.Text.RegularExpressions;
 18 
 19 using System.Data.SqlClient;
 20 
 21 enum gameread : int
 22 {
 23             石头 = 1,
 24             剪刀 = 2,
 25             布 = 3
 26 }
 27 
 28 static void 石头剪刀布游戏()
 29 
 30 {
 31 
 32 int result_player = 0, result_pc = 0, deuce = 0, runnumber = 0;//声名变量,玩家得分,电脑得分,平手数,游戏次数
 33 
 34  string chue;//变量是否重玩
 35 
 36  do//断续玩的循环
 37 
 38  {
 39 
 40  Console.WriteLine("\n********************");
 41 
 42 Console.WriteLine("来玩石头剪刀布游戏");
 43 
 44 Console.WriteLine("********************");
 45 
 46 Console.Write("你想玩几局,请输入:");
 47 
 48 runnumber = Convert.ToInt32(Console.ReadLine());//输入局数
 49 
 50 int i = 0;//局数增加变量
 51 
 52 do//局数的循环
 53 
 54 {
 55 
 56 agn://选择不正确时重新选择的标记
 57 
 58 Console.Write("\n你想出什么?1).石头   2).剪刀   3).布:");
 59 
 60 int player;//玩家的游戏选择变量
 61 
 62 try//如果输入的不能转换为数,则重新选择
 63 
 64 {
 65 
 66 player = int.Parse(Console.ReadLine());
 67 
 68 }
 69 
 70 catch
 71 
 72 {
 73 
 74  Console.WriteLine("你的选择有误,请重新选择!");
 75 
 76  goto agn;
 77 
 78 }
 79 
 80 Thread.Sleep(100);//等待电脑选择随机数
 81 
 82 int pc = new Random().Next(14);
 83 
 84 Console.Write("电脑出是:{0}\n你出是;{1}\n", (gameread)pc, (gameread)player);//输入的选择转换为枚举变量,石头,剪刀,布
 85 
 86 if (player == pc)//判断是否和电脑出的是同样参数
 87 
 88 {
 89 
 90  Console.WriteLine("你和电脑平手!");
 91 
 92  deuce++;
 93 
 94 }
 95 
 96 else
 97 
 98 {
 99 
100 switch (player)//输赢的判断
101 
102 {
103 
104 case 1://玩家是石头时
105 
106 if (pc == 3)//电脑是布则为输
107 
108 result_pc++;//电脑加分
109 
110 else
111 
112 result_player++;//玩家加分
113 
114 Console.WriteLine(pc == 3 ? "这一局你输了!" : "这一局你赢了!"); break;
115 
116 case 2://玩家是剪刀时
117 
118 if (pc == 1)//电脑是石头则为输
119 
120 result_pc++;//电脑加分
121 
122 else
123 
124 result_player++;//玩家加分
125 
126 Console.WriteLine(pc == 1 ? "这一局你输了!" : "这一局你赢了!"); break;
127 
128 case 3://玩家是布时
129 
130 if (pc == 2)//电脑是剪刀则为输
131 
132  result_pc++;//电脑加分
133 
134 else
135 
136 result_player++;//玩家加分
137 
138 Console.WriteLine(pc == 2 ? "这一局你输了!" : "这一局你赢了!");
139 
140 break;
141 
142 default://如果没有以上的选择,则玩家选择有误码,重新选择
143 
144 Console.WriteLine("你的选择不对,请重玩。");
145 
146 goto agn;
147 
148 }
149 
150 }
151 
152 i++;//局数变量增加
153 
154 while (i < runnumber);
155 
156  
157 
158  Console.WriteLine("是否还玩?1).yes 2).其它键结束");
159 
160 chue = Console.ReadLine();
161 
162 while (chue == "1");//选择1时继续玩
163 
164 Console.WriteLine("你的得分为:{0}分 电脑的得分为:{1}分 有{2}次是平手", result_player, result_pc, deuce);
165 
166 if (result_pc == result_player)//判断总输赢
167 
168 {
169 
170 Console.WriteLine("结局你们是平手!");
171 
172 }
173 
174 else if (result_player > result_pc)
175 
176 {
177 
178  Console.WriteLine("恭喜你,最后还是你赢。了!");
179 
180 }
181 
182 else
183 
184 {
185 
186  Console.WriteLine("很遗憾,结局是电脑赢。");
187 
188 }
189 
190 }
191 
原文地址:https://www.cnblogs.com/Witkey/p/1628793.html