-4

I have been coding the battleship game in C but I have a few problems. First of all, I would like to have a counter for the "hit and sunk" ships (it is coded, but it doesn't seem to work, after you hit a ship, it always prints 1), and therefore, this doesn't end the program when all ships are sunk. Here is my code for the main and "shoot" functions:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define rows 5
#define columns 5

int mainint main(){
srand(time(NULL));
int grid[rows][columns], attemps=0, ships;
int sunk;

printf("Let's play the battleship game!\n");
printf("You have 20 tries.\n");

printf("Enter the number of ships: ");
scanf("%d",&ships);

prepare_grid(grid); //Prepare grid with items of value -1 
gen_ships(grid,ships); //Generate random ships surrounded by water (value 1)
print_grid(grid); //Print grid without showing the generated ships (all items will be "~" meaning "undiscovered water).

sunk=0;

for (int a=0;a<20;a++){
    shoot(grid,sunk,ships);
    attemps++;
    print_grid(grid);

    printf("\nAttemps: %d\n",attemps);
}

print_secret_grid(grid); //Print final grid, showing all sunk ships and positions shot

return 0;
}

void shoot(int grid[rows][columns], int sunk, int ships) {

int x, y;

printf("\nLine --> ");
scanf("%d", &x);
printf("Column --> ");
scanf("%d", &y);

do {
    if (grid[x-1][y-1] == 1) {
        grid[x-1][y-1] = 2;                               //We assign value 2 because we want to print only the ones the user hits, it will print X which means "hit and sunk".
        sunk++;
        printf("\nHit and sunk\n");
        printf("Sunk ships:%d \n\n", sunk);
    } else if (grid[x - 1][y - 1] == -1) {  //It will print "*" which means "discovered water".
        grid[x - 1][y - 1] = 0;  
        printf("\nMiss\n\n");
    }
  }while (sunk=!ships);
}
bymrk
  • 3
  • 1
  • 3
  • 3
    Not a single comment in the entire program. You are a very naughty boy. – Bathsheba Nov 28 '17 at 07:58
  • 5
    SO is not a debugging service, you need to debug the code yourself first. Only if you manage to find the issue and don't understand it you should consider asking on SO (after doing some research on it first) – UnholySheep Nov 28 '17 at 07:59
  • Ah, one comment: just in an odd position. Still not enough. – Bathsheba Nov 28 '17 at 07:59
  • 2
    `while (sunk=!ships)` I'd fix that first. `!=` – yacc Nov 28 '17 at 08:01
  • If you share a code snippet, try to make sure either that it is runnable from what you have provided, or that you don't need to run it to figure it out. I would have run your code but you are missing includes, definitions for column, and other stuff. – vasia Nov 28 '17 at 08:01
  • Possible duplicate of [Update (int) variable in C inside a function](https://stackoverflow.com/questions/23667497/update-int-variable-in-c-inside-a-function) – JJJ Nov 28 '17 at 08:02
  • @Bathsheba good code has comments. Great code doesn't need them. It is a skill to choose variable names and function names such that comments are unnecessary, and I'd recommend the book "Clean Code" for a better description of what I am badly trying to state :) – Edwin Buck Nov 28 '17 at 08:04
  • Sorry, I'm new to this website, I'll keep that in mind for other questions. – bymrk Nov 28 '17 at 08:04
  • 4
    @EdwinBuck: Trust me, the C++ command line application that used Einstein's field equations for General Relativity for the wormhole modelling in the film "Interstellar" needed a few comments. – Bathsheba Nov 28 '17 at 08:05
  • 2
    "Good code doesn't need comments" is one of the most damaging myths in the business. – JJJ Nov 28 '17 at 08:06
  • 2
    @JJJ: Indeed and at least one contributor on this site is in the "no hire" box. – Bathsheba Nov 28 '17 at 08:06
  • @Bathsheba To make things clear, I didn't state that all code should be comment free. I stated that great code wouldn't require many comments. Judging code by its comment content is a pretty poor metric, and I'd be happy to not be hired by a company that does so. Just as I would be happy to not be hired by a company that uses "lines of code" as a metric. – Edwin Buck Nov 28 '17 at 08:22

2 Answers2

0

When using a function call in C, the value is passed by copy. This means that

int value = 0;
function(value);
printf("%d\n", value);

will always print the value 0, even if the function reads like

void function(int value) {
   value++;
}

because within the function function, a copy of the int value is being incremented.

To fix this, pass a copy of the memory location of value, and then increment the number stored at that location.

int value = 0;
function(&value);

with

void function(int* value) {
  (*value)++;
}
Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

sunk is passed by value. It needs to be passed by reference. Send the pointer to sunk, and receive sunk as a pointer in the shoot function.

Alternatively, make sunk a global variable and increment it, although I'd not suggest this for bigger programs!

Read more about passing by value and passing by reference to prevent such things from happening in the future! :D

Harsh
  • 389
  • 2
  • 18