function [ xwin, ywin, ties] = bingo ( A, B )

%*****************************************************************************80
%
%% bingo() analyzes a nontransitive set of bingo cards.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    11 January 2022
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Will You Be Alive 10 Years From Now?,
%    Princeton, 2014,
%    ISBN: 978-0691156804,
%    LC: QA273.25.N344
%
%  Input:
%
%    integer A(2,2), B(2,2): the bingo cards for players X and Y.
%
%  Output:
%
%    xwins, ywins, ties: the number of times X wins, Y wins, or there is a tie.
%

%
%  Compute all 720 permutations of the integers 1 through 6.
%
  P = perms ( [ 1, 2, 3, 4, 5, 6 ] );

  xwin = 0;
  ywin = 0;
  ties = 0;

  for loop = 1 : 720
    X = A;
    Y = B;
    row = P(loop,:);
    keepplaying = true;
    i = 1;
    xbingo = 0;
    ybingo = 0;

    while ( keepplaying )

      n = row(i);
      for j = 1 : 2
        for k = 1 : 2
          if ( X(j,k) == n )
            X(j,k) = 0;
          end
          if ( Y(j,k) == n )
            Y(j,k) = 0;
          end
        end
      end

      if ( X(1,1) + X(1,2) == 0 )
        xbingo = true;
      elseif ( X(2,1) + X(2,2) == 0 )
        xbingo = true;
      end

      if ( Y(1,1) + Y(1,2) == 0 )
        ybingo = true;
      elseif ( Y(2,1) + Y(2,2) == 0 )
        ybingo = true;
      end

      if ( xbingo && ybingo )
        ties = ties + 1;
        keepplaying = false;
      elseif ( xbingo && ~ ybingo )
        xwin = xwin + 1;
        keepplaying = false;
      elseif ( ~ xbingo && ybingo )
        ywin = ywin + 1;
        keepplaying = false;
      end

      i = i + 1;

    end

  end

  return
end
