| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1107 人关注过本帖
标题:Number Guessing Game
只看楼主 加入收藏
hn626589
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-7-25
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:6 
Number Guessing Game
这个猜数字游戏规则是这样:电脑随机产生一个4位数。用户从TEXT BOX  里 输入。
如果对:显示"0"    如果错:显示"X"  如果位置不对:显示:"P"
我已经编了大部分,现在遇到一个问题,希望能尽快解决。
电脑随机产生1843(只是举例),而我键入8888. ListBox 里面显示 的 是:P 0 P P.而我想要的是:X 0 X X
我的意思是我的if statement  并不符合游戏规则,会误导游戏者。显示P的话,意思是位置错了,但是我能确定这个数字存在。所以出现P不符合规则。
我的意思是我想不出来要怎么应用 if  statement。红色部分要修改了,重写,或者添加什么能解决问题就可以。
不过要理解。
现把我的code 粘上:
namespace Number_Guessing_Game
{
    public partial class Frm090076EHuangZinan : Form
    {
        Random ram = new Random();
        int intRandom;
        int intNumberOfGuessings = 0;
        private System.Media.SoundPlayer mediaPlayer = new System.Media.SoundPlayer();
        /*Learn from Learn Visual C# Lesson 2*/

        //initialize the Random Number at the beginning
        //set the intNumberOfguessings to 0 so that it won't increase after u click btnGo each time
        public Frm090076EHuangZinan()
        {

            InitializeComponent();
            
            intRandom = ram.Next(0,9999);
            MessageBox.Show("You have 5 times to try.Good Luck!","Information",
                MessageBoxButtons.OK,MessageBoxIcon.Information);
            mediaPlayer.Stream = Properties.Resources.Kiss_the_rain;
            mediaPlayer.PlayLooping();
            windowsFormsTimer.Enabled = true;

        }



        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
            intNumberOfGuessings++;
         
               
                /*Set a intCount so that I can show the times of guessing when u get the correct Number
                At the same time I only one cloum of titles only when intCount==1*/

                int intE1;
                int intE2;
                int intE3;
                int intE4;
                int intEnterNumbers;
                //declare variables of EnterNumbers

                int intR1;
                int intR2;
                int intR3;
                int intR4;
                //declare variables of RandomNumbers


                string string1 = "W";
                string string2 = "W";
                string string3 = "W";
                string string4 = "W";
                //declare variables used in listbox
               
                intEnterNumbers = Int32.Parse(txtEnterNumbers.Text);
                txtEnterNumbers.Clear();
                txtEnterNumbers.Focus();
                //Clear the txtbox after every time u entered

                lblDisplay.Text = Convert.ToString(intRandom);
                /*I set lblDisplay's visible property to invisible.
                  But this label display me the answer to check whether my application works*/


                if (intEnterNumbers > 9999)/*Another if condition also can work:
                                            if(intEnterNumbers/1000>=10)
                                            Like:10000/1000=10
                                                 11111/1000=11*/
                {
                    MessageBox.Show("Fuck u,enter 4-digit Number lar", "Exclamation",
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    intE1 = intEnterNumbers / 1000;
                    intE2 = intEnterNumbers / 100 % 10;
                    intE3 = intEnterNumbers / 10 % 10;
                    intE4 = intEnterNumbers % 10;
                    //Breaking the EnterNumbers into 4 seperate Numbers

                    intR1 = intRandom / 1000;
                    intR2 = intRandom / 100 % 10;
                    intR3 = intRandom / 10 % 10;
                    intR4 = intRandom % 10;
                    //Breaking the RandomNumbers into 4 seperate Numbers



                    if (intNumberOfGuessings > 5)/*Limit the Numbers of Guessings up to 8 times*/
                    {
                        MessageBox.Show("Lol,Dump donkey,u fail.Press OK Button to Restart", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Application.Restart();
                    }
                    else   /*(intNumbersOfGuessings<=5&&intNumbersOfGuessings>=0)*/
                    {


                        if (intE1 == intR1)
                        {
                            string1 = "0";
                        }
                        else if (intE1 == intR2 || intE1 == intR3 || intE1 == intR4)
                        {
                            string1 = "P";
                        }
                        else   //(intE1!=intR1)
                        {
                            string1 = "X";
                        }

                        if (intE2 == intR2)
                        {
                            string2 = "0";

                        }
                        else if (intE2 == intR1 || intE2 == intR3 || intE2 == intR4)
                        {
                            string2 = "P";
                        }
                        else   //(intE2!=intR2)
                        {
                            string2 = "X";
                        }

                        if (intE3 == intR3)
                        {
                            string3 = "0";
                        }
                        else if (intE3 == intR1 || intE3 == intR2 || intE3 == intR4)
                        {
                            string3 = "P";
                        }
                        else   //(intE3!=intR3)
                        {
                            string3 = "X";
                        }

                        if (intE4 == intR4)
                        {
                            string4 = "0";
                        }
                        else if (intE4 == intR1 || intE4 == intR2 || intE4 == intR3)
                        {
                            string4 = "P";
                        }
                        else   //(intE4!=intR4)
                        {
                            string4 = "X";
                        }

                        if (listBox1.Items.Count==0)  /*I learn this way in class average:
                                                       if(listBox1.Items.Count==0)
                                                       {
                                                         listBox1.Items.Add("Number Entered:" + "\t" + "1st" + "\t" + "2nd" + "\t" + "3rd" + "\t" + "4th");              
                                                       }
                                                       I find this way better:
                                                       because when you use if(intNumberOfGuessings==1)....
                                                       when you first type Non-number or Blank,
                                                       the intNumberOfGuessings also become 1,then next time
                                                       you click,listBox1 wont have the first title row.
                                                       */
                        {
                            listBox1.Items.Add("Number Entered:" + "\t" + "1st" + "\t" + "2nd" + "\t" + "3rd" + "\t" + "4th");
                            listBox1.Items.Add(intEnterNumbers + "==>" + "\t" + string1 + "\t" + string2 + "\t" + string3 + "\t" + string4);
                        }
                        else     //(intNumberOfGuessings>1)
                        {
                            listBox1.Items.Add(intEnterNumbers + "==>" + "\t" + string1 + "\t" + string2 + "\t" + string3 + "\t" + string4);

                        }
                        if (intE1 == intR1 &&
                          intE2 == intR2 &&
                          intE3 == intR3 &&
                          intE4 == intR4)
                        {
                            lblRandom1.Text = Convert.ToString(intR1);
                            lblRandom2.Text = Convert.ToString(intR2);
                            lblRandom3.Text = Convert.ToString(intR3);
                            lblRandom4.Text = Convert.ToString(intR4);
                            /*Only Display the Random Number initialise at the end*/

                           
                            MessageBox.Show("Congratulations!!Press OK Button to Restart.Numbers of Guessing:" + Convert.ToString(intNumberOfGuessings),
                                "Congratulation!!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            windowsFormsTimer.Enabled = false;
                            Application.Restart(); ;
                           
                        }
                        
                    }
                }
            }



            catch
            {
                MessageBox.Show("PLEASE DO NOT ENTER NON-INTEGER OR LEAVE IT BLANK");
                intNumberOfGuessings = 0;        /*I put a restart here because when you click go button
                                               intNumbersOfGuessings also increse 1,so that's something
                                               wrong.We only need to increase 1 in intNumbersOfGuessings
                                               when you type in a 4-digit Number
                                                  *
                                               Before Method: Application.Restart();
                                                  *
                                              */
                 
            }

        }

        private void Frm090076EHuangZinan_Load(object sender, EventArgs e)
        {
            lblWinTimer.Text = System.DateTime.Now.ToLongTimeString();
        }

        private void windowsFormsTimer_Tick(object sender, EventArgs e)
        {
            lblWinTimer.Text = System.DateTime.Now.ToLongTimeString();
        }
红字的部分是我觉得要重写的地方,或着添加什么能解决问题就好。不过我要理解才行。。。
谢谢各路高手。
      

      
      

      

        

      
        

        
   
    }
    }
搜索更多相关主题的帖子: Guessing Number Game 
2009-07-25 01:43
乖老鼠
Rank: 5Rank: 5
来 自:四川省
等 级:职业侠客
威 望:2
帖 子:434
专家分:394
注 册:2008-9-8
收藏
得分:0 
什么叫位置不对?不能理解

转眼就从编程菜鸟混成了半灌水
2009-07-25 08:40
ljc_zy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:56
专家分:131
注 册:2009-7-14
收藏
得分:0 
这段代码写的可读性太差。惨不忍睹。

恋恋花事 www.
2009-07-25 14:21
名沁
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:132
专家分:173
注 册:2009-4-5
收藏
得分:40 
你的程序问题出现在重复问题没有处理好
首先猜数字一般是不能有重复的
因为有重复的话,就很难判断,出现你的这种情况就是因为你没有限制重复
从结果来看,你的程序逻辑是没有什么大问题。
你看,8888,因为原来的数是1843,第一个8程序判断时,就认为有个8但是不在第一位。
同理所以结果是POPP,而不是你想象中的XOXX。
如果原来的数字也有重复如1848,结果就会XOXO。
这样难度会很大。
所以建议,在写这个程序时,先限定MOD生成的4位数不能有重复的数字
其次,输入时不能有重复。
这样的话就不会出现你说的情况了
PS:
程序没有测试,也没有看完,因为读有点难度
2009-07-25 16:05
名沁
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:132
专家分:173
注 册:2009-4-5
收藏
得分:0 
我想你最好不要用
intRandom = ram.Next(0,9999);
这样不好处理的
你试一下分成4个部分
2009-07-25 16:08
baikil
Rank: 10Rank: 10Rank: 10
来 自:X星球
等 级:青峰侠
威 望:4
帖 子:282
专家分:1580
注 册:2009-7-20
收藏
得分:60 
这个猜数字游戏规则是这样:
电脑随机产生一个4位数。
如果对:显示"0"    如果错:显示"X"  如果位置不对:显示:"P"

说这么说的我的头都大了, " 如果错:显示"X"  如果位置不对:显示:"P" "
位置不对不就是错了吗?????

同学把问题复杂化了!其实很简单的...
建意重新编码..
步骤为:
1.要生成四位数吗--->0-9999是不一定可以生成4位数的,改为1000-9999就是一个4位数
或一位一位生成, a=(0-9) b=(0-9) c=(0-9) d=(0-9) 四位---简单吧!!
2.对输入的数据的一个判断-->先判断输入的长度是不是四位,
a.不是--->全错.
b.是----> 对每个字符的判断,对的位为"O"表示,不对的用"X"表示
3.输出结果.完成

代码可参考:
//生成四位数
                Random rd = new Random();
                int a = rd.Next(9);
                int b = rd.Next(9);
                int c = rd.Next(9);
                int d = rd.Next(9);

                //输入猜的数
                string str = Console.ReadLine();
                while (str.Length != 4)
                {
                    Console.Write("输入的长度不对,请重新输入(XXXX)!  ");
                    str = Console.ReadLine();
                }
                //分解四位
                string a1 = str.Substring(0, 1);//第一位
                string b1 = str.Substring(1, 1);//.....
                string c1 = str.Substring(2, 1);//.....
                string d1 = str.Substring(3, 1);//.....

                //判断四位
                string workout = "";
                int tem = -1;
                if (int.TryParse(a1, out tem))
                    if (tem == a)
                    {
                        workout += "O";
                    }
                    else
                        workout += "X";
                else
                    workout += "X";
                tem = -1;
                if (int.TryParse(b1, out tem))
                    if (tem == b)
                        workout += "O";
                    else
                        workout += "X";
                else
                    workout += "X";
                tem = -1;
                if (int.TryParse(c1, out tem))
                    if (tem == c)
                        workout += "O";
                    else
                        workout += "X";
                else
                    workout += "X";
                tem = -1;
                if (int.TryParse(b1, out tem))
                    if (tem == d)
                        workout += "O";
                    else
                        workout += "X";
                else
                    workout += "X";
                Console.WriteLine("结果为:"+workout);
                Console.WriteLine("生成的数为:"+a.ToString() + b.ToString() + c.ToString() + d.ToString());
                Console.Read();

自创QQ群64885635,方便C#学习交流.希望各项工作路好学都好入!
2009-07-29 12:21
hn626589
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-7-25
收藏
得分:0 
说实话我看不懂。。。因为我才刚学C#
不过还是很感谢。
2009-07-31 11:27
快速回复:Number Guessing Game
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021820 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved