• 0

[C] Battleship


Question

I'm trying to solve this thing:

Realize an electronic version of "Battleship". In this version two process cooperate between them.

First the player must decide where to place the ships, when both players have placed all ships the processes decide who start. The starting process accepts a move, transmit it to the opponent proocess, waits for the opponent's process response about its move and then waits for the opponent's move. Everytime each process receive a move or a response from the opponent, check if the opponent has hit a ship, check if someone has win and if so tell it to players. Communication channel between process must be implemented by a shared memory area.

To play the program have to be run twice on the same computer (this is an exercise, it won't be a real game to be played on two PCs).

Now I post what I have written till now, please, give me suggestions, correct me if there's a more simple way to do something I've already done, propose me whatever you want to help me to finish this exercise.

Thanks :)

battleship.txt

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

I have written the code to make the program work (there're just few more thing to be added in some functions).

I have this problem, are a couple of days I'm trying to think how to manage the shared memory access and be sure that the shared memor is used by the proper instance in a determinate moment.

I know it must be done with semaphores, but I can't think a right way to do it.

Here is the algorithm I want to follow:

Between " " the name of function that do what's written on the step, I1 = first instance of the program, I2 = second instance

1 - I1 and I2 asks to place the ships "PosizionaNavi()".
2 - I1 decide who starts. "PrimaIstanza()" generate a random number between 1 and 2,
    this number is compared with istanza... (by now I suppose I1 starts)
3 - I2 waits that I1 place a coordinate (the shot) in the shared memory
4 - I1 asks its user to give a coordinate "Scrivi()".
5 - I1 accept the coordinate, checks the validity and writes it in the
    shared memory, then I1 release the semaphore to I2
6 - I2 checks if the coordinates matches with one of those decided at the beginning
    by its user. "Controlla()"
7 - if it matches one, I2 decrease the number of non-hits ship and check how many
    ships are still alive.
8 - if the remaining ships are more than 0 tells to the shared memory that a ship
    has been hit and release the semaphore to I1.
9 - if the remaining ships are 0 tells to the shared memory that it has lost and
    release the semaphore to I1
10 - I1 computes the data from I2 in the shared memory, displays an appropriate simbol
     on the field (i.E. *=hit, x=miss) and waits a coordinate from I2, or tells its
     user about the victory.
11 - repeat step 4 to 10 inverting I1 and I2 and so on till someone wins.

battleship.txt

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.