Solved

Developing a 2 player game on linux server help

Posted on 2011-03-24
4
498 Views
Last Modified: 2012-05-11
I am a student andI need to implement a 2-player PacMan game (in C) for a Unix Machine. Each player will login into the same server with their respective user accounts, type the command ‘pacman’, and instantly will be placed in a FIFO queue (fifo.c) and wait for access to the game. When it is their turn to play, both users should find themselves in the ‘same maze’, competing against each other.

I did not implement the FIFO queue yet. Up till now, I have a one-player pacman game, pacman.c and in it I already have an IPC between player one and the ghosts via shared memory. Now, I would like to implement another IPC between player 1 and player 2, and I was thinking of using signals but I am not sure whether they are practical.  

Can I assume that one instance of pacman.c is loaded on the server? Therefore, is it possible to load only one instance of the game (fifo.c will call exec() ONCE) and associate different processes (in pacman.c) with different user accounts (one process associated with player one, and maybe the child process with player 2)?

 

0
Comment
Question by:emcict
  • 2
4 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 35206550
Hi emcict,

Signals might be useful within the application (client program) but they aren't much help with the communication to/from the game server.

You'll have to establish a "turn protocol" with the server.  The application will query the server with a simple "is it my turn" question.  If not, the application pauses slightly and resends the question.  When it is this players turn, any update of the game (player position, board changes, etc.) will need to be sent to the application (probably by a different question/message type from the application) and then the turn taken.

What you're describing seems like an awfully complicated way to do this.


Good Luck,
Kent
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35208180
We did a project much like this in our OS class. We ran the game on the server and just sent the board position to the clients and accepted the input (we used sockets). Of course, we sent raw position data to the clients and rendered the graphics at the client.
The reason we did it like that was that we were using UDP so that if any packets were dropped, the game just skipped, it didn't make the two clients see totally different games.

I don't see how a "turn protocol" would work for pac-man since it's real-time. But as you've probably noticed, there's a window of time between when you need input (when pac-mac is between intersection points on the grid). So you could just accept one input packet in each period and drop the rest.
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 125 total points
ID: 35260206
My 2 cents, rather than having multiple client programs communicating between each other (player 1, player 2, ghosts) you should implement the game server to do all of the tracking and to house all of the data structures (along the lines of Tommy's suggestion) and your clients should simply handle the user input and displaying the game. There should be one copy of the actual gameboard, on the server process. Could be in shared memory allocated by the server and attached by each client, or you could do all communication via IPC.

As far as signals, you don't need them for this. Since you aren't using TCP/UDP for everything, we would simply use shared memory and semaphores to control access to the central game structures.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35260610
Ah, yes. That's good advice. Shared memory isn't really realistic for most Client-Server applications, but if that's what you have then use it! It actually makes it a lot easier. You just need a queue for input commands that the clients add to and the server consumes.
I wasn't really paying attention when you said you had shared memory. I just assumed your project was like mine. Most of my earlier comments still apply though, but the server doesn't need to send the board position, it can just write it to the shared memory.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question