# Help for a number guessing game

Page 1 of 1

## 2 Replies - 1565 Views - Last Post: 02 July 2009 - 01:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=112626&amp;s=393f431a1beff779f1d0e8d7a4de4c39&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 L1989

• New D.I.C Head

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

# Help for a number guessing game

Posted 01 July 2009 - 05:42 AM

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.

```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: 01 July 2009 - 06:29 AM

Is This A Good Question/Topic? 0

## Replies To: Help for a number guessing game

### #2 janne_panne

• WinRT Dev

Reputation: 428
• Posts: 1,047
• Joined: 09-June 09

## Re: Help for a number guessing game

Posted 01 July 2009 - 07:24 AM

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.

```
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;
}

```

Was This Post Helpful? 0

### #3 L1989

• New D.I.C Head

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

## Re: Help for a number guessing game

Posted 02 July 2009 - 01:57 AM

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
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }