function life(board)

if nargin == 0
   board = defaultBoard;
end

[board gens speed generation] = resetGame(board);

displayBoard(board, generation);

choice = menu('Main Menu for Life', 'run', 'gen count', 'speed', 'reset', 'quit');
while choice ~= 5
   switch choice
      case 1
         [board generation] = runSimulation(board, generation, gens, speed);
      case 2
         gens = setGenerations(gens);
      case 3
         speed = setSpeed(speed);
      case 4
         [board gens speed generation] = resetGame(defaultBoard);
   end
   choice = menu('Main Menu for Life', 'run', 'gen count', 'speed', 'reset', 'quit');
end

return

% --------------------------------------------------------------------
function board = defaultBoard
board = ['+-------------+';
         '|             |';
         '|    XX X     |';
         '|     XX X    |';
         '|  X  X   XX  |';
         '|   X  X XXX  |';
         '|    XX  X    |';
         '|             |';
         '|             |';
         '+-------------+';];
return

% --------------------------------------------------------------------
function displayBoard(board, generation)
[r c] = size(board);

fprintf('\n\n');
for ii = 1:r
   fprintf('      %s\n', board(ii,:))
end
fprintf('\n Generation number %d\n\n', generation)

return

% --------------------------------------------------------------------
function [board generation] = runSimulation(board, generation, gens, speed)
fprintf('running simulation for %d generations at %.2f seconds per generation.\n', ...
         gens, speed)

for ii = generation+1: generation+gens
   board = nextBoard(board);
   pause(speed)
   displayBoard(board, ii)
end
generation = generation + gens;

return

% --------------------------------------------------------------------
function newBoard = nextBoard(board)
[r c] = size(board);
newBoard = board;
for ii = 2:r-1
   for jj = 2:c-1
      n = neighbors(board, ii, jj);
      if board(ii,jj) == ' '
         if n == 3
            newBoard(ii, jj) = 'X';
         end
      else
         if n == 2 || n == 3
            newBoard(ii, jj) = 'X';
         else
            newBoard(ii, jj) = ' ';
         end
      end
   end
end 
return

% --------------------------------------------------------------------
function n = neighbors(board, r, c)
t = ['XXX';'XCX';'XXX'];
n = sum(sum(t == board(r-1:r+1, c-1:c+1)));
return

% --------------------------------------------------------------------
function gens = setGenerations(gens)
fprintf('\n\nCurrently doing %d generations per run.\n', gens)
gens = input('Enter new number of generations per run: ');
fprintf('\n\n')
return

% --------------------------------------------------------------------
function speed = setSpeed(speed)
fprintf('\n\nCurrently doing %d seconds per generation.\n', speed)
speed = input('Enter new seconds per generation: ');
fprintf('\n\n')
return

% --------------------------------------------------------------------

function [board gens speed generation] = resetGame(board)
board = board;
gens = 10;
speed = 1;
generation = 0;
return

% --------------------------------------------------------------------
