function winnings = chuckaluck_simulation ( spot, rounds, bet, triple )

%*****************************************************************************80
%
%% chuckaluck_simulation() simulates the Chuck-a-Luck gambling game.
%
%  Discussion:
%
%    The expected value of the game can be estimated by
%      EV = winnings / rounds
%    The exact expected value is
%      EV = ( -1 * 125 + 1 * 75 + 2 * 15 + TRIPLE * 1 ) / 216
%    and in the usual case when TRIPLE is 10, this works out to
%      EV = - 10/216 = -0.0463...
%      
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    22 August 2019
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer SPOT, the bettor's number, between 1 and 6.
%
%    integer ROUNDS, the number of rounds of the game.
%
%    real BET, the amount bet on every round.  Defaults to $1.00
%
%    real TRIPLE, the payoff for triples.  Default is 10.
%
%  Output:
%
%    real WINNINGS, the total winnings.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'CHUCKALUCK_SIMULATION:\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Simulation Chuck-a-Luck gambling.\n' );
%
%  Get SPOT.
%
  if ( nargin < 1 )
    fprintf ( 1, '\n' );
    spot = input ( 'Enter bettor''s spot, between 1 and 6: ' );
  elseif ( ischar ( spot ) )
    spot = str2num ( spot );
  end
%
%  Get ROUNDS.
%
  if ( nargin < 2 )
    fprintf ( 1, '\n' );
    rounds = input ( 'Enter number of rounds: ' );
  elseif ( ischar ( rounds ) )
    rounds = str2num ( rounds );
  end
%
%  Get or default BET.
%
  if ( nargin < 3 )
    bet = 1.00;
  elseif ( ischar ( bet ) )
    bet = str2num ( bet );
  end
%
%  Get or default TRIPLE.
%
  if ( nargin < 4 )
    triple = 10.0;
  elseif ( ischar ( triple ) )
    triple = str2num ( triple );
  end

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Bettor''s spot is %d\n', spot );
  fprintf ( 1, '  %d rounds will be played\n', rounds );
  fprintf ( 1, '  Every round involves a bet of $%g\n', bet );
  fprintf ( 1, '  A triple spot scores a payoff of $%g\n', triple * bet );
  fprintf ( 1, '\n' );
%
%  Throw all three dice, ROUNDS times.
%
  throws = randi ( 6, rounds, 3 );
%
%  Count the number of dice that match SPOT.
%
  matches = ( throws == spot );
%
%  Sum the number of dice that match SPOT on each round
%
  counts = sum ( matches, 2 );
%
%  Locate and count the number of 0, 1, 2 and 3 matches.
%
  none = find ( counts == 0 );
  none_count = length ( none );
  one = find ( counts == 1 );
  one_count = length ( one );
  two = find ( counts == 2 );
  two_count = length ( two );
  three = find ( counts == 3 );
  three_count = length ( three );
%
%  Determine the payoff for each round.
%
  score(none) = -1;
  score(one) = +1;
  score(two) = +2;
  score(three) = triple;
%
%  Determine the total winnings at each round.
%
  score_cum = cumsum ( score );
  score_cum = [ 0, score_cum ] * bet;
%
%  Make a frequency plot.
%
  figure ( 1 );
  hist ( counts );
  bar ( [0,1,2,3], [none_count, one_count, two_count, three_count] );
  xlabel ( '<-- Number of spots showing -->' );
  ylabel ( '<-- Frequency -->' );
  title ( 'Chuck-a-Luck Frequency' );
  grid ( 'on' );
  filename = 'chuckaluck_frequency.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Make a history plot.
%
  figure ( 2 );
  clf ( );
  hold ( 'on' );
  plot ( 0:rounds, score_cum, 'b-', 'linewidth', 3 );
  plot ( [0, rounds ], [0, 0], 'r-', 'linewidth', 3 );
  xlabel ( '<-- Round -->' );
  ylabel ( '<-- Winnings -->' );
  title ( 'Chuck-a-Luck History' );
  grid ( 'on' );
  hold ( 'off' );
  filename = 'chuckaluck_history.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Estimate probabilities.
%
  winnings = score_cum(rounds+1);
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Spots  Frequency  Probability      Payoff\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '      0     %6d        %5.3f  %10.2f\n', ...
    none_count,  none_count / rounds, -1.0 * none_count * bet );
  fprintf ( 1, '      1     %6d        %5.3f  %10.2f\n', ...
    one_count,   one_count / rounds,  +1.0 * one_count * bet );
  fprintf ( 1, '      2     %6d        %5.3f  %10.2f\n', ...
    two_count,   two_count / rounds,  +2.0 * two_count * bet );
  fprintf ( 1, '      3     %6d        %5.3f  %10.2f\n', ...
    three_count, three_count / rounds, triple * three_count * bet );
  fprintf ( 1, '    ---     ------        -----  ----------\n' );
  fprintf ( 1, '      *     %6d        %5.3f  %10.2f\n', ...
    rounds, 1.0, winnings );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'CHUCKALUCK_SIMULATION:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end

