欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# Winform实现石头剪刀布游戏

程序员文章站 2023-11-29 15:03:58
本文实例为大家分享了winform实现石头剪刀布游戏的具体代码,供大家参考,具体内容如下 新建一个windows窗体程序,用数字1代表石头,用数字2代表剪刀,用数字3代表...

本文实例为大家分享了winform实现石头剪刀布游戏的具体代码,供大家参考,具体内容如下

新建一个windows窗体程序,用数字1代表石头,用数字2代表剪刀,用数字3代表布,结果取玩家和电脑出拳之差,有三种结果

玩家赢: -1,2

平手: 0

玩家输: 其它值

新建3个类: 

1)computer.cs 电脑随机出拳

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace 石头剪刀布

{

 class computer

 {

 public string fist
 {

  get;
  set;

 } 

 public int showfist()

 {
  random rnd = new random();
  int fist = rnd.next(1, 4);
  switch (fist)

  {

  case 1: fist = "石头"; break;
  case 2: fist = "剪刀"; break;
  case 3: fist = "布"; break;

  }

  return fist;

 }

 }

}

2)、judge.cs 裁判类 判断输赢 

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace 石头剪刀布

{
 class judge

 {

 public enum result

 {

  玩家赢,
  电脑赢,
  平手

 }

 public static result whowin(int playernum, int computernum)

 {

  int result = playernum - computernum;
  if (result == -1 || result == 2)

  {

  return result.玩家赢;

  }

  else if (result == 0)

  {

  return result.平手;

  }

  else

  {

  return result.电脑赢;

  } 

 } 

 }

}

3)、player.cs 玩家,出拳 

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;

namespace 石头剪刀布

{

 class player

 {

 public static int showfist(string fist)

 {

  switch (fist)

  {

  case "石头": return 1;
  case "剪刀": return 2;
  case "布": return 3;
  default: return 0;

  }

 }

 }

}

界面后台实现代码: 

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.io;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;

namespace 石头剪刀布

{

 public partial class form1 : form

 {

 public form1()

 {

  initializecomponent();

 }

 /// <summary>
 /// 点击石头按钮
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnstone_click(object sender, eventargs e)

 {

  string fist = "石头";

  game(fist);

 }

 /// <summary>
 /// 点击剪刀按钮
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>

 private void btnscissors_click(object sender, eventargs e)

 {

  string fist = "剪刀";
  game(fist);

 }

 /// <summary>
 /// 点击布按钮
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>

 private void btncloth_click(object sender, eventargs e)

 {

  string fist = "布";
  game(fist);

 

 }

 

 //背景图片轮播
 string[] paths = directory.getfiles(@"c:\work\stone");//此目录里面必须有图片,否则会报错
 private void timer1_tick(object sender, eventargs e)

 {

  this.backgroundimage = image.fromfile(paths[new random().next(0, paths.length)]);

 

 }
 static int playerwintimes = 0;//玩家赢的次数
 static int gametimes = 0;//总共次数
 static int tietimes = 0;//平手次数


 /// <summary>
 /// 通用方法
 /// </summary>
 /// <param name="fist"></param>

 private void game(string fist)

 {

  gametimes++;
  lbplayer.text = fist;
  int playernum = player.showfist(fist);
  computer cpu = new computer();
  int cpunum = cpu.showfist();
  lbcomputer.text = cpu.fist;
  judge.result result = judge.whowin(playernum, cpunum);
  lbjudge.text = result.tostring();
  lbstatistics.text = "统计信息:\n\n1.您赢了" + playerwintimes + "场比赛!\n\n" + "2.平手了" + tietimes + "次; \n\n" + "3.输掉了" + (gametimes - playerwintimes - tietimes) + "场比赛; \n\n" + "4.共进行了" + gametimes + "场比赛!\n\n";


  if (result == judge.result.玩家赢)

  {

  playerwintimes++;
  messagebox.show("恭喜,您已经赢了" + playerwintimes + "场比赛!" + " 共进行了" + gametimes + "场比赛!");

  }

  else if (result == judge.result.平手)

  {

  tietimes++;

  }


 }

 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。