clear all
global A1 A2 W1 W2

A1=[[2 1];[2 2]];A2=[[2 2];[1 2]];% Harm thy neighbor game. [0 1 0 1]--strong NE
%A1=[[3 1];[4 2]];A2=[[3 4];[1 2]];% Prisoner's Dilemma. [0 1 0 1]-- Unique NE, and ESS
A1=[[0 -1];[1 0]];A2=[[0 1];[-1 0]];% Prisoner's Dilemma. 
%A1=[[2 1];[2 0]];A2=[[2 2];[1 0]];% Harm everyone. [1 0 1 0]-- ESS
%A1=[[0 -1];[1 -2]];A2=[[0 1];[-1 -2]];% Chicken with saddle NE
%A1=[[2 1];[1 2]];A2=A1;% Coordination game with saddle NE

A1=[[-2 2]; [4 -3]];A2=-A1; % zero-sum game 
A1=[[2 1];[1 2]];A2=-A1; % zero-sum game, spiral.
%A1=[[0 -1];[1 -2]];A2=-A1; % zero-sum game without spiral

%A1=[[-2 2 1]; [4 -3 3]; [1 5 -1]];A2=-A1; % zero-sum game
%A1=randn(2,2);A2=randn(2,2);

%Saddle: A1 = [[-0.3298    2.5367];[0.1684    2.2936]]; A2 = [[-1.3187    0.9874];[-0.4741   -2.0730]];
%Another Saddle: A1 = [[-1.0203    2.2325];[1.1278   -0.7742]]; A2 = [[-1.0015   -0.0240];[1.1383   -1.3638]];
%Spiral: A1 =[[0.8326    0.7680];[0.5346    1.7549]];A2 = [[-1.7043   -0.0543];[0.4695   -0.7489]];

ss=size(A1);

W1=rand(ss(1),1);
W2=rand(ss(2),1);
W1=ones(ss(1),1);
W2=ones(ss(2),1);

eps=.01;
%eps=1;
W1=eps*W1;W2=eps*W2;

%%
figure(3)
hold off

N=2000;
numofruns=20;

for i=1:numofruns
%xinit=[.2 .8 .6 .4]';
xinit=rand(sum(ss),1);
%xinit=[0.9891 0   0.0669 0]';
xinit(ss(1))=1-sum(xinit(1:(ss(1)-1),1));
xinit(end)=1-sum(xinit((ss(1)+1):end-1,1));
%xinit=[7/11 1-7/11 5/11 1-5/11]';
orbit0=xinit;
orbit=[];
for i=1:N
    orbit=[orbit,orbit0];
    orbit0=twoplayersgeneralgame([],orbit0);
end
plot(orbit(1,:),orbit(ss(1)+1,:),'bo')
hold on
end
%plot([7/11],[5/11],'r.','markersize',20)
axis([0 1 0 1])

%return
%%
options=odeset('e','off','BDF','on','AbsTol',1e-8,'RelTol',1e-8);
Tend=100;
numruns=1;
W1=W1/eps;W2=W2/eps;

for k=1:numruns
xinit=rand(1,sum(ss));
%xinit(1)=1;xinit(3)=1;
xinit(ss(1))=1-sum(xinit(1:ss(1)-1));
xinit(end)=1-sum(xinit((ss(1)+1):end-1));
[t,X]=ode15s('twoplayersgeneralgamevectorfield', [0,Tend], xinit, options);

x=X(:,1);
y=X(:,ss(1)+1);
plot(x,y,'m.')
end
