| | 1 | /* |
| | 2 | * HTML status line test |
| | 3 | */ |
| | 4 | |
| | 5 | #include "t3.h" |
| | 6 | #include "tads.h" |
| | 7 | |
| | 8 | _say_embed(str) { tadsSay(str); } |
| | 9 | |
| | 10 | class RuntimeError: object |
| | 11 | construct(errno, ...) { errno_ = errno; } |
| | 12 | display = "Runtime Error: <<errno_>>" |
| | 13 | errno_ = 0 |
| | 14 | ; |
| | 15 | |
| | 16 | global: object |
| | 17 | turncount = 0 |
| | 18 | score = 0 |
| | 19 | statmsg = 'Start Room' |
| | 20 | ; |
| | 21 | |
| | 22 | _main(args) |
| | 23 | { |
| | 24 | try |
| | 25 | { |
| | 26 | t3SetSay(&_say_embed); |
| | 27 | main(); |
| | 28 | } |
| | 29 | catch (RuntimeError rte) |
| | 30 | { |
| | 31 | "\n<<rte.display>>\n"; |
| | 32 | } |
| | 33 | } |
| | 34 | |
| | 35 | main() |
| | 36 | { |
| | 37 | "<center> |
| | 38 | <i><b><font size=+2> |
| | 39 | Welcome! |
| | 40 | </font></b></i> |
| | 41 | </center> |
| | 42 | |
| | 43 | <br><br><br> |
| | 44 | <font color=blue face='TADS-Sans' size=+1> |
| | 45 | Type QUIT to quit, SCORE to add points to the score.\b |
| | 46 | </font>"; |
| | 47 | |
| | 48 | for (;;) |
| | 49 | { |
| | 50 | local cmd; |
| | 51 | |
| | 52 | /* update the status line */ |
| | 53 | showStatus(); |
| | 54 | |
| | 55 | /* count the turn */ |
| | 56 | ++global.turncount; |
| | 57 | |
| | 58 | /* display the prompt and wait for a command */ |
| | 59 | "\b><font face=TADS-Input> "; |
| | 60 | cmd = inputLine(); |
| | 61 | "</font>"; |
| | 62 | |
| | 63 | /* see what we have */ |
| | 64 | switch(cmd.toLower()) |
| | 65 | { |
| | 66 | case 'quit': |
| | 67 | "Bye!\n"; |
| | 68 | return; |
| | 69 | |
| | 70 | case 'score': |
| | 71 | "Adding ten points to the score.\n"; |
| | 72 | global.score += 10; |
| | 73 | break; |
| | 74 | |
| | 75 | default: |
| | 76 | "Ignored.\n"; |
| | 77 | break; |
| | 78 | } |
| | 79 | } |
| | 80 | } |
| | 81 | |
| | 82 | showStatus() |
| | 83 | { |
| | 84 | local scoreStr = spellNum(global.score); |
| | 85 | local turnStr = spellNum(global.turncount); |
| | 86 | |
| | 87 | "<banner id=StatusLine height=previous border> |
| | 88 | <body bgcolor=statusbg text=statustext> |
| | 89 | <b>"; |
| | 90 | |
| | 91 | tadsSay(global.statmsg); |
| | 92 | |
| | 93 | "</b><tab align=right> |
| | 94 | <i><<scoreStr>>/<<turnStr>> |
| | 95 | </i></banner>"; |
| | 96 | } |
| | 97 | |
| | 98 | spellNum(num) |
| | 99 | { |
| | 100 | local str; |
| | 101 | local neg = nil; |
| | 102 | |
| | 103 | /* start with an empty string */ |
| | 104 | str = ''; |
| | 105 | |
| | 106 | /* if it's zero, just return "zero" */ |
| | 107 | if (num == 0) |
| | 108 | return 'zero'; |
| | 109 | |
| | 110 | /* if it's less than zero, note it but work with the absolute value */ |
| | 111 | if (num < 0) |
| | 112 | { |
| | 113 | neg = true; |
| | 114 | num = -num; |
| | 115 | } |
| | 116 | |
| | 117 | /* add the billions if we're in the billions */ |
| | 118 | if (num >= 1000000000) |
| | 119 | { |
| | 120 | str += spellNum(num / 1000000000) + ' billion '; |
| | 121 | num %= 1000000000; |
| | 122 | } |
| | 123 | |
| | 124 | /* add the millions */ |
| | 125 | if (num >= 1000000) |
| | 126 | { |
| | 127 | str += spellNum(num / 1000000) + ' million '; |
| | 128 | num %= 1000000; |
| | 129 | } |
| | 130 | |
| | 131 | /* add the thousands */ |
| | 132 | if (num >= 1000) |
| | 133 | { |
| | 134 | str += spellNum(num / 1000) + ' thousand '; |
| | 135 | num %= 1000; |
| | 136 | } |
| | 137 | |
| | 138 | /* |
| | 139 | * add the hundreds (just a single-digit number of hundreds can |
| | 140 | * remain, because we've already dispatched the thousands) |
| | 141 | */ |
| | 142 | if (num >= 100) |
| | 143 | { |
| | 144 | str += spellNum(num / 100) + ' hundred '; |
| | 145 | num %= 100; |
| | 146 | } |
| | 147 | |
| | 148 | /* |
| | 149 | * if the remainder is zero, we've unnecessarily left a trailing |
| | 150 | * space in the number - if this is the case, remove the trailing |
| | 151 | * space |
| | 152 | */ |
| | 153 | if (num == 0) |
| | 154 | str = str.substr(1, str.length() - 1); |
| | 155 | |
| | 156 | /* |
| | 157 | * Add the tens (just a single-digit number of tens can remain, |
| | 158 | * because we've already dispatched the hundreds). If we're in the |
| | 159 | * teens, add nothing now; we consider the teens to be a number of |
| | 160 | * units because of the irregular naming English uses for the teens. |
| | 161 | */ |
| | 162 | if (num >= 20) |
| | 163 | { |
| | 164 | local tens = ['twenty', 'thirty', 'forty', 'fifty', 'sixty', |
| | 165 | 'seventy', 'eighty', 'ninety']; |
| | 166 | |
| | 167 | /* add the tens name */ |
| | 168 | str += tens[(num - 10)/10]; |
| | 169 | num %= 10; |
| | 170 | |
| | 171 | /* add a dash if we have any units left */ |
| | 172 | if (num != 0) |
| | 173 | str += '-'; |
| | 174 | } |
| | 175 | |
| | 176 | /* add the units (or teens) */ |
| | 177 | if (num != 0) |
| | 178 | { |
| | 179 | local units = ['one', 'two', 'three', 'four', 'five', 'six', |
| | 180 | 'seven', 'eight', 'nine', 'ten', 'eleven', |
| | 181 | 'twelve', 'thirteen', 'fourteen', 'fifteen', |
| | 182 | 'sixteen', 'seventeen', 'eighteen', 'nineteen']; |
| | 183 | |
| | 184 | /* add the units name */ |
| | 185 | str += units[num]; |
| | 186 | } |
| | 187 | |
| | 188 | /* if it's negative, put it in parentheses */ |
| | 189 | if (neg) |
| | 190 | str = '(' + str + ')'; |
| | 191 | |
| | 192 | /* return the result */ |
| | 193 | return str; |
| | 194 | } |
| | 195 | |