Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- 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;
- using System.IO;
- namespace HangManHW
- {
- //Dominik Lazowskileja
- // HomeWork 4 -- CPSC 275
- // This program is hangman game which is generated through dynamic labels
- // and a picks an answer at random for a text file.
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); //alphabet array for dynamic labels
- List<Label> alphaLabels = new List<Label>(); //list of labels for each alphabet letter
- List<HangManUIDisplay> presLabels = new List<HangManUIDisplay>();
- int guess = 0; // will store wrong guesses
- int winCount = 0; // will be set equal to the length of the random chosen president
- int correctGuess = 0;
- private void Form1_Load(object sender, EventArgs e)
- {
- List<String> presidents = GetPresisdentList(); //getting the list of presidents via method
- Random rand = new Random(); //start of logic used to pick a
- //random president from the list and turn it into a char array
- int randPresident = rand.Next(1, presidents.Count() - 1); //setting the range from 1 to the president list count
- char[] chosenPresident = presidents[randPresident].ToCharArray();
- winCount = presidents[randPresident].Replace(" ", "").Length;
- //Title Label
- Label title = new Label();
- //Control Properties for Label
- title.Location = new System.Drawing.Point(333, 9);
- title.Text = presidents[0].ToString();
- title.AutoSize = true;
- title.Font = new Font("Arial", 16, FontStyle.Bold);
- //Adding Control to Form
- this.Controls.Add(title);
- for (int i = 0; i < chosenPresident.Length; i++) //dynamically creates labels filled with
- {
- if (char.IsWhiteSpace(chosenPresident[i])) //Skips past spaces in president name
- {
- continue;
- }
- //Create Control for Form
- Label presidentLetter = new Label();
- Label blank = new Label();
- //Set control properties for labels
- presidentLetter.Location = new System.Drawing.Point(12 + i * 54, 122);
- presidentLetter.Text = chosenPresident[i].ToString();
- presidentLetter.AutoSize = true;
- presidentLetter.BackColor = System.Drawing.Color.Yellow;
- presidentLetter.Font = new Font("Arial", 14, FontStyle.Bold);
- presidentLetter.Hide();
- blank.Location = new System.Drawing.Point(12 + i * 54, 122); //blank labels as place holders
- blank.AutoSize = true;
- blank.BackColor = System.Drawing.Color.LightGray;
- blank.Font = new Font("Arial", 14, FontStyle.Bold);
- blank.Text = " ";
- blank.Show();
- presLabels.Add(new HangManUIDisplay
- {
- PresidentLetter = presidentLetter,
- Blank = blank
- });
- //Add Control to Form
- this.Controls.Add(presidentLetter);
- this.Controls.Add(blank);
- }
- for (int i = 0; i < alpha.Length; i++) //creating the labels for the alphabet
- {
- //Create Control for Form
- Label alphaChar = new Label();
- //Set control properties for labels
- alphaChar.Location = new System.Drawing.Point(22 + i * 54, 64);
- alphaChar.Text = alpha[i].ToString();
- alphaChar.AutoSize = true;
- alphaChar.BackColor = System.Drawing.Color.LightBlue;
- alphaChar.Font = new Font("Arial", 14, FontStyle.Bold);
- alphaChar.Click += AlphaChar_Click; //click event to label
- //Add Control to Form
- this.Controls.Add(alphaChar);
- }
- }
- private void AlphaChar_Click(object sender, EventArgs e)
- {
- Label alphaLabel = (Label)sender; //giving the label clicked a value inside this event
- string letter = alphaLabel.Text;
- bool found = false;
- foreach (HangManUIDisplay presLetter in presLabels)
- {
- if (presLetter.PresidentLetter.Text == letter) //compares the letter clicked by the user to the current president
- {
- presLetter.PresidentLetter.Show();
- presLetter.Blank.Hide();
- found = true;
- correctGuess++;
- }
- }
- alphaLabel.Hide(); // Hides the label after clicked
- if (!found) // logic used to display the hangman if guess is incorrect
- {
- guess++;
- if (guess == 1)
- {
- pnlString.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlString.Visible = true;
- }
- if (guess == 2)
- {
- pnlHead.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlHead.Visible = true;
- }
- if (guess == 3)
- {
- pnlEye1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlEye1.Visible = true;
- }
- if (guess == 4)
- {
- pnlEye2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlEye2.Visible = true;
- }
- if (guess == 5)
- {
- pnlMouth.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlMouth.Visible = true;
- }
- if (guess == 6)
- {
- pnlTorso.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlTorso.Visible = true;
- }
- if (guess == 7)
- {
- pnlArmR.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlArmR.Visible = true;
- }
- if (guess == 8)
- {
- pnlArmLeft.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlArmLeft.Visible = true;
- }
- if (guess == 9)
- {
- pnlLegL.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlLegL.Visible = true;
- }
- if (guess == 10)
- {
- pnlLegR.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- pnlLegR.Visible = true;
- }
- }
- if (winCount == correctGuess)
- {
- MessageBox.Show("You have won!");
- }
- if (guess >= 10)
- {
- MessageBox.Show("You have reached the max number of guesses");
- }
- }
- private List<string> GetPresisdentList()//list used to read in file
- {
- List<string> presidents = new List<string>();
- using (StreamReader fileInput = new StreamReader("Assignment4.txt")) // passing in object in an array to split it
- {
- presidents = fileInput.ReadToEnd().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
- return presidents;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement