Forest fire: Difference between revisions

Content added Content deleted
No edit summary
(Added ZX81 BASIC)
Line 1,551: Line 1,551:
render #g</lang>
render #g</lang>
[[File:ForestFire.png]]
[[File:ForestFire.png]]

==={{header|Sinclair ZX81 BASIC}}===
Requires 16k of RAM.

In essence this is an enhanced version of my ZX Spectrum implementation (see below). The main improvement is that this version shows the ages of the trees: the age is represented using <code>0</code> to <code>9</code>, then <code>A</code> to <code>Z</code>, followed theoretically by the special characters <code>£$:?()><=+-*/;,.</code> (in that order) and only then cycling back to <code>0</code>. Realistically, no tree is likely to live that long.

The subroutine at line 1000 takes a number <code>N</code> and returns its inverse-video string representation as <code>I$</code>.

A couple of other notes on the listing:

(1) some characters need to be entered in <code>G</code>raphics mode, which is accessed using <code>SHIFT</code><code>9</code>. I have represented this using square brackets: so if the listing says <code>[ROSETTA CODE]</code>, you need to go into <code>G</code> mode and type <code>ROSETTA CODE</code> (which will be displayed on the ZX81 screen in inverse video). As a special case, <code>[a]</code> means for you to go into <code>G</code> mode and then type <code>SHIFT</code><code>A</code>. The ZX81 character set does not include either square brackets or lower-case letters, so I hope this convention will not lead to too much confusion.

(2) this program differs from most BASIC examples on Rosetta Code, but resembles most real BASIC programs of more than about 20 lines, in that the line numbers do not always go up smoothly in multiples of ten.
<lang basic> 10 DIM F$(20,30)
20 DIM N$(20,30)
30 LET INIT=.5
40 LET F=.02
50 LET P=.05
60 PRINT AT 0,1;"[FOREST FIRE FOR ROSETTA CODE]"
70 FOR I=0 TO 21
80 PRINT AT I,0;"[ ]"
90 PRINT AT I,31;"[ ]"
100 NEXT I
110 FOR I=1 TO 30
120 PRINT AT 21,I;"[ ]"
130 NEXT I
140 LET G=0
150 LET T=0
160 PRINT AT 21,1;"[GENERATION 0]"
170 PRINT AT 21,20;"[COVER]"
180 FOR I=1 TO 20
190 FOR J=1 TO 30
200 IF RND>=INIT THEN GOTO 240
210 PRINT AT I,J;"0"
220 LET F$(I,J)="0"
230 LET T=T+1
240 NEXT J
250 NEXT I
300 PRINT AT 21,26;"[ ]"
310 LET N=INT (.5+T/6)
320 GOSUB 1000
330 PRINT AT 21,26;I$;"[ PC]"
340 FOR I=1 TO 20
350 PRINT AT I,0;"[>]"
360 FOR J=1 TO 30
380 IF F$(I,J)<>"[a]" THEN GOTO 410
390 LET N$(I,J)=" "
400 GOTO 530
410 IF F$(I,J)<>" " THEN GOTO 433
420 IF RND<=P THEN LET N$(I,J)="0"
430 GOTO 530
433 LET N$(I,J)=CHR$ (1+CODE F$(I,J))
437 IF N$(I,J)>"Z" THEN LET N$(I,J)="£"
440 FOR K=I-1 TO I+1
450 FOR L=J-1 TO J+1
460 IF K=0 OR L=0 OR K=21 OR L=21 THEN GOTO 480
470 IF F$(K,L)="[a]" THEN GOTO 510
480 NEXT L
490 NEXT K
500 GOTO 520
510 LET N$(I,J)="[a]"
520 IF RND<=F THEN LET N$(I,J)="[a]"
530 NEXT J
540 PRINT AT I,0;"[ ]"
550 NEXT I
552 LET G=G+1
554 LET N=G
556 GOSUB 1000
558 PRINT AT 21,12;I$
560 LET T=0
570 FOR I=1 TO 20
575 PRINT AT I,31;"[<]"
580 FOR J=1 TO 30
590 IF N$(I,J)<>"[a]" AND N$(I,J)<>" " THEN LET T=T+1
600 NEXT J
610 LET F$(I)=N$(I)
620 PRINT AT I,1;F$(I)
625 PRINT AT I,31;"[ ]"
630 GOTO 300
1000 LET S$=STR$ N
1010 LET I$=""
1020 FOR K=1 TO LEN S$
1030 LET I$=I$+CHR$ (128+CODE S$(K))
1040 NEXT K
1050 RETURN</lang>
{{out}}
Screenshot [http://www.edmundgriffiths.com/zx81forest.jpg here].


==={{header|Visual Basic .NET}}===
==={{header|Visual Basic .NET}}===