Hier noch meine Variante mit GUI, wobei ich versucht habe so wenig wie möglich am ursprünglichen Code zu verändern (und ja, normalerweise würde ich start des Programms und GUI-Erstellung trennen, ka warum ich das diesmal anders gemacht habe^^):
Spoiler anzeigen
AutoIt
Opt('MustDeclareVars', 1)
#include "word-list.au3"
#include <Array.au3>
#include <AutoItConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIConstants.au3>
;~ GUI
Global $Main, $LGreeting, $LWord, $EWord, $BEnter, $LInfo
;~ Script
Global Const $aWords = _WordList()
While 1
_start($aWords)
GUIDelete($Main)
WEnd
Func _GUICreate(Const $sTitle, Const $aLength, Const $iTries)
Local $Word
Local Const $Width = 800
Local Const $Height = $Width /2
Local Const $LineHeight = 20
Local Const $InputWidth = 80
Local $Greeting = '+---------------+' & @CRLF & _
'|--' & StringUpper($sTitle) &'--|' & @CRLF & _
'+---------------+' & @CRLF & @CRLF & _
'Errate ein Wort mit ' & $aLength[0][0] & ' Buchstaben'
For $Char = 1 to $aLength[0][0]
$Greeting &= ' _'
$Word &= ' _'
Next
$Greeting &= ' , aus der Kategorie Software Entwicklung.' & @CRLF & _
'Du hast ' & $iTries & ' Versuche, jeder falsche Buchstabe reduziert deine Versuche.' & @CRLF &@CRLF
$Main = GUICreate($sTitle, $Width, $Height)
$LGreeting = GUICtrlCreateLabel($Greeting, 0, 0, $Width, 6*$LineHeight, $SS_CENTER)
GUICtrlSetFont($LGreeting, Default, Default, Default, 'Courier New')
$LWord = GUICtrlCreateLabel('', 0, 6*$LineHeight, $Width, $LineHeight, $SS_CENTER)
GUICtrlSetFont($LWord, Default, Default, Default, 'Courier New')
_Try($aLength, $iTries)
$EWord = GUICtrlCreateEdit('', ($Width - (3*$InputWidth))/2, 8*$LineHeight, $InputWidth, $LineHeight, $SS_CENTER)
GUICtrlSetFont($EWord, Default, Default, Default, 'Courier New')
$BEnter = GUICtrlCreateButton('Versuchen', ($Width + $InputWidth)/2, 8*$LineHeight, $InputWidth, $LineHeight, BitOR($SS_CENTER, $BS_DEFPUSHBUTTON))
GUICtrlSetFont($BEnter, Default, Default, Default, 'Courier New')
$LInfo = GUICtrlCreateLabel('', 0, 10*$LineHeight, $Width, $LineHeight, $SS_CENTER)
GUICtrlSetFont($LInfo, Default, Default, Default, 'Courier New')
GUISetState(@SW_SHOW, $Main)
EndFunc
Func _start(Const $Words, $tries = 8)
Local $input, $hit, $text, $used[0]
Local $search = StringLower($Words[Random(0, Ubound($Words, $UBOUND_ROWS) - 1)])
Local Const $Title = 'Galgenraten'
Local Const $Lost = 'Verloren! Das gesuchte Wort war "' & $search & '".'
Local Const $tried = 'Diesen Buchstaben hast du bereits ausprobiert!'
Local Const $searchComplete = $search
$search = Stringsplit($search, '')
_ArrayColInsert($search, 1)
For $Row = 1 to $search[0][0]
$search[$Row][1] = '_'
Next
_GUICreate($Title, $search, $tries)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
Exit
Case $BEnter
$input = StringSplit(GUICtrlRead($EWord), '')
GUICtrlSetData($EWord, '')
GUICtrlSetData($LInfo, '')
If ($input[0] > $search[0][0]) and _
$input[0] <> 1 then
GUICtrlSetData($LInfo, 'Du hast ein zu langes Wort eingegeben. Versuche es nochmal.')
$tries -= 1
_Try($search, $tries)
ElseIf $input[0] > 1 Then
$text = ''
For $Char = 1 to $input[0]
$text &= $input[$Char]
Next
If $text = $searchComplete Then
_Won($tries)
Return True
Else
$tries -= 1
_Try($search, $tries)
EndIf
ElseIf $input[0] = 1 Then
$hit = false
If _ArraySearch($used, $input[1]) = -1 Then
_ArrayAdd($used, $input[1])
For $Char = 1 to $search[0][0]
If $input[1] = $search[$Char][0] then
$search[$Char][1] = $search[$Char][0]
$hit = true
EndIf
Next
Else
GUICtrlSetData($LInfo, $tried)
Consolewrite($tried & @CRLF)
EndIf
If not $hit Then $tries -= 1
_Try($search, $tries)
EndIf
If $tries = 0 then
GUICtrlSetData($LInfo, $Lost)
ConsoleWrite(@CRLF & $Lost & @CRLF)
Return False
Else
If _ArraySearch($search, '_') = -1 Then
_Won($tries)
Return True
EndIf
EndIf
ControlFocus($Main, '', $EWord)
EndSwitch
WEnd
EndFunc
Func _Try(Const $aArray, Const $iTries)
Local $text = 'Wort:'
For $Row = 1 to $aArray[0][0]
$text &= ' ' & $aArray[$Row][1]
Next
$text &= ' (' & $iTries & ' Versuche noch)' & @CRLF
GUICtrlSetData($LWord, $text)
ConsoleWrite($text)
EndFunc
Func _Won(Const $iTries)
Local Const $Message = 'Gewonnen! ' & $iTries & ' Versuche übrig, gut gemacht.'
GUICtrlSetData($LInfo, $Message)
Consolewrite(@CRLF & $Message & @CRLF)
EndFunc
Alles anzeigen