function winnings = top ( p )

%*****************************************************************************80
%
%% top() analyzes the dreidel game.
%
%  Modified:
%
%    07 May 2019
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Will You Be Alive 10 Years From Now?,
%    Princeton, 2014,
%    ISBN: 978-0691156804,
%    LC: QA273.25.N344
%
%  Input:
%
%    real p, the number of players.
%
%  Output:
%
%    real winnings(p), the expected winnings per player.
%
  winnings = zeros ( p, 1 );

  trial_num = 10000000;

  for game = 1 : trial_num

    pot = p;
    keepplaying = 1;
    player = 1;

    while ( keepplaying == 1 )

      spin = rand ( );

      if ( spin < 0.25 )
      elseif ( spin < 0.50 )
        pot = pot / 2;
        winnings(player) = winnings(player) + pot;
      elseif ( spin < 0.75 )
        winnings(player) = winnings(player) + pot;
        pot = 0;
        keepplaying = 0;
      else
        pot = pot + 1;
        winnings(player) = winnings(player) - 1;
      end

      player = mod ( player, p ) + 1;   

    end

  end

  winnings = winnings / trial_num;

  return
end
