3 Replies - 3021 Views - Last Post: 19 January 2011 - 06:42 AM Rate Topic: -----

#1 BlueWorld  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-January 11

How to shorten if-else statement using Arrays?

Posted 18 January 2011 - 08:34 AM

Quote

I am creating a number guessing game and I need to use arrays.
However, I do not know how to use arrays.
Is there any way I can use arrays to replace the if-else statements?


    public partial class NumberGuessingGame : Form
    {
        int RandomNum;
        int GuessingNum;
        int count = 1;
        
        public NumberGuessingGame()
        {
            InitializeComponent();

            Random number = new Random();
            RandomNum = number.Next(0000, 9999);
            Convert.ToString(RandomNum);
            txtNumGuessed.MaxLength = 4;
            btnOK.Enabled = false;

        }

        private void txtNumGuessed_TextChanged(object sender, EventArgs e)
        {
            if (txtNumGuessed.Text.Length == txtNumGuessed.MaxLength)
            {
                btnOK.Enabled = true;
            }

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int FirstDigit;
            int SecondDigit;
            int ThirdDigit;
            int FourthDigit;
            
            int FirstDigitGuessed;
            int SecondDigitGuessed;
            int ThirdDigitGuessed;
            int FourthDigitGuessed;

            string A, B, C, D;

            btnOK.Enabled = false;
            
            try
            {
                GuessingNum = int.Parse(txtNumGuessed.Text);

                FirstDigit = ((RandomNum / 1000) % 10);
                SecondDigit = ((RandomNum / 100) % 10);
                ThirdDigit = ((RandomNum / 10) % 10);
                FourthDigit = ((RandomNum / 1) % 10);

                FirstDigitGuessed = ((GuessingNum / 1000) % 10);
                SecondDigitGuessed = ((GuessingNum / 100) % 10);
                ThirdDigitGuessed = ((GuessingNum / 10) % 10);
                FourthDigitGuessed = ((GuessingNum / 1) % 10);

                int[]Actual=new int[4]{FirstDigit,SecondDigit,ThirdDigit,FourthDigit};
                int[] User = new int[4]{ FirstDigitGuessed, SecondDigitGuessed, ThirdDigitGuessed, FourthDigitGuessed };
                string[,]Result = new string[,]{{"O","X"}};

                if (GuessingNum != RandomNum)
                {
                    txtNumGuessed.Clear();
                    if (FirstDigitGuessed == FirstDigit)
                    {
                        A = "O";
                    }
                    else
                    {
                        A = "X";
                    }
                    if (SecondDigitGuessed == SecondDigit)
                    {
                        B = "O";
                    }
                    else
                    {
                        B = "X";
                    }
                    if (ThirdDigitGuessed == ThirdDigit)
                    {
                        C = "O";
                    }
                    else
                    {
                        C = "X";
                    }
                    if (FourthDigitGuessed == FourthDigit)
                    {
                        D = "O";
                    }
                    else
                    {
                        D = "X";
                    }

                    lstResult.Items.Add(Convert.ToString(FirstDigitGuessed) + Convert.ToString(SecondDigitGuessed) + Convert.ToString(ThirdDigitGuessed) + Convert.ToString(FourthDigitGuessed) + " ==>" + A + B + C + D);
                    txtNumGuessed.Focus();
                    count++;
                }
                else
                {
                    lbl1.Text = Convert.ToString(FirstDigit);
                    lbl2.Text = Convert.ToString(SecondDigit);
                    lbl3.Text = Convert.ToString(ThirdDigit);
                    lbl4.Text = Convert.ToString(FourthDigit);
                    MessageBox.Show("Congratulation! Number of gusses: " + count);
                }
            }
            catch
            {
                MessageBox.Show("Invalid input. Please enter a 4-digit number.");
            }
        }

    }



Is This A Good Question/Topic? 0
  • +

Replies To: How to shorten if-else statement using Arrays?

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8937
  • View blog
  • Posts: 33,463
  • Joined: 12-June 08

Re: How to shorten if-else statement using Arrays?

Posted 18 January 2011 - 08:42 AM

You want to replace conditional logic with a container of objects? How would that even work?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5777
  • View blog
  • Posts: 12,591
  • Joined: 16-October 07

Re: How to shorten if-else statement using Arrays?

Posted 18 January 2011 - 09:20 AM

I'm not sure where you'd have another array. I assume in that result?

If it helps, I'd do it something like:
public partial class NumberGuessingGame : Form {
	const int MAX_NUM_LEN = 4;
	int RandomNum;
	// int GuessingNum;
	int count = 1;

	public NumberGuessingGame() {
		InitializeComponent();

		RandomNum = GetRandNum();
		// Convert.ToString(RandomNum); this doesn't do anything
		txtNumGuessed.MaxLength = MAX_NUM_LEN;
		btnOK.Enabled = false;
	}
	
	private int GetRandNum() { return new Random().Next(0000, 9999); }

	private int [] ArrayFromNum(int n) {
		int[] a = new int[MAX_NUM_LEN];
		for(int i=MAX_NUM_LEN-1; i>=0; i--) {
			a[i] = n % 10;
			n /= 10;
		}
		return a;
	}

	private string CompareGuess(int GuessingNum) {
		int [] aGn = ArrayFromNum(GuessingNum);
		int [] aRn = ArrayFromNum(RandomNum);
		string s = "", guessed = "";
		for(int i=0; i<MAX_NUM_LEN; i++) {
			guessed += Convert.ToString(aGn[i]);
			s += (aGn[i]==aRn[i]) ? "O" : "X";
		}
		return guessed + " ==>" + s;
	}
	
	
	private void txtNumGuessed_TextChanged(object sender, EventArgs e) {
		if (txtNumGuessed.Text.Length == txtNumGuessed.MaxLength) {
			btnOK.Enabled = true;
		}
	}
	
	private void btnOK_Click(object sender, EventArgs e) {
		btnOK.Enabled = false;
		int GuessingNum;
		try {
			GuessingNum = int.Parse(txtNumGuessed.Text);
		} catch {
			MessageBox.Show("Invalid input. Please enter a 4-digit number.");
			return;
		}
		if (GuessingNum != RandomNum) {
			txtNumGuessed.Clear();
			lstResult.Items.Add(CompareGuess(GuessingNum));
			txtNumGuessed.Focus();
			count++;
		} else {
			int [] a = ArrayFromNum(RandomNum);
			int i = 0;
			lbl1.Text = Convert.ToString(a[i++]);
			lbl2.Text = Convert.ToString(a[i++]);
			lbl3.Text = Convert.ToString(a[i++]);
			lbl4.Text = Convert.ToString(a[i++]);
			MessageBox.Show("Congratulation! Number of gusses: " + count);
		}
	}
}



Though, realistically, we can get away with less arrays, not more:
public partial class NumberGuessingGame : Form {
	const int MAX_NUM_LEN = 4;
	int RandomNum;
	// int GuessingNum;
	int count = 1;

	public NumberGuessingGame() {
		InitializeComponent();

		RandomNum = GetRandNum();
		// Convert.ToString(RandomNum); this doesn't do anything
		txtNumGuessed.MaxLength = MAX_NUM_LEN;
		btnOK.Enabled = false;
	}
	
	private int GetRandNum() { return new Random().Next(0000, 9999); }

	private string CompareGuess(int guessNum, int answerNum) {
		string s = "", guessed = "";
		for(int i=0; i<MAX_NUM_LEN; i++) {
			int gDigit = guessNum % 10;
			int aDigit = answerNum % 10;
			guessNum \= 10; answerNum \= 10;
			guessed = Convert.ToString(aGn[i]) + guessed;
			s = s + (gDigit==aDigit) ? "O" : "X";
		}
		return guessed + " ==>" + s;
	}
	
	
	private void txtNumGuessed_TextChanged(object sender, EventArgs e) {
		if (txtNumGuessed.Text.Length == txtNumGuessed.MaxLength) {
			btnOK.Enabled = true;
		}
	}
	
	private void btnOK_Click(object sender, EventArgs e) {
		btnOK.Enabled = false;
		int GuessingNum;
		try {
			GuessingNum = int.Parse(txtNumGuessed.Text);
		} catch {
			MessageBox.Show("Invalid input. Please enter a 4-digit number.");
			return;
		}
		if (GuessingNum != RandomNum) {
			txtNumGuessed.Clear();
			lstResult.Items.Add(CompareGuess(GuessingNum));
			txtNumGuessed.Focus();
			count++;
		} else {
			int n = RandomNum;
			lbl4.Text = Convert.ToString(n % 10); n /= 10;
			lbl3.Text = Convert.ToString(n % 10); n /= 10;
			lbl2.Text = Convert.ToString(n % 10); n /= 10;
			lbl1.Text = Convert.ToString(n % 10);
			MessageBox.Show("Congratulation! Number of gusses: " + count);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 Guest_BlueWorld*


Reputation:

Re: How to shorten if-else statement using Arrays?

Posted 19 January 2011 - 06:42 AM

Thanks very much!
Was This Post Helpful? 0

Page 1 of 1