function result = tictactoe_test (n_games, player1, player2, order)
% TICTACTOE_TEST    Tabulating statistics based on many tic-tac-toe games
%
% function result = tictactoe_test (n_games, player1, player2, order)
%   A histogram of results will be shown
%
% Input:
%   n_games:   How many games to play
%   player1:     Name of player one (i.e., 'monkey', 'wessel')
%   player2:     Name of player two (i.e., 'monkey', 'wessel')
%                To play against yourself give the same name twice
%   order:       1 = Player 1 starts, 2 = player 2 starts, 0 alternates
% Output:
%   result:       Array of length 3 with the percentages of wins for
%                   player one, draws, and wins for player two

trials = zeros(1,n_games);  % Initialize empty table of each result.
figure (2)
clf
if strcmp (player1, 'user') | strcmp (player2, 'user')
    watch = 1;
else
    watch = 0;
end
result = zeros(1,3);
for i=1:n_games % Play all the games
    if order == 0
        turn = mod (i,2)+1;     % This will alternate between 1 and 2
    else
        turn = order;
    end
    winner = tictactoe (player1, player2, watch, turn);  % Play one game
    result(winner+2) = result(winner+2) + 1;
    figure (2)
    bar (-1:+1, result)
    axis ([-1.5 1.5 0 n_games])
    title (['Games won by player ' player1 ' (-1), Player ' player2 ' (+1), and Draws (0) after ' int2str(i) ' games'])
    drawnow
end
input ('==> Hit return to plot percentages: ');
clf
result = 100*result/n_games;
bar (-1:+1, result)
axis ([-1.5 1.5 0 100])
title ([player1 ' won ' int2str(round(result(1))) '%, ' player2 ' won ' int2str(round(result(3))) '%, with Draws = ' int2str(round(result(2))) '%.'])

