program Life; { For the specification of this program, please refer to the BIO Web pages on http://www.christs.cam.ac.uk/bio/ { See copyright notice at the end of this file. } { For Delphi or Turbo Pascal for Windows, include this line } { uses WinCrt; } { otherwise comment it out. } const BoardSize = 11; var Gen0, OldBoard, Board: array[0..BoardSize+1, 0..BoardSize+1] of Boolean; procedure ShowBoard; var i, j: Integer; begin for i := 1 to BoardSize do begin for j := 1 to BoardSize do begin if Board[i, j] then Write('0') else Write('.'); end; WriteLn; end; end; procedure ReadBoard; const SubBoardSize = 5; var SubBoard: array[1..SubBoardSize, 1..SubBoardSize] of Boolean; i, j: Integer; c: Char; begin WriteLn('Enter starting board with . representing off, anything else means on:'); for i := 1 to SubBoardSize do begin for j := 1 to SubBoardSize do begin Read(c); SubBoard[i, j] := not (c = '.'); end; ReadLn; end; Write('Board read in...'); for i := 0 to BoardSize+1 do for j := 0 to BoardSize+1 do Gen0[i, j] := False; for i := 1 to SubBoardSize do for j := 1 to SubBoardSize do Gen0[ i + (BoardSize - SubBoardSize) div 2, j + (BoardSize - SubBoardSize) div 2] := SubBoard[i, j]; WriteLn('making Generation 0 as follows:'); Board := Gen0; ShowBoard; end; procedure Step; var i, j, c: Integer; begin OldBoard := Board; for i := 1 to BoardSize do for j := 1 to BoardSize do begin c := 0; if OldBoard[i, j - 1] then Inc(c); if OldBoard[i, j + 1] then Inc(c); if OldBoard[i - 1, j - 1] then Inc(c); if OldBoard[i - 1, j] then Inc(c); if OldBoard[i - 1, j + 1] then Inc(c); if OldBoard[i + 1, j - 1] then Inc(c); if OldBoard[i + 1, j] then Inc(c); if OldBoard[i + 1, j + 1] then Inc(c); Board[i, j] := (c = 3) or (OldBoard[i, j] and (c = 2)); end; end; procedure StepN(N: Integer); begin while N > 0 do begin Step; Dec(N); end; end; procedure Spawn; var p: Real; i, j: Integer; begin WriteLn; Write('Enter probability of life:'); ReadLn(p); Randomize; for i := 1 to BoardSize do for j := 1 to BoardSize do Board[i, j] := (Random(65535)/65535) <= p; end; procedure Run; var o: Char; s: String; n: Integer; c: Integer; begin repeat WriteLn; WriteLn('Type command: +N - step N generations, #N - go to generation N, -1 - exit.'); ReadLn(s); case s[1] of '+': begin s := Copy(s, 2, 255); Val(s, n, c); if (c = 0) and (n >= 0) then { no error } begin WriteLn('Stepping ', n:1, ' generations on.'); StepN(n); ShowBoard; end else WriteLn(s, ' is not a valid number.'); end; '-', 'x', 'X': WriteLn('Bye...'); '#': begin s := Copy(s, 2, 255); Val(s, n, c); if (c = 0) and (n >= 0) then { no error } begin WriteLn('Stepping to generation ', n:1); Board := Gen0; StepN(n); ShowBoard; end else WriteLn(s, ' is not a valid number.'); end end; until (s[1] = 'x') or (s[1] = 'X') or (s[1] = '-'); end; begin WriteLn('The game of Life.'); WriteLn('Copyright (c) 1997 The British Informatics Olympiad'); ReadBoard; Run; end. { Program copyright (c) 1997 The British Informatics Olympiad (BIO). This program may be freely copied by persons or organisations involved in the British Informatics Olympiad or the International Olympiad in Informatics, on condition that no changes are made and this notice is not altered. Distribution for profit is forbidden unless permission is first obtained in writing from the BIO. This program is for educational purposes only and comes with no warranty, implied or otherwise, as to its fitness for any purpose. Author: Antony Rix Internet: http://www.christs.cam.ac.uk/bio/ E-mail: a.rix@lineone.net S-mail: The British Informatics Olympiad Christ's College Cambridge CB2 3BU United Kingdom }