1. 程式人生 > 程式設計 >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#實現石頭剪刀布遊戲

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。