C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,443 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,523 people online right now. Registration is fast and FREE... Join Now!




Help for a number guessing game

 

Help for a number guessing game

L1989

1 Jul, 2009 - 04:42 AM
Post #1

New D.I.C Head
*

Joined: 21 Jun, 2009
Posts: 26

I want to create a number guessing game which:
1. guesses a 4 digit number where smaller numbers are also applicable(using 0 to replace the digits)
2. use "O" to indicate correct, "X" to indicate wrong and "P" to indicate that the number is in the wrong place

the code that I have come up with is functional but the problem is that when I'm guessing a number with same digits(like 1222) if I only guess put something like 1111 the output is 4 P's, what I would like is to have just one "O" indicating that my first digit is correct and three "X" indicating the three 1s are not correct, i.e. if I input the same number 4 times to guess one number it will not give me three P's saying that they're in the wrong place but three "X" saying thy're wrong.


CODE

namespace Word_Guessing_Game
{
    public partial class FrmNumberGuessingGame : Form
    {
        private Random whatever = new Random();
        int intRandom = 0;
        // Random Number
        public FrmNumberGuessingGame()
        {
            InitializeComponent();
            intRandom = whatever.Next(0,9999);    
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            int intInputFirstDigit = 0;
            int intInputSecondDigit = 0;
            int intInputThirdDigit = 0;
            int intInputFourthDigit = 0;
            int IntInput = 0;
            //Declare input
            
            int intFirstDigit = 0;
            int intSecondDigit = 0;
            int intThirdDigit = 0;
            int intFourthDigit = 0;
            //Declare Digits

            string stringOne="O";
            string stringTwo="O";
            string stringThree="O";
            string stringFour="O";
            //Declare Strings used in listbox
          

            intFirstDigit = intRandom / 1000 % 10;
            intSecondDigit = intRandom / 100 % 10;
            intThirdDigit = intRandom / 10 % 10;
            intFourthDigit = intRandom % 10;
            //Breaking random number

            
  
            //try if input is numbers
            

            try
            {
                if (Int32.Parse(txtEnter.Text) > 9999)
                {
                    MessageBox.Show("Please key in a four digit number",
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }


                else
                {

                    if (lstOutput.Items.Count == 0)
                    {
                        lstOutput.Items.Add("Numbers" + "\t" + "Entered:" + "\t" + "1st" + "\t" + "2nd" + "\t" + "3rd" + "\t" + "4th");
                    }

                    IntInput = Int32.Parse(txtEnter.Text);
                    intInputFirstDigit = IntInput / 1000 % 10;
                    intInputSecondDigit = IntInput / 100 % 10;
                    intInputThirdDigit = IntInput / 10 % 10;
                    intInputFourthDigit = IntInput % 10;
                    //Breaking input number


                    if (intFirstDigit == intInputFirstDigit)
                    {
                        stringOne = "O";
                    }
                    else if (intInputFirstDigit == intSecondDigit || intInputFirstDigit == intThirdDigit || intInputFirstDigit == intFourthDigit)
                    {
                        stringOne = "P";
                    }

                    else //if (intFirstDigit != intInputFirstDigit)
                    {
                        stringOne = "X";
                    }


                    if (intSecondDigit == intInputSecondDigit)
                    {
                        stringTwo = "O";
                    }
                    else if (intInputSecondDigit == intFirstDigit || intInputSecondDigit == intThirdDigit || intInputSecondDigit == intFourthDigit)
                    {
                        stringTwo = "P";
                    }

                    else //if (intSecondDigit != intInputSecondDigit)
                    {
                        stringTwo = "X";
                    }
                    if (intThirdDigit == intInputThirdDigit)
                    {
                        stringThree = "O";
                    }
                    else if (intInputThirdDigit == intFirstDigit || intInputThirdDigit == intSecondDigit || intInputThirdDigit == intFourthDigit)
                    {
                        stringThree = "P";
                    }

                    else //if (intThirdDigit != intInputThirdDigit)
                    {
                        stringThree = "X";
                    }
                    if (intFourthDigit == intInputFourthDigit)
                    {
                        stringFour = "O";
                    }
                    else if (intInputFourthDigit == intFirstDigit || intInputFourthDigit == intSecondDigit || intInputFourthDigit == intThirdDigit)
                    {
                        stringFour = "P";
                    }

                    else //if (intFourthDigit != intInputFourthDigit)
                    {
                        stringFour = "X";
                    }



                    lstOutput.Items.Add(intInputFirstDigit+""+intInputSecondDigit+""+intInputThirdDigit+""+intInputFourthDigit+
                        "==>" + "\t" + stringOne + "\t" + stringTwo + "\t" + stringThree + "\t" + stringFour);
                    if (intFirstDigit == intInputFirstDigit
                        && intSecondDigit == intInputSecondDigit
                        && intThirdDigit == intInputThirdDigit
                        && intFourthDigit == intInputFourthDigit)
                    {
                        lblFirstDigit.Text = String.Format("{0:D}", intInputFirstDigit);
                        lblSecondDigit.Text = String.Format("{0:D}", intInputSecondDigit);
                        lblThirdDigit.Text = String.Format("{0:D}", intInputThirdDigit);
                        lblFourthDigit.Text = String.Format("{0:D}", intInputFourthDigit);


                        MessageBox.Show("Congratulations! You've Got it! Number of Guesses:" + Convert.ToString(lstOutput.Items.Count-1),
                            "", MessageBoxButtons.OK
                            , MessageBoxIcon.Asterisk);
                    }
                }
            }

                //input is not numbers
            catch
            {
                MessageBox.Show("Please key in numbers",
                   "Error", MessageBoxButtons.OK,
                   MessageBoxIcon.Error);
            }
            txtEnter.Clear();
            txtEnter.Focus();



          
        }

        private void btnTryAgain_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }
          

    }
}


This post has been edited by L1989: 1 Jul, 2009 - 05:29 AM

User is offlineProfile CardPM
+Quote Post


janne_panne

RE: Help For A Number Guessing Game

1 Jul, 2009 - 06:24 AM
Post #2

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 527



Thanked: 107 times
My Contributions
I made a similar program about 9 months ago. Here is the code I used for checking the right numbers or right numbers at wrong place. Hope this gives you a hint how you should do it. I made it with Java so this is a quick translation which is not tested. And of course there are some bad coding (didn't use consts etc) but anyway.

CODE


        private int[] correctLine = new int[4];

        public void DrawNewCorrectLine() {
            Random random = new Random();
            for (int i = 0; i < correctLine.Length; i++) {
                correctLine[i] = (int)(random.NextDouble() * 10);
            }
        }

        public int[] CheckLine(int[] playersLine) {
            // hits:
            // 1 = no hit
            // 2 = right number at wrong place
            // 3 = right number at right place

            int hitIndex = 0;
            int[] hits = new int[correctLine.Length];

            // Temporary arrays
            int[] correctLineTemp = new int[correctLine.Length];
            int[] playersLineTemp = new int[playersLine.Length];

            // Set all hits to 1 (no hit) at first.
            // also copy the right arrays we don't want to change here to Temp arrays.
            for (int i = 0; i < correctLineTemp.Length; i++) {
                correctLineTemp[i] = correctLine[i];
                hits[i] = 1;
                playersLineTemp[i] = playersLine[i];
            }

            // Check right numbers at right places
            for (int i = 0; i < 4; i++) {
                if (playersLineTemp[i] == correctLineTemp[i]) {
                    hits[hitIndex] = 3;
                    hitIndex++;
                    correctLineTemp[i] = -1;
                    playersLineTemp[i] = -2;
                }
            }

            // Check right numbers at wrong places
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (playersLineTemp[i] == correctLineTemp[j]) {
                        hits[hitIndex] = 2;
                        hitIndex++;
                        correctLineTemp[j] = -1;
                        playersLineTemp[i] = -2;
                        break;
                    }
                }
            }
            return hits;
        }

User is offlineProfile CardPM
+Quote Post

L1989

RE: Help For A Number Guessing Game

2 Jul, 2009 - 12:57 AM
Post #3

New D.I.C Head
*

Joined: 21 Jun, 2009
Posts: 26

I don't quite understand your code cause I'm just a beginner in C# and I haven't learned other programming language before, but I'll try to see if it helps. Thank you
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:28AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month