Loans - Home Loan - Car Finance - Ali Aboutaam - Neopets Cheats, Games and Neopoints

PDA

View Full Version : [C] Battleship for NT platforms


Alhazred
Jul 12th 2004, 7:48 am
I'm trying to solve this thing:

Make 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 :)

l0cke
Jul 12th 2004, 7:55 am
I'll see if I can help you out after work..

Alhazred
Jul 13th 2004, 6:35 am
I've modified the placeShip's code in this way:

void PlaceShip(){
int Count = numeroNavi, nave = 1;
char c_col = ' ', c_raw = ' ', confirm = ' ';
char coorx[3];
char tmpStr[3];
BOOL correct = FALSE;
coorx[0] = '\0';
tmpStr[0] = '\0';

for(i=0;i<numeroNavi;i++) {
mArray[i].colonna = ' ';
mArray[i].riga = 0;
}

printf("Insert coordinates for %i dhips.\n",numeroNavi);
while(!correct){
while(Count > 0) {
printf("Ship %i: Column_", nave);

// A-J
while((c_col < 65) || (c_col > 74)) // enter = confirm
c_col = getch();
coorx[0] = c_col;

mArray[nave-1].colonna = coorx[0];
printf("%c Raw_", c_col);
c_col = ' '; // reset

// 0-9
while(((c_raw < 48) || (c_raw > 57))) // enter = confirm
c_raw = getch();
printf("%c", c_raw);
tmpStr[0] = c_raw;
c_raw = ' '; // reset

// 0-9 o ENTER to not give a single digit
while(((c_raw < 48) || (c_raw > 57)) && (c_raw != 13)) // enter = confirm
c_raw = getch();
printf("%c", c_raw);
tmpStr[1] = c_raw;
c_raw = ' '; // reset
printf("\n\r");
mArray[nave-1].riga = atoi(tmpStr);
Count --;
nave++;
}
printf(Do you confirm the ships coordinates? (Y/N) ");
scanf("%c",&confirm);
if(((confirm < 'N') || (confirm > 'Y)) || ((confirm > 'N') && (confirm < 'Y')))
do {
fflush(stdin);
printf("\nDo you confirm the ships coordinates? (Y/N) ");
scanf("%c",&confirm);
}while((confirm == 'N') || (confirm == 'Y'));
if(confirm == 'Y')
correct = TRUE;
}
}

The bold part should ask the player if the ship's coordinates are right (and ask the same thing till the player insert Y or N), if not the player have to insert them again, but it doesn't do that.
I think I have a problem with the underlined parts.

Alhazred
Jul 20th 2004, 3:13 am
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.