TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jonathan Oberhaus
NA
6
11.1k
Making number guessing game object oriented
Dec 7 2012 2:42 AM
I am trying to learn object oriented concepts. I have code for a simple number guessing game using windows forms. I am trying to make object oriented. I have had some success with console applications, but with windows forms I am increasingly confused. I realize I methods like initializegame(), gameWon(), GameLost(), FinishGame() but I'm having a hard time creating objects, and calling them, etc... Anybody have any suggestions? Also, is there a code formatting tool for this forum?
Namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Random number = new Random(); // This is declaring number; our randomizer
int randomNumber; // Our randomized number
int guessedNumber; // Our guessed number
private void Form1_Load(object sender, EventArgs e)
{
randomNumber = number.Next(1, 10);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
try
{
guessedNumber = int.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("Please enter an integer from 1-10", "Error!");
textBox1.Clear();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
guessedNumber = int.Parse(textBox1.Text);
bool win = false;
bool wrong = false;
if (textBox1.Text == "")
{
MessageBox.Show("Please enter an integer from 1-10!", "Error!");
textBox1.Clear();
}
else
{
if (guessedNumber >= 1 && guessedNumber <= 10)
{
if (guessedNumber == randomNumber)
{
win = true;
if (win == true)
{
if (MessageBox.Show("You have won! Would you like to play again ?", "You win!", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
randomNumber = number.Next(1, 10);
label3.Text = "4";
textBox1.Clear();
}
else
{
this.Close();
}
}
else
{
// Do nothing
}
}
else
{
wrong = true;
}
}
else
{
MessageBox.Show("Please enter an integer from 1-10!", "Error!");
textBox1.Clear();
}
}
if (wrong == true)
{
label3.Text = (int.Parse(label3.Text) - 1).ToString();
if (label3.Text == "0")
{
if (MessageBox.Show("You have lost! The randomized number was " + randomNumber + ". Would you like to play again ?", "You lost!", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
randomNumber = number.Next(1, 10);
label3.Text = "4";
textBox1.Clear();
}
else
{
this.Close();
}
}
else
{
if (guessedNumber > randomNumber)
{
MessageBox.Show("Lower!", "Wrong!");
textBox1.Clear();
}
else
{
MessageBox.Show("Higher!", "Wrong!");
textBox1.Clear();
}
}
}
else
{
// Do nothing
}
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
}
}
Reply
Answers (
0
)
when i click the drop down that value should be come in text box how to do in asp.net with csharp
how to use aggregate functions when combining two datatables