服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现石头剪刀布游戏

C#实现石头剪刀布游戏

2022-10-11 14:27Handsome Wong C#

这篇文章主要为大家详细介绍了C#实现石头剪刀布游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 石头剪刀布
{
  public partial class Form1 : Form
  {
    int computer_sorce = 0;
    int user_sorce = 0;
    int i=0,j = 0;
    int last_step = -1;//0表示平局,1为电脑上步加一,2为用户上步加一
    public Form1()
    {
      InitializeComponent();
      this.Text = "石头剪刀布";
      label3.Text = ":";
      label1.Visible = false;
      label2.Visible = false;
      label3.Visible = false;
      label4.Visible = false;
      button1.Text = "开始游戏";
      button2.Text = "重新开始";
      button3.Text = "悔棋";
      button4.Text = "退出";
      button2.Visible = false;
      button3.Visible = false; 
      button4.Visible = false;
      pictureBox2.Visible = false;
      pictureBox3.Visible = false;
      pictureBox3.Visible = false;
      pictureBox4.Visible = false;
      pictureBox5.Visible = false;
      pictureBox6.Visible = false;
      pictureBox7.Visible = false;
      pictureBox8.Visible = false;
      pictureBox9.Visible = false;
      pictureBox10.Visible =false;
      pictureBox11.Visible = false;
    }
    private void Form1_Load(object sender, EventArgs e)
    {

    }
    private void button1_Click(object sender, EventArgs e)
    {
      pictureBox1.Visible = false;
      button1.Visible = false;
      button1.Enabled = false;
      pictureBox2.Visible = true;
      pictureBox3.Visible = true;
      pictureBox4.Visible = true;
      pictureBox5.Visible = true;
      pictureBox6.Visible = true;
      pictureBox7.Visible = true;
      pictureBox8.Visible = true;
      pictureBox9.Visible = true;
      pictureBox10.Visible = true;
      pictureBox11.Visible = true;
      button2.Visible = true;
      button3.Visible = true;
      button4.Visible = true;
      
    }
    private void game_computer()
    {
      Random rand = new Random();
      i = rand.Next(4);
      if (i == 1)
      {
        pictureBox10.Load("..\..\picture\1.jpg");
      }
      else if (i == 2)
      {
        pictureBox10.Load("..\..\picture\2.jpg");
      }
      else if(i == 3)
      {
        pictureBox10.Load("..\..\picture\3.jpg");
      }
    }
    private void game_judgment()
    {
      if (i == 1)//电脑出石头
      {
        if (j == 1)
        {
          last_step = 0;
        }
        else if (j == 2)//用户出剪刀
        {
          computer_sorce++;
          last_step = 1;
        }
        else if (j == 3)//用户出布
        {
          user_sorce++;
          last_step = 2;
        }
      }
      else if (i == 2)//电脑出剪刀
      {
        if (j == 1)//用户出石头
        {
          user_sorce++;
          last_step = 2;
        }
        else if (j == 2)
        {
          last_step = 0;
        }
        else if (j == 3)//用户出布
        {
          computer_sorce++;
          last_step = 1;
        }
      }
      else if (i == 3)//电脑出布
      {
        if (j == 1)//用户出石头
        {
          computer_sorce++;
          last_step = 1;
        }
        else if (j == 2)//用户出剪刀
        {
          user_sorce++;
          last_step = 2;
        }
        else if (j == 3)
        {
          last_step = 0;
        }
      }
      if (computer_sorce == 2)//当电脑分数到2时,显示you lose
      {
        label1.Visible = true;
        pictureBox7.Enabled = false;//结果出来后三个图片按钮失效
        pictureBox8.Enabled = false;
        pictureBox9.Enabled = false;
        label1.Text = "YOU LOSE!";
      }
      else if (user_sorce == 2)//当用户分数到2时,显示you win
      {
        label1.Visible = true;
        pictureBox7.Enabled = false;
        pictureBox8.Enabled = false;
        pictureBox9.Enabled = false;
        label1.Text = "YOU WIN!";
      }
    }
    private void button4_Click(object sender, EventArgs e)//退出按钮
    {
      this.Close();
    }
    private void pictureBox7_Click(object sender, EventArgs e)//用户石头图片
    {
      j = 1;
      pictureBox10.Visible = true;//划拳图片显示正常
      pictureBox11.Visible = true;
      label2.Visible = true;//计分器三个label显示
      label3.Visible = true;
      label4.Visible = true;
      game_computer();
      game_judgment();
      label2.Text = computer_sorce.ToString();
      label4.Text = user_sorce.ToString();
      pictureBox11.Load("D:\newdesktop\1.jpg");
    }
    private void pictureBox8_Click(object sender, EventArgs e)//用户剪刀图片
    {
      j = 2;
      pictureBox10.Visible = true;//划拳图片显示正常
      pictureBox11.Visible = true;
      label2.Visible = true;//计分器三个label显示
      label3.Visible = true;
      label4.Visible = true;
      game_computer();
      game_judgment();
      label2.Text = computer_sorce.ToString();
      label4.Text = user_sorce.ToString();
      pictureBox11.Load("D:\newdesktop\2.jpg");
    }
    private void pictureBox9_Click(object sender, EventArgs e)//用户布图片
    {
      j = 3;
      pictureBox10.Visible = true;//划拳图片显示正常
      pictureBox11.Visible = true;
      label2.Visible = true;//计分器三个label显示
      label3.Visible = true;
      label4.Visible = true;
      game_computer();
      game_judgment();
      label2.Text = computer_sorce.ToString();
      label4.Text = user_sorce.ToString();
      pictureBox11.Load("D:\newdesktop\3.jpg");
    }
    private void button2_Click(object sender, EventArgs e)
    {
      pictureBox7.Enabled = true;//重新开始后三个图片按钮生效
      pictureBox8.Enabled = true;
      pictureBox9.Enabled = true;
      i = 0;
      j = 0;
      computer_sorce = 0;
      user_sorce = 0;
      label2.Text = computer_sorce.ToString();
      label4.Text = user_sorce.ToString();
      pictureBox10.Visible = false;
      pictureBox11.Visible = false;
      label1.Visible = false;
      label2.Visible = false;
      label3.Visible = false;
      label4.Visible = false;
    }
    private void button3_Click(object sender, EventArgs e)
    {
      label1.Visible = false;
      pictureBox7.Enabled = true;
      pictureBox8.Enabled = true;
      pictureBox9.Enabled = true;
      pictureBox10.Visible = false;
      pictureBox11.Visible = false;
      if (last_step == 0)
      {
      }
      else if (last_step == 1)
      {
        computer_sorce--;
        label2.Text = computer_sorce.ToString();
        label4.Text = user_sorce.ToString();
      }
      else if (last_step == 2)
      {
        user_sorce--;
        label2.Text = computer_sorce.ToString();
        label4.Text = user_sorce.ToString();
      }
      last_step = -1;
    }
  }
}

效果图

开始游戏

C#实现石头剪刀布游戏

点击开始

C#实现石头剪刀布游戏

划拳

C#实现石头剪刀布游戏

判定胜败

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

重新开始

C#实现石头剪刀布游戏

悔棋

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

退出

C#实现石头剪刀布游戏

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

原文链接:https://blog.csdn.net/dalao_whs/article/details/108978301

延伸 · 阅读

精彩推荐
  • C#C# websocket及时通信协议的实现方法示例

    C# websocket及时通信协议的实现方法示例

    说到websocket大家一定不会陌生,WebSocket是HTML5一种新的协议。下面这篇文章主要给大家介绍了关于C# websocket及时通信协议的实现方法,文中通过示例代码介...

    C#教程网7452022-02-12
  • C#Unity Shader实现素描风格的渲染

    Unity Shader实现素描风格的渲染

    这篇文章主要为大家详细介绍了Unity Shader实现素描风格的渲染,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    vvc223c10552022-09-06
  • C#C# WPF 通过委托实现多窗口间的传值的方法

    C# WPF 通过委托实现多窗口间的传值的方法

    这篇文章主要介绍了C# WPF 通过委托实现多窗口间的传值的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    hello黄先森6332022-02-28
  • C#C#程序连接数据库及读取数据库中字段的简单方法总结

    C#程序连接数据库及读取数据库中字段的简单方法总结

    包括C#连接Access、Oracle或者SQL Server,这里整理了一些C#连接数据库及从读取数据库中字段的简单方法总结,需要的朋友可以参考下...

    shylx1239902021-11-22
  • C#Unity3D网格功能生成球体网格模型

    Unity3D网格功能生成球体网格模型

    这篇文章主要为大家详细介绍了Unity3D网格功能生成球体网格模型,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    雁回晴空10192022-03-10
  • C#C#身份证号码验证是否正确

    C#身份证号码验证是否正确

    这一篇关于C#语言验证18位身份证号码的验证方法和实例代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    C#教程网7002021-10-28
  • C#c#关于JWT跨域身份验证的实现代码

    c#关于JWT跨域身份验证的实现代码

    这篇文章主要介绍了c#关于JWT跨域身份验证的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友...

    流月无双5842022-08-07
  • C#C# 导出Excel的6种简单方法实现

    C# 导出Excel的6种简单方法实现

    C# 导出 Excel 的6种简单方法:数据表导出到 Excel,对象集合导出到 Excel,数据库导出到 Excel,微软网格控件导出到 Excel,数组导出到 Excel,CSV 导出到 Excel,...

    Johnson Manohar4132022-08-05