Author Topic: Hangman script  (Read 9488 times)

Morio

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 256
Hangman script
« on: May 03, 2009, 04:20:48 AM »
I got bored one day and decided to write this script.

Code: [Select]
Dim Hangman,HMChannel,TotalGuesses,GuessesLeft
Set Hangman = CreateObject("Scripting.Dictionary")
Set GuessesLeft = CreateObject("Scripting.Dictionary")

If GetIniFile("hangman.ini","GameInfo","Total","null") = "null" Then WriteIniFile "hangman.ini","GameInfo","Total",0
If GetIniFile("hangman.ini","GameInfo","Wins","null") = "null" Then WriteIniFile "hangman.ini","GameInfo","Wins",0
If GetIniFile("hangman.ini","GameInfo","Losses","null") = "null" Then WriteIniFile "hangman.ini","GameInfo","Losses",0

TotalGuesses = 5

Sub ONTEXT(Message,Channel,Nickname,Host,ServerNumber)
' If Not Channel = "#Somechannel" Then Exit Sub


If Message = "!playhangman" And Not Hangman.Exists(Channel & ":" & ServerNumber) then
Hangman.Add Channel & ":" & ServerNumber, Nickname
GuessesLeft.add Channel & ":" & ServerNumber, TotalGuesses
PlayHangman "newgame","",Channel & ":" & ServerNumber

End If

If Hangman.Exists(Channel & ":" & ServerNumber) then
If Left(Message,7) = "!guess " And Len(Message) > 7 then
Guess = LCase(Mid(Message,8))
If Len(Guess) > 1 then
PlayHangman "word",Guess,Channel & ":" & ServerNumber
Else
PlayHangman "letter",Guess,Channel & ":" & ServerNumber
End If
End If
End If

End Sub

Sub PlayHangman(Action,Guess,Destination)
If Action = "newgame" then

Word = LCase(GetIdentifier("$randphrase(hangman.txt)"))
TempWord = String(Len(Word),"_")
Channel = Left(Destination,InStr(Destination,":")-1)
ServerNumber = Int(Mid(Destination,InStr(Destination,":")+1))
Hangman.item(Destination) = Hangman.Item(Destination) & "," & Word & "," & TempWord
SendCommand "/echo " & Channel & " " & Word,ServerNumber
SendCommand "/msg " & Channel & " New game of Hangman started, the word is: " & SplitWord(TempWord),ServerNumber
ElseIf Action = "letter" then
Nickname = Left(Hangman.Item(Destination),InStr(Hangman.Item(Destination),",")-1)
TempString = Mid(Hangman.Item(Destination),InStr(Hangman.Item(Destination),",")+1)
Word = Left(TempString,InStr(TempString,",")-1)
TempWord = Mid(TempString,InStr(TempString,",")+1)
Channel = Left(Destination,InStr(Destination,":")-1)
ServerNumber = Int(Mid(Destination,InStr(Destination,":")+1))

If InStr(Word,Guess) > 0 then
TempWord = Reveal(Word,Guess,TempWord)
Hangman.Item(Destination) = Nickname & "," & Word & "," & TempWord
If TempWord = Word then
SendCommand "/msg " & Channel & " The word is " & Word & ". " & Nicnkname & " has been saved",ServerNumber
TotalPlayed = GetIniFile("hangman.ini","GameInfo","Total",0)
WriteIniFile "hangman.ini","GameInfo","Total",TotalPlayed + 1
Wins = GetIniFile("hangman.ini","GameInfo","Wins",0)
WriteIniFile "hangman.ini","GameInfo","Wins",Wins + 1
HangMan.Remove(Destination)
GuessesLeft.Remove(Destination)
Else
SendCommand "/msg " & Channel & " There are one or more " & Guess & "'s in the word: " & SplitWord(TempWord),ServerNumber
End If
Else
GuessesLeft.Item(Destination) = GuessesLeft.Item(Destination) - 1
SendCommand "/msg " & Channel & " There are no " & Guess & "'s in the word. Guesses left: " & GuessesLeft.Item(Destination) & ". " & SplitWord(TempWord),ServerNumber
If GuessesLeft.Item(Destination) = 0 then
SendCommand "/msg " & Channel & " Game over! The word was "& Word & ". " & Nickname & " has been hung",ServerNumber
TotalPlayed = GetIniFile("hangman.ini","GameInfo","Total",0)
WriteIniFile "hangman.ini","GameInfo","Total",TotalPlayed + 1
Losses = GetIniFile("hangman.ini","GameInfo","Losses",0)
WriteIniFile "hangman.ini","GameInfo","Losses",Losses + 1
HangMan.Remove(Destination)
GuessesLeft.Remove(Destination)
End If
End If
ElseIf Action = "word" then
Nickname = Left(Hangman.Item(Destination),InStr(Hangman.Item(Destination),",")-1)
TempString = Mid(Hangman.Item(Destination),InStr(Hangman.Item(Destination),",")+1)
Word = Left(TempString,InStr(TempString,",")-1)
TempWord = Mid(TempString,InStr(TempString,",")+1)
Channel = Left(Destination,InStr(Destination,":")-1)
ServerNumber = Int(Mid(Destination,InStr(Destination,":")+1))
GuessesLeft.Item(Destination) = GuessesLeft.Item(Destination) - 1

If Guess = Word then
SendCommand "/msg " & Channel & " Correct! " & Nickname & " has been saved",ServerNumber
TotalPlayed = GetIniFile("hangman.ini","GameInfo","Total",0)
WriteIniFile "hangman.ini","GameInfo","Total",TotalPlayed + 1
Wins = GetIniFile("hangman.ini","GameInfo","Wins",0)
WriteIniFile "hangman.ini","GameInfo","Wins",Wins + 1

HangMan.Remove(Destination)
GuessesLeft.Remove(Destination)

Else
SendCommand "/msg " & Channel & " Sorry, the word is not " & Guess & ". Guesses left: " & GuessesLeft.Item(Destination),ServerNumber
If GuessesLeft.Item(Destination) = 0 then
SendCommand "/msg " & Channel & " Game over! The word was "& Word & ". " & Nickname & " has been hung",ServerNumber
TotalPlayed = GetIniFile("hangman.ini","GameInfo","Total",0)
WriteIniFile "hangman.ini","GameInfo","Total",TotalPlayed + 1
Losses = GetIniFile("hangman.ini","GameInfo","Losses",0)
WriteIniFile "hangman.ini","GameInfo","Losses",Losses + 1
HangMan.Remove(Destination)
GuessesLeft.Remove(Destination)
End If
End If

End If

End Sub

Function Reveal(Word,Guess,TempWord)
NewTempWord = ""
For i = 1 to Len(Word)
If Mid(TempWord,i,1) = "_" then
If Mid(Word,i,1) = Guess then
NewTempWord = NewTempWord & Guess
Else
NewTempWord = NewTempWord & "_"
End If
Else
NewTempWord = NewTempWord & Mid(TempWord,i,1)
End If
Next
Reveal = NewTempWord
End Function

Function SplitWord(Word)
NewTempWord = ""
For i = 1 to Len(Word)
NewTempWord = NewTempWord  & Mid(Word,i,1) & " "
Next
SplitWord = NewTempWord
End Function

For this script to work you also need to make a file hangman.txt in the script directory, it should just be a normal textfile with one word on each line. You can find plenty of lists like that on the internet but due to some copyright stuff, I won't be posting the list I personally use. So the text file should look something like this:
Code: [Select]
dog
cat
house
tree

Also this script won't work for yourself, and you will see the word when a game is started. The script supports multiple simultaneous games on multiple servers, but only one game per channel though.

To start a game type !playhangman and to guess a letter or word type !guess letter/word

If you have any questions, feel free to ask
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein

zanthal

  • Guest
Re: Hangman script
« Reply #1 on: December 17, 2009, 04:07:59 PM »

 Got this script working without a hitch.  I made a looooong list of biiiiig words and then came to the realization that hangman is a word game that offers no hints, and guessing a word like "deionization" even in a hundred guesses isn't a whole lot of fun.

 I understand that the game would no longer be the According to Hoyle Hangman rules, but would you care to code up a change?  Something to this effect:

 The word is "outfielder"

 The script outputs:   

 The word is:  _ _ _ _ _ _ _ _ _ _

 The player guesses:

 outyielder

 The script outputs: 

 No, but you weren't too far off!  You guessed these letters correctly:

 The word is:  o u t _ i e l d e r


 
 And the game progresses until the word is guessed exactly.  To make it even better, have a points awarded based on how many guesses it took.

 
 I know enough about programming to know that it's a time consuming pain in the @#^ so I understand if you never get around to this.   But if you were to code it, I would use it! 

 
 Thanks

 

Snerf

  • Administrator
  • Hero Member
  • *****
  • Posts: 1968
    • IceChat IRC Client
Re: Hangman script
« Reply #2 on: December 17, 2009, 07:31:54 PM »
Well, this would no longer be a hangman game anymore then.
The IceChat God

Morio

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 256
Re: Hangman script
« Reply #3 on: December 18, 2009, 12:45:17 AM »
I did consider something like this, but as Snerf said, it wouldn't be hangman anymore. Feel free to modify it as you wish though, it would require less than 10 lines of code, but I'm not going to do it.
"I don't know what World War 3 will be fought with, but I know World War 4 with be fought with sticks and stones." - Albert Einstein

zanthal

  • Guest
Re: Hangman script
« Reply #4 on: December 18, 2009, 09:24:22 AM »


 Really, less than 10 lines of script.  Awesome.


zanthal

  • Guest
Re: Hangman script
« Reply #5 on: December 18, 2009, 10:24:47 AM »

 I knew it wouldn't be exactly Hangman anymore.  The more I think about it though, the more what I had in mind is just like Wheel of Fortune, so I'm going to try for that.

zanthal

  • Guest
Re: Hangman script
« Reply #6 on: December 18, 2009, 01:20:13 PM »



  After looking closer at the script here, I'm not sure about something:


 
Quote
SendCommand "/msg " & Channel & " There are one or more " & Guess & "'s in the word: " & SplitWord(TempWord),ServerNumber

  It appears that your script does inform the players if the guessed a letter correctly.  I didn't see that happen when I tested it.  Am I right and that's what this code I quoted is supposed to do?



 

zanthal

  • Guest
Re: Hangman script
« Reply #7 on: December 18, 2009, 02:24:48 PM »

in Sub ONTEXT(Message,Channel,Nickname,Host,ServerNumber)

I'm wondering why it's necessary to go so far as to include Host and Servernumber

Is this required by Icechat Script to identify the sender of a message or did you include it so no one could use someone else's nick and play?


DarkStar

  • Full Member
  • ***
  • Posts: 135
  • Scripts for Praise
    • CorpseCorp
Re: Hangman script
« Reply #8 on: December 18, 2009, 08:58:55 PM »
to be simple, yes, it is required.

Icechat sends the script engine five variables when it receives a text message. those variables must be put somewhere. Even if you are not going to use them in the subroutine. However, it is perfectly acceptable to change the names as you see fit

Sub OnText (A, B, C, D, E)

The above is perfectly acceptable as far as the script engine is concerned, but for obvious reasons it makes it difficult for us.
This message brought to you by the Wonderful Wizard of Oz.

Snerf

  • Administrator
  • Hero Member
  • *****
  • Posts: 1968
    • IceChat IRC Client
Re: Hangman script
« Reply #9 on: December 18, 2009, 10:17:45 PM »
And ServerNumber is totally required, as IceChat is a multiple server capable client, and you need to know on which server the ONTEXT was fire, so you send that variable back with the SendCommand variable.
The IceChat God