0

I am working on a Battleship program for an assignment. I am having trouble trying to find a good way to place the ships. The idea I have been trying is filling a 10x10 array with zeros then replacing specific coordinates with c for carrier, d for destroyer, etc. But I haven't been too successful. If anyone has any ideas of how I can do this, or if you know a better way then any help or guidance will be appreciated.

Sorry This is the first time I have been anywhere online to help. And to answer the question. No I do not know how to do arrays that is part of the reason I asking for help. I have been reading through my textbook but I havent found anything similar and without a concrete understanding it has been hard to think beyond exactly what the book is saying. But this is the code I have so far just to create the stratagey board and the playing board and my attempt at placing the ships

   //================================FillArray==========================================
void FillArray(int pboard[], int sboard[]);
{

    //Statement
    for(int y; y < col; y++);
    {
        printf("%d", y);//prints y axis boarder
        for(int x=0; x < row; x++);
        {
        printf("%d", x);//prints x axis boarder
        for(int y=0; y < col; y++);
            pboard[row][col] = 0;
        }//for
    }//Fills array for play


    for(int y; y < col; y++);
    {
        printf("%d", y);//prints y axis boarder
        for(int x=0; x < row; x++)
        {
        printf("%d", x);//prints x axis boarder
        for(int y=0; y < col; y++);
            sboard[row][col] = 0;
        }//for
    }//Fills array for Ships

    return;
}//FillArray


//================================PlaceShips==========================================

void PlaceShips(int air=5, int bat=4, int cru=3, int des=1, int sub=2);
{

    char ship;
    int x,y;
    int k;

    //Statement

    printf("Enter first letter of ship name: ");
    if(scanf("%c", ship) == a)
        { printf("You have 5 space, Please enter furthest left or futhest up coordinate: ");
            scanf("%d%d", &x,&y);
            while(x>10 && y > 10)
                {
                    printf("Enter 1. to place ship vertically\nEnter 2. to place ship horizontally: ")
                    scanf("%d", &k)
                    for(k==1, y > 6, y++);
                    {   pboard[row][y-1] = a;
                            if(y<0 || y >9)
                        printf("error");
                    }
                    for(k==2, x > 6, x++);
                    {
                        pboard[x-1][col] = a; 
                        if(x < 0 || x > 9)
                        printf("error");
                    }
                }
        }
}

Is this a good way to go about doing this, or are there more efficient ways. I tried structures similar to:

typedef struct
{
char id[5]={a,a,a,a,a}
char name[]=aircraft
int hits=5
} Aircraft Carrier

but i wasnt sure how they would help.

3
  • 3
    Welcome to the site! You are generally expected to provide more details as to the approaches that you have tried, and how your code currently looks. This will help others give you better help. Commented Dec 15, 2012 at 1:39
  • 1
    What can you do :) You don't know how to do with array, or something wrong ... ? You should put additional information and post your code, too Commented Dec 15, 2012 at 1:44
  • Show us what you've tried. Commented Dec 15, 2012 at 1:54

2 Answers 2

1
 for(&k==1, y > 6, y++)

I imagine this is one of the problems you are having.

First off, you need to delimit for() statements with ;.

Second, you are accessing the address of k, and checking for equality. The first part of a for statement generally sets a variable. You need to rethink your logic.

        while(x>10 && y >> 10)

>> is not he same as >, be careful.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I did not know that I could not use the address instant of the variable. Is this a good way to go about doing what I am trying to do. It seems like there should be a different way. Im not asking for any code just a suggestion and some ideas to go about this a more efficient way. I tried making structure typedef struct { char id[5]={a,a,a,a,a} char name[]=aircraft int hits=5 } but i wasnt sure how it would be helpful
0

It looks like you're trying to do too much in the PlaceShips function. This is a matter of style, but I might first write a PlaceOneShipHorizontally() and a PlaceOneShipVertically() function.

For example,

void PlaceOneShipHorizontally(int board[], int ox, int oy, int character, int size) {
  for (int x = ox; x < (size+ox); x++) {
    board[x][oy] = character;
  }
}

Then, edit the PlaceShips() function to use the above functions once you determine what the user wants to do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.