|
1 | 1 | #! /usr/bin/env tclsh
|
2 | 2 | # The game 2048 implemented in Tcl.
|
3 | 3 | # Version 1.0.0.
|
4 |
| -# This code is released under the terms of the MIT license. See the file |
5 |
| -# LICENSE for details. |
| 4 | +# |
| 5 | +# This code is released under the terms of the MIT license. |
| 6 | +# See the file LICENSE for details. |
| 7 | +# |
6 | 8 | # More at:
|
7 |
| -# - https://github.com/dbohdan/2048.tcl -- Git repository; |
8 |
| -# - https://wiki.tcl-lang.org/40557 -- discussion. |
| 9 | +# - https://github.com/dbohdan/2048.tcl -- Git repository |
| 10 | +# - https://wiki.tcl-lang.org/40557 -- discussion |
9 | 11 |
|
10 |
| -package require Tcl 8.5 9 |
| 12 | +package require Tcl 8.6 9 |
11 | 13 |
|
12 | 14 | namespace eval 2048 {
|
13 | 15 | namespace ensemble create
|
@@ -56,15 +58,18 @@ namespace eval 2048 {
|
56 | 58 | foreach cell $cellList {
|
57 | 59 | lassign $cell i j
|
58 | 60 | set c [get-cell $cell]
|
59 |
| - set status [catch [list uplevel $script] cres copts] |
60 |
| - switch $status { |
61 |
| - 2 { |
62 |
| - return -options [dict replace $copts -level 2] $cres |
63 |
| - } |
64 |
| - default { |
65 |
| - return -options $copts $cres |
66 |
| - } |
| 61 | + |
| 62 | + try { |
| 63 | + uplevel $script |
| 64 | + } on ok {res opts} - \ |
| 65 | + on error {res opts} - \ |
| 66 | + on break {res opts} - \ |
| 67 | + on continue {res opts} { |
| 68 | + return -options $opts $res |
| 69 | + } on return {res opts} { |
| 70 | + return -options [dict replace $opts -level 2] $res |
67 | 71 | }
|
| 72 | + |
68 | 73 | set-cell [list $i $j] $c
|
69 | 74 | }
|
70 | 75 | }
|
@@ -352,7 +357,7 @@ namespace eval 2048 {
|
352 | 357 |
|
353 | 358 | switch [scan $playerInput %c] {
|
354 | 359 | 3 {
|
355 |
| - if {$playType eq random} { |
| 360 | + if {$playType eq {random}} { |
356 | 361 | set playType user
|
357 | 362 | } else {
|
358 | 363 | quit-game 0
|
@@ -490,25 +495,24 @@ namespace eval 2048 {
|
490 | 495 | variable inputModeSaved {}
|
491 | 496 | variable inputMethod {}
|
492 | 497 | chan configure stdin -blocking 0
|
493 |
| - if {![catch {package require twapi}]} { |
| 498 | + |
| 499 | + try { |
| 500 | + package require twapi |
| 501 | + |
494 | 502 | set inputModeSaved [twapi::get_console_input_mode stdin]
|
495 | 503 | twapi::modify_console_input_mode stdin -lineinput false \
|
496 | 504 | -echoinput false
|
497 | 505 | set inputMethod twapi
|
498 |
| - } else { |
| 506 | + } on error _ { |
499 | 507 | catch {
|
500 |
| - if {[auto_execok stty] ne {}} { |
501 |
| - if {[catch {set inputModeSaved [ |
502 |
| - exec stty -g 2>@stderr]} eres eopts]} { |
503 |
| - return |
504 |
| - # TODO: Find other ways to save the state of the |
505 |
| - # terminal. |
506 |
| - } |
507 |
| - package require term::ansi::ctrl::unix |
508 |
| - package require term::ansi::send |
509 |
| - term::ansi::ctrl::unix::raw |
510 |
| - set inputMethod raw |
511 |
| - } |
| 508 | + set inputModeSaved [exec stty -g 2>@stderr] |
| 509 | + # TODO: Find other ways to save the state of the |
| 510 | + # terminal. |
| 511 | + |
| 512 | + package require term::ansi::ctrl::unix |
| 513 | + package require term::ansi::send |
| 514 | + term::ansi::ctrl::unix::raw |
| 515 | + set inputMethod raw |
512 | 516 | }
|
513 | 517 | }
|
514 | 518 |
|
|
0 commit comments