using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
/*
Mark Hesser
October 17, 2017
Random Number Game Form App
*/
namespace Random_Number_Game
{
public partial class rand_num : Form
{
public rand_num()
{
InitializeComponent();
}
int randNum, guess, prevGuess, tryCounter;
string guessTemp;
bool debugOn = false;
Random rand = new Random();
private void rand_num_Load(object sender, EventArgs e)
{
gameReset();
}
private void inputGuess_Click(object sender, EventArgs e)
{
checkGuess();
}
private void guess_textbox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
checkGuess();
}
}
private void playAgain_Click(object sender, EventArgs e)
{
gameReset();
}
private void tryCounter_label_Click(object sender, EventArgs e)
{
loseTry();
tryCountCheck();
}
private void gameReset()
{
//Generate a new Random Number
randNum = rand.Next(1, 100);
tryCounter = 10;
tryCounter_label.Visible = true;
tryCounter_label.Text = ("Trys: " + Convert.ToString(tryCounter));
guess_btn.Enabled = true;
guess_textbox.Enabled = true;
guess_textbox.Clear();
result_label.Visible = false;
randnum_label.Visible = false;
debugMode();
}
private void checkGuess()
{
guessTemp = guess_textbox.Text; // input guess
result_label.Visible = true;
// get the guess from the textbox
if (Int32.TryParse(guessTemp, out guess))
{
// check the guess
if (guess != prevGuess)
{
if (guess > 0 && guess < 101) { if (guess == randNum) { result_label.Text = "That is Correct, You Win!"; guess_btn.Enabled = false; guess_textbox.Enabled = false; } else { if (guess > randNum)
{
result_label.Text = "Try Again, Guess Lower";
}
if (guess < randNum)
{
result_label.Text = "Try Again, Guess Higher";
}
loseTry();
}
}
else
{
result_label.Text = "Enter between 1 and 100";
loseTry();
}
}
else
{
result_label.Text = "Enter a different number";
}
}
else
{
result_label.Text = "oops, enter a number";
}
tryCountCheck();
}
private void tryCountCheck()
{
if (tryCounter <= 0 && guess != randNum)
{
result_label.Text = ("Game Over");
randnum_label.Text = ("The Number was " + randNum.ToString());
randnum_label.Visible = true;
guess_btn.Enabled = false;
guess_textbox.Enabled = false;
}
}
private void loseTry()
{
tryCounter–;
prevGuess = guess;
tryCounter_label.Text = ("Trys: " + Convert.ToString(tryCounter));
}
private void debugMode()
{
if(debugOn)
{
debug_label.Visible = true;
debug_label.Text = Convert.ToString(randNum);
}
}
}
}
[/code]