Results 1 to 3 of 3

Thread: [VB.Net]Hangman

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    [VB.Net]Hangman

    This is the source code and the .exe

    Features:
    1. Plays hangman


    Drawbacks:
    1. Not enough words in my word list :P


    Notes:
    • It's a fun game.


    Full Project:
    hangman.zip

    Source Code:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private word_list() As String
        Private word As String
        Private r As New Random
        Private damage As Single
    
    #Region "Button Clicks"
    
        Private Sub btn_click(ByVal btn As Button)
            btn.Enabled = False
    
            Call check(btn.Text)
    
        End Sub
    
        Private Sub Buttons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button26.Click, Button25.Click, Button24.Click, Button23.Click, Button22.Click, Button21.Click, Button20.Click, Button2.Click, Button19.Click, Button18.Click, Button17.Click, Button16.Click, Button15.Click, Button14.Click, Button13.Click, Button12.Click, Button11.Click, Button10.Click, Button1.Click
            Dim btn As Button = DirectCast(sender, Button)
    
            Call btn_click(btn)
        End Sub
    
        Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            For Each btn As Button In Panel1.Controls.OfType(Of Button)()
                If e.KeyCode.ToString.ToLower = btn.Text.ToLower Then
                    Call btn_click(btn)
                End If
            Next
        End Sub
    
    #End Region
    
    #Region "Check Guess and New Game"
    
        Private Sub check(ByVal letter As String)
            If word.Contains(letter.ToLower) Then
                'Match
                Dim indexes As New List(Of Integer)
                For i As Integer = 0 To word.Length - 1
                    If word.Substring(i, 1).ToLower = letter.ToLower Then
                        indexes.Add(i)
                    End If
                Next
    
                For Each Int As Integer In indexes
                    TextBox1.Text = TextBox1.Text.Remove(Int * 4, 4).Insert(Int * 4, letter.ToUpper & "   ")
                Next
    
                If TextBox1.Text.Contains("_") = False Then
                    MessageBox.Show("Congratulations, you've won!", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Call newgame()
                End If
    
            Else
                'Not-a-Match
                damage += 1
    
                Select Case damage
                    Case 1
                        Head.Visible = True
                    Case 2
                        body.Visible = True
                    Case 3
                        left_arm.Visible = True
                    Case 4
                        right_arm.Visible = True
                    Case 5
                        left_leg.Visible = True
                    Case 6
                        right_leg.Visible = True
                        MessageBox.Show("Sorry, but you lost. The word was: " & word, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Call newgame()
                End Select
            End If
        End Sub
    
        Private Sub newgame()
            'Reset Body
            Head.Visible = False
            body.Visible = False
            left_arm.Visible = False
            right_arm.Visible = False
            left_leg.Visible = False
            right_leg.Visible = False
    
            'No more damage
            damage = 0
    
            'Choose random word
            Dim i As Integer = r.Next(0, word_list.Count)
            word = word_list(i)
    
            'Decrypt word
            TextBox1.Clear()
            For int As Integer = 0 To word.Length - 1
                TextBox1.Text &= "__  "
            Next
    
            'Reset button
            For Each btn As Button In Panel1.Controls.OfType(Of Button)()
                btn.Enabled = True
            Next
    
        End Sub
    
    #End Region
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim del() As String = {Environment.NewLine}
            word_list = My.Resources.word_file.Split(del, StringSplitOptions.RemoveEmptyEntries)
    
            Call newgame()
        End Sub
    
    End Class
    To compile the source you must have:
    • One(1) oval shape named head
    • Five(5) line shapes named: body, left_arm, right_arm, left_leg, and right_leg
    • One(1) textbox, default name
    • One(1) panel, default name
    • Twenty-Six(26) buttons, default name, inside panel1
    • One(1) text file in My.Resources named: word_file
    Last edited by dday9; Mar 25th, 2013 at 05:19 PM. Reason: Forgot to list my.resources.word_file in the requirements
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2

    Re: [VB.Net]Hangman

    Hy,

    Hangman, the game of our childhood

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Re: [VB.Net]Hangman

    Yep, it was just something simple that was whipped together. Eventually I'd like to spice it up with some fancy graphics, but for now it's OK ;]
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width