By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,977 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,977 IT Pros & Developers. It's quick & easy.

Hangman Game newbie

P: 23
Hi

I'm a newbie to C# and have been instructed to create a Hangman game in SharpDevelop. I don't want the answer to the full code, just some help along the way.

I have included my code thus far and at this stage would like to know how I can get the RandomWordManager, which I found on another site, to display a newly generated word as the textBox1 text when the user enters a new game.

I have enclosed tags around the two forms I have created(one for launch and another for entry into a new game) and then lastly the RandomWordManager class for creating random words for the hangman game.

I'm really not sure if this is on track so far but my idea is to have the correct guesses made by the user in textBox2 replace the special characters in textBox1 if they're correct. Can someone please give me some idea whether this is possible and how to get the RandomWordManager to occupy the textBox1 text on entry into the game?

Thanks a mil.


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5.  
  6. namespace MyHangMan
  7. {
  8.     /// <summary>
  9.     /// Description of MainForm.
  10.     /// </summary>
  11.     public class MainForm : System.Windows.Forms.Form
  12.     {
  13.         private System.Windows.Forms.Button NewGame;
  14.         private System.Windows.Forms.Button Quit;
  15.  
  16.  
  17.  
  18.         public MainForm()
  19.         {
  20.  
  21.             //
  22.             // The InitializeComponent() call is required for Windows Forms designer support.
  23.             //
  24.             InitializeComponent();
  25.  
  26.             //
  27.             // TODO: Add constructor code after the InitializeComponent() call.
  28.             //
  29.         }
  30.  
  31.         [STAThread]
  32.         public static void Main(string[] args)
  33.         {
  34.             Application.Run(new MainForm());
  35.         }
  36.  
  37.         #region Windows Forms Designer generated code
  38.         /// <summary>
  39.         /// This method is required for Windows Forms designer support.
  40.         /// Do not change the method contents inside the source code editor. The Forms designer might
  41.         /// not be able to load this method if it was changed manually.
  42.         /// </summary>
  43.         private void InitializeComponent() {
  44.             this.Quit = new System.Windows.Forms.Button();
  45.             this.NewGame = new System.Windows.Forms.Button();
  46.             this.SuspendLayout();
  47.             // 
  48.             // Quit
  49.             // 
  50.             this.Quit.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  51.                         | System.Windows.Forms.AnchorStyles.Right)));
  52.             this.Quit.Location = new System.Drawing.Point(152, 128);
  53.             this.Quit.Name = "Quit";
  54.             this.Quit.Size = new System.Drawing.Size(152, 48);
  55.             this.Quit.TabIndex = 1;
  56.             this.Quit.Text = "Quit";
  57.             this.Quit.Click += new System.EventHandler(this.Button2Click);
  58.             // 
  59.             // NewGame
  60.             // 
  61.             this.NewGame.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
  62.                         | System.Windows.Forms.AnchorStyles.Right)));
  63.             this.NewGame.Location = new System.Drawing.Point(152, 48);
  64.             this.NewGame.Name = "NewGame";
  65.             this.NewGame.Size = new System.Drawing.Size(152, 48);
  66.             this.NewGame.TabIndex = 0;
  67.             this.NewGame.Text = "New Game";
  68.             this.NewGame.Click += new System.EventHandler(this.Button1Click);
  69.             // 
  70.             // MainForm
  71.             // 
  72.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  73.             this.ClientSize = new System.Drawing.Size(472, 573);
  74.             this.Controls.Add(this.Quit);
  75.             this.Controls.Add(this.NewGame);
  76.             this.Name = "MainForm";
  77.             this.Text = "MainForm";
  78.             this.ResumeLayout(false);
  79.         }
  80.         #endregion
  81.         void Button2Click(object sender, System.EventArgs e)
  82.         {
  83.             Application.Exit();
  84.         }
  85.  
  86.         void Button1Click(object sender, System.EventArgs e)
  87.         {
  88.             StickMan sm = new StickMan();
  89.             sm.Show();
  90.         }
  91.  
  92.  
  93.     }
  94. }
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4.  
  5. namespace MyHangMan
  6. {
  7.     /// <summary>
  8.     /// Description of Form1.
  9.     /// </summary>
  10.     public class StickMan : System.Windows.Forms.Form
  11.     {
  12.         private System.Windows.Forms.TextBox textBox1;
  13.                                 private System.Windows.Forms.TextBox textBox2;
  14.         private System.Windows.Forms.PictureBox pictureBox1;
  15.         private RandomWordManager TheWordManager;
  16.  
  17.         public StickMan()
  18.         {
  19.             //
  20.             // The InitializeComponent() call is required for Windows Forms designer support.
  21.             //
  22.             InitializeComponent();
  23.             TheWordManager = new RandomWordManager();
  24.             //
  25.             // TODO: Add constructor code after the InitializeComponent() call.
  26.             //
  27.         }
  28.  
  29.         #region Windows Forms Designer generated code
  30.         /// <summary>
  31.         /// This method is required for Windows Forms designer support.
  32.         /// Do not change the method contents inside the source code editor. The Forms designer might
  33.         /// not be able to load this method if it was changed manually.
  34.         /// </summary>
  35.         private void InitializeComponent() {
  36.             this.pictureBox1 = new System.Windows.Forms.PictureBox();
  37.                                                 this.textBox2 = new 
  38. System.Windows.Forms.TextBox();
  39.             this.textBox1 = new System.Windows.Forms.TextBox();
  40.             this.SuspendLayout();
  41.             // 
  42.             // pictureBox1
  43.             // 
  44.             this.pictureBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(128)), ((System.Byte)(128)), ((System.Byte)(255)));
  45.             this.pictureBox1.Location = new System.Drawing.Point(152, 64);
  46.             this.pictureBox1.Name = "pictureBox1";
  47.             this.pictureBox1.Size = new System.Drawing.Size(360, 496);
  48.             this.pictureBox1.TabIndex = 0;
  49.             this.pictureBox1.TabStop = false;
  50.                         // 
  51.             // textBox2
  52.             // 
  53.             this.textBox2.Location = new System.Drawing.Point(208, 656);
  54.             this.textBox2.Name = "textBox2";
  55.             this.textBox2.Size = new System.Drawing.Size(248, 20);
  56.             this.textBox2.TabIndex = 2;
  57.             this.textBox2.Text = "";
  58.                                                 // 
  59.             // textBox1
  60.             // 
  61.             this.textBox1.Location = new System.Drawing.Point(208, 608);
  62.             this.textBox1.Name = "textBox1";
  63.             this.textBox1.PasswordChar = '*';
  64.             this.textBox1.ReadOnly = true;
  65.             this.textBox1.Size = new System.Drawing.Size(248, 20);
  66.             this.textBox1.TabIndex = 1;
  67.             this.textBox1.Tag = "";
  68.             this.textBox1.Text = "";
  69.  
  70.             // 
  71.             // StickMan
  72.             // 
  73.             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  74.             this.ClientSize = new System.Drawing.Size(664, 749);
  75.             this.Controls.Add(this.textBox1);
  76.             this.Controls.Add(this.pictureBox1);
  77.             this.Name = "StickMan";
  78.             this.Tag = "";
  79.             this.Text = "Form1";
  80.             this.ResumeLayout(false);
  81.         }
  82.         #endregion
  83.  
  84.     }
  85. }


Expand|Select|Wrap|Line Numbers
  1. namespace MyHangMan
  2. {
  3.     using System;
  4.  
  5.     /// <summary>
  6.     /// Manages choosing a random word for playing hangman
  7.     /// </summary>
  8.     public class RandomWordManager
  9.     {
  10.         private Random RandomPick;
  11.         private string[] words = new string[] 
  12.     {
  13.         "zenith",
  14.         "zephyr",
  15.         "tennis",
  16.         "information",
  17.         "courier",
  18.         "basketball",
  19.         "distinction",
  20.         "telephone",
  21.         "hardship",
  22.         "congressional",
  23.         "fulfillment",
  24.         "courtesy",
  25.         "banana",
  26.         "programmer",
  27.         "canopy",
  28.         "friendship",
  29.         "heaven",
  30.         "chocolate",
  31.         "helicopter",
  32.         "financial",
  33.         "integration",
  34.         "lightheaded",
  35.         "mountainside",
  36.         "snowflake",
  37.         "socialism",
  38.         "monarchy",
  39.         "lucrative",
  40.         "bibliography",
  41.         "television",
  42.         "radiology",
  43.         "peppermint",
  44.         "parakeet",
  45.         "circulation",
  46.         "anatomical",
  47.         "zeppelin",
  48.         "xylophone",
  49.         "sequester",
  50.         "cantaloupe",
  51.         "bonanza",
  52.         "beekeeper",
  53.         "volleyball",
  54.         "massage",
  55.         "cheetah",
  56.         "environmental",
  57.         "horsdoeuvre",
  58.         "flashlight",
  59.         "butterfly",
  60.         "lightweight",
  61.         "extraordinary",
  62.         "hardship",
  63.         "salamander",
  64.         "salesmanship",
  65.         "gigantic",
  66.         "metamorphosis",
  67.         "popularity",
  68.         "punctuation",
  69.         "masquerade",
  70.         "footloose",
  71.         "fellowship",
  72.         "immigration",
  73.         "knowledge"
  74.     };
  75.  
  76.         public RandomWordManager()
  77.         {
  78.           DateTime aTime = new DateTime(1000);
  79.           aTime = DateTime.Now;
  80.           int nSeed = (int)(aTime.Millisecond);
  81.           RandomPick = new Random(nSeed);
  82.             // 
  83.             // TODO: Add Constructor Logic here
  84.             //
  85.         }
  86.  
  87.         public string Pick()
  88.         {
  89.           string newword = "";
  90.           int index = (int)(RandomPick.NextDouble() * words.GetUpperBound(0));
  91.           newword = words[index];
  92.           newword = newword.ToUpper();
  93.           return newword;
  94.         }
  95.     }
  96. }
Mar 21 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.