Rock-paper-scissors: Difference between revisions
Content added Content deleted
(→{{header|Tcl}}: Small correction to show error handling) |
m (→{{header|Tcl}}: More comments) |
||
Line 430: | Line 430: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<lang tcl>package require Tcl 8.5 |
<lang tcl>package require Tcl 8.5 |
||
### Choices are represented by integers, which are indices into this list: |
|||
### Rock, Paper, Scissors |
|||
### Normally, idiomatic Tcl code uses names for these sorts of things, but it |
|||
### turns out that using integers simplifies the move-comparison logic. |
|||
# How to ask for a move from the human player |
# How to ask for a move from the human player |
||
Line 443: | Line 448: | ||
set len [string length $line] |
set len [string length $line] |
||
foreach play {0 1 2} name {"rock" "paper" "scissors"} { |
foreach play {0 1 2} name {"rock" "paper" "scissors"} { |
||
# Do a prefix comparison |
|||
if {$len && [string equal -nocase -length $len $line $name]} { |
if {$len && [string equal -nocase -length $len $line $name]} { |
||
return $play |
return $play |