#include <stdio.h>
#include <pthread.h>
//#include <semaphore.h>
//#include <queue>
#include <unistd.h>
#include <iostream>
#include <stdlib.h>
#include <fstream>
using namespace std;
#define SEED time(0)
#define N 30 /* Number of total students */
#define R 6 /*grid row*/
#define C 5 /*grid column*/
#define M 5 /* Number of cunning students */
#define T 3 /* Number of total teachers */
//looking direction
#define self 0
#define left 1
#define right 2
#define front 3
//seconds for looking
#define K 4
#define L 3
#define D 30
#define V 5
#define W 3
int End =1;
int status[N];
int shame[N];
int grid[R][C];
int t_grid[R][C];
int limit = N;
time_t start,current;
int wicked[M];
int dir1[]={0,0,-1,+1,-1,-1,+1,+1};
int dir2[]={-1,+1,0,0,-1,+1,-1,+1};
//semaphore & mutex declarations
pthread_mutex_t statlock;
pthread_mutex_t tgridlock;
pthread_mutex_t shamelock;
pthread_mutex_t printlock;
void mutex() //initializing mutex
{
pthread_mutex_init(&statlock,0);
pthread_mutex_init(&tgridlock,0);
pthread_mutex_init(&shamelock,0);
pthread_mutex_init(&printlock,0);
}
int timer() // calculate running time
{
current = time(NULL) ;
return (int) (current-start)<D;
}
void * student (void * ID)
{
int id = *((int *) ID);
int row;
int col;
for(int i=0;i<M;i++)
{
for (int a=0;a<R;a++)
{
for(int b=0;b<C;b++)
{
if (id == grid[a][b])
{
row =a;
col = b;
break;
}
}
}
while (id == wicked[i] && shame[id-1]==0 && timer())
{
//cout<<"i am wicked. Id : "<<id<<"\n";
int decision = rand()%5;
int look;
if (decision >= 1)
{
if (row==0 )
{
if (col == 0)
look = 2;
else if (col == C-1) look = 1;
else look = rand()%2 +1;
}
else if (row == R-1)
{
if (col == 0)
{
int temp2[2];
temp2[0]=2;
temp2[1]=3;
int val= rand()%2;
look = temp2[val];
}
else if (col == C-1)
{
int temp2[2];
temp2[0]=1;
temp2[1]=3;
int val= rand()%2;
look = temp2[val];
}
else
{
look = rand()%3 +1;
}
}
else
{
if (col ==0 )
{
int temp2[2];
temp2[0]=2;
temp2[1]=3;
int val= rand()%2;
look = temp2[val];
}
else if (col == C-1)
{
int temp2[2];
temp2[0]=1;
temp2[1]=3;
int val= rand()%2;
look = temp2[val];
}
else
{
look = rand()%3 +1;
}
}
pthread_mutex_lock(&statlock);
if (status[id-1]!=5) status[id-1]= look;
pthread_mutex_unlock(&statlock);
sleep(L);
pthread_mutex_lock(&statlock);
if (status[id-1]!=5) status[id-1]=0;
pthread_mutex_unlock(&statlock);
sleep(K);
}
else
{
look=self;
sleep(K);
}
pthread_mutex_lock(&printlock);
// printf("i am wicked. Id : %d && looking -> %d\n",id,look);
if(look == 0) printf("cunning student : %d looking -> self\n",id);
else if(look==1) printf("cunning student : %d looking -> left\n",id);
else if(look==2) printf("cunning student : %d looking -> right\n",id);
else if(look==3) printf("cunning student : %d looking -> front\n",id);
pthread_mutex_unlock(&printlock);
}
}
}
void * teacher (void * id)
{
int t_id = *((int *) id);
int s_id;
int row2;
int col2;
printf("I am a teacher. Id : %d\n",t_id);
while(timer())
{
for (int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
if(t_grid[i][j]==t_id)
{
s_id = grid[i][j];
row2 = i;
col2 = j;
}
}
}
pthread_mutex_lock(&printlock);
printf("teacher position......\n");
for (int i =0; i<R;i++)
{
for(int j=0;j<C;j++)
{
printf("%d ",t_grid[i][j]);
}
printf("\n");
}
pthread_mutex_unlock(&printlock);
//printf("I am a teacher. Id : %d\n",t_id);
pthread_mutex_lock(&statlock);
if (status[s_id-1]!=0 && status[s_id-1]!=5)
{
pthread_mutex_lock(&shamelock);
shame[s_id-1]=1;
pthread_mutex_unlock(&shamelock);
//status[s_id-1]= 5;
pthread_mutex_lock(&printlock);
printf("teacher : %d expelled : %d looking %d \n",t_id, s_id,status[s_id-1]);
pthread_mutex_unlock(&printlock);
status[s_id-1]= 5;
}
pthread_mutex_unlock(&statlock);
//sleep(1);
printf("\n");
int x[]={0,0,-1,+1,-1,-1,+1,+1};
int y[]={-1,+1,0,0,-1,+1,-1,+1};
int l=8;
while(1)
{
printf("looping.........\n");
int k = rand()%l;
l=l-1;
int a = row2 + x[k];
int b = col2 + y[k];
int flag =0;
pthread_mutex_lock(&tgridlock);
//printf("teacher : %d moving.... %d:%d\n", t_id, x[k],y[k]);
if (a>=0 && a<R && b>=0 && b<C && t_grid[a][b] == 0)
{
t_grid[a][b] = t_id;
t_grid[row2][col2]=0;
pthread_mutex_unlock(&tgridlock);
flag=1;
// printf("teacher : %d moving.... %d:%d\n", t_id, a,b);
break;
}
if(flag==0)
pthread_mutex_unlock(&tgridlock);
printf("not matching....\n");
for(int i = k; i<8; i++)
{
x[i]=x[i+1];
y[i]=y[i+1];
}
}
sleep(W);
}
}
int main(void)
{
pthread_t Student[N];
pthread_t Teacher[M];
int id[N];
int t_id[T];
srand(SEED);
mutex();
FILE *fp;
// wicked id generate
int temp1[N];
for (int i=0;i<N;i++) temp1[i]=i+1;
int ran1;
for(int k=0;k<M;k++)
{
int ran1= rand()%limit;
limit=limit-1;
wicked[k]=temp1[ran1];
for(int p=ran1; p<N;p++)
{
temp1[p]= temp1[p+1];
}
}
//for(int i=0; i<M;i++) cout<<"wicked:"<<wicked[i]<<" , ";
//randomlly pc assignment
int temp[N];
for (int i=0;i<N;i++) temp[i]=i+1;
int ran;
limit=N;
for (int i=0; i<R;i++)
{
for (int j=0;j<C;j++)
{
int ran = rand()%limit;
grid[i][j]=temp[ran];
limit= limit-1;
for(int k=ran; k<N;k++)
{