daily pastebin goal
16%
SHARE
TWEET

Untitled

a guest Sep 25th, 2018 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11.  
  12. namespace HangManHW
  13. {
  14.  
  15.     //Dominik Lazowskileja
  16.     // HomeWork 4 -- CPSC 275
  17.     // This program is hangman game which is generated through dynamic labels
  18.     // and a picks an answer at random for a text file.
  19.     public partial class Form1 : Form
  20.     {
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         private char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray(); //alphabet array for dynamic labels
  27.         List<Label> alphaLabels = new List<Label>(); //list of labels for each alphabet letter
  28.         List<HangManUIDisplay> presLabels = new List<HangManUIDisplay>();
  29.         int guess = 0; // will store wrong guesses
  30.         int winCount = 0; // will be set equal to the length of the random chosen president
  31.         int correctGuess = 0;
  32.  
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.             List<String> presidents = GetPresisdentList(); //getting the list of presidents via method
  36.             Random rand = new Random(); //start of logic used to pick a
  37.                                         //random president from the list and turn it into a char array
  38.             int randPresident = rand.Next(1, presidents.Count() - 1); //setting the range from 1 to the president list count
  39.             char[] chosenPresident = presidents[randPresident].ToCharArray();
  40.             winCount = presidents[randPresident].Replace(" ", "").Length;
  41.  
  42.             //Title Label
  43.             Label title = new Label();
  44.  
  45.             //Control Properties for Label
  46.             title.Location = new System.Drawing.Point(333, 9);
  47.             title.Text = presidents[0].ToString();
  48.             title.AutoSize = true;
  49.             title.Font = new Font("Arial", 16, FontStyle.Bold);
  50.  
  51.             //Adding Control to Form
  52.             this.Controls.Add(title);
  53.  
  54.             for (int i = 0; i < chosenPresident.Length; i++) //dynamically creates labels filled with
  55.             {
  56.                 if (char.IsWhiteSpace(chosenPresident[i])) //Skips past spaces in president name
  57.                 {
  58.                     continue;
  59.                 }
  60.  
  61.                 //Create Control for Form
  62.                 Label presidentLetter = new Label();
  63.                 Label blank = new Label();
  64.  
  65.                 //Set control properties for labels
  66.                 presidentLetter.Location = new System.Drawing.Point(12 + i * 54, 122);
  67.                 presidentLetter.Text = chosenPresident[i].ToString();
  68.                 presidentLetter.AutoSize = true;
  69.                 presidentLetter.BackColor = System.Drawing.Color.Yellow;
  70.                 presidentLetter.Font = new Font("Arial", 14, FontStyle.Bold);
  71.                 presidentLetter.Hide();
  72.  
  73.                 blank.Location = new System.Drawing.Point(12 + i * 54, 122); //blank labels as place holders
  74.                 blank.AutoSize = true;
  75.                 blank.BackColor = System.Drawing.Color.LightGray;
  76.                 blank.Font = new Font("Arial", 14, FontStyle.Bold);
  77.                 blank.Text = " ";
  78.                 blank.Show();
  79.  
  80.                 presLabels.Add(new HangManUIDisplay
  81.                 {
  82.                     PresidentLetter = presidentLetter,
  83.                     Blank = blank
  84.                 });
  85.  
  86.                 //Add Control to Form
  87.                 this.Controls.Add(presidentLetter);
  88.                 this.Controls.Add(blank);
  89.             }
  90.  
  91.  
  92.             for (int i = 0; i < alpha.Length; i++) //creating the labels for the alphabet
  93.             {
  94.                 //Create Control for Form
  95.                 Label alphaChar = new Label();
  96.  
  97.                 //Set control properties for labels
  98.                 alphaChar.Location = new System.Drawing.Point(22 + i * 54, 64);
  99.                 alphaChar.Text = alpha[i].ToString();
  100.                 alphaChar.AutoSize = true;
  101.                 alphaChar.BackColor = System.Drawing.Color.LightBlue;
  102.                 alphaChar.Font = new Font("Arial", 14, FontStyle.Bold);
  103.  
  104.                 alphaChar.Click += AlphaChar_Click; //click event to label
  105.                 //Add Control to Form
  106.                 this.Controls.Add(alphaChar);
  107.             }
  108.  
  109.         }
  110.  
  111.         private void AlphaChar_Click(object sender, EventArgs e)
  112.         {
  113.             Label alphaLabel = (Label)sender; //giving the label clicked a value inside this event
  114.             string letter = alphaLabel.Text;
  115.             bool found = false;
  116.  
  117.  
  118.             foreach (HangManUIDisplay presLetter in presLabels)
  119.             {
  120.                 if (presLetter.PresidentLetter.Text == letter) //compares the letter clicked by the user to the current president
  121.                 {
  122.                     presLetter.PresidentLetter.Show();
  123.                     presLetter.Blank.Hide();
  124.                     found = true;
  125.                     correctGuess++;
  126.                 }
  127.             }
  128.  
  129.             alphaLabel.Hide(); // Hides the label after clicked
  130.             if (!found) // logic used to display the hangman if guess is incorrect
  131.             {
  132.                 guess++;
  133.                 if (guess == 1)
  134.                 {
  135.                     pnlString.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  136.                     pnlString.Visible = true;
  137.                 }
  138.                 if (guess == 2)
  139.                 {
  140.                     pnlHead.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  141.                     pnlHead.Visible = true;
  142.                 }
  143.  
  144.                 if (guess == 3)
  145.                 {
  146.                     pnlEye1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  147.                     pnlEye1.Visible = true;
  148.                 }
  149.  
  150.                 if (guess == 4)
  151.                 {
  152.                     pnlEye2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  153.                     pnlEye2.Visible = true;
  154.                 }
  155.                 if (guess == 5)
  156.                 {
  157.                     pnlMouth.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  158.                     pnlMouth.Visible = true;
  159.                 }
  160.                 if (guess == 6)
  161.                 {
  162.                     pnlTorso.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  163.                     pnlTorso.Visible = true;
  164.                 }
  165.                 if (guess == 7)
  166.                 {
  167.                     pnlArmR.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  168.                     pnlArmR.Visible = true;
  169.                 }
  170.  
  171.                 if (guess == 8)
  172.                 {
  173.                     pnlArmLeft.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  174.                     pnlArmLeft.Visible = true;
  175.                 }
  176.                 if (guess == 9)
  177.                 {
  178.                     pnlLegL.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  179.                     pnlLegL.Visible = true;
  180.                 }
  181.                 if (guess == 10)
  182.                 {
  183.                     pnlLegR.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  184.                     pnlLegR.Visible = true;
  185.                 }
  186.             }
  187.  
  188.             if (winCount == correctGuess)
  189.             {
  190.                 MessageBox.Show("You have won!");
  191.             }
  192.  
  193.             if (guess >= 10)
  194.             {
  195.                 MessageBox.Show("You have reached the max number of guesses");
  196.             }
  197.         }
  198.  
  199.         private List<string> GetPresisdentList()//list used to read in file
  200.         {
  201.             List<string> presidents = new List<string>();
  202.  
  203.             using (StreamReader fileInput = new StreamReader("Assignment4.txt")) // passing in object in an array to split it
  204.             {
  205.                 presidents = fileInput.ReadToEnd().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
  206.  
  207.                 return presidents;
  208.             }
  209.         }
  210.  
  211.  
  212.     }
  213. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top