C# Tutorial: Create a Number Cracking Game
- Subject: C# Tutorials
- Learning Time: 4 hours
//load defaults and settings
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;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
//variable numbers currently used on the buttons
int Num1 = 0, Num2 = 0, Num3 = 0, Num4 = 0;
//Random variable numbers that are generated by code
int Rand1 = 0, Rand2 = 0, Rand3 = 0, Rand4 = 0;
//int's needed lives, line number on list and reset number
int Lives = 5, OnLineNumber = 0, Zero = 0;
//Initialize Set up
public Form1()
{
InitializeComponent();
//calls function to start a new game the moment program is loaded
NewGame();
}
//function that resets everything to defaults for starting a new game or clearing existing game
public void NewGame()
{
SavedList1.Items.Clear();
SavedList2.Items.Clear();
SavedList3.Items.Clear();
SavedList4.Items.Clear();
Num1 = Zero;
Num2 = Zero;
Num3 = Zero;
Num4 = Zero;
Number1.Text = Zero.ToString();
Number2.Text = Zero.ToString();
Number3.Text = Zero.ToString();
Number4.Text = Zero.ToString();
Rand1 = 0;
Rand2 = 0;
Rand3 = 0;
Rand4 = 0;
OnLineNumber = 0;
Lives = 5;
FindMeANumber();
LivesLeftCounter.Text = "Lives Left " + Lives;
}
//Set up the four random numbers
//Function called from the NewGame function
private void FindMeANumber()
{
Random random1 = new Random();
Rand1 = random1.Next(0, 9);
Rand2 = random1.Next(0, 9);
Rand3 = random1.Next(0, 9);
Rand4 = random1.Next(0, 9);
}
//Button 1, changes the number on it up/down depending on mouse click
private void Number1_MouseDown(object sender, MouseEventArgs e)
{
//if left mouse click
if(e.Button == MouseButtons.Left)
{
//if pressed increment number by 1
Num1++;
//if number reaches 10, reset it to 0
if (Num1 >= 10)
{
Num1 = 0;
}
//change the string on the button to read this new number
Number1.Text = Num1.ToString();
}
// if not left mouse, check to see if it’s a right mouse click
else if (e.Button == MouseButtons.Right)
{
//if pressed decrement number by 1
Num1--;
//if number goes lower then 0, change number to 9
if (Num1 < 0)
{
Num1 = 9;
}
//Change button string to new number
Number1.Text = Num1.ToString();
}
}
//Button 2, changes the number on it up/down depending on mouse click
private void Number2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Num2++;
if (Num2 >= 10) {
Num2 = 0;
}
Number2.Text = Num2.ToString();
}
else if (e.Button == MouseButtons.Right)
{
Num2--;
if (Num2 < 0) {
Num2 = 9;
}
Number2.Text = Num2.ToString();
}
}
//Button 3, changes the number on it up/down depending on mouse click
private void Number3_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Num3++;
if (Num3 >= 10)
{
Num3 = 0;
}
Number3.Text = Num3.ToString();
}
else if (e.Button == MouseButtons.Right)
{
Num3--;
if (Num3 < 0)
{
Num3 = 9;
}
Number3.Text = Num3.ToString();
}
}
//Button 4, changes the number on it up/down depending on mouse click
private void Number4_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Num4++;
if (Num4 >= 10)
{
Num4 = 0;
}
Number4.Text = Num4.ToString();
}
else if (e.Button == MouseButtons.Right)
{
Num4--;
if (Num4 < 0)
{
Num4 = 9;
}
Number4.Text = Num4.ToString();
}
}
//Button "Enter Code", compares the 4 button numbers to the 4 random numbers, adds them to the correct
//list box for each and changes the colours when needed.
private void button1_Click(object sender, EventArgs e)
{
// Add answer from button 1 to list 1
SavedList1.Items.Add(Number1.Text);
//1st List Colour Changes
//compare button1 number with random number 1, if the same, make that line of text blue
if (Number1.Text == Rand1.ToString())
{
//onlinenumber is incremented each guess later on to keep it on right line,
SavedList1.Items[OnLineNumber].ForeColor = System.Drawing.Color.Blue;
}
if (Number1.Text == Rand2.ToString() || Number1.Text == Rand3.ToString() || Number1.Text == Rand4.ToString())
{
// compare button 1 number to the other random numbers to see if it appears, if so make it red
SavedList1.Items[OnLineNumber].ForeColor = System.Drawing.Color.Red;
}
if (Number1.Text == Rand1.ToString())
{
//finally check to see if the rand numner matches another rand number, make it yellow but only if button 1 number is
//correct
if (Rand1 == Rand2 || Rand1 == Rand3 || Rand1 == Rand4)
{
SavedList1.Items[OnLineNumber].ForeColor = System.Drawing.Color.Yellow;
}
}
//2nd List Colour changes
SavedList2.Items.Add(Number2.Text);
if (Number2.Text == Rand2.ToString())
{
SavedList2.Items[OnLineNumber].ForeColor = System.Drawing.Color.Blue;
}
if (Number2.Text == Rand1.ToString() || Number2.Text == Rand3.ToString() || Number2.Text == Rand4.ToString())
{
SavedList2.Items[OnLineNumber].ForeColor = System.Drawing.Color.Red;
}
if (Number2.Text == Rand2.ToString())
{
if (Rand2 == Rand1 || Rand2 == Rand3 || Rand2 == Rand4)
{
SavedList2.Items[OnLineNumber].ForeColor = System.Drawing.Color.Yellow;
}
}
//3rd List Colour Changes
SavedList3.Items.Add(Number3.Text);
if (Number3.Text == Rand3.ToString())
{
SavedList3.Items[OnLineNumber].ForeColor = System.Drawing.Color.Blue;
}
if (Number3.Text == Rand1.ToString() || Number3.Text == Rand2.ToString() || Number3.Text == Rand4.ToString())
{
SavedList3.Items[OnLineNumber].ForeColor = System.Drawing.Color.Red;
}
if (Number3.Text == Rand3.ToString())
{
if (Rand3 == Rand1 || Rand3 == Rand2 || Rand3 == Rand4)
{
SavedList3.Items[OnLineNumber].ForeColor = System.Drawing.Color.Yellow;
}
}
//4th List Colour Changes
SavedList4.Items.Add(Number4.Text);
if (Number4.Text == Rand4.ToString())
{
SavedList4.Items[OnLineNumber].ForeColor = System.Drawing.Color.Blue;
}
if (Number4.Text == Rand1.ToString() || Number4.Text == Rand2.ToString() || Number4.Text == Rand3.ToString())
{
SavedList4.Items[OnLineNumber].ForeColor = System.Drawing.Color.Red;
}
if (Number4.Text == Rand4.ToString())
{
if (Rand4 == Rand1 || Rand4 == Rand2 || Rand4 == Rand3)
{
SavedList4.Items[OnLineNumber].ForeColor = System.Drawing.Color.Yellow;
}
}
//used to check to see what line the colours should be changed on
OnLineNumber++;
//Check to see if all 4 numbers are correct?
if (Number1.Text == Rand1.ToString() && Number2.Text == Rand2.ToString() && Number3.Text == Rand3.ToString()
&& Number4.Text == Rand4.ToString() )
{
//if correct then we have a winner!!! Game over, create a message box!
var result = MessageBox.Show("Winner!!" + "\n" + "Correct Answer: " + Rand1 + Rand2 + Rand3 + Rand4,
"Game Over", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// Game over, time to restart when message box is closed.
if (result == DialogResult.OK)
{
//calls the function that resets everything to defaults
NewGame();
}
}
else
{
//if the answer was not correct! We minus 1 life
Lives--;
//update the life counter text
LivesLeftCounter.Text = "Lives Left " + Lives;
//check lives left to see if they can have another go?
if (Lives == 0)
{
//game over, no lives, display message box and reset when box closed
MessageBox.Show("Failed, the answer was = " + Rand1 + Rand2 + Rand3 + Rand4 + "", "Game Over");
NewGame();
}
}
}
//Menu Button titled "Clear" - reset up game
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
//simply call the reset function
NewGame();
}
//Menu Button titled "Close" - message box to confrim exit?
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
//closes the program
Application.Exit();
}
}
//Menu Button "Help" - message box with info on how to play
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("How To Play"
+ "\n"
+ "\n"
+ "The idea of the game is to crack the code"
+ "\n"
+ "\n"
+ "1 - Press each of the four number buttons to select a different number"
+ "\n"
+ "2 - Left mouse increases number, Right decreases "
+ "\n"
+ "3 - Press the 'Enter Code' button to submit your code"
+ "\n"
+ "4 - The four numbers are added to the list above"
+ "\n"
+ "5 - If the number is Red, the number is correct but in the wrong place"
+ "\n"
+ "6 - If the number is Blue, the number is correct and in the right place"
+ "\n"
+ "7 - If the number is Yellow, the number is correct,in the right place, but appears more then once in the code"
+ "\n"
+ "8 - Try different combinations of numbers to try and crack the code"
+ "\n"
+ "\n"
+ "Good Luck :)"
, "Help");
}
//Menu Button "Info" - Random Info in a message box??
private void infoToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Number Cracker"
+ "\n"
+ "\n"
+ "Version 1.0"
+ "\n"
+ "Created by Matt Garrett"
+ "\n"
+ "\n"
+ "Copyright (c) 2013 Matt Garrett"
+ "\n"
, "Info");
}
}
}
Pages: 1 2