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