Sign in to follow this  
Followers 0
tomtomtom

Wordgame

2 posts in this topic

#1 ·  Posted (edited)

Written as an exercise for myself. I am sure many people have done this before. I believe it is called "word mastermind". I would be interested in helpful comments from more experienced prorgrammers. You need to download a dictionary with either English or Dutch words. Follow the instructions in the heading of the code.

Regards,

Tom

; Wordgame. Guess the secret word. The computer will tell you how many matches there are (the right character in the right place = one match). See how many attempts you need.

;

; You need a sorted file with puzzlewords;

;Either (English): Download and unzip "ENABLE" from http://personal.riverusers.com/~thegrendel/. Then paste WORD.LST into the directory in which this program is located

;Or (Dutch): Download and unzip Woordenlijst.zip from http://members.chello.nl/g.de.blaauw/downloads.html. Then paste NED.TXT into the same directory in which this program is located

;

#include <GuiConstants.au3>

#include <GuiListView.au3>

#include <Array.au3>

;

DIM $NL[200]

DIM $EN[200]

DIM $TX[200]

$NL[10] = "Raad het geheime woord"

$EN[10] = "Guess the secret word"

$NL[20] = "DIT IS HET WOORDSPEL GALGJE"

$EN[20] = "Wordgame"

$NL[30] = "Met hoeveel letters wilt u spelen? Min 2, max 9, aanbevolen 5-7 "

$EN[30] = "Desired length of the secret word? Min 2, max 9, recommended 5-7 "

$NL[40] = "Even geduld alstublieft"

$EN[40] = "One moment please"

$NL[50] = "Uw computer stelt een tabel samen met woorden van "

$EN[50] = "Your computer is composing a list of words with a length of "

$NL[60] = " letters"

$EN[60] = " letters"

$NL[70] = "Overzicht van woorden die u hebt ingevoerd"

$EN[70] = "These are the words that you have tried so far"

$NL[75] = "Poging"

$EN[75] = "Attempt"

$NL[78] = "Matches"

$NL[78] = "Matches"

$NL[80] = "Volgende poging"

$EN[80] = "Next attempt"

$NL[90] = "woord "

$EN[90] = "your guess "

$NL[100] = "fout"

$EN[100] = "wrong"

$NL[110] = "Gefeliciteerd"

$EN[110] = "Congratulations"

$NL[120] = "U hebt het geheime woord gevonden in "

$EN[120] = "You have found the secret word in "

$NL[130] = " pogingen"

$EN[130] = " attempts"

If FileExists("word.lst") then

$PuzzleFile = "word.lst"

$TX = $EN

else

$PuzzleFile = "ned.txt"

$TX = $EN

EndIf

Dim $ParseGuess [10]; array to parse the guess

Dim $ParseSecret[10]; array to parse the secret word

Global $hListView

;create and display window for background

$MainHandle = GUICreate($TX[10]& " "&"release 0", 1440, 850, -1, -1,$WS_MAXIMIZEBOX)

GUISetState()

;MsgBox(1, "debug50","main window created");DebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebug

; ask for desired wordlength >1 and <10 or "test" for testmode

$test="n"; flag to indicate test mode

$wordlength = 0

While ($wordlength < 2 or $wordlength > 9)

$wordlength=InputBox($TX[20], $TX[30], "","",400, 50)

If (@Error=1) then Exit

If ($wordlength = "test" or $wordlength = "TEST") then $test = "y"

WEnd

;Open file with puzzlewords

$FHandle= FileOpen($PuzzleFile,0)

If $FHandle = -1 then

MsgBox(1, "error", "file "&$PuzzleFile& " not found")

exit

EndIf

;create an array with all puzzlewords with the desired length

SplashTextOn($TX[40], $TX[50] & $wordlength & $TX[60],500,50)

;count the number of words with the specified length

$count = 0

$EndOfFile = "n"

While $EndOfFile = "n"

$word = FileReadLine($FHandle)

If @error <> 0 then $EndOfFile = "y"

if stringlen($word) = $wordlength Then

$count = $count + 1

EndIf

WEnd

FileClose($FHandle)

Dim $Tabel[$count]; define array now that we know the number of words

$FHandle= FileOpen($PuzzleFile,0); re-open the file and copy every word with the specified lenght to the array:

$count = 0

$EndOfFile = "n"

While $EndOfFile = "n"

$word = FileReadLine($FHandle)

If @error <> 0 then $EndOfFile = "y"

if stringlen($word) = $wordlength Then

$Tabel[$count]= $word

$count = $count + 1

;MsgBox(1, "debug75",$count &""& $word);DebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebug

EndIf

WEnd

SplashOff()

If($test = "y") Then

MsgBox(1, "Nr of words of the rquired length", $count)

EndIf

;random selection of the secret word

$SecretIndex = Random(1,$count,1)

$SecretWord = $Tabel[$SecretIndex]

If($test = "y") Then

MsgBox(1, "The secret word is ", $SecretWord)

EndIf

;

;

;Parse the secret word:

For $k = 1 to $wordlength; Parse the guess

$ParseSecret[$k]=StringMid($SecretWord,$k,1)

Next

CreateTable() ;create the table in which all attempts are displayed

;MsgBox(1, "debug100","table created");DebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebug

Algorithm(); this is the main loop to enter and check attempts

Exit

Func CreateTable();create the table in which all attempts are displayed

GUICreate($TX[70], 400, 695)

$hListView = GUICtrlCreateListView("", 2, 2, 394, 695)

GUISetState()

; Add columns

_GuiCtrlListView_AddColumn ($hListView, $TX[75], 70,0)

For $J = 1 to $wordlength

_GuiCtrlListView_AddColumn ($hListView,"",20,2)

Next

_GuiCtrlListView_AddColumn ($hListView, $TX[78], 70, 2)

EndFunc ; ==>CreateTable()

Func Algorithm(); main loop for new attempts

; Add items

;_GuiCtrlListView_BeginUpdate ($hListView)

$AttemptNr = 1

$matches =0

While ($matches <>$wordlength); carry on until all letters match

$guess =""

While (StringLen($guess) <> $wordlength); do not accept words with a wrong length

$guess = InputBox($TX[80], $TX[90],"","",400,50,1,100)

If (@Error=1) then Exit

Wend

;

;

For $k = 1 to $wordlength; Parse the guess

$ParseGuess[$k]=StringMid($guess,$k,1)

Next

;

; Find the guess word in the table:

$find =_ArrayBinarySearch ( $Tabel, $guess , 1 )

_GuiCtrlListView_AddItem ($hListView, $AttemptNr)

;

; display the word as separate characters:

For $iii = 1 to $wordlength

_GUICtrlListView_AddSubItem($hListView, $AttemptNr-1, $ParseGuess[$iii], $iii); $AttemptNr-1 becuause the index of the item should be zero based

next

; count the number of matches:

$matches = 0

For $iii = 1 to $wordlength

If ($ParseGuess [$iii] = $ParseSecret[$iii]) then

$matches = $matches + 1

EndIf

Next

If $find = "" then $matches = $TX[100]; this word does not exist

;

_GUICtrlListView_AddSubItem($hListView, $AttemptNr -1, $matches, $iii); $AttemptNr-1 becuause the index of the item should be zero based

If $AttemptNr > 47 Then

_GUICtrlListView_Scroll ($hListView, 0, 14); Scroll control 14 pixels = 1 line

EndIf

$AttemptNr = $AttemptNr + 1

WEnd

MsgBox(1,$TX[110],$TX[120]& $AttemptNr-1 & $TX[130])

exit

EndFunc

Edited by tomtomtom

Share this post


Link to post
Share on other sites



Do yourself a favor by editing your script and using "

" tags. (also, deleting the second thread would be nice too  )

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0