zoukankan      html  css  js  c++  java
  • 使用 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();
                }

            }

        }

    }


  • 相关阅读:
    CodeForces 681D Gifts by the List (树上DFS)
    UVa 12342 Tax Calculator (水题,纳税)
    CodeForces 681C Heap Operations (模拟题,优先队列)
    CodeForces 682C Alyona and the Tree (树上DFS)
    CodeForces 682B Alyona and Mex (题意水题)
    CodeForces 682A Alyona and Numbers (水题,数学)
    Virtualizing memory type
    页面跳转
    PHP Misc. 函数
    PHP 5 Math 函数
  • 原文地址:https://www.cnblogs.com/s1ihome/p/GuessNumber.html
Copyright © 2011-2022 走看看