6 Replies - 1727 Views - Last Post: 21 June 2009 - 11:15 AM Rate Topic: -----

#1 L1989  Icon User is offline

  • New D.I.C Head

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

Help for a number guessing game

Post icon  Posted 21 June 2009 - 07:13 AM

I want to program a game to guess a four digit number, it's suppose to give "O" and "X" for the right and wrong guess and "P" when you have guessed a right number but in a wrong place , this is my code, the problem is that it can function with some numbers but goes totally crazy with some others. thanks a lot

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;

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(999, 10000);
			lstOutput.Items.Add("Numbers" + "\t" + "Entered:" + "\t" + "1st" + "\t" + "2nd" + "\t" + "3rd" + "\t" + "4th");
		}

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

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

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

			
			//Number of Clicks
			intClick =Int32.Parse(lstOutput.Items.Count.ToString());
			


			//O and X start
			//try if input is numbers
			try
			{
				IntInput = Int32.Parse(txtEnter.Text);
				intInputFirstDigit = IntInput / 1000 % 10;
				intInputSecondDigit = IntInput / 100 % 10;
				intInputThirdDigit = IntInput / 10 % 10;
				intInputFourthDigit = IntInput % 10;
				//Breaking input number

				//O and X start  all numbers different
				if (intFirstDigit == intInputFirstDigit&&intInputFirstDigit!=intSecondDigit&&intInputFirstDigit!=intThirdDigit&&intInputFirstDigit!=intFourthDigit)
				{
					stringOne = "O";

				}
				else
				{
					stringOne = "X";
				}
				if (intSecondDigit == intInputSecondDigit&&intInputSecondDigit!=intFirstDigit&&intInputSecondDigit!=intThirdDigit&&intInputSecondDigit!=intFourthDigit)
				{
					stringTwo = "O";
				}
				else
				{
					stringTwo = "X";
				}
				if (intThirdDigit == intInputThirdDigit&&intInputThirdDigit!=intFirstDigit&&intInputThirdDigit!=intSecondDigit&&intInputThirdDigit!=intFourthDigit)
				{
					stringThree = "O";
				}
				else
				{
					stringThree = "X";
				}
				if (intFourthDigit == intInputFourthDigit&&intInputFourthDigit!=intFirstDigit&&intInputFourthDigit!=intSecondDigit&&intInputFourthDigit!=intThirdDigit)
				{
					stringFour = "O";
				}
				else
				{
					stringFour = "X";
				}
				//O and X end all numbers different

				
				
				
				//O and X  start  have numbers the same
				if (intFirstDigit == intInputFirstDigit)
				{
					stringOne="O";
					if (intInputFirstDigit == intSecondDigit)
					{
						stringTwo = "P";
					}
					if (intInputFirstDigit == intThirdDigit)
					{
						stringThree = "P";
					}
					if (intInputFirstDigit == intFourthDigit)
					{
						stringFour = "P";
					}
				}
					


				if (intSecondDigit == intInputSecondDigit)
				{
					stringTwo = "O";
					if (intInputSecondDigit == intFirstDigit)
					{
						stringOne = "P";
					}
					if (intInputSecondDigit == intThirdDigit)
					{
						stringThree = "P";
					}
					if (intInputSecondDigit == intFourthDigit)
					{
						stringFour = "P";
					}
				}
					



				 if (intThirdDigit == intInputThirdDigit)
				{
					stringThree = "O";
					if (intInputThirdDigit == intFirstDigit)
					{
						stringOne = "P";
					}
					if (intInputThirdDigit == intSecondDigit)
					{
						stringTwo = "P";
					}
					if (intInputThirdDigit == intFourthDigit)
					{
						stringFour = "P";
					}
				}



				if (intFourthDigit == intInputFourthDigit)
				{
					stringFour = "O";
					if (intInputFourthDigit == intFirstDigit)
					{
						stringOne = "P";
					}
					if (intInputFourthDigit == intSecondDigit)
					{
						stringTwo = "P";
					}
					if (intFourthDigit == intThirdDigit)
					{
						stringThree = "P";
					}
				}
				   
				//O and X end  have numbers the same
				  
				 
				
				
				//P start  number in the wrong place
				if (intInputFirstDigit!=intFirstDigit&&intInputFirstDigit == intSecondDigit || intInputFirstDigit == intThirdDigit || intInputFirstDigit == intFourthDigit)
				{
					stringOne = "P";
				}
				if (intInputSecondDigit != intSecondDigit && intInputSecondDigit == intFirstDigit || intInputSecondDigit == intThirdDigit || intInputSecondDigit == intFourthDigit)
				{
					stringTwo = "P";
				}
				if (intInputThirdDigit != intThirdDigit && intInputThirdDigit == intFirstDigit || intInputThirdDigit == intSecondDigit || intInputThirdDigit == intFourthDigit)
				{
					stringThree = "P";
				}
				if (intInputFourthDigit != intFourthDigit && intInputFourthDigit == intFirstDigit || intInputFourthDigit == intSecondDigit || intFourthDigit == intThirdDigit)
				{
					stringFour = "P";
				}
				//P end number in the wrong place


				lstOutput.Items.Add(IntInput + "==>" + "\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(intClick),
						"", MessageBoxButtons.OK
						, MessageBoxIcon.Asterisk);
				}
			}
 
				//input is not numbers
			catch
			{
				 MessageBox.Show("Please key in numbers",
					"Error", MessageBoxButtons.OK,
					MessageBoxIcon.Error);
			}



This post has been edited by L1989: 21 June 2009 - 07:23 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Help for a number guessing game

#2 diputs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 01-June 09

Re: Help for a number guessing game

Posted 21 June 2009 - 07:56 AM

Just off-hand, I noticed that you have to heaps of code to make "p" which means that the digit was used but not in this place where it is needed both for if 1)it was originally used in a place where it was needed, and if 2)it was originally not used correctly.
This means that you can save a lot of code by not checking if it was needed in the place it was entered.

To save a heck of a lot more code, and make it a heck of a lot easier to read, I would have made it as an array. I would have had a string for the whole number and used: stringname.charAt(i) (if this were java) in a series of nested for loops (nested to cycle through the string entered seperately from the string of correct values) that check all conditions. If I am quick, I might even reply later with an example. It wouldn't take that long. I am not familiar with c# (if this is c#) but I am sure that even if doesn't have such a function to get characters from strings, you can explicitly make an array of characters can't you?

P.S. The website only seems to display code segments up to 64 characters wide and you have to scroll horizontally to get the heaps of code you wrote (i copied the code to a word document).

P.P.S. If you must use variables over and over, you can make them shorter like "digit4" for example.

This post has been edited by diputs: 21 June 2009 - 08:02 AM

Was This Post Helpful? 0
  • +
  • -

#3 L1989  Icon User is offline

  • New D.I.C Head

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

Re: Help for a number guessing game

Posted 21 June 2009 - 08:20 AM

View Postdiputs, on 21 Jun, 2009 - 06:56 AM, said:

Just off-hand, I noticed that you have to heaps of code to make "p" which means that the digit was used but not in this place where it is needed both for if 1)it was originally used in a place where it was needed, and if 2)it was originally not used correctly.
This means that you can save a lot of code by not checking if it was needed in the place it was entered.

To save a heck of a lot more code, and make it a heck of a lot easier to read, I would have made it as an array. I would have had a string for the whole number and used: stringname.charAt(i) (if this were java) in a series of nested for loops (nested to cycle through the string entered seperately from the string of correct values) that check all conditions. If I am quick, I might even reply later with an example. It wouldn't take that long. I am not familiar with c# (if this is c#) but I am sure that even if doesn't have such a function to get characters from strings, you can explicitly make an array of characters can't you?

P.S. The website only seems to display code segments up to 64 characters wide and you have to scroll horizontally to get the heaps of code you wrote (i copied the code to a word document).

P.P.S. If you must use variables over and over, you can make them shorter like "digit4" for example.

I've been told about the array thing but problem is that I only know there is something called an array but got no idea whatsoever of how to use it, and yes this is C#(sorry for not mentioning), I've only just started learning so I'm complete rooky when coming to this programing thing. I'll be more than happy if you could give me an example, thank you very much.
Was This Post Helpful? 0
  • +
  • -

#4 diputs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 01-June 09

Re: Help for a number guessing game

Posted 21 June 2009 - 08:28 AM

string input = "1234";
string answ = "9476";
string resp = "####";

for (int i=0; i<input.length; i++)
for (int a=0; a<answ.length; a++)
{
if (input[i]==answ[a])
{
if (i==a) resp[a]='O'
else resp[a]='P'
}
}

for (int r=0; r<resp.length; r++) if (resp[r]=='#') resp[r]='X'

I found out that c# treats strings (as does c and c++) as just arrays of characters. The syntax is very similar to java. That means that my java expertise leads me to guess that something like this (which may have bad syntax or something but you can fix that if it does) should be along the right lines. Pretty short huh!

This post has been edited by diputs: 21 June 2009 - 08:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 L1989  Icon User is offline

  • New D.I.C Head

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

Re: Help for a number guessing game

Posted 21 June 2009 - 08:45 AM

View Postdiputs, on 21 Jun, 2009 - 07:28 AM, said:

string input = "1234";
string answ = "9476";
string resp = "####";

for (int i=0; i<input.length; i++)
for (int a=0; a<answ.length; a++)
{
if (input[i]==answ[a])
{
if (i==a) resp[a]='O'
else resp[a]='P'
}
}

for (int r=0; r<resp.length; r++) if (resp[r]=='#') resp[r]='X'

I found out that c# treats strings (as does c and c++) as just arrays of characters. The syntax is very similar to java. That means that my java expertise leads me to guess that something like this (which may have bad syntax or something but you can fix that if it does) should be along the right lines. Pretty short huh!

Thanks
Was This Post Helpful? 0
  • +
  • -

#6 diputs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 01-June 09

Re: Help for a number guessing game

Posted 21 June 2009 - 09:06 AM

It's not important anymore I think, but I found an error in your code. When you simplify code, the errors tend to be fewer and easier to find, but for future reference, c# apparently makes && have precedence over || and the last "P" part of your code didn't put the or part in parethesis to make them precede.

So that was one error. Good bye.
Was This Post Helpful? 0
  • +
  • -

#7 diputs  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 01-June 09

Re: Help for a number guessing game

Posted 21 June 2009 - 11:15 AM

View Postdiputs, on 21 Jun, 2009 - 07:28 AM, said:

string input = "1234";
string answ = "9476";
string resp = "####";

for (int i=0; i<input.length; i++)
for (int a=0; a<answ.length; a++)
{
if (input[i]==answ[a])
{
if (i==a) resp[a]='O'
else resp[a]='P'
}
}

for (int r=0; r<resp.length; r++) if (resp[r]=='#') resp[r]='X'

I found out that c# treats strings (as does c and c++) as just arrays of characters. The syntax is very similar to java. That means that my java expertise leads me to guess that something like this (which may have bad syntax or something but you can fix that if it does) should be along the right lines. Pretty short huh!


It just occured to me how much I oversimplified. It's funny how my brain can remember so well but think so slowly. My version will occasionally replace 'O' where it belongs with 'P' if there are any duplicate digits. Correction:

for (int i=0; i<input.length; i++)
for (int a=0; a<answ.length; a++)
{
if (input[i]==answ[a])
{
if (i==a) resp[a]='O';
else if(resp[a]!='O') resp[a]='P'; //the changed part
}
}

The bits I don't think I'll bother with are the bits I think you already did or know how to do (get input, make random numbers, respond to input)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1