MENU CODE ************************************************************************************************************************ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; using System.Media; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace prjHangTheMan { public partial class Menu : Form { SoundPlayer player = new SoundPlayer();//Initialize the music player /*********************** **Variables Start Here** ***********************/ //use public static so it doesn't reset when changing forms public static int[] highScore = new int[6]; public static Boolean loadOnce = true;//Only read the scores once, we don't want to overwrite them. DialogResult dr;//Store the result of a dialog box /********************* **Variables End Here** *********************/ /********************* *Functions Start Here* *********************/ void readScores() { int x = 0; string line; if (File.Exists("scores.txt") == true)//Check if there are any highscores saved. If there are, load them { // Read the file line by line, putting each value into the highScore array StreamReader file = new StreamReader("scores.txt"); while ((line = file.ReadLine()) != null) { highScore[x] = Convert.ToInt32(line); x++; } file.Close(); } } /****************************************** *This function makes use of bubble sorting* ******************************************/ void checkLastScore() { //sets the score from the other form to highScore[5] //this makes bubble sorting the scores much easier highScore[5] = Game.score; for (int x = 0; x < 5; x++) { for (int y = 0; y < 5; y++) { //checks if highScore is less than the highScore above it in the array if (highScore[y] < highScore[y + 1]) { //highscores switch places int temp = 0; temp = highScore[y]; highScore[y] = highScore[y + 1]; highScore[y + 1] = temp; } } } //sets the highscores to the labels lblHighscore1.Text = Convert.ToString(highScore[0]); lblHighscore2.Text = Convert.ToString(highScore[1]); lblHighscore3.Text = Convert.ToString(highScore[2]); lblHighscore4.Text = Convert.ToString(highScore[3]); lblHighscore5.Text = Convert.ToString(highScore[4]); } void saveScores() { //Output the scores to a text file. StreamWriter file = new StreamWriter("scores.txt"); for (int x = 0; x < highScore.Length; x++) { file.WriteLine(highScore[x]); } file.Close(); } void clearScores() { //Set all of the labels to read 0 lblHighscore1.Text = "0"; lblHighscore2.Text = "0"; lblHighscore3.Text = "0"; lblHighscore4.Text = "0"; lblHighscore5.Text = "0"; StreamWriter file = new StreamWriter("scores.txt");//Write 0s to the scores.txt file for (int x = 0; x < highScore.Length; x++) { file.WriteLine(0); } file.Close(); } void toggleMusic() { if (Game.music == true) { pbMusic.Image = Image.FromFile("Images/musicoff.png"); Game.music = false; player.Stop();//Stop the music } else { pbMusic.Image = Image.FromFile("Images/musicon.png"); Game.music = true; player.PlayLooping();//Play the music } } /********************* **Functions End Here** *********************/ public Menu() { InitializeComponent(); player.SoundLocation = "Audio/Bob-ombBattlefield.wav";//Tell the music player what file we want to play } private void Menu_Load(object sender, EventArgs e)//Code when the form loads { if (loadOnce == true)//Only read the scores once, we don't want to overwrite them. { readScores(); loadOnce = false;//Make sure we don't overwrite the scores } checkLastScore();//Check the score from the previous game if (Game.music == true)//Check if the music was already playing { pbMusic.Image = Image.FromFile("Images/musicon.png"); } else { pbMusic.Image = Image.FromFile("Images/musicoff.png"); } } private void Menu_FormClosed(object sender, FormClosedEventArgs e)//Code when the form closes { saveScores();//Save the current highscores list Application.Exit();//Close the program } /************************************************************************ *The following events take place when the user clicks one of the buttons* ************************************************************************/ private void lblCreators_Click(object sender, EventArgs e)//The "Creators" button { MessageBox.Show("Created By: Kevin, Owen, and Jatin\nJune 1st, 2017");//Show the creators of the game } private void pbMusic_Click(object sender, EventArgs e)//The music button { toggleMusic();//Toggle the music on or off } private void lblClearScores_Click(object sender, EventArgs e)//The "Clear Scores" button { dr = MessageBox.Show("Are you sure?", "Clear Scores", MessageBoxButtons.YesNo);//Show a mesage to warn the user of the severity of their actions. if (dr == DialogResult.Yes) { clearScores();//If they chose "Yes", clear the scores and reset them. readScores(); } } private void lblScoringInfo_Click(object sender, EventArgs e)//The "Scoring Info" button { MessageBox.Show("Score gained per letter: 5\nScore gained when completed a word: 50\nScore gained when solved with text per letter: 50\n\nIf you guess wrong more than six times, our poor stickman volunteer is a goner!*\nPlease note that if you guess the word by typing but guess wrong, its game over!\n\n\n*No stickmen were harmed in the making of this game."); //Display information about scoring } private void lblWebsite_Click(object sender, EventArgs e)//The "Visit Website" button { Process.Start("http://sdsscomputers.com/GoodwinOwen/ASSIGNMENTS/COMPLETED/Unit4Assignment/index.html");//Opens our website } private void lblExit_Click(object sender, EventArgs e)//The "Exit" button { saveScores(); Application.Exit();//Close the program } private void lblPlay_Click(object sender, EventArgs e)//The "Click to Play" button { //Start the game Game Game = new Game();//Load the Game form Game.Show();//Show the Game form this.Visible = false;//hide the Menu form } /***************** *End click events* *****************/ /************************************************************************ *The following events change the colour of the buttons when hovered over* ************************************************************************/ private void lblPlay_MouseEnter(object sender, EventArgs e) { lblPlay.BackColor = Color.Gray; } private void lblPlay_MouseLeave(object sender, EventArgs e) { lblPlay.BackColor = Color.FromArgb(42, 49, 50); } private void lblExit_MouseEnter(object sender, EventArgs e) { lblExit.BackColor = Color.Gray; } private void lblExit_MouseLeave(object sender, EventArgs e) { lblExit.BackColor = Color.FromArgb(42, 49, 50); } private void lblWebsite_MouseEnter(object sender, EventArgs e) { lblWebsite.BackColor = Color.Gray; } private void lblWebsite_MouseLeave(object sender, EventArgs e) { lblWebsite.BackColor = Color.FromArgb(42, 49, 50); } private void lblScoringInfo_MouseEnter(object sender, EventArgs e) { lblScoringInfo.BackColor = Color.Gray; } private void lblScoringInfo_MouseLeave(object sender, EventArgs e) { lblScoringInfo.BackColor = Color.FromArgb(42, 49, 50); } private void lblClearScores_MouseEnter(object sender, EventArgs e) { lblClearScores.BackColor = Color.Gray; } private void lblClearScores_MouseLeave(object sender, EventArgs e) { lblClearScores.BackColor = Color.FromArgb(42, 49, 50); } private void lblCreators_MouseLeave(object sender, EventArgs e) { lblCreators.BackColor = Color.FromArgb(51, 107, 135); } private void lblCreators_MouseEnter(object sender, EventArgs e) { lblCreators.BackColor = Color.DeepSkyBlue; } /********************* *End mouseover events* *********************/ } } GAME CODE ******************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Media; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; namespace prjHangTheMan { public partial class Game : Form { SoundPlayer player = new SoundPlayer();//Initialize the music player /*********************** **Variables Start Here** ***********************/ Random random = new Random();//Initialize the random object String randomWord = "";//To hold our random word that the user must guess String lastWord = "";//The word from the previous round String letter = "";//Holds whatever letter is chosen String underscoresBefore = "";//Number of underscores before the guessed letter String underscoresAfter = "";//Number of underscores after the guessed letter String a = "";//These letter variables are used for guessing words String b = ""; String c = ""; String d = ""; String e = ""; String f = ""; String g = ""; String h = ""; String i = ""; String j = ""; String k = ""; String l = ""; String m = ""; String n = ""; String o = ""; String p = ""; String q = ""; String r = ""; String s = ""; String t = ""; String u = ""; String v = ""; String w = ""; String x = ""; String y = ""; String z = ""; int guessWrong = 0;//Number of wrong guesses in the current round int guessWrongTotal = 0;//Total number of wrong guesses Boolean typed = false;//Used to tell whether the user typed the word in the box /*These 3 variables are declared as public static. The "public" means that they can be accessed in any form, * and the "static" means the variables will always be the same thing in every form. * score needs to be public, because after the player loses and returns to the menu, we need to "send" their score * to the highscore[] array and check if they got a high score. * music is also public, because the music can be turned on or off in either form.*/ public static int score = 0;//In our game it is possible to get a negative score. It is better that way. public static Boolean music = false;//Represents whether the music should be playing or not. /********************* **Variables End Here** *********************/ /********************* **Arrays Start Here** *********************/ String[] Word = new String[53];//Will hold all of our words. String[] loader = new String[53];//Use to load words into the text file. /********************* **Arrays End Here** *********************/ /*********************** **Functions Start Here** ***********************/ /********************************************** *The following 12 functions have no parameters* **********************************************/ void letterCheckAndReplace() { /*this function assigns lblGuess.Text to underscoresBefore before the letterReplace function and lblGuess.Text to underscoresAfter after the letterReplace function. this is to check if the string has changed or not to determine whether the player got a letter in the word or not*/ underscoresBefore = lblGuess.Text; //go to the function for more information lblGuess.Text = letterReplace(letter, randomWord, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z); underscoresAfter = lblGuess.Text; } void completeWord()//The player finished a word. They've won the round! { lblOutput.Text = "Congragulations, you just completed a word!" + "\n" + "Click 'Continue' to keep playing!";//Output message if (typed == true)//They've typed the word { score += 50 * numberUnderscores(underscoresBefore, underscoresAfter);//Give them 50 points per letter that hasn't been already guessed lblScore.Text = Convert.ToString(score);//Show the new score } else//THey've guessed normally - by clicking the individual letters { score += 50;//Give the player 50 points for finishing the word lblScore.Text = Convert.ToString(score);//Show the new score } buttonsOff();//Disables all the letter buttons to prevent unwanted actions lblContinue.Enabled = true;//Enables the Continue button to be able to continue } void lose()//Either they typed the word and got it wrong, or chose 6 wrong letters. Game over! { lblOutput.Text = "Dang man, you lose! The word was " + randomWord + ".\n" + "Click 'Menu' to return to the menu.";//Output message buttonsOff();//Disables all the letter buttons to prevent unwanted actions lblContinue.Enabled = false;//Enables the Continue button to be able to continue } void buttonsOff()//Disable all of the buttons and textbox { lblA.Enabled = false; lblB.Enabled = false; lblC.Enabled = false; lblD.Enabled = false; lblE.Enabled = false; lblF.Enabled = false; lblG.Enabled = false; lblH.Enabled = false; lblI.Enabled = false; lblJ.Enabled = false; lblK.Enabled = false; lblL.Enabled = false; lblM.Enabled = false; lblN.Enabled = false; lblO.Enabled = false; lblP.Enabled = false; lblQ.Enabled = false; lblR.Enabled = false; lblS.Enabled = false; lblT.Enabled = false; lblU.Enabled = false; lblV.Enabled = false; lblW.Enabled = false; lblX.Enabled = false; lblY.Enabled = false; lblZ.Enabled = false; lblGuessWord.Enabled = false; txtWordGuess.Enabled = false; } void buttonsOn()//Enable all the buttons and textbox { lblA.Enabled = true; lblB.Enabled = true; lblC.Enabled = true; lblD.Enabled = true; lblE.Enabled = true; lblF.Enabled = true; lblG.Enabled = true; lblH.Enabled = true; lblI.Enabled = true; lblJ.Enabled = true; lblK.Enabled = true; lblL.Enabled = true; lblM.Enabled = true; lblN.Enabled = true; lblO.Enabled = true; lblP.Enabled = true; lblQ.Enabled = true; lblR.Enabled = true; lblS.Enabled = true; lblT.Enabled = true; lblU.Enabled = true; lblV.Enabled = true; lblW.Enabled = true; lblX.Enabled = true; lblY.Enabled = true; lblZ.Enabled = true; lblGuessWord.Enabled = true; txtWordGuess.Enabled = true; } void letterReset() { a = ""; b = ""; c = ""; d = ""; e = ""; f = ""; g = ""; h = ""; i = ""; j = ""; k = ""; l = ""; m = ""; n = ""; o = ""; p = ""; q = ""; r = ""; s = ""; t = ""; u = ""; v = ""; w = ""; x = ""; y = ""; z = ""; } void incorrect()//When the guessed letter is wrong { lblOutput.Text = "Oops, there is no " + letter + "!";//Output message score -= 3;//Subtract 3 points lblScore.Text = Convert.ToString(score);//Show the new score guessWrongTotal += 1;//adds one to the total amount of wrong guesses the player made guessWrong += 1;//adds one to the number of wrong guesses the player made for a word lblWrong.Text = Convert.ToString(guessWrongTotal);//displays guessWrongTotal pbHang.Image = Image.FromFile("Images/HangMan" + guessWrong + ".png");//changes pbHang to the next stage of the "hanging" based from the guessWrong variable } void correct()//When the guessed letter is right { lblOutput.Text = "Nice, there is a letter " + letter + "!";//Output message score += 5 * numberUnderscores(underscoresBefore, underscoresAfter);//Add points lblScore.Text = Convert.ToString(score);//Show the new score } void checkGuess()//Checks if the typed word is correct { String guessedWord = txtWordGuess.Text;//Sets guessWord to the Text of txtWordGuess given by the players input guessedWord = guessedWord.ToLower();//Converts all the characters to lowercase txtWordGuess.Text = "";//removes the text in txtWordGuess if (guessedWord == randomWord)//checks if guessWord is equal to the randomWord { String end = ""; for (int x = 0; x < randomWord.Length; x++)//This loop adds spaces between each letter in the randomWord { end += (randomWord.Substring(x, 1) + " "); } lblGuess.Text = end;//Display the word with spaces between each letter, now it looks the same as if you guessed it normally underscoresAfter = lblGuess.Text;//Used for scoring completeWord();// } else if (guessedWord == "") { //Nothing happens if the textbox is blank //This makes sure that the player won't lose the game if they accidentaly click the Guess button } else { lose();//The player lost. Guessing the word by typing is a gamble, you only get one shot. } } void toggleMusic() { if (music == true) { pbMusic.Image = Image.FromFile("Images/musicoff.png");//Change the image music = false; player.Stop();//Stop the music } else { pbMusic.Image = Image.FromFile("Images/musicon.png");//Change the image music = true; player.PlayLooping();//Play the music } } /**************************** *This function is recursive!* ****************************/ void loadWords() { int x = 0; string line; if (File.Exists("words.txt") == true) { // Read the file line by line, putting each value into the highScore array StreamReader file = new StreamReader("words.txt"); while ((line = file.ReadLine()) != null) { Word[x] = line; x++; } file.Close();//Close the file. The program will glitch out if we don't. } else//This means that words.txt doesn't exist. Create it with our default words. { loader[0] = "nifty"; loader[1] = "brick"; loader[2] = "microwave"; loader[3] = "lucky"; loader[4] = "bombs"; loader[5] = "powder"; loader[6] = "nintendo"; loader[7] = "mario"; loader[8] = "fire"; loader[9] = "spongebob"; loader[10] = "transcript"; loader[11] = "klutz"; loader[12] = "bookkeeper"; loader[13] = "chophouse"; loader[14] = "handkerchief"; loader[15] = "hangman"; loader[16] = "bamboozled"; loader[17] = "cycle"; loader[18] = "cobweb"; loader[19] = "wizard"; loader[20] = "twelfth"; loader[21] = "keyhole"; loader[22] = "lanschool"; loader[23] = "jackblack"; loader[24] = "crypt"; loader[25] = "bandwagon"; loader[26] = "blitz"; loader[27] = "injury"; loader[28] = "meme"; loader[29] = "oxygen"; loader[30] = "subway"; loader[31] = "strength"; loader[32] = "stretch"; loader[33] = "vaporize"; loader[34] = "abruptly"; loader[35] = "beekeeper"; loader[36] = "pepper"; loader[37] = "salt"; loader[38] = "jumbo"; loader[39] = "wavy"; loader[40] = "scratch"; loader[41] = "jukebox"; loader[42] = "yummy"; loader[43] = "jawbreaker"; loader[44] = "galaxy"; loader[45] = "ivy"; loader[46] = "joking"; loader[47] = "funny"; loader[48] = "jazzy"; loader[49] = "kiwi"; loader[50] = "jelly"; loader[51] = "kazoo"; loader[52] = "zombie"; StreamWriter file = new StreamWriter("words.txt"); while (x