c猜数游戏代码.docx
《c猜数游戏代码.docx》由会员分享,可在线阅读,更多相关《c猜数游戏代码.docx(18页珍藏版)》请在冰豆网上搜索。
c猜数游戏代码
c#猜数游戏代码
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
namespace猜数字
{
publicpartialclassForm1:
Form
{
publicForm1()
{
InitializeComponent();
}
privatestaticbool_beginflag;
privatestaticint_guesstimes=1;
publicint[]correctnumber=newint[4];
publicvoidBeginButton_Click(objectsender,EventArgse)
{
_guesstimes=0;
#region生成随机数
Randomrand=newRandom();
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(inti=0;i<correctnumber.Length;i++)
{
textBox6.Text+=correctnumber[i].ToString();
}
_beginflag=true;
BeginButton.Enabled=false;
}
privatevoidGuessButton_Click(objectsender,EventArgse)
{
intallcorrectcount=0;
intnumbercorrectcount=0;
if(_beginflag==false)
{
MessageBox.Show("游戏尚未开始!
");
}
elseif(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="";
}
elseif(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(inti=0;i<4;i++)
{
if(textBox1.Text==correctnumber[i].ToString())
{
if(i==0)
{
allcorrectcount+=1;
}
else
{
numbercorrectcount+=1;
}
}
}
for(inti=0;i<4;i++)
{
if(textBox2.Text==correctnumber[i].ToString())
{
if(i==1)
{
allcorrectcount+=1;
}
else
{
numbercorrectcount+=1;
}
}
}
for(inti=0;i<4;i++)
{
if(textBox3.Text==correctnumber[i].ToString())
{
if(i==2)
{
allcorrectcount+=1;
}
else
{
numbercorrectcount+=1;
}
}
}
for(inti=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+"次就猜出来了!
";
}
}
}
privatevoidClearButton_Click(objectsender,EventArgse)
{
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
textBox4.Text="";
}
privatevoidTryAgainButton_Click(objectsender,EventArgse)
{
textBox5.Text="";
_guesstimes=0;
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
textBox4.Text="";
#region生成随机数
Randomrand=newRandom();
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(inti=0;i<correctnumber.Length;i++)
{
textBox6.Text+=correctnumber[i].ToString();
}
_beginflag=true;
BeginButton.Enabled=false;
}
}
}
namespace猜数字
{
partialclassForm1
{
///<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.IContainercomponents=null;
///<summary>
///清理所有正在使用的资源。
///</summary>
///<paramname="disposing">如果应释放托管资源,为true;否则为false。
</param>
protectedoverridevoidDispose(booldisposing)
{
if(disposing&&(components!
=null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#regionWindows窗体设计器生成的代码
///<summary>
///设计器支持所需的方法-不要
///使用代码编辑器修改此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.label1=newSystem.Windows.Forms.Label();
this.label2=newSystem.Windows.Forms.Label();
this.textBox1=newSystem.Windows.Forms.TextBox();
this.textBox2=newSystem.Windows.Forms.TextBox();
this.textBox3=newSystem.Windows.Forms.TextBox();
this.textBox4=newSystem.Windows.Forms.TextBox();
this.textBox5=newSystem.Windows.Forms.TextBox();
this.GuessButton=newSystem.Windows.Forms.Button();
this.ClearButton=newSystem.Windows.Forms.Button();
this.TryAgainButton=newSystem.Windows.Forms.Button();
this.BeginButton=newSystem.Windows.Forms.Button();
this.textBox6=newSystem.Windows.Forms.TextBox();
this.SuspendLayout();
//
//label1
//
this.label1.AutoSize=true;
this.label1.Font=newSystem.Drawing.Font("黑体",21.75F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(134)));
this.label1.ForeColor=System.Drawing.Color.Green;
this.label1.Location=newSystem.Drawing.Point(12,9);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(216,29);
this.label1.TabIndex=0;
this.label1.Text="数字猜猜猜游戏";
//
//label2
//
this.label2.AutoSize=true;
this.label2.Font=newSystem.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=newSystem.Drawing.Point(12,79);
this.label2.Name="label2";
this.label2.Size=newSystem.Drawing.Size(216,48);
this.label2.TabIndex=1;
this.label2.Text="请在下面的输入框中输入一个\r\n包含数字0-9的不要重复的4位\r\n数:
";
//
//textBox1
//
this.textBox1.Location=newSystem.Drawing.Point(50,141);
this.textBox1.Name="textBox1";
this.textBox1.Size=newSystem.Drawing.Size(21,21);
this.textBox1.TabIndex=2;
//
//textBox2
//
this.textBox2.Location=newSystem.Drawing.Point(77,141);
this.textBox2.Name="textBox2";
this.textBox2.Size=newSystem.Drawing.Size(21,21);
this.textBox2.TabIndex=2;
//
//textBox3
//
this.textBox3.Location=newSystem.Drawing.Point(104,141);
this.textBox3.Name="textBox3";
this.textBox3.Size=newSystem.Drawing.Size(21,21);
this.textBox3.TabIndex=2;
//
//textBox4
//
this.textBox4.Location=newSystem.Drawing.Point(131,141);
this.textBox4.Name="textBox4";
this.textBox4.Size=newSystem.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=newSystem.Drawing.Font("楷体",10.5F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point,((byte)(134)));
this.textBox5.ForeColor=System.Drawing.Color.Red;
this.textBox5.Location=newSystem.Drawing.Point(234,73);
this.textBox5.Multiline=true;
this.textBox5.Name="textBox5";
this.textBox5.Size=newSystem.Drawing.Size(497,289);
this.textBox5.TabIndex=3;
//
//GuessButton
//
this.GuessButton.Location=newSystem.Drawing.Point(32,221);
this.GuessButton.Name="GuessButton";
this.GuessButton.Size=newSystem.Drawing.Size(154,43);
this.GuessButton.TabIndex=4;
this.GuessButton.Text="猜一猜";
this.GuessButton.UseVisualStyleBackColor=true;
this.GuessButton.Click+=newSystem.EventHandler(this.GuessButton_Click);
//
//ClearButton
//
this.ClearButton.Location=newSystem.Drawing.Point(32,270);
this.ClearButton.Name="ClearButton";
this.ClearButton.Size=newSystem.Drawing.Size(154,43);
this.ClearButton.TabIndex=4;
this.ClearButton.Text="清空";