1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

[C] Battleship for NT platforms

Discussion in 'Programming' started by Alhazred, Jul 12, 2004.

  1. #1
    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).
    SEMrush
    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:

    Alhazred, Jul 12, 2004 IP
    SEMrush
  2. l0cke

    l0cke Active Member

    Messages:
    178
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    73
    #2
    I'll see if I can help you out after work..
     
    l0cke, Jul 12, 2004 IP
  3. Alhazred

    Alhazred Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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++;
    		}
    		[b]printf(Do you confirm the ships coordinates? (Y/N) ");
    		scanf("%c",&confirm);
    		[u]if(((confirm < 'N') || (confirm > 'Y)) || ((confirm > 'N') && (confirm < 'Y')))[/u]
    			do {
    				fflush(stdin);
    				printf("\nDo you confirm the ships coordinates? (Y/N) ");
    				scanf("%c",&confirm);
    			}[u]while((confirm == 'N') || (confirm == 'Y'));[/u]
    		[u]if(confirm == 'Y')[/u]
    			[u]correct = TRUE;[/u]
    	}[/b]
    }
    
    Code (markup):
    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 13, 2004 IP
  4. Alhazred

    Alhazred Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
    
    Code (markup):
     

    Attached Files:

    Alhazred, Jul 20, 2004 IP