使用 C# 2008 Express Edition 编写的猜数字游戏

刚刚下来c#2008 Express Edition,想起以前用js编写的猜数字游戏是比较简单的一个小例子,正好拿来转成c#版的当下练习。

点击这里下载编译好的EXE/Files/s1ihome/GuessNumber.rar

代码主要部分如下:

using  System;
using  System.Collections.Generic;
using  System.Windows.Forms;

namespace  GuessNumber
{
    
public partial class frmMain : Form
    
{

        
string theNumber = string.Empty;
        
int tryCount = 0//共输入几次

        
public frmMain()
        
{
            InitializeComponent();
        }



        
private void frmMain_Load(object sender, EventArgs e)
        
{
            Init();
        }


        
private void GenRndNumber()
        
{
            List
<int> list = new List<int>();
            list.Clear();
            
for (int i = 0; i < 10; i++) list.Insert(i, i);

            
int rndNo;
            Random rnd 
= new Random();
            
for (int i = 0; i < 4; i++)
            
{
                rndNo 
= rnd.Next(10 - i);
                theNumber 
+= list[rndNo].ToString();
                list.RemoveAt(rndNo);
            }

        }


        
private void WinReset()
        
{
            txtNumber.ReadOnly 
= true;
            btnShowNumber.Text 
= "点击进行下一轮";
            lblResult.Text 
= "尝试了" + tryCount + "次之后你终于成功了!\n请点击按钮下一轮了。";
        }

        
private void Init()
        
{
            theNumber 
= string.Empty;
            tryCount 
= 0;
            txtNumber.ReadOnly 
= false;
            txtNumber.Text 
= "";
            lblTryCount.Text 
= " 0 次";
            GenRndNumber();
            lblTipNumber.Visible 
= false;
            lblResult.Text 
= "请在上面的输入框中输入4位不重复数字";
            btnShowNumber.Text 
= "显示正确数字";
            lstInputNumberHistory.Items.Clear();
        }

        
private void txtNumber_TextChanged(object sender, EventArgs e)
        
{
            
string t = txtNumber.Text;
            
if (t.Length < 4return;
            tryCount
++;
            txtNumber.Select(
04);
            lblTryCount.Text 
= tryCount.ToString() + "";

            
int a = 0;//猜的数字位置和大小都正确
            int b = 0;//大小正确,位置错误
            string ni, ne;
            
if (t == theNumber) { WinReset(); return; }

            
for (int i = 0; i < 4; i++)
            
{
                ni 
= theNumber.Substring(i, 1);
                
for (int j = 0; j < 4; j++)
                
{
                    ne 
= t.Substring(j, 1);
                    
if (i == j && ni == ne) a++;
                    
else if (ni == ne) b++;
                }

            }

            
string result = a.ToString() + " A " + b.ToString() + " B ";
            lblResult.Text 
= result;
            lstInputNumberHistory.Items.Insert(
0,tryCount.ToString() + "   " + t + "     " + result);
        }


        
private void btnShowNumber_Click(object sender, EventArgs e)
        
{
            
if (btnShowNumber.Text == "显示正确数字")
            
{
                lblTipNumber.Text 
= "正确数字:" + theNumber;
                lblTipNumber.Visible 
= true;
            }

            
else//重新开始下一轮
            {
                Init();
            }

        }

    }

}


转载于:https://www.cnblogs.com/s1ihome/archive/2008/04/01/GuessNumber.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值