1 Replies - 2851 Views - Last Post: 08 August 2009 - 10:55 AM Rate Topic: -----

#1 L1989  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 21-June 09

Number Guessing Game

Posted 08 August 2009 - 09:59 AM

I've been designing a number game but have encountered some problems. The game is suppose to guess a 4 digit number which is generated by the computer, and when you guess a 4 digit number it outputs "O" for right, "X" for wrong and "P" for wrong position, the problems is with the P. The idea is that there can be only one P in the output unless your guess really is a scrambled version of the answer i.e. random number 1234, your guess 4321 then 4 P's. But if the number is 1234 and your guess is 1111 then the ouput should be one "O" and three "X" or if the guess is 1711, the output should be P X X X. I know it sounds a bit confusing but the main idea is only one P except for guess and answer is scrambled

I came up with a code which uses a switch to indicate whether the P can be used or not, but it's having some minor problems. Can someone help me? Thanks.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Guessing_Game;
using Color_Guessing_Game;

namespace Word_Guessing_Game
{
	public partial class FrmNumberGuessingGame : Form
	{
		private Random whatever = new Random();
		int intRandom = 0;
		// Random Number			   

		public FrmNumberGuessingGame(Form Form1)
		{
			InitializeComponent();
			intRandom = whatever.Next(0,9999);
			txtEnter.MaxLength = 4;
		}
		
		private void btnGo_Click(object sender, EventArgs e)
		{
			int intInputFirstDigit = 0,intInputSecondDigit = 0,intInputThirdDigit = 0,intInputFourthDigit = 0;
			int IntInput = 0;
			//Declare input

			bool bool1 = true, bool2 = true, bool3 = true, bool4 = true;
			//Declare flags
			
			int intFirstDigit = 0,intSecondDigit = 0,intThirdDigit = 0,intFourthDigit = 0;
			//Declare Digits

			string stringOne="O",stringTwo="O",stringThree="O",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

					// O X and P
					if (intFirstDigit == intInputFirstDigit)
					{
						stringOne = "O";	
					}
					else if (intInputFirstDigit == intSecondDigit && intInputSecondDigit != intSecondDigit && bool2 == true)					   
					{
						stringOne = "P";
						bool2 = false;
					}
					else if (intInputFirstDigit == intThirdDigit && intThirdDigit != intInputThirdDigit && bool3 == true)
					{
						stringOne = "P";
						bool3 = false;
					}
					else if (intInputFirstDigit == intFourthDigit && intInputFourthDigit != intFourthDigit && bool4 == true)
					{
						stringOne = "P";
						bool4 = false;
					}

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


					if (intSecondDigit == intInputSecondDigit)
					{
						stringTwo = "O";
					}
					else if (intInputSecondDigit == intFirstDigit && intFirstDigit != intInputFirstDigit && bool1 == true)					   
					{
						stringTwo = "P";
						bool1 = false;
					}
					else if (intInputSecondDigit == intThirdDigit && intThirdDigit != intInputThirdDigit && bool3 == true)
					{
						stringTwo = "P";
						bool3 = false;
					}
					else if (intInputSecondDigit == intFourthDigit && intInputFourthDigit != intFourthDigit && bool4 == true)
					{
						stringTwo = "P";
						bool4 = false;
					}
					else //if (intSecondDigit != intInputSecondDigit)
					{
						stringTwo = "X";
					}


					if (intThirdDigit == intInputThirdDigit)
					{
						stringThree = "O";
					}
					else if (intInputThirdDigit == intFirstDigit && intFirstDigit != intInputFirstDigit && bool1 == true)
					{
						stringThree = "P";
						bool1 = false;
					}
					else if (intInputThirdDigit == intSecondDigit && intInputSecondDigit != intSecondDigit && bool2 == true)
					{
						stringThree = "P";
						bool2 = false;
					}
					else if (intInputThirdDigit == intFourthDigit && intInputFourthDigit != intFourthDigit && bool4 == true)
					{
						stringThree = "P";
						bool4 = false;
					}

					else //if (intThirdDigit != intInputThirdDigit)
					{
						stringThree = "X";
					}


					if (intFourthDigit == intInputFourthDigit)
					{
						stringFour = "O";
					}
					else if (intInputFourthDigit == intFirstDigit && intFirstDigit != intInputFirstDigit && bool1 == true)
					{
						stringFour = "P";
						bool1 = false;
					}
					else if (intInputFourthDigit == intSecondDigit && intInputSecondDigit != intSecondDigit && bool2 == true)
					{
						stringFour = "P";
						bool2 = false;
					}
					else if (intInputFourthDigit == intThirdDigit && intThirdDigit != intInputThirdDigit && bool3 == true)
					{
						stringFour = "P";
						bool3 = false;
					}

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


					// output of input
					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);
						MessageBox.Show("You've used" + "\t" + time1 + "\t" + "minute(s) and" + "\t" + time2 + "\t" + "seconds");
						
					}
				}
			}

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



Is This A Good Question/Topic? 0
  • +

Replies To: Number Guessing Game

#2 doucet.damon  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 06-August 09

Re: Number Guessing Game

Posted 08 August 2009 - 10:55 AM

This looks a lot more complicated than it has to be.

I'd recommend just using a string (generate a random integer, then convert that to a string) and looping through it rather than doing the 1, 2, 3, 4 thing.

This is how I might look at it:

string _answer;

public FrmNumberGuessingGame()
{
	_answer = whatever.Next(9999).ToString();
}

private void btnGo_Click(object sender, EventArgs e)
{
	uint guess; //no negative number
	if (txtEnter.Text.Length != 4 || !uint.TryParse(txtEnter.Text, out guess))
	{
		MessageBox.Show("Invalid");
		return;
	}

	string realGuess = txtEnter.Text;
	string output = "XXXX"; int ind;
	for (int i = 0; i < 4; i++)
	{
		if (realGuess[i] == _answer[i])
			output[i] = 'O';
		
		//check if there's a mismatched one of these somewhere else
		ind = _answer.IndexOf(realGuess[i]);
		while (ind != -1 && _answer[ind] == realGuess[ind])
			ind = _answer.IndexOf(realGuess[i], ind + 1); //I think
		 if (ind == -1)
			 continue; //nope
		 output[i] = 'P'; //there's another one somewhere that isn't matched
	}

	//..do whatever with the output string
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1