function one_coin_one_die(who_first)

if (who_first ~= 0 & who_first ~= 1)
    fprintf('Sorry, the argument to one_coin_one_die must be 0 (for coin) or 1 (for die).\n');
    return;
end

N = 220;

%s = zeros(N,1);
cwin = 0;
dwin = 0;
steps = 0;

for k = 1:N,
    turn = who_first;
    while 1
        if (turn == 0)
            c = irand(2);
            steps = steps + 1;
            if c == 1
                cwin = cwin+1;
                break;
            end
        else
            dturn = 1;
            while dturn
                d = irand(6);
                steps = steps + 1;
                if ((d == 1) | (d == 6))
                    dturn = 0;
                end
            end
            if d == 1
                dwin = dwin+1;
                break;
            end
        end
        turn = 1-turn;
    end
end
avgsteps = steps/N;
if (who_first == 0)
    fprintf('Coin went first in %d games.\n',N);
else
    fprintf('Die went first in %d games.\n',N);
end
fprintf('Number of coin wins: %6d  %7.4f%%\n',cwin,100*cwin/N);
fprintf('Number of die  wins: %6d  %7.4f%%\n',dwin,100*dwin/N);
fprintf('Average number of turns: %7.4f\n',avgsteps);


end

        

function j = irand(n)
    j = floor(rand(1)*n)+1;
end
