UNIT lifeADT; {Methods for the ADT for the Game of Life} INTERFACE const row_max = 20; column_max = 75; blank = ' '; type matrix = array[0..21, 0..76] of char; row_type = 0..21; column_type = 0..76; grid_type = object procedure zero; {pre: matrix has arbitrary values. post: all lelements of matix set to zero.} function isZero:boolean; {pre: none. post: if a non-blank element is found, function set to false else true.} procedure readit; {pre: none. post: matrix has original population and num_generation's value determines how many generations will be generated.} procedure print; {pre: none. post: matrix is printed.} procedure generate; {pre: new generation set to all blanks. post: if old cell is an X and has 2 or 3 neighbors, new cell is X. if old cell is an ' ' and has 3 neighbors, new cell is an X.} private grid:matrix; end {object grid_type}; IMPLEMENTATION procedure zero_em( var new_grid : matrix ); { enables other methods to zero matrices besides grid} var row: row_type; column: column_type; begin for row:= 0 to row_max + 1 do for column:= 0 to column_max + 1 do new_grid[row, column]:= blank end {zero}; procedure grid_type.zero; { Places blanks in the array} begin zero_em( grid ) end; function grid_type.isZero:boolean; {determines if the array is all blanks.} var row: row_type; column: column_type; allBlanks:boolean; begin allBlanks:= true; row:= 0; while (row < row_max) and allBlanks do begin column:= 0; row:= row + 1; while (column < column_max) and allBlanks do begin column:= column + 1; if grid[row, column] <> blank then allBlanks:= false end {while} end {while}; isZero:= allBlanks end {zero}; procedure grid_type.readit; var name, file_name:string; infile:text; row: row_type; column: column_type; begin writeln('type the file name, excluding the DAT extension'); readln(name); file_name:= name + '.dat'; writeln('The full file name is: ', file_name); assign(infile, file_name); reset(infile); row:= 0; while not eof(infile) do BEGIN column:= 0; row := row + 1; while not eoln(infile) do BEGIN column:= column + 1; read(infile, grid[row, column]) end {while not eoln}; readln(infile) {erases eoln mark} end {while not eof}; close(infile) end {readit}; procedure grid_type.print; var row: row_type; column: column_type; begin for row:= 1 to row_max do begin for column:= 1 to column_max do write(grid[row, column]); writeln end end {print}; function neighbor(new_grid: matrix; row, col:integer):integer; {pre: none. post: determines how many non-blank neighbors a cell has.} var j,k, count:integer; begin count:= 0; for j:= row -1 to row + 1 do for k:= col - 1 to col + 1 do if (new_grid[j, k] <> blank) then count:= count + 1; if new_grid[ row, col ] = 'X' then neighbor:= count - 1 {you must remove the target cell} else neighbor:= count end {neighbor}; procedure grid_type.generate; {generates a new generation} var j:row_type; k:column_type; new_grid:matrix; begin zero_em( new_grid ); for j:= 1 to row_max do for k:= 1 to column_max do begin if (grid[j,k] ='X') and (neighbor(grid, j,k) in [2, 3]) then new_grid[j, k]:= 'X'; if (grid[j,k] = blank) and (neighbor(grid, j,k) = 3) then new_grid[j, k]:= 'X' end {for k}; grid:=new_grid; {print(new_grid)} end; end.