This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

can i ask a question ?

Help for those learning Tcl or writing their own scripts.
Post Reply
p
pektek
Halfop
Posts: 42
Joined: Sat Jul 01, 2023 4:51 pm

can i ask a question ?

Post by pektek »

it sets it, and does most of it but it doesnt reply to the number, any ideas?

Thanks for your help guys ?

Code: Select all

bind pub "-|-" number msg_number 
proc msg_number { nick host hand chan text } {
putquick "PRIVMSG $chan :\00307\002\037(\037\002\00304$nick has started The Number Guessing Game.\00307\037\002)"
putquick "PRIVMSG $chan :\00307\002\037(\037\002\00304The Number Range is From:\002 1 \002To:\002 50 \002\00307\037\002)"
putquick "PRIVMSG $chan :\00307\002\037(\037\002\00304To Guess What Number Type: `guess (Number)\002\00307\037\002)"
}
set number {
"\"1\""
"\"2\""
"\"3\""
"\"4\""
"\"5\""
"\"6\""
"\"7\""
"\"8\""
"\"9\""
"\"10\""
"\"11\""
"\"12\""
"\"13\""
"\"14\""
"\"15\""
"\"16\""
"\"17\""
"\"18\""
"\"19\""
"\"20\""
"\"21\""
"\"22\""
"\"23\""
"\"24\""
"\"25\""
"\"26\""
"\"27\""
"\"28\""
"\"29\""
"\"30\""
"\"31\""
"\"32\""
"\"33\""
"\"34\""
"\"35\""
"\"36\""
"\"37\""
"\"38\""
"\"39\""
"\"40\""
"\"41\""
"\"42\""
"\"43\""
"\"44\""
"\"45\""
"\"46\""
"\"47\""
"\"48\""
"\"49\""
"\"50\""
}
bind pub "-|-" guess msg_guess
proc msg_guess { nick chan host handle text number } {
if {$number = $text} { putquick "PRIVMSG $chan :Congragulations, $nick, You Have Won!" }
if {$number != $text} {
if {$number > $text} { putquick "PRIVMSG $chan :This Is Bigger than the correct Number" }
if {$number < $text} { putquick "PRIVMSG $chan :This is Smaller than the correct Number" }
} else {
return
}
}
User avatar
CrazyCat
Revered One
Posts: 1290
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: can i ask a question ?

Post by CrazyCat »

Remove the quotes:
bind pub -|- number msg_number
w
willyw
Revered One
Posts: 1203
Joined: Thu Jan 15, 2009 12:55 am

Re: can i ask a question ?

Post by willyw »

pektek wrote: Sun Sep 22, 2024 8:01 pm ...
proc msg_guess { nick chan host handle text number } {

...
[/code]
Compared to: https://docs.eggheads.org/using/tcl-commands.html
( text search down to find: bind pub )
where it says:
4. PUB

bind pub <flags> <command> <proc>

procname <nick> <user@host> <handle> <channel> <text>

Aren't the parameters labeled out of order?


I hope this helps.
For a fun (and popular) Trivia game, visit us at: irc.librairc.net #science-fiction . Over 300K Q & A to play in BogusTrivia !
User avatar
CrazyCat
Revered One
Posts: 1290
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Re: can i ask a question ?

Post by CrazyCat »

Lol, I didn't see there was a second bind/proc.
And willyw is right:
proc msg_guess { nick chan host handle text } {
   set unumber [join [lindex [split $text] 0]]
...
But your script can't work as number is a list, not an element of the list.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Re: can i ask a question ?

Post by caesar »

If, for whatever reason, you want to generate a list containing elements from 1 to 50 don't use the stuff you've used in set number { ... }, use something like:

Code: Select all

proc BuildRange {start end} {
    set result {}
    for {set i $start} {$i <= $end} {incr i} {
        lappend result $i
    }
    return $result
}
instead to generate the list of numbers for you. Basic usage would be:

Code: Select all

set numbers [BuildRange 1 50]
for example with this result:

Code: Select all

% set range [BuildRange 1 50]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Anyway, you don't need to generate a list of numbers just to pick a random one from the list in the end, so instead go with something like:

Code: Select all

proc RandomNumber {min max} {
    return [expr {int(rand() * ($max - $min + 1)) + $min}]
}
that would pick a random number in the range of min and max, for example:

Code: Select all

% RandomNumber 1 50
26
The main issue in your code was that the random picked number wasn't stored anywhere, thus when the function terminated the number was erased from memory as well. Anyway, without further due test this out:

Code: Select all

namespace eval GuessNumber {

    bind pub - !randomnumber [namespace current]::StartGame
    bind pub - !guess [namespace current]::Guess

    proc StartGame {nick host hand chan text} {
        variable number
        set number [RandomNumber 1 50]
        puthelp "PRIVMSG $chan :\00307\002\037(\037\002\00304$nick has started The Number Guessing Game.\00307\037\002)"
        puthelp "PRIVMSG $chan :\00307\002\037(\037\002\00304The Number Range is From:\002 1 \002To:\002 50 \002\00307\037\002)"
        puthelp "PRIVMSG $chan :\00307\002\037(\037\002\00304To Guess What Number Type: !guess <number>\002\00307\037\002)"
    }

    proc Guess {nick uhost hand chan text} {
        namespace upvar [namespace current] number no
        if {[scan $text {%d} value] != 1} {
            puthelp "PRIVMSG $chan :Sorry, that's not a valid number."
            return
        }

        CheckNumber $chan $nick $value
    }

    proc RandomNumber {min max} {
        return [expr {int(rand() * ($max - $min + 1)) + $min}]
    }

    proc CheckNumber {chan nick value} {
        variable number
        if {![info exists number] } {
            puthelp "PRIVMSG $chan :Sorry, there's nothing to guess cos game hasn't been started yet."
            return
        }
        if {$value == $number} {
            puthelp "PRIVMSG $chan :Congragulations, $nick, You Have Won!"
            unset number
        } elseif {$value > $number} {
            puthelp "PRIVMSG $chan :This Is Bigger than the correct Number"
        } else {
            puthelp "PRIVMSG $chan :This is Smaller than the correct Number"
        }
    }
}
Once the game is over, the king and the pawn go back in the same box.
Post Reply