Langton's ant: Difference between revisions

Content added Content deleted
Line 3,792: Line 3,792:
Pascal does not offer complex number arithmetic, so adjusting directions via multiplication of ±i is out. Similarly, it does not offer array manipulation statements, so <code>Cell:=White;</code> must be achieved via explicit for-loops with explicitly stated indices and bounds, and the adjustment of the (x,y) position by (dx,dy) can't be done by array addition. Otherwise, matters are straightforward, so instead this version tries to animate the ant on the screen. Alas, the maximum screen size is 80 characters by 50 lines, except that output to the last line causes a screen scroll so that only 49 lines are available. Alas, this cell array is too small and the bounds are exceeded in step 5,156 - before the ant starts its migration.
Pascal does not offer complex number arithmetic, so adjusting directions via multiplication of ±i is out. Similarly, it does not offer array manipulation statements, so <code>Cell:=White;</code> must be achieved via explicit for-loops with explicitly stated indices and bounds, and the adjustment of the (x,y) position by (dx,dy) can't be done by array addition. Otherwise, matters are straightforward, so instead this version tries to animate the ant on the screen. Alas, the maximum screen size is 80 characters by 50 lines, except that output to the last line causes a screen scroll so that only 49 lines are available. Alas, this cell array is too small and the bounds are exceeded in step 5,156 - before the ant starts its migration.


The animation shows the arrival at a cell with a yellow arrow pointing in the arrival direction. The cell state is investigated to decide the new direction (which is shown as a green arrow), the current cell's state is flipped, and the move to the new cell position is made. To show these events, the programme waits for a keystroke but if the S key is pressed, full speed results.
The animation shows the arrival at a cell with a yellow arrow pointing in the arrival direction. The cell state is investigated to decide the new direction (which is shown as a green arrow), the current cell's state is flipped, and the move to the new cell position is made. To show these events, the programme waits for a keystroke but if the S key is pressed, full speed results. Each stepwise ant move thus requires two keystrokes (one for each of the two directions being shown) however a quirk of Pascal's processing of keyboard symbols has certain keystrokes represented via ''two'' values from ReadKey, so pressing the arrow keys for example provides a doubled advance.


{{works with| Free Pascal}}
{{works with| Free Pascal}}