Game Theory Calculation in C++

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university

//Program For Game Theory Calculation

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class game
{
private:
int A[100][100],i,j,minimax1,minimax2,maximin1,maximin2,max,min;
int r1,r2,c1,c2;
float p1,p2,q1,q2,v;

public :
void getdata(void);
void disp(void);
};
void game::getdata(void)
{
cout<<“\n\n Enter the 2X2 matrix…”;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<“\n\n Enter the value for row “<<i+1<<” : “;
cin>>A[i][j];
}
}
}
void game::disp(void)
{
if(A[0][0]<=A[0][1])
{
minimax1 = A[0][0];
}
else
{
minimax1 = A[0][1];
}
if(A[1][0]<=A[1][1])
{
minimax2 = A[1][0];
}
else
{
minimax2 = A[1][1];
}
if(A[0][0]>=A[1][0])
{
maximin1 = A[0][0];
}
else
{
maximin1 = A[1][0];
}
if(A[0][1]>=A[1][1])
{
maximin2 = A[0][1];
}
else
{
maximin2 = A[1][1];
}
r1 = A[1][0] – A[1][1];
if(r1<0)
{
r1 = -r1;
}
r2 = A[0][0] – A[0][1];
if(r2<0)
{
r2 = -r2;
}
c1 = A[0][1] – A[1][1];
if(c1<0)
{
c1 = -c1;
}
c2 = A[0][0] – A[1][0];
if(c2<0)
{
c2 = -c2;
}

p1 = float(r1)/(r1+r2);
p2 = float(r2)/(r1+r2);
q1 = float(c1)/(c1+c2);
q2 = float(c2)/(c1+c2);

v =float(((A[0][0]*r1)+(A[1][0]*r2)))/(r1+r2);

cout<<“\n\n Your Entered matrix is…\n\t\t\t\t MINIMAX Oddments”;
cout<<“\n\t\t\t ———–\n\t\t\t”;
for(i=0;i<2;i++)
{
cout<<” |”;
for(j=0;j<2;j++)
{
cout<<” “<<setw(2)<<A[i][j];
cout<<” |”;
}
if(i == 0)
{
cout<<” “<<minimax1<<” “<<r1;
}
else if(i == 1)
{
cout<<” “<<minimax2<<” “<<r2;
}
cout<<“\n\t\t\t ———–\n\t\t\t”;
}
cout<<“\n\t\t MAXIMIN “<<maximin1<<” “<<maximin2;
cout<<“\n\n\t\t Oddments “<<c1<<” “<<c2;
if(minimax1>=minimax2)
{
cout<<“\n\n\n MAXIMIN (the maximum value of minimax) is : “<<minimax1;
max = minimax1;
}
else
{
cout<<“\n\n\n MAXIMIN (the maximum value of minimax) is : “<<minimax2;
max = minimax2;
}
if(maximin1<=maximin2)
{
cout<<“\n\n\n MINIMAX (the minimum value of maximin) is : “<<maximin1;
min = maximin1;
}
else
{
cout<<“\n\n\n MINIMAX (the minimum value of maximin) is : “<<maximin2;
min = maximin2;
}

if(max == min)
{
cout<<“\n\n\n\n The MAXIMIN == MINIMAX ” ;
cout<<“\n\n\t So, the problem has saddle point”;
cout<<“\n\n\t hence game is of pure strategy.”;
cout<<“\n\n\t And the value of game is : “<<max;
}
else
{
cout<<“\n\n\n\n The MAXIMIN != MINIMAX ” ;
cout<<“\n\n\t So, the problem has no saddle point”;
cout<<“\n\n\t hence game is of mix strategy.”;
cout<<“\n\n\t And the value of game is : “<<v;
}
}
void main()
{
clrscr();
game g;
g.getdata();
g.disp();
getch();
}


output :

Enter the 2X2 matrix…

Enter the value for row 1 : 6
Enter the value for row 1 : 9
Enter the value for row 2 : 8
Enter the value for row 2 : 4

Your Entered matrix is…
MINIMAX Oddments
———–
| 6 | 9 | 6 4
———–
| 8 | 4 | 4 3
———–
MAXIMIN 8 9

Oddments 5 2

MAXIMIN (the maximum value of minimax) is : 6
MINIMAX (the minimum value of maximin) is : 8

The MAXIMIN != MINIMAX

So, the problem has no saddle point
hence game is of mix strategy.
And the value of game is : 6.857143

One thought on “Game Theory Calculation in C++

Leave a Reply

Your email address will not be published. Required fields are marked *