[IPC, Shared memory] Design and implementation ideas for a server-client program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • EARNEST
    New Member
    • Feb 2010
    • 128

    [IPC, Shared memory] Design and implementation ideas for a server-client program

    hi guys,
    i am trying to implement a multiplayer snake game on the same machine where each player can see the same map, control his own snake. but i do not really see a concrete idea for managing multiplaying aspect.
    can you share your thoughts about it?
    so far, there are few things i tried to came up with.
    1. on execution, a program checks if host(server) is online, if true, join the game, else create the arena.
    a)create arena: it will use standard window (stdscr, using ncurses), it will create a snake and places there, then I think i should create a structure boardMAP and pass snakes and power up fruit to a method that would fill that boardMAP up.
    if movement happens, games pauses (nano seconds), each snake on the map writes its on snake structure (location of body, length, etc) to certain memory segment (parallel, to different locations, lets sanke snake1 to memloc1, snake2 memloc2 so on), then host will read that data (FIFO principle?), it will analyze map, players (collision detection, progress move step etc), then it will write back to certain memory back, and the clients (and host) will read from that memory and print whats in there on their own screens.
    b)joins: it checks for the host, waits for the next pause, when all the snakes made their mvoe and collision detection passed, it will show a flag that "hey, iam waiting to join", then host will take that guy and place him somewhere on the map.
    and so on in that matter. what do you think about it?
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Not sure in what area your concerns are:
    How does each player access the game?
    1. "each snake on the map writes its on snake structure (location of body, length, etc)"
    If using a client, are you concerned about hacking? eg, they feed the program incorrect information.
    If so, the only information the client should send should be the direction they intend to go, and the server should calculate the other information.

    2. I would suggest only allowing players to join the game in between rounds, after every one but one has died, and the players are reset to starting positions.

    Comment

    • EARNEST
      New Member
      • Feb 2010
      • 128

      #3
      Hi, thanks for your reply. About 1st point - I was thinking the same thing, since the only thing I'd need is snakes's coordinates. However, snakes can grow, thus the coordinates number would grow also (structure holding the coordinates). Would it be a problem with segment's size? Would I need to resize it all the time?

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Since this seems to be a graphically related program, why wouldn't you have a static grid like memory structure that stores all of that data? eg grid[11,4] is snake green head, grid[11,5] = snake green body horizontal, etc.. (don't know how complex your graphics are)

        with 2 from before, I realize now that I have assumed that there are specific rounds like in the old tron game, but that may not be the case.?

        Comment

        • EARNEST
          New Member
          • Feb 2010
          • 128

          #5
          i have snake structure and location structs in there. so snake has something what you are saying.
          2 is not round based.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            ... I'm really tempted to say it depends on your implementation structure. When a snake 'dies', does it disappear from the board immediately? Have you considered any sort of linked list or stack implementation?

            Comment

            • EARNEST
              New Member
              • Feb 2010
              • 128

              #7
              when a snake dies, it disappears from the board. and, no, i did not consider linked list or stack. why? should i?

              Comment

              • jkmyoung
                Recognized Expert Top Contributor
                • Mar 2006
                • 2057

                #8
                A possible example where the absolute memory would not change is have a pointer only to the position of the snake head. There is then a grid of pointers, each pointing to the next position of the snake, or null if the space is empty, or contains a snake's tail.

                This eliminates the need to resize the structure holding the snake's coordinates, but does require more space to begin with.

                Comment

                • EARNEST
                  New Member
                  • Feb 2010
                  • 128

                  #9
                  didn't quite get you. if we are going to store only coordinates of a head (int, int), thus 8 bytes, so why would it require more space? also, is it a problem to realloc memory for a segment of locations?

                  Comment

                  • jkmyoung
                    Recognized Expert Top Contributor
                    • Mar 2006
                    • 2057

                    #10
                    I'm probably just confusing you with this other suggestion, so I suggest you go with the realloc method, rather than needlessly rewrite a lot of code.

                    Comment

                    • EARNEST
                      New Member
                      • Feb 2010
                      • 128

                      #11
                      :) Also, i am not sure if it is more efficient to have lets say N number of memory slots for N snakes of [startlength..en dlength] struct of 2 int, rather than having let's say a struct of the size of a border (M x N, lets say 100x60, so 6K) and every snake updates that map struct "turn based" (can update 1 mem seg at a time only), while in my original idea, it would be kinda "parallel" writing of the snakes's locations. what you think?

                      Comment

                      Working...