function games  % simulate a two person zero-sum game
%                                                      
  
%    Edit this program to enter the pay-off matrix P, the probabilities x1
%    (and so x2) and w1 (and so w2), and the length of the game (N). 
%
%    Output will appear in the workspace (see bottom of program);
%    right click on avg_pay to graph the average pay-off as game proceeds. 
%   
%
%
fprintf('\n')
fprintf( '*****************  \n')
fprintf('\n')

fprintf('Three calculations will be performed;\n' )
fprintf('(1) moves, (2) total pay, (3) average pay.\n')
fprintf('\n')
fprintf('Relative time taken for each is approximately equal\n')
fprintf('\n')
fprintf( '*****************  \n')


% enter the strategies
w1=.636364; w2=1-w1;   %  Row player
x1=.3; x2=1-x1;     %  Column player

% Enter the pay-off matrix
P = [2 -2
           -3 4];
           
 N = 10000;%00;  % length of game  (try 10,000 first!)
                     
 tic
 h=waitbar(0,'computing moves...');

 
 
 %  simulate the moves according to the probabilities (w1,w2), (x1,x2)
 for i=1:N
     z=rand; 
        if z < w1
               R(i)=1;
     elseif z > w1
                 R(i)=2;
         end 
             
      z=rand; if z<x1 
                C(i)=1;
      elseif z > x1
                C(i)=2;
          end 
                         waitbar(i/N) 
 
         end 
    toc;         
%  Compute (average) pay-off of game as the game proceeds; avg_pay
 h=waitbar(0,'computing total pay...');
tic

tot_pay(1) = P(R(1),C(1));
for i=2:N;
tot_pay(i) = P(R(i),C(i))+tot_pay(i-1);
                    
    waitbar(i/N) 
                        
  end 
 toc; 

h=waitbar(0,'computing avgerage pay...');
tic
for i=2:N;
  avg_pay(i)=tot_pay(i)/i;
  
                           waitbar(i/N) 

end
 toc;
 
assignin('base', 'x1',x1);
assignin('base', 'x2',x2);
assignin('base', 'w1',w1);
assignin('base', 'w2',w2);
assignin('base', 'P',P);
assignin('base', 'N',N);
assignin('base', 'avg_pay',avg_pay);
assignin('base', 'C',C);
assignin('base', 'R',R);



