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.
Help for those learning Tcl or writing their own scripts.
wikked
Voice
Posts: 1 Joined: Sat May 24, 2014 4:03 pm
Post
by wikked » Sat May 24, 2014 4:06 pm
I'm new to TCL, and I need a lil help with rand and if's.
Code: Select all
proc fight_kick { nick host hand chan text } {
set $rfu rand(1,6)
if { $rfu == 1 } { putmsg $chan "!fu" }
if { $rfu == 2 } { putmsg $chan "!fd" }
if { $rfu == 3 } { putmsg $chan "!fg" }
if { $rfu == 4 } { putmsg $chan "!fk" }
if { $rfu == 5 } { putmsg $chan "!fp" }
if { $rfu == 6 } { putmsg $chan "!fj" }
}
Madalin
Master
Posts: 310 Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:
Post
by Madalin » Sat May 24, 2014 4:46 pm
Try this
Code: Select all
proc fight_kick { nick host hand chan text } {
set rfu [rand 6]
if { $rfu == 1 } { putmsg $chan "!fu" }
if { $rfu == 2 } { putmsg $chan "!fd" }
if { $rfu == 3 } { putmsg $chan "!fg" }
if { $rfu == 4 } { putmsg $chan "!fk" }
if { $rfu == 5 } { putmsg $chan "!fp" }
if { $rfu == 6 } { putmsg $chan "!fj" }
}
caesar
Mint Rubber
Posts: 3778 Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory
Post
by caesar » Mon May 26, 2014 1:10 am
Since [rand 6] will return results from 0 to 5 you need to either add 1 to it like [expr [rand 6] +1] to have both included or change the if lines to start with 0.
Anyway, I would honestly go with a
switch than a bunch of
if statements cos the interpreter will pass through them all, while the switch will go directly to the corresponding number and exit.
Code: Select all
proc fight_kick {nick host hand chan text} {
set rfu [expr [rand 6] +1]
switch -- $rfu {
1 {
set msg "!fu"
}
2 {
set msg "!fd"
}
3 {
set msg "!fg"
}
4 {
set msg "!fk"
}
5 {
set msg "!fp"
}
6 {
set msg "!fj"
}
}
putmsg $chan $msg
}
Once the game is over, the king and the pawn go back in the same box.