用C#模仿文曲星上的猜数字游戏

       文曲星上的猜数字游戏实现起来比较简单。下面这个例子中,生成number时用集合list提高效率,验证输入是否合法和验证结果正确用hashtable提高效率。相比用数组循环来说会好了很多。

      

private   static  Hashtable hstableCheckSame  =   new  Hashtable();   // 用来检查输入答案是否有重复的数字
         private   static  Hashtable hstableAnswer  =   new  Hashtable();     // 用来存放随机生成的结果
         private   const   int  INT_TRY_TIMES  =   10 ;

        
public   static  Hashtable HstableAnswer
        
{
            
get return hstableAnswer; }
        }


        
public   static   void  Main( string [] args)
        
{
            Console.WriteLine(
"***************** Game Guess Number ***************** ");
            Console.WriteLine(
"The result will be given in the way of "2A1B".");
            Console.WriteLine(
""A" means the value and position both are correct.");
            Console.WriteLine(
""B" means the value is correct but the position is not.");
            Console.WriteLine(
"You have total " + INT_TRY_TIMES + " times to guess the number.");
            Console.WriteLine(
" ***************************************************** ");

            
while (true)
            
{
                Console.WriteLine(
"Enter "start" to begin the game or "end" to exit.");
                
switch (Console.ReadLine().ToLower())
                
{
                    
case "start":
                        
if (BeginGame() == false)
                        
{
                            
goto End;
                        }

                        
break;
                    
case "end":
                        
goto End;
                    
default:
                        
break;
                }

            }


        End:
            Console.WriteLine(
" *************  End  ****************");
        }


        
private   static   bool  BeginGame()
        
{
            
//随机生成一个结果
            string strAnswer = Generate();

            
for (int i = 0; i < INT_TRY_TIMES; i++)
            
{
                System.Console.Write(
"Please input your guess (or enter "end" to end the game):");
                
string strInput = System.Console.ReadLine();

                
//判断是否要退出游戏
                if (strInput.Trim().ToLower() == "end")
                
{
                    
return false;
                }


                
//检查输入是否合法
                if (CheckValid(strInput) == false)
                
{
                    i
--;
                    
continue;
                }


                
//检查输入的答案是否正确
                if (CheckInput(strInput, strAnswer) == true)
                
{
                    System.Console.WriteLine(
"  >>> Well done.Congratulations! ");
                    
break;
                }

                
else
                
{
                    
if (i >= INT_TRY_TIMES - 1)
                    
{
                        System.Console.WriteLine(
"  >>> You lose.The answer is " + strAnswer + " ");
                    }

                    
else
                    
{
                        System.Console.WriteLine(
"    Chances left: " + (INT_TRY_TIMES - 1 - i) + ". Good luck!  ");
                    }

                }

            }


            
return true;
        }


        
private   static   string  Generate()
        
{
            
string strNumberGenerated = string.Empty;

            
//初始化数字列表
            List<string> numberList = new List<string>();
            
for (int intNum = 0; intNum < 10; intNum++)
            
{
                numberList.Add(intNum.ToString());
            }


            
//随机生成一个结果
            Random random = new Random(DateTime.Now.Millisecond);
            hstableAnswer.Clear();
            
for (int i = 0; i < 4; i++)
            
{
                
int intIndex = random.Next(numberList.Count);
                strNumberGenerated 
+= numberList[intIndex];
                hstableAnswer.Add(numberList[intIndex], numberList[intIndex]); 
//添加到hashtable
                numberList.RemoveAt(intIndex);
            }


            
return strNumberGenerated;
        }


        
private   static   bool  CheckInput( string  strInput,  string  strAnswer)
        
{
            
int intValuePosition = 0;  //数值和所处位置都正确的数字个数
            int intValue = 0;    //数值正确但是所处位置不正确的数字个数

            
for (int i = 0; i < 4; i++)
            
{
                
if (strInput[i] == strAnswer[i])
                
{
                    
//数值和所处位置都正确
                    intValuePosition++;
                }

                
else
                
{
                    
//数值正确但是所处位置不正确
                    if (hstableAnswer[strInput[i].ToString()] != null)
                    
{
                        intValue
++;
                    }

                }

            }


            
//输出判断结果
            System.Console.WriteLine("    Result : " + intValuePosition + "A" + intValue + "B");

            
if (intValuePosition >= 4)
            
{
                
return true;
            }


            
return false;
        }


        
private   static   bool  CheckValid( string  strInput)
        
{
            
//检查输入的长度是否合法
            if (strInput.Trim().Length != 4)
            
{
                System.Console.WriteLine(
"  Input Error:Please input 4 digits. ");
                
return false;
            }


            
//检查输入是否数字
            try
            
{
                
int.Parse(strInput);
            }

            
catch
            
{
                System.Console.WriteLine(
"  Input Error:Only digits allowed. ");
                
return false;
            }


            
//检查是否有重复的数字
            hstableCheckSame.Clear();
            
for (int i = 0; i < strInput.Length; i++)
            
{
                
if (hstableCheckSame[strInput[i]] == null)
                
{
                    hstableCheckSame.Add(strInput[i], strInput[i]);
                }

                
else
                
{
                    System.Console.WriteLine(
"  Input Error:Each digit can only be used once");
                    
return false;
                }

            }


            
return true;
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#编写的GUI游戏 源代码 using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace gn { public partial class Form1 : Form { string ready; int counti = 1; public Form1() { InitializeComponent(); this.label1.Hide(); this.button2.Hide(); this.button1.Hide(); this.button3.Hide(); this.button4.Hide(); this.textBox1.Hide(); this.pictureBox1.Hide(); this.pictureBox2.Hide(); } private void button1_Click(object sender, EventArgs e) { string number; int x, y; int count = 0; number = textBox1.Text; if (number.Length != 4) { MessageBox.Show("请输入4位数字"); } else { if (counti <=8) { if (count <= 3) { x = 0; y = 0; for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (number[i] == ready[j]) { if (i == j) ++x; else ++y; } } } label2.Text = "你已经猜过" + counti + "次"; ++count; label3.Text = x.ToString() + "A" + y.ToString() + "B"; if (number == ready) { this.label2.Text = "答对了"; this.pictureBox1.Show(); } } if (counti == 8) { this.button1.Enabled = false; this.pictureBox2.Show(); } } else { this.button1.Enabled = false; this.pictureBox2.Show(); } ++counti; } } private void button2_Click(object sender, EventArgs e) { MessageBox.Show(ready); } private void button3_Click(object sender, EventArgs e) { //this.button2.Hide(); this.textBox1.Text = ""; this.label3.Text = ""; this.label2.Text = ""; this.button1.Enabled = true; counti = 1; this.pictureBox1.Hide(); this.pictureBox2.Hide(); Random r = new Random(); string[] a; a = new string[8]; a[0] = "1263"; a[1] = "6598"; a[2] = "9654"; a[3] = "5986"; a[4] = "2915"; a[5] = "2046"; a[6] = "9035"; a[7] = "1057"; int z; z = r.Next(8); ready = a[z]; } private void label1_Click(object sender, EventArgs e) { } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void button4_Click(object sender, EventArgs e) { Close(); } private void button5_Click(object sender, EventArgs e) { Random r = new Random(); string[] a; a = new string[8]; a[0] = "1263"; a[1] = "6598"; a[2] = "9654"; a[3] = "5986"; a[4] = "2915"; a[5] = "2046"; a[6] = "9035"; a[7] = "1057"; int z; z = r.Next(8); ready = a[z]; this.textBox1.Show(); this.button1.Show(); this.button3.Show(); this.button4.Show(); this.label1.Show(); this.button2.Show(); this.button5.Hide(); } private void label4_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void pictureBox1_Click_1(object sender, EventArgs e) { } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值