c猜数游戏代码.docx

上传人:b****1 文档编号:3224066 上传时间:2023-05-05 格式:DOCX 页数:18 大小:16.52KB
下载 相关 举报
c猜数游戏代码.docx_第1页
第1页 / 共18页
c猜数游戏代码.docx_第2页
第2页 / 共18页
c猜数游戏代码.docx_第3页
第3页 / 共18页
c猜数游戏代码.docx_第4页
第4页 / 共18页
c猜数游戏代码.docx_第5页
第5页 / 共18页
c猜数游戏代码.docx_第6页
第6页 / 共18页
c猜数游戏代码.docx_第7页
第7页 / 共18页
c猜数游戏代码.docx_第8页
第8页 / 共18页
c猜数游戏代码.docx_第9页
第9页 / 共18页
c猜数游戏代码.docx_第10页
第10页 / 共18页
c猜数游戏代码.docx_第11页
第11页 / 共18页
c猜数游戏代码.docx_第12页
第12页 / 共18页
c猜数游戏代码.docx_第13页
第13页 / 共18页
c猜数游戏代码.docx_第14页
第14页 / 共18页
c猜数游戏代码.docx_第15页
第15页 / 共18页
c猜数游戏代码.docx_第16页
第16页 / 共18页
c猜数游戏代码.docx_第17页
第17页 / 共18页
c猜数游戏代码.docx_第18页
第18页 / 共18页
亲,该文档总共18页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

c猜数游戏代码.docx

《c猜数游戏代码.docx》由会员分享,可在线阅读,更多相关《c猜数游戏代码.docx(18页珍藏版)》请在冰点文库上搜索。

c猜数游戏代码.docx

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="清空";

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2