猜数字小游戏,C#实现
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 猜数字
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static bool _beginflag;
private static int _guesstimes = 1;
public int[] correctnumber = new int[4];
public void BeginButton_Click(object sender, EventArgs e)
{
_guesstimes = 0;
#region 生成随机数
Random rand = new Random();
correctnumber[0] = rand.Next(0, 9);
correctnumber[1] = rand.Next(0, 9);
correctnumber[2] = rand.Next(0, 9);
correctnumber[3] = rand.Next(0, 9);
while (correctnumber[0] == correctnumber[1] ||
correctnumber[0] == correctnumber[2] ||
correctnumber[0] == correctnumber[3]
)
{
correctnumber[0] = rand.Next(0, 9);
}
while (correctnumber[1] == correctnumber[0] ||
correctnumber[1] == correctnumber[2] ||
correctnumber[1] == correctnumber[3]
)
{
correctnumber[1] = rand.Next(0, 9);
}
while (correctnumber[2] == correctnumber[0] ||
correctnumber[2] == correctnumber[1] ||
correctnumber[2] == correctnumber[3]
)
{
correctnumber[2] = rand.Next(0, 9);
}
while (correctnumber[3] == correctnumber[1] ||
correctnumber[3] == correctnumber[2] ||
correctnumber[3] == correctnumber[0]
)
{
correctnumber[3] = rand.Next(0, 9);
}
#endregion
textBox5.Text += "开始游戏..." + "\r\n";
textBox5.Text += "提示信息..." + "\r\n";
for (int i = 0; i < correctnumber.Length; i++)
{
textBox6.Text += correctnumber[i].ToString();
}
_beginflag = true;
BeginButton.Enabled = false;
}
private void GuessButton_Click(object sender, EventArgs e)
{
int allcorrectcount = 0;
int numbercorrectcount = 0;
if (_beginflag == false)
{
MessageBox.Show("游戏尚未开始!");
}
else if(textBox1.Text == textBox2.Text ||textBox1.Text == textBox3.Text ||textBox1.Text == textBox4.Text ||
textBox2.Text == textBox3.Text ||textBox2.Text == textBox4.Text ||
textBox3.Text == textBox4.Text )
{
MessageBox.Show("请输入不重复的四个数字且不能为空!");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
else if (textBox1.Text == "" ||
textBox2.Text == "" ||
textBox3.Text == "" ||
textBox4.Text == "")
{
MessageBox.Show("各个数字均不能为空!");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
else
{
if (textBox1.Text == correctnumber[0].ToString() &&
textBox2.Text == correctnumber[1].ToString() &&
textBox3.Text == correctnumber[2].ToString() &&
textBox4.Text == correctnumber[3].ToString())
{
MessageBox.Show("恭喜你猜对了!");
_guesstimes = 0;
}
for (int i = 0; i < 4; i++)
{
if (textBox1.Text == correctnumber[i].ToString())
{
if (i == 0)
{
allcorrectcount += 1;
}
else
{
numbercorrectcount += 1;
}
}
}
for (int i = 0; i < 4; i++)
{
if (textBox2.Text == correctnumber[i].ToString())
{
if (i == 1)
{
allcorrectcount += 1;
}
else
{
numbercorrectcount += 1;
}
}
}
for (int i = 0; i < 4; i++)
{
if (textBox3.Text == correctnumber[i].ToString())
{
if (i == 2)
{
allcorrectcount += 1;
}
else
{
numbercorrectcount += 1;
}
}
}
for (int i = 0; i < 4; i++)
{
if (textBox4.Text == correctnumber[i].ToString())
{
if (i == 3)
{
allcorrectcount += 1;
}
else
{
numbercorrectcount += 1;
}
}
}
textBox5.Text += "本次有" + allcorrectcount + "个数字和位置都正确,有" + numbercorrectcount + "个数字正确,位置不正确。你还有" + (9 - _guesstimes) + "次机会!" + "\r\n";
_guesstimes++;
if (allcorrectcount == 4)
{
textBox5.Text += "恭喜你,你使用了" + _guesstimes + "次就猜出来了!";
}
}
}
private void ClearButton_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
}
private void TryAgainButton_Click(object sender, EventArgs e)
{
textBox5.Text = "";
_guesstimes = 0;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
#region 生成随机数
Random rand = new Random();
correctnumber[0] = rand.Next(0, 9);
correctnumber[1] = rand.Next(0, 9);
correctnumber[2] = rand.Next(0, 9);
correctnumber[3] = rand.Next(0, 9);
while (correctnumber[0] == correctnumber[1] ||
correctnumber[0] == correctnumber[2] ||
correctnumber[0] == correctnumber[3]
)
{
correctnumber[0] = rand.Next(0, 9);
}
while (correctnumber[1] == correctnumber[0] ||
correctnumber[1] == correctnumber[2] ||
correctnumber[1] == correctnumber[3]
)
{
correctnumber[1] = rand.Next(0, 9);
}
while (correctnumber[2] == correctnumber[0] ||
correctnumber[2] == correctnumber[1] ||
correctnumber[2] == correctnumber[3]
)
{
correctnumber[2] = rand.Next(0, 9);
}
while (correctnumber[3] == correctnumber[1] ||
correctnumber[3] == correctnumber[2] ||
correctnumber[3] == correctnumber[0]
)
{
correctnumber[3] = rand.Next(0, 9);
}
#endregion
textBox5.Text += "\r\n" + "开始游戏..." + "\r\n";
textBox5.Text += "提示信息..." + "\r\n";
for (int i = 0; i < correctnumber.Length; i++)
{
textBox6.Text += correctnumber[i].ToString();
}
_beginflag = true;
BeginButton.Enabled = false;
}
}
}
namespace 猜数字
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.GuessButton = new System.Windows.Forms.Button();
this.ClearButton = new System.Windows.Forms.Button();
this.TryAgainButton = new System.Windows.Forms.Button();
this.BeginButton = new System.Windows.Forms.Button();
this.textBox6 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("黑体", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label1.ForeColor = System.Drawing.Color.Green;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(216, 29);
this.label1.TabIndex = 0;
this.label1.Text = "数字猜猜猜游戏";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
this.label2.Location = new System.Drawing.Point(12, 79);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(216, 48);
this.label2.TabIndex = 1;
this.label2.Text = "请在下面的输入框中输入一个\r\n包含数字0-9的不要重复的4位\r\n数:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(50, 141);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(21, 21);
this.textBox1.TabIndex = 2;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(77, 141);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(21, 21);
this.textBox2.TabIndex = 2;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(104, 141);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(21, 21);
this.textBox3.TabIndex = 2;
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(131, 141);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(21, 21);
this.textBox4.TabIndex = 2;
//
// textBox5
//
this.textBox5.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
this.textBox5.Font = new System.Drawing.Font("楷体_GB2312", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.textBox5.ForeColor = System.Drawing.Color.Red;
this.textBox5.Location = new System.Drawing.Point(234, 73);
this.textBox5.Multiline = true;
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(497, 289);
this.textBox5.TabIndex = 3;
//
// GuessButton
//
this.GuessButton.Location = new System.Drawing.Point(32, 221);
this.GuessButton.Name = "GuessButton";
this.GuessButton.Size = new System.Drawing.Size(154, 43);
this.GuessButton.TabIndex = 4;
this.GuessButton.Text = "猜一猜";
this.GuessButton.UseVisualStyleBackColor = true;
this.GuessButton.Click += new System.EventHandler(this.GuessButton_Click);
//
// ClearButton
//
this.ClearButton.Location = new System.Drawing.Point(32, 270);
this.ClearButton.Name = "ClearButton";
this.ClearButton.Size = new System.Drawing.Size(154, 43);
this.ClearButton.TabIndex = 4;
this.ClearButton.Text = "清 空";
this.ClearButton.UseVisualStyleBackColor = true;
this.ClearButton.Click += new System.EventHandler(this.ClearButton_Click);
//
// TryAgainButton
//
this.TryAgainButton.Location = new System.Drawing.Point(32, 319);
this.TryAgainButton.Name = "TryAgainButton";
this.TryAgainButton.Size = new System.Drawing.Size(154, 43);
this.TryAgainButton.TabIndex = 4;
this.TryAgainButton.Text = "再试一次";
this.TryAgainButton.UseVisualStyleBackColor = true;
this.TryAgainButton.Click += new System.EventHandler(this.TryAgainButton_Click);
//
// BeginButton
//
this.BeginButton.Location = new System.Drawing.Point(32, 172);
this.BeginButton.Name = "BeginButton";
this.BeginButton.Size = new System.Drawing.Size(154, 43);
this.BeginButton.TabIndex = 4;
this.BeginButton.Text = "开 始";
this.BeginButton.UseVisualStyleBackColor = true;
this.BeginButton.Click += new System.EventHandler(this.BeginButton_Click);
//
// textBox6
//
this.textBox6.Location = new System.Drawing.Point(267, 17);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(115, 21);
this.textBox6.TabIndex = 5;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(743, 376);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.TryAgainButton);
this.Controls.Add(this.ClearButton);
this.Controls.Add(this.BeginButton);
this.Controls.Add(this.GuessButton);
this.Controls.Add(this.textBox5);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "猜数字游戏";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button GuessButton;
private System.Windows.Forms.Button ClearButton;
private System.Windows.Forms.Button TryAgainButton;
private System.Windows.Forms.Button BeginButton;
private System.Windows.Forms.TextBox textBox6;
}
}
这次实现的是猜数字的游戏,用户输入4个不重复的数字,系统具有指示信息,缺点:交互不好,友好性不好。