Jump to content

RCRPG/Tcl: Difference between revisions

m
Fixed syntax highlighting.
(Minor improvements for readability)
m (Fixed syntax highlighting.)
 
(4 intermediate revisions by one other user not shown)
Line 1:
{{collection|RCRPG}}[[Category:Tcl]]This versionimplementation of [[task::RCRPG| ]] was typed and tested on a cellphone, so pardon my brevity.
This [[Tcl]] version of [[RCRPG]] was typed and tested on a cellphone, so pardon my brevity.
 
<langsyntaxhighlight Tcllang="tcl">#!/usr/bin/env tclsh
proc help args {
return "RosettaCode 3D single user dungeon in Tcl. Type a command:
Line 33 ⟶ 34:
proc Room {xyz {name {}} {items {}}} { #-- "constructor"
if {$name eq ""} {set name R.[incr ::R()]}
if {![llength $items]} {set items [lpick {sledge {} ladder gold}]}
array set ::R [list $xyz.name $name $xyz.items $items $xyz.exits {}]
}
Line 81 ⟶ 82:
}
set exits $::R($xyz.exits)
if {![llength $exits]} {set exits nowhere}
puts "There are exits towards: [join $exits {, }]"
inventory
Line 112 ⟶ 113:
}
set xyz $x,$y,$z
if {![info exists ::R($xyz.name)]} {Room $xyz}
set ::Self(coords) $xyz
if {$describe} describe
}
 
Line 147 ⟶ 148:
}
proc pretty lst {
if {![llength $lst]} {return nothing}
foreach i $lst {lappend tmp "a $i"}
regsub {(.+),} [join $tmp ", "] {\1, and}
}
 
main $argv</langsyntaxhighlight>
 
==Alternative Version==
Line 164 ⟶ 165:
lappend [@ my items &] teacup ;#-- returns a reference
 
<langsyntaxhighlight Tcllang="tcl">proc help args {
return "RosettaCode 3D single-user dungeon in Tcl. Type a command:
e(ast), s(outh), n(orth), w(est), u(p), d(own)
Line 261 ⟶ 262:
}
set exits [@ here exits]
if {![llength $exits]} {set exits nowhere}
puts "There are exits towards: [join $exits {, }]."
inventory
Line 292 ⟶ 293:
return "No exit towards $where, consider an attack..."
}
if {[catch {@ $coords name}]} {Room $coords}
@ my coords $coords
if {$describe} describe
}
 
Line 322 ⟶ 323:
}
proc pretty lst {
if {![llength $lst]} {return nothing}
foreach i $lst {lappend tmp [expr {$i eq "gold"? $i : "a $i"}]}
regsub {(.+),} [join $tmp ", "] {\1, and}
}
main $argv</langsyntaxhighlight>
9,476

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.