Thread: There must be a better way?!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58

    There must be a better way?!

    I began writing this battleship code today and as I'm working on it, I'm finding that I have a function that could be more efficient(understatment). I'm just not sure how. The code compiles and runs, but I have to repeat this 15 times per player.
    Code:
    int assignFleet1(int board1 [] [10])
    
    {
    	int row, col;
    	int carrier1;
    
    	printf("Player 1 Welcome, it is time to place your fleet!\n");
    	printf("First place your carrier on the board\n");
    	printf("A carrier takes up 5 spots\n");
    	printf("Carrier 1st coordinate:");
    	scanf("%d", &carrier1);
    	
            row = (carrier1 / 10);
    	col = (carrier1 % 10);
    	printf("row: %d\n", row);//just to make sure I'm getting what I expect
    	printf("col: %d\n", col);
    	board1[row] [col] = 5;
    
            return 1;
    any thoughts?
    here is what I have come up with: a for loop using the variable car1 for carrier 1st player.
    Code:
    for (car1 = 0; car1 < 5; car1++)
    	{ for (coordinate =0; coordinate < 5; coordinate++)
    		printf("Enter coordinate %d for carrier\n", car1);
    			scanf("%d\n", &coordinate);
    				printf("Coordinate %d is %d \n", car1, coordinate);
    			row = (coordinate / 10);
    			col = (coordinate % 10);
    		board1[row][col] = 5;
    but it doesn't work.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are looping five times to get coordinates for car1, then looping five times to get coordinates for "car2", etc. But there's only one carrier in the traditional game of battleship, so you probably don't need the outside loop for five different carriers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,745
    Code:
    	{ for (coordinate =0; coordinate < 5; coordinate++)
    		printf("Enter coordinate %d for carrier\n", car1);
    			scanf("%d\n", &coordinate);
    Check your variable re-use.

    You have the right approach, you just need to consider the detail a little more.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    Awesome! it worked Salem. I had to change the variable. Here is my updated code
    Code:
    int assignFleet1(int board1 [] [10])
    
    {	
    	
    	int car1, coordinate, location;
    	int row, col;
    	int carrier1;
    	printf("Player 1 Welcome, it is time to place your fleet!\n");
    	printf("First place your carrier on the board\n");
    	printf("A carrier takes up 5 spots\n");
    
    	for (car1 = 0; car1 < 5; car1++)
    	 {for (coordinate =0; coordinate < 5; coordinate++)
    		{printf("Enter coordinate for carrier\n" );
    			scanf("%d", &location);
    				printf("Coordinate  is %d \n",  location);
    			row = (location / 10);
    			printf("row: %d\n", row);
    			col = (location % 10);
    			printf("col: %d\n", col);
    		board1[row][col] = 5;
    	}//for
    	}
    
    	return 1;
    }//assignFleet
    works great!
    Also Tabstop, thanks for looking over my code. Although there is only 1 carrier, it is 5 places long, so I have to assign it to 5 indexes.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by time4f5 View Post
    Also Tabstop, thanks for looking over my code. Although there is only 1 carrier, it is 5 places long, so I have to assign it to 5 indexes.
    So it doesn't bother you that you're typing in 25 indexes instead?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    So it doesn't bother you that you're typing in 25 indexes instead?
    Actually that should be 5 sets of 5 contiguous indexes... One might appease the player somewhat by getting the bow and stern coordinates and filling in the rest automagically...

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    That was what I was going to suggest Tater. Not only does the user have to do less typing, the programmer wins too. Less work for you and easier error checking. Instead of checking 5 coordinates to make sure they're all in a row, and horizontal or vertical, the check for the bow and stern coordinates is easy. For the carrier, which is 5 units long:
    • Assume the bow is at (x1,y1) and the stern at (x2,y2)
    • If x1 == x2 and |y2 - y1| == 5, then it's good
    • If y1 == y2 and |x2 - x1| == 5, then it's good
    • Otherwise, ask the user for a new set of coordinates and check again

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    I was going to suggest the user enter a "start" location and a direction

    Code:
    "Enter Grid Row location for Carrier:"   (user enters '4')
    "Enter Grid Column location for Carrier:" (user enters '8')
    "Enter direction for Carrier (U, D, L, R):" (user enters 'L')
    but I think I like Tater's better...

  9. #9
    Registered User
    Join Date
    Mar 2011
    Location
    Baltimore Md. USA
    Posts
    58
    I had thought about that. I'll have to add that.

Popular pages Recent additions subscribe to a feed