| View previous topic :: View next topic |
| Author |
Message |
wikked Voice
Joined: 24 May 2014 Posts: 1
|
Posted: Sat May 24, 2014 4:06 pm Post subject: Rand and if's |
|
|
I'm new to TCL, and I need a lil help with rand and if's.
| Code: | 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" }
} |
|
|
| Back to top |
|
 |
Madalin Master

Joined: 24 Jun 2005 Posts: 310 Location: Constanta, Romania
|
Posted: Sat May 24, 2014 4:46 pm Post subject: Re: I'm new to TCL, and I need a lil help with rand and if's |
|
|
Try this
| Code: | 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" }
} |
_________________ https://github.com/MadaliNTCL - To chat with me: https://tawk.to/MadaliNTCL |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Mon May 26, 2014 1:10 am Post subject: |
|
|
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: |
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. |
|
| Back to top |
|
 |
|