运行效果:
这次添加了Textbox用来显示输入过的值,还有一些messageBox提示。
对代码进行了一些修改
namespace WindowsFormsApp7
{
public partial class Form1 : Form
{
public static Form1 form; //创建一个可以在其他类调用的窗口属性
public int A = 0;//位置正确的个数
public int B = 0;//A+C
public int C = 0;//位置不对但数对的个数
public String[] GuessNum = new String[4];//存放输入的数字
public String[] RightNum = new String[4];//存放正确的数字
public bool start;//定义一个布尔 判别游戏是否开始
public Form1()
{
InitializeComponent();
form = this; //赋值给窗口属性
}
private void Form1_Load(Object sender, EventArgs e)
{
beginGame();
button12.Visible = false;//隐藏 重新开始 按钮
button13.Visible = false;//隐藏 确定 按钮
button16.Visible = false;//隐藏 谜底 按钮
}
private void beginGame()
{
//首先清空数组
for(int i=0; i<4;i++)
{
GuessNum[i] = null;
}
Class1.display(GuessNum);
}
//开始游戏
private void button11_Click(Object sender, EventArgs e)
{
start = true; //游戏开始
beginGame();
button12.Visible = true;//显示 重新开始 按钮
button13.Visible = true;//显示 确定 按钮
button16.Visible = true;//显示 谜底 按钮
button11.Visible = false;//隐藏 开始游戏 按钮
Class1.random4(RightNum); //生成四个随机数
Class1.display(GuessNum);//调用显示函数
}
//重新开始
private void button12_Click(Object sender, EventArgs e)
{
beginGame();
button11_Click(sender, e); //调用开始游戏函数
textBox1.Text = null;
}
//确定
private void button13_Click(Object sender, EventArgs e)
{
//判断是否有相同的数字
if ((GuessNum[0] == GuessNum[1]) || (GuessNum[0] == GuessNum[2]) ||
(GuessNum[0] == GuessNum[3]) || (GuessNum[1] == GuessNum[2]) ||
(GuessNum[1] == GuessNum[3]) || (GuessNum[2] == GuessNum[3]))
{
MessageBox.Show("请勿输入相同的数字");
}
//是否输入了四位数
else if((GuessNum[0]==null)||(GuessNum[1]==null)||(GuessNum[2]==null)||(GuessNum[3]==null))
{
MessageBox.Show("请输入完整的数字");
}
else
{
//调用判断函数
Class1.judgement(GuessNum, RightNum, A, B, C);
beginGame();
}
}
//退出游戏
private void button14_Click(Object sender, EventArgs e)
{
Application.Exit();//程序退出
}
//删除
private void button15_Click(Object sender, EventArgs e)
{
if (GuessNum[0] == null)
{
return;//整体函数结束
}
for(int i=GuessNum.Length-1;i>=0;i--)
{
if(GuessNum[i]==null)
{
continue;//跳出本循环
}
GuessNum[i] = null; //删除数字
Class1.display(GuessNum);//调用显示函数
break;//结束
}
}
//添加数字
private void button_Click(Object sender, EventArgs e)
{
if (start == true)
{
Class1.AddNum(GuessNum, ((Button)sender).Text);//获取button的text并传入
}
else
{
MessageBox.Show("请先开始游戏");
}
}
private void button16_Click(Object sender, EventArgs e)
{
Class1.display(RightNum);
}
private void button17_Click(Object sender, EventArgs e)
{
MessageBox.Show("如正确答案为 5234,而猜的人猜 5346,则是 1A2B,其中有一个5的位置对了," +
"记为1A,而3和4这两个数字对了,而位置没对,因此记为 2B,合起来就是 1A2B。");
}
}
}
定义了一个类
namespace WindowsFormsApp7
{
class Class1
{
//显示数字方法
public static void display(String[] RightNum)
{
//把传进来的值Form1 中的label
Form1.form.label1.Text = RightNum[0];//个位
Form1.form.label2.Text = RightNum[1];//十位
Form1.form.label3.Text = RightNum[2];//百位
Form1.form.label4.Text = RightNum[3];//千位
}
//写数字
public static void AddNum(String[] GuessNum, String wei)
{
if (GuessNum[3] != null)
{
return; //如果第三位为不为null 跳出次函数
}
for (int i = 0; i < GuessNum.Length; i++) //循环赋值
{
if (GuessNum[i] == null)
{
GuessNum[i] = wei; //获取传入的label的text
display(GuessNum);
break;
}
}
}
//生成四个不重复的0—9
public static void random4(String[] RightNum)
{
//有重复的用goto重复执行
ran:
Random num = new Random();//生成四个随机数
for (int i = 0; i < 4; i++)
{
string ran = num.Next(0,9).ToString();
RightNum[i] = ran;
}
if ((RightNum[0] == RightNum[1])||(RightNum[0] == RightNum[2]) ||(RightNum[0] == RightNum[3]) ||(RightNum[1] == RightNum[2])||(RightNum[1] == RightNum[3]) ||(RightNum[2] == RightNum[3]) )
{
goto ran;//判断是否存在重复的,一个个比较,这个方法比较有局限
}
else
{
return;//结束本函数
}
}
// 判断几A几B
public static void judgement(string []GuessNum,String[] RightNum,int A,int B,int C)
{
for(int i=0;i<4;i++)
{
if(GuessNum[i]==RightNum[i])
{
A++; //正确的数字和猜的数字按位比较,输出A为位置正确的数字
}
for(int j=0;j<4;j++)
{
if(GuessNum[i]==RightNum[j])//正确的数字每位都和猜的数字每位比较 一共16次
{
B++; //B表示位正确的数字和位置不对但数对的和
}
}
}
C = B - A;//位置不对但数对的个数
if(C<=0)//C可能存在负数的情况
{
C = 0;
}
Form1.form.textBox1.Text += (Form1.form.label1.Text + " " + Form1.form.label2.Text + " " +
Form1.form.label3.Text + " " + Form1.form.label4.Text + " " + A + " " + "A" + " " + C + " " + "B" + "\r\n").ToString();
}
}
}