| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1107 人关注过本帖
标题:Number Guessing Game
取消只看楼主 加入收藏
hn626589
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2009-7-25
结帖率:100%
收藏
已结贴  问题点数:100 回复次数:1 
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
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.019254 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved