GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Jul-2004, 09:18
Alhazred Alhazred is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: Italy
Posts: 17
Alhazred will become famous soon enough

[C] Battleship for NT platforms


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
Attached Files
File Type: txt battleship.txt (6.0 KB, 57 views)
  #2  
Old 13-Jul-2004, 08:38
Alhazred Alhazred is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: Italy
Posts: 17
Alhazred will become famous soon enough
I've modified the placeShip's code in this way:
Code:
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.
  #3  
Old 13-Jul-2004, 08:49
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hello Alhazred. Logic comparisons can be difficult, try replacing this:
CPP / C++ / C Code:

		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;
	}
}

with this:

CPP / C++ / C Code:
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;
  #4  
Old 13-Jul-2004, 14:04
Alhazred Alhazred is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: Italy
Posts: 17
Alhazred will become famous soon enough
Thanks, now it works
  #5  
Old 20-Jul-2004, 05:12
Alhazred Alhazred is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Location: Italy
Posts: 17
Alhazred will become famous soon enough
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
Code:
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.
Attached Files
File Type: txt battleship.txt (11.0 KB, 20 views)
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.