📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SuperMind
{
public partial class MainBoard : Form
{
//private System.Windows.Forms.Button btnTry;
//private System.Windows.Forms.TextBox txtPlayerSeq;
//private System.Windows.Forms.ListView lvwResults;
private byte[] randomSeq;
public MainBoard()
{
InitializeComponent();
}
//Restart the game
private void RestartGame()
{
//generate a new random sequence
GenerateRandomSequence();
//clear the results list
lvwResults.Items.Clear();
//reset the number of attempts
lblAttempts.Text="0 attempts";
//empty the input textbox,and give it the focus
txtPlayerSeq.Text = "";
txtPlayerSeq.Focus();
}
//Generates a new random sequence for the randomSeq arry
private void GenerateRandomSequence()
{
//reset the current array
randomSeq = new byte[4];
Random rndGen=new Random();
int rndNum;
//fill the array with new random values
for(int i=0;i<4;i++)
{
//continue getting a random value until we find one that is not already
//already used in another array slot
do
{rndNum=rndGen.Next(10);}
while(Array.IndexOf(randomSeq,(byte)rndNum)>-1);
//set the new random value in the i-th array slot
randomSeq[i]=(byte)rndNum;
}
}
//Checks the validity of the player input.Returns true if it's ok
private bool CheckInputValidity()
{
//check if the input sequence is 4 chars long
if(txtPlayerSeq.Text.Length<4)
{
MessageBox.Show("请输入一个4位数字!",
"Invalid sequence",MessageBoxButtons.OK,MessageBoxIcon.Error);
return false;
}
byte[] playerSeq=GetPlayerSequence();
//check whether there are repeated ditgits
if(playerSeq[0]==playerSeq[1]||playerSeq[0]==playerSeq[2]||
playerSeq[0]==playerSeq[3]||playerSeq[1]==playerSeq[2]||
playerSeq[1]==playerSeq[3]||playerSeq[2]==playerSeq[3])
{
MessageBox.Show("不可以使用相同的数字!"+"请改正!",
"Invalid sequence",MessageBoxButtons.OK,MessageBoxIcon.Error);
return false;
}
return true;
}
//Return an array with the input digits
private byte[] GetPlayerSequence()
{
byte[] playerSeq=new byte[4];
//extract the single values
playerSeq[0]=byte.Parse(txtPlayerSeq.Text[0].ToString());
playerSeq[1]=byte.Parse(txtPlayerSeq.Text[1].ToString());
playerSeq[2]=byte.Parse(txtPlayerSeq.Text[2].ToString());
playerSeq[3]=byte.Parse(txtPlayerSeq.Text[3].ToString());
return playerSeq;
}
//process the player input
private void ProcessPlayerInput()
{
//first check the player input,and if it is not valid then return
//(which in a void returning)
if(CheckInputValidity()==false)
return;
string result="";
byte[] playerSeq=GetPlayerSequence();
for(int i=0;i<4;i++)
{
//if the i-th value in the player array matches the i-th value
//of the secret sequence,add a "+" to the result string
if(playerSeq[i]==randomSeq[i])
result +="+";
//else if the i-th value int player array matches a value
//in the secret sequence,but in a wrong position,add a "-"
else if(Array.IndexOf(randomSeq,playerSeq[i])>-1)
result +="-";
}
//if the result is 4 "+",the input sequence is correct
if(result=="++++")
{
//in that case,display a congratulations message
MessageBox.Show("猜对了!\n\n...您用了"+(lvwResults.Items.Count+1).ToString()+"次!",
"恭喜你!",
MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//start a new game
RestartGame();
}
else //otherwise
{
//add the player sequence and its result to the list
AddResultItem(txtPlayerSeq.Text,result);
//reset the txtPlayerSeq textbox and give it th focus
txtPlayerSeq.Text="";
txtPlayerSeq.Focus();
}
}
//Adds the specified input string and result to the lvwResults ListView
private void AddResultItem(string input,string result)
{
//create a new ListViewItem and add it to the lvwResult's Items collection
ListViewItem li=new ListViewItem(new string[]{input,result});
lvwResults.Items.Add(li);
//ensure that the new item is visible
li.Selected=true;
li.EnsureVisible();
//update the number of attempts
lblAttempts.Text=lvwResults.Items.Count.ToString()+"attempts";
}
private void MainBoard_Load(object sender, EventArgs e)
{
RestartGame();
}
private void btnTry_Click(object sender, EventArgs e)
{
ProcessPlayerInput();
}
private void txtPlyerSeq_KeyPress(object sender, KeyPressEventArgs e)
{
//ignor all keys except digits and the backspace
if((e.KeyChar>='0'&&e.KeyChar<='9')||
(e.KeyChar==(char)((int)Keys.Back)))
e.Handled=false;
else
e.Handled=true;
//if the user pressed Enter,process the input
if(e.KeyChar==(char)((int)Keys.Enter))
ProcessPlayerInput();
}
private void restartToolStripMenuItem_Click(object sender, EventArgs e)
{
//ask the player if he/she wants to restart the game
DialogResult ret=MessageBox.Show(
"确定要重新快是吗?",
"重玩",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
//if the user chose Yes,restart the game
if(ret==DialogResult.Yes)
RestartGame();
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
About aboutForm=new About();
aboutForm.ShowDialog();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
Update UpdateData = new Update();
UpdateData.ShowDialog();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -