다음을 통해 공유


Visual Basic: hangMan

Table of Contents

You can play the Online version on my website:
http://www.scproject.biz/hangman.php

hangman VB2013:

http://www.scproject.biz/18-11-2013_14.25.18.jpg

Download here

Here's the code:

Public Class  Form1
 
    Dim wc As New  Net.WebClient
    Dim words As List(Of String)
    Dim r As New  Random
 
    Dim alphabetButtons() As Button
    Dim labels As New  List(Of Label)
 
    Dim ignore As Boolean
    Dim stage As Integer  = 0
 
    Dim currentWord As String  = ""
 
    Private Function  getWords() As  List(Of String)
        'get words from online word list
        Dim contents As String  = wc.DownloadString("http://www.scproject.biz/hangMan_words.txt")
        Dim templist As New  List(Of String)(contents.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
        Return templist
    End Function
 
    ''' <summary>
    ''' load event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>initializes alphabetButtons array + sets up click handler.</remarks>
    Private Sub  Form1_Load(sender As  Object, e As EventArgs) Handles MyBase.Load
        Me.DoubleBuffered = True
        alphabetButtons = Me.Controls.OfType(Of Button).Except(New Button() {Button1, Button28}).ToArray
        Array.ForEach(alphabetButtons, Sub(b) AddHandler  b.Click, AddressOf  btn_click)
        Button1.PerformClick() 'new game
    End Sub
 
    ''' <summary>
    ''' letter buttons click event handler
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>disables clicked button.
    ''' reveals any correct letters or increments stage variable.
    ''' sets ignore variable if player wins or loses to prevent further play until new game.</remarks>
    Private Sub  btn_click(sender As  Object, e As EventArgs)
        If ignore Then Return
        Dim b As Button = DirectCast(sender, Button)
        b.Enabled = False
 
        Array.ForEach(labels.ToArray, Sub(lbl) lbl.Text = If(lbl.Tag.ToString = b.Text, b.Text, lbl.Text))
        For x As Integer  = 1 To  labels.Count - 1
            labels(x).Left = labels(x - 1).Right
        Next
 
        If labels(labels.Count - 1).Right > Me.ClientSize.Width - 14 Then
            Me.SetClientSizeCore(labels(labels.Count - 1).Right + 14, 381)
        End If
 
        stage += If(Not labels.Any(Function(lbl) lbl.Text = b.Text), 1, 0)
        ignore = labels.All(Function(lbl) lbl.Text <> " ")  OrElse  stage = 10
 
        Me.Invalidate()
    End Sub
 
    ''' <summary>
    ''' new game
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>resets buttons, labels, + all variables.
    ''' downloads latest word list from server.</remarks>
    Private Sub  Button1_Click(sender As Object, e As  EventArgs) Handles  Button1.Click
        words = getWords()
        Dim word As String  = words(r.Next(0, words.Count)).ToUpper
        While word = currentWord
            word = words(r.Next(0, words.Count)).ToUpper
        End While
        currentWord = word
        Array.ForEach(Me.Controls.OfType(Of Label).ToArray, Sub(lbl) lbl.Dispose())
        Array.ForEach(alphabetButtons, Sub(b) b.Enabled = True)
        Me.SetClientSizeCore(546, 381)
        labels = New  List(Of Label)
        Dim startX As Integer  = 14
        For Each  c As  Char In  word
            Dim lbl As New  Label
            lbl.Text = " "
            lbl.Font = New  Font(Me.Font.Name, 35, FontStyle.Underline)
            lbl.Location = New  Point(startX, 250)
            lbl.Tag = c.ToString
            lbl.AutoSize = True
            Me.Controls.Add(lbl)
            labels.Add(lbl)
            startX = lbl.Right
        Next
        ignore = False
        stage = 0
        Me.Invalidate()
    End Sub
 
    ''' <summary>
    ''' paint event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>this is where the hanged man is drawn, depending on the stage variable.</remarks>
    Private Sub  Form1_Paint(ByVal  sender As  Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        If stage >= 1 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 85, 190, 210, 190)
        End If
        If stage >= 2 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 148, 190, 148, 50)
        End If
        If stage >= 3 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 148, 50, 198, 50)
        End If
        If stage >= 4 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 198, 50, 198, 70)
        End If
        If stage >= 5 Then
            e.Graphics.DrawEllipse(New Pen(Color.Black, 2), New Rectangle(188, 70, 20, 20))
        End If
        If stage >= 6 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 198, 90, 198, 130)
        End If
        If stage >= 7 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 198, 95, 183, 115)
        End If
        If stage >= 8 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 198, 95, 213, 115)
        End If
        If stage >= 9 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 198, 130, 183, 170)
        End If
        If stage >= 10 Then
            e.Graphics.DrawLine(New Pen(Color.Black, 2), 198, 130, 213, 170)
        End If
    End Sub
 
    Private Sub  Button28_Click(sender As Object, e As  EventArgs) Handles  Button28.Click
        'add words to online word list
        Process.Start("http://www.scproject.biz/hangman.php")
    End Sub
 
End Class

Samples Gallery VB2012 version

Samples Gallery VB2010 + C#2010 versions


VB.Net - WordSearch
VB.Net - Vertex
VB.Net - Perspective
VB.Net - MasterMind
VB.Net - OOP BlackJack
VB.Net - Numbers Game
Console BlackJack - VB.Net | C#
TicTacToe - VB.Net | C#
OOP Conway's Game of Life - VB.Net | C#
OOP Sudoku - VB.Net | C#
OctoWords VB.Net | C#
OOP Buttons Guessing Game VB.Net | C#
OOP Tangram Shapes Game VB.Net | C#
VB.Net - Three-card Monte
VB.Net - Split Decisions
VB.Net - Pascal's Pyramid
VB.Net - Random Maze Games
(Office) Wordsearch Creator
VB.Net - Event Driven Programming - LockWords Game
C# - Crack the Lock
VB.Net - Totris