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();
}
}
}

New Topic/Question
Reply




MultiQuote



|