LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-01-2013, 12:35 AM   #1
siya25
LQ Newbie
 
Registered: Jan 2013
Posts: 8

Rep: Reputation: Disabled
multiplayer tic-tac-toe using semaphore...


modified tic-tac-toe game using semaphore where
a. one player is elected as a leader
b. when a leader checks others should wait
c. others can check simultaneously on free cells
d. the turn is between the leader and others
e. the winner is the checks of a player on same row/column/diagonal
note: when 2 players try to check on a same cell the player whose id is less is given chance.
 
Old 03-02-2013, 07:57 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Was there a question in there somewhere? We're not going to write your homework assignment for you. If you have a specific problem or area where you're stuck, then make that clear and ask a question.
 
Old 03-02-2013, 11:24 AM   #3
siya25
LQ Newbie
 
Registered: Jan 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
i have written this code.smtimes it runs fine bt mostly it gets hang whn asked fr input of coordinates frm user.
i think there is problem when it reach to semaphore l_move in leader_move function.
so anyone can see where is the problem..??

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<sys/sem.h>
#include<sys/ipc.h>

#define KEY 12345
#define PERM 0666


int a[5][5];
void init();
void display();
char check();
int semid;
//int th_args[8]={0,1,2,3,4,5,6,7};
static struct sembuf move[2]={ 0,0,0,
0,1,0};

static struct sembuf release[1]={0,-1,IPC_NOWAIT};

void l_move()
{
semop(semid,&move[0],2);
}

void l_release()
{
semop(semid,&release[0],1);
}


void leader_move()
{
int x, y;
printf("Enter X,Y coordinates for ur move: ");
printf("\n");
scanf("%d%*c%d", &x, &y);
x--; y--;
l_move(); //here cn be the problem
if(a[x][y]!= ' '){
printf("Invalid move, try again.\n");
leader_move();
}
// l_move();
else a[x][y] = 'x';
l_release();
}

void *th1_move()
{
int i, j;
for(i=0; i<5; i++){
for(j=0; j<5; j++)
if(a[i][j]==' ') break;
if(a[i][j]==' ') break;
}

if(i*j==9) {
printf("draw\n");
exit(0);
}
else
a[i][j] = 'O';
}



void *th2_move()
{
int i, j;
for(i=0; i<5; i++){
for(j=0; j<5; j++)
if(a[i][j]==' ') break;
if(a[i][j]==' ') break;
}

if(i*j==9) {
printf("draw\n");
exit(0);
}
else
a[i][j] = '1';
}



void *th3_move()
{
int i, j;
for(i=0; i<5; i++){
for(j=0; j<5; j++)
if(a[i][j]==' ') break;
if(a[i][j]==' ') break;
}

if(i*j==9) {
printf("draw\n");
exit(0);
}
else
a[i][j] = '2';
}




void main()
{ printf("game of tic tac toe\n");
char done=' ';
int i,j;
pthread_t th,th1;
if((semid=semget(KEY,1,IPC_CREAT|PERM))<0)
printf("error");
init();

do {
display();
//l_move(semid);
leader_move();
//pthread_create(&th,NULL,leader_move,NULL);
//l_release(semid);
done=check();
if (done!=' ') break;
pthread_create(&th1,NULL,th1_move,NULL);
done=check();
pthread_create(&th1,NULL,th2_move,NULL);
done=check();
pthread_create(&th1,NULL,th3_move,NULL);
done=check();
} while(done==' ');
if(done=='x')
printf("leader win\n");
else if(done=='0')
printf("player1 win\n");
else if(done=='1')
printf("player2 win\n");
else
printf("player3 win");
display();


// create players
// for(i=0;i<3;i++)
}



void init()
{
int i, j;

for(i=0; i<5; i++)
for(j=0; j<5; j++) a[i][j] = ' ';


}



void display()
{
int i;

for(i=0; i<5; i++) {
printf(" %c | %c | %c | %c | %c ",a[i][0],
a[i][1], a[i][2], a[i][3], a[i][4]);
if(i!=4) printf("\n---|---|---|---|---\n");
}
printf("\n");

}


char check()
{
int i;
for(i=0; i<5; i++)
if(a[i][0]==a[i][1] &&
a[i][0]==a[i][2] &&
a[i][0]==a[i][3] &&
a[i][0]==a[i][4] )

return a[i][0];

for(i=0; i<5; i++)
if(a[0][i]==a[1][i] &&
a[0][i]==a[2][i] &&
a[0][i]==a[3][i] &&
a[0][i]==a[4][i] )
return a[0][i];


if(a[0][0]==a[1][1] &&
a[1][1]==a[2][2] &&
a[2][2]==a[3][3] &&
a[3][3]==a[4][4])
return a[0][0];

if(a[0][4]==a[1][3] &&
a[1][3]==a[2][2] &&
a[2][2]==a[3][1] &&
a[3][1]==a[4][0])
return a[0][4];

return ' ';
}
 
Old 03-10-2013, 05:08 PM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Sorry for taking such a long time to get back to you - real life caught up with me.

Why are you even using threads for this? Surely you want it to alternate between "Leader", "1" and "2" in order? In any case, you are using the variable th1 3 times - once for each thread - which may be the cause of the problem.
 
Old 03-11-2013, 06:44 AM   #5
siya25
LQ Newbie
 
Registered: Jan 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
how can i do without threads?there is 1 thread for 1 player...
i dont want in order.first leader then among the other players whose id is less is given the chance..i hv to do like that but i m not getting how to do that so simply done without considering ids n all..
n what is the problem using th1 3 times?can u pls tell in detail?
 
Old 03-12-2013, 05:26 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
You have the following code:

Code:
    pthread_t th,th1;
    if((semid=semget(KEY,1,IPC_CREAT|PERM))<0)
        printf("error");
    init();

    do {
        display();
        leader_move();
        done=check();
        if (done!=' ') break;
        pthread_create(&th1,NULL,th1_move,NULL);
        done=check();
        pthread_create(&th1,NULL,th2_move,NULL);
        done=check();
        pthread_create(&th1,NULL,th3_move,NULL);
You've started 3 threads, all using the 'th1' variable. You should change it to:

Code:
    pthread_t th1,th2,th3;
...
        pthread_create(&th1,NULL,th1_move,NULL);
        done=check();
        pthread_create(&th2,NULL,th2_move,NULL);
        done=check();
        pthread_create(&th3,NULL,th3_move,NULL);
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to play console games in multiplayer with NET, with MEDNAFEN MULTIPLAYER? frenchn00b Linux - General 0 01-04-2010 06:35 PM
kdenlive: on export, there's a tac tac tac in the sound output eantoranz Linux - Software 5 10-20-2007 12:20 PM
creating an interoperable program for Tic tac toe sachitha Programming 2 08-25-2006 12:56 PM
Dipping my toe ImpelGD LinuxQuestions.org Member Intro 3 01-09-2006 04:42 PM
searching "TIC TAC TOE" bash script LV-chronos Linux - Newbie 5 05-29-2005 03:20 PM


All times are GMT -5. The time now is 11:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration