samedi 16 novembre 2019

Program printing different co ordinates from the ones I generated for it

Basically I need my program to place points I type in manually on a grid (which it does fine) and then place a point randomly on the same grid and repeat for as many times as user requests. Currently, my code prints the numbers computer selects but doesn't place them on grid until I type another number and press enter, where it then places it in a seemingly random place. Here's my code

void placeCShip(){
    int a,b;
    srand(time(NULL));
    a = printf("%d\n", rand()%15);
    b = printf("%d\n", rand()%15);
    scanf("%d, %d", &a, &b);
    if (grid[a][b] == SEA, grid[a][b] != PSHIP){
        grid[a][b] = CSHIP;
    }
}

and this is the function that repeats it, it works perfectly fine when I exclude CShips and Pships still works when I include it, but Cships doesnt give correct co ordinates

void placePShips(){
    int i,fleetSize;
    printf("\nEnter fleet size : ");
    scanf("%d", &fleetSize);
    for(i=0;i<fleetSize;i++){
        placePShip();
        placeCShip();
        printGrid();
    }
}

What I've tried; Changing printf to scanf on CShips but that makes PShips stop working and doesn't fix CShips




Aucun commentaire:

Enregistrer un commentaire