Thread: help with battleship program

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    12

    help with battleship program

    im doing a battleship program for my c++ class and i keep gettin these errors when i compile:


    line

    84 `grid' undeclared (first use this function)

    84 `y' undeclared (first use this function)

    84 `x' undeclared (first use this function)

    93 `game' undeclared (first use this function)

    93 `shoot' undeclared (first use this function)

    In function `int main()':

    151 `ships' undeclared (first use this function)
    167 a function-definition is not allowed here before '{' token
    167 expected `,' or `;' before '{' token
    301 `shoot' undeclared (first use this function)
    `void placement2(int (*)[12], int (*)[12], int)':

    my source code is


    Code:
    #include <stdio.h>
    #include <iostream>
    
    using namespace std;
    
    
    
    
    
    int choose(){
    int num;
    
    void placement(int ships[][12]);
    void shoot(int grid[][12], int ships[][12], int y, int x, int *game);
    scanf( "%d", &num );
    
    if(num < 0 || num > 12){
    printf( "Invalid choice, choose again.\n" );
    num = choose();
    }
    
    return num;
    }
    
    void graph(int grid[][12])
    {
    int i;
    int j;
    
    cout << " A B C D E F G H I J K L\n";
    cout << " ----------------------------\n";
    
    
    
    for(i = 0; i < 12; i++){
    printf( "%d", i );
    
    for(j = 0; j < 12; j++){
    
    if(grid[i][j] == 0)
    cout << " " ;
    else if(grid[i][j] == 1)
    cout<< "X" ;
    else if(grid[i][j] == 2)
    cout<< "-" ;
    }
    cout<<"\n";
    }
    
    cout<<"\n" ;
    }
    void placement2(int map[][12], int ships[][12], int num)
    
    
    {
    if(grid[y][x] == 1 || grid[y][x] == 2){
    cout<<"This space has already been chosen, choose another space\n";
    
    cout<< "Row:";
    y = choose();
    
    cout<< "Column:" ;
    x = choose();
    
    shoot(grid, ships, y, x, game);
    }
    
    else{
    if(ships[y][x] == 1){
    cout<< "HIT!\n" ;
    grid[y][x] = 1;
    ships[y][x] = 0;
    }
    else if(ships[y][x] == 0){
    cout<< "Miss.\n" ;
    grid[y][x] = 2;
    }
    }
    
    int k;
    int l;
    int check = 0;
    
    for(k = 0; k < 12; k++){
    for(l = 0; l < 12; l++){
    
    if(ships[k][l] == 1)
    check = 1;
    }
    }
    
    if(check == 1)
    *game = 0;
    
    }
    
    int main()
    {
    /* Grid is what the players see, ships is where the ships are */
    /* Player 1 ships */
    int grid1[12][12] = {{0}, {0}};
    int ships1[12][12] = {{0}, {0}};
    
    /* Player 2 ships */
    int grid2[12][12] = {{0}, {0}};
    int ships2[12][12] = {{0}, {0}};
    
    
    
    
    
    
    
    
    
    
    
    
    {
    int map[12][12] = {0};
    
    printf( "Place your 5 space ship\n" );
    placement2(map, ships, 5);
    
    printf( "Place your 4 space ship\n" );
    placement2(map, ships, 4);
    
    printf( "Place your 3 space ship\n" );
    placement2(map, ships, 3);
    
    printf( "Place your second 3 space ship\n" );
    placement2(map, ships, 3);
    
    printf( "Place your 2 space ship\n" );
    placement2(map, ships, 2);
    }
    
    void placement2(int map[][12], int ships[][12], int num)
    {
    int y;
    int x;
    char voh;
    
    int i;
    int check = 0;
    
    graph(map);
    
    printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
    
    printf( "Row:" );
    y = choose();
    
    printf( "Column:" );
    x = choose();
    
    printf( "Vertical or Horizontal? (V/H):" );
    scanf( " %c", &voh ); /* BREAKS DOWN HERE */
    
    if(voh == 'V' || voh == 'v'){
    if(y + num >= 8){
    printf( "Goes off the grid. Retry.\n" );
    placement2(map, ships, num);
    }
    else{
    for(i = y; i < y + num; i++){
    if(ships[i][x] == 1)
    check = 1;
    }
    if(check == 1){
    printf( "Intersects another ship. Retry.\n" );
    placement2(map, ships, num);
    }
    else{
    for(i = y; i < y + num; i++){
    map[i][x] = 2;
    ships[i][x] = 1;
    }
    }
    }
    }
    
    else if(voh == 'H' || voh == 'h'){
    if(num + x >= 8){
    printf( "Goes off the grid. Retry.\n" );
    placement2(map, ships, num);
    }
    else{
    for(i = x; i < x + num; i++){
    if(ships[y][i] == 1)
    check = 1;
    }
    if(check == 1){
    printf( "Intersects another ship. Retry.\n" );
    placement2(map, ships, num);
    }
    else{
    for(i = x; i < x + num; i++){
    map[y][i] = 2;
    ships[y][i] = 1;
    }
    }
    }
    }
    
    else{
    printf( "Invalid. Retry.\n" );
    placement2(map, ships, num);
    }
    }
    int y;
    int x;
    
    int gamestatus = 1;
    
    while(gamestatus == 1){
    
    cout<< "Player 1's turn\n" ;
    
    graph(grid2);
    
    cout<< "Row:" ;
    y = choose();
    
    cout<<"Column:" ;
    x = choose();
    
    shoot(grid2, ships2, y - 1, x - 1, &gamestatus);
    
    if(gamestatus == 0){
    cout<< "PLAYER 1 WINS!\n" ;
    break;
    }
    
    cout<< "Player 2's turn.\n";
    
    graph(grid1);
    
    cout<< "Row:" ;
    y = choose();
    
    cout<<"Column:" ;
    x = choose();
    
    shoot(grid1, ships1, y - 1, x - 1, &gamestatus);
    
    if(gamestatus == 0)
    cout<< "PLAYER 2 WINS!\n" ;
    
    else
    cout<<"Next round.\n" ;
    }
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Thanks to the code formatter I could look at this code for a while.
    The first thing I noticed is wrong function declaration, which is placed in the body of choose(). This wrong declaration could lead to the other errors.
    And indent your code.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    12

    i fixed that part but im still having errors

    i fixed that but im still gettin erros. heres my code

    Code:
    #include <stdio.h>
        #include <iostream>
        #include <iomanip>
        using namespace std;
        int main () { 
        int choose;
        int num;
        int ships;
        int y;
        int x;
        
        scanf( "%d", &num );
        
        if(num < 1 || num > 8){
        printf( "Invalid choice, choose again.\n" );
        num = choose;
        }
        
        return num;
        }
        
        void graph(int grid[][8])
        {
        int i;
        int j;
        
        printf( " 12345678\n" );
        for(i = 0; i < 8; i++){
        printf( "%d", i );
        
        for(j = 0; j < 8; j++){
        
        if(grid[i][j] == 0)
        printf( " " );
        else if(grid[i][j] == 1)
        printf( "X" );
        else if(grid[i][j] == 2)
        printf( "O" );
        }
        printf( "\n" );
        }
        
        printf( "\n" );
        }
        
        void shoot(int grid[][8], int ships[][8], int y, int x, int *game)
        {
        if(grid[y][x] == 1 || grid[y][x] == 2){
        printf( "This space has already been chosen, choose another space\n" );
        
        printf( "Row:" );
        y = choose;
        
        printf( "Column:" );
        x = choose();
        
        shoot(grid, ships, y, x, game);
        }
        
        else{
        if(ships[y][x] == 1){
        printf( "HIT!\n" );
        grid[y][x] = 1;
        ships[y][x] = 0;
        }
        else if(ships[y][x] == 0){
        printf( "Miss.\n" );
        grid[y][x] = 2;
        }
        }
        
        int k;
        int l;
        int check = 0;
        
        for(k = 0; k < 8; k++){
        for(l = 0; l < 8; l++){
        
        if(ships[k][l] == 1)
        check = 1;
        }
        }
        
        if(check == 1)
        *game = 0;
        
        }
        
        int main()
        {
        /* Grid is what the players see, ships is where the ships are */
        /* Player 1 ships */
        int grid1[8][8] = {{0}, {0}};
        int ships1[8][8] = {{0}, {0}};
        
        /* Player 2 ships */
        int grid2[8][8] = {{0}, {0}};
        int ships2[8][8] = {{0}, {0}};
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        void placement2(int map[][8], int ships[][8], int num);
        
        void placement(int ships[][8]);
        {
        int map[8][8] = {0};
        
        printf( "Place your 5 space ship\n" );
        placement2(map, ships, 5);
        
        printf( "Place your 4 space ship\n" );
        placement2(map, ships, 4);
        
        printf( "Place your 3 space ship\n" );
        placement2(map, ships, 3);
        
        printf( "Place your second 3 space ship\n" );
        placement2(map, ships, 3);
        
        printf( "Place your 2 space ship\n" );
        placement2(map, ships, 2);
        }
        
        void placement2(int map[][8], int ships[][8], int num)
        {
        int y;
        int x;
        char voh;
        
        int i;
        int check = 0;
        
        graph(map);
        
        printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
        
        printf( "Row:" );
        y = choose();
        
        printf( "Column:" );
        x = choose();
        
        printf( "Vertical or Horizontal? (V/H):" );
        scanf( " %c", &voh ); /* BREAKS DOWN HERE */
        
        if(voh == 'V' || voh == 'v'){
        if(y + num >= 8){
        printf( "Goes off the grid. Retry.\n" );
        placement2(map, ships, num);
        }
        else{
        for(i = y; i < y + num; i++){
        if(ships[i][x] == 1)
        check = 1;
        }
        if(check == 1){
        printf( "Intersects another ship. Retry.\n" );
        placement2(map, ships, num);
        }
        else{
        for(i = y; i < y + num; i++){
        map[i][x] = 2;
        ships[i][x] = 1;
        }
        }
        }
        }
        
        else if(voh == 'H' || voh == 'h'){
        if(num + x >= 8){
        printf( "Goes off the grid. Retry.\n" );
        placement2(map, ships, num);
        }
        else{
        for(i = x; i < x + num; i++){
        if(ships[y][i] == 1)
        check = 1;
        }
        if(check == 1){
        printf( "Intersects another ship. Retry.\n" );
        placement2(map, ships, num);
        }
        else{
        for(i = x; i < x + num; i++){
        map[y][i] = 2;
        ships[y][i] = 1;
        }
        }
        }
        }
        
        else{
        printf( "Invalid. Retry.\n" );
        placement2(map, ships, num);
        }
        }
        /* SOME KIND OF SHIP PLACEMENT HERE */
        
        
        int y;
        int x;
        
        int gamestatus = 1;
        
        while(gamestatus == 1){
        
        printf( "\nPlayer 1's turn\n" );
        
        graph(grid2);
        
        printf( "Row:" );
        y = choose();
        
        printf( "Column:" );
        x = choose();
        
        shoot(grid2, ships2, y - 1, x - 1, &gamestatus);
        
        if(gamestatus == 0){
        printf( "PLAYER 1 WINS!\n" );
        break;
        }
        
        printf( "\nPlayer 2's turn.\n" );
        
        graph(grid1);
        
        printf( "Row:" );
        y = choose();
        
        printf( "Column:" );
        x = choose();
        
        shoot(grid1, ships1, y - 1, x - 1, &gamestatus);
        
        if(gamestatus == 0)
        printf( "PLAYER 2 WINS!\n" );
        
        else
        printf( "\nNext round.\n" );
        }
        
        return 0;
        }

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Before you attempt to fix your code, INDENT it properly. If I wrote code like you do, I would keep getting lost too.

    Code:
    int main ()
    {
        int choose;
        int num;
        int ships;
        int y;
        int x;
    
        scanf( "%d", &num );
    
        if(num < 1 || num > 8)
        {
            printf( "Invalid choice, choose again.\n" );
            num = choose;
        }
    
        return num;
    }
    This is your INDENTED main(). Think of why it will not work.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    12

    i indented it the proper way.

    im sorry for all of that. my compiler didnt have a format option. but i tried to compiled it and i declared choose in the beginning and its giving me these other errors :

    In function `void shoot(int (*)[8], int (*)[8], int, int, int*)':
    line 52 `choose' undeclared (first use this function)
    line 139 a function-definition is not allowed here before '{' token

    Code:
    #include <stdio.h>
     #include <iostream>
     #include <iomanip>
    
    using namespace std;
    int main()
    {
        int choose;
        int num;
        int ships;
        int y;
        int x;
    
        scanf( "%d", &num );
    
        if ( num < 1 || num > 8 )
        {
            printf( "Invalid choice, choose again.\n" );
            num = choose;
        }
    
        return num;
    }
    
    void graph( int grid[][ 8 ] )
    {
        int i;
        int j;
    
        printf( " 12345678\n" );
    
        for ( i = 0; i < 8; i++ )
        {
            printf( "%d", i );
    
            for ( j = 0; j < 8; j++ )
            {
    
                if ( grid[ i ][ j ] == 0 )
                    printf( " " );
                else if ( grid[ i ][ j ] == 1 )
                    printf( "X" );
                else if ( grid[ i ][ j ] == 2 )
                    printf( "O" );
            }
    
            printf( "\n" );
        }
    
        printf( "\n" );
    }
    
    void shoot( int grid[][ 8 ], int ships[][ 8 ], int y, int x, int *game )
    {
        if ( grid[ y ][ x ] == 1 || grid[ y ][ x ] == 2 )
        {
            printf( "This space has already been chosen, choose another space\n" );
    
            printf( "Row:" );
            y = choose;
    
            printf( "Column:" );
            x = choose();
    
            shoot( grid, ships, y, x, game );
        }
    
        else
        {
            if ( ships[ y ][ x ] == 1 )
            {
                printf( "HIT!\n" );
                grid[ y ][ x ] = 1;
                ships[ y ][ x ] = 0;
            }
            else if ( ships[ y ][ x ] == 0 )
            {
                printf( "Miss.\n" );
                grid[ y ][ x ] = 2;
            }
        }
    
        int k;
        int l;
        int check = 0;
    
        for ( k = 0; k < 8; k++ )
        {
            for ( l = 0; l < 8; l++ )
            {
    
                if ( ships[ k ][ l ] == 1 )
                    check = 1;
            }
        }
    
        if ( check == 1 )
            * game = 0;
    
    
    
    
        {
            /* Grid is what the players see, ships is where the ships are */
            /* Player 1 ships */
            int grid1[ 8 ][ 8 ] = {{0}, {0}};
            int ships1[ 8 ][ 8 ] = {{0}, {0}};
    
            /* Player 2 ships */
            int grid2[ 8 ][ 8 ] = {{0}, {0}};
            int ships2[ 8 ][ 8 ] = {{0}, {0}};
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
            void placement2( int map[][ 8 ], int ships[][ 8 ], int num );
    
            void placement( int ships[][ 8 ] );
            {
                int map[ 8 ][ 8 ] = {0};
    
                printf( "Place your 5 space ship\n" );
                placement2( map, ships, 5 );
    
                printf( "Place your 4 space ship\n" );
                placement2( map, ships, 4 );
    
                printf( "Place your 3 space ship\n" );
                placement2( map, ships, 3 );
    
                printf( "Place your second 3 space ship\n" );
                placement2( map, ships, 3 );
    
                printf( "Place your 2 space ship\n" );
                placement2( map, ships, 2 );
            }
    
            void placement2( int map[][ 8 ], int ships[][ 8 ], int num )
            {
                int y;
                int x;
                char voh;
    
                int i;
                int check = 0;
    
                graph( map );
    
                printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                printf( "Vertical or Horizontal? (V/H):" );
                scanf( " %c", &voh ); /* BREAKS DOWN HERE */
    
                if ( voh == 'V' || voh == 'v' )
                {
                    if ( y + num >= 8 )
                    {
                        printf( "Goes off the grid. Retry.\n" );
                        placement2( map, ships, num );
                    }
                    else
                    {
                        for ( i = y; i < y + num; i++ )
                        {
                            if ( ships[ i ][ x ] == 1 )
                                check = 1;
                        }
    
                        if ( check == 1 )
                        {
                            printf( "Intersects another ship. Retry.\n" );
                            placement2( map, ships, num );
                        }
                        else
                        {
                            for ( i = y; i < y + num; i++ )
                            {
                                map[ i ][ x ] = 2;
                                ships[ i ][ x ] = 1;
                            }
                        }
                    }
                }
    
                else if ( voh == 'H' || voh == 'h' )
                {
                    if ( num + x >= 8 )
                    {
                        printf( "Goes off the grid. Retry.\n" );
                        placement2( map, ships, num );
                    }
                    else
                    {
                        for ( i = x; i < x + num; i++ )
                        {
                            if ( ships[ y ][ i ] == 1 )
                                check = 1;
                        }
    
                        if ( check == 1 )
                        {
                            printf( "Intersects another ship. Retry.\n" );
                            placement2( map, ships, num );
                        }
                        else
                        {
                            for ( i = x; i < x + num; i++ )
                            {
                                map[ y ][ i ] = 2;
                                ships[ y ][ i ] = 1;
                            }
                        }
                    }
                }
    
                else
                {
                    printf( "Invalid. Retry.\n" );
                    placement2( map, ships, num );
                }
            }
    
            /* SOME KIND OF SHIP PLACEMENT HERE */
    
    
            int y;
            int x;
    
            int gamestatus = 1;
    
            while ( gamestatus == 1 )
            {
    
                printf( "\nPlayer 1's turn\n" );
    
                graph( grid2 );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                shoot( grid2, ships2, y - 1, x - 1, &gamestatus );
    
                if ( gamestatus == 0 )
                {
                    printf( "PLAYER 1 WINS!\n" );
                    break;
                }
    
                printf( "\nPlayer 2's turn.\n" );
    
                graph( grid1 );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                shoot( grid1, ships1, y - 1, x - 1, &gamestatus );
    
                if ( gamestatus == 0 )
                    printf( "PLAYER 2 WINS!\n" );
    
                else
                    printf( "\nNext round.\n" );
            }
    
            return 0;
        }

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    You need to format code on your own. Maybe you had tabs which were removed by the forum. Nevermind.

    In your first post 'choose' was a function, and that was right way to do it. Now you made it a variable.

    Code:
    int choose()
    {
        int num;
    
        void placement(int ships[][12]);
        void shoot(int grid[][12], int ships[][12], int y, int x, int *game);
        scanf( "%d", &num );
    
        if(num < 0 || num > 12)
        {
            printf( "Invalid choice, choose again.\n" );
            num = choose();
        }
    
        return num;
    }
    Change to:

    Code:
    void placement(int ships[][12]);
    void shoot(int grid[][12], int ships[][12], int y, int x, int *game);
    
    int choose()
    {
        int num;
    
        scanf( "%d", &num );
    
        if(num < 0 || num > 12)
        {
            printf( "Invalid choice, choose again.\n" );
            num = choose();
        }
    
        return num;
    }

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    i did that and now im gettin new error and im just confused now. here my code :

    Code:
     #include <stdio.h>
        #include <iostream>
        #include <iomanip>
        using namespace std;
        
    void placement(int ships[][12]);
    void shoot(int grid[][12], int ships[][12], int y, int x, int *game);
    
    
        int choose() { 
        
        int num;
        int ships;
        int y;
        int x;
        
        scanf( "%d", &num );
        
        if(num < 1 || num > 8){
        printf( "Invalid choice, choose again.\n" );
        num = choose;
        }
        
        return num;
        }
        
        void graph(int grid[][8])
        {
        int i;
        int j;
        
        printf( " 12345678\n" );
        for(i = 0; i < 8; i++){
        printf( "%d", i );
        
        for(j = 0; j < 8; j++){
        
        if(grid[i][j] == 0)
        printf( " " );
        else if(grid[i][j] == 1)
        printf( "X" );
        else if(grid[i][j] == 2)
        printf( "O" );
        }
        printf( "\n" );
        }
        
        printf( "\n" );
        }
        
        void shoot(int grid[][8], int ships[][8], int y, int x, int *game)
        {
        if(grid[y][x] == 1 || grid[y][x] == 2){
        printf( "This space has already been chosen, choose another space\n" );
        
        printf( "Row:" );
        y = choose;
        
        printf( "Column:" );
        x = choose();
        
        shoot(grid, ships, y, x, game);
        }
        
        else{
        if(ships[y][x] == 1){
        printf( "HIT!\n" );
        grid[y][x] = 1;
        ships[y][x] = 0;
        }
        else if(ships[y][x] == 0){
        printf( "Miss.\n" );
        grid[y][x] = 2;
        }
        }
        
        int k;
        int l;
        int check = 0;
        
        for(k = 0; k < 8; k++){
        for(l = 0; l < 8; l++){
        
        if(ships[k][l] == 1)
        check = 1;
        }
        }
        
        if(check == 1)
        *game = 0;
        
        
        
    
        {
        /* Grid is what the players see, ships is where the ships are */
        /* Player 1 ships */
        int grid1[8][8] = {{0}, {0}};
        int ships1[8][8] = {{0}, {0}};
        
        /* Player 2 ships */
        int grid2[8][8] = {{0}, {0}};
        int ships2[8][8] = {{0}, {0}};
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        void placement2(int map[][8], int ships[][8], int num);
        
        void placement(int ships[][8]);
        {
        int map[8][8] = {0};
        
        printf( "Place your 5 space ship\n" );
        placement2(map, ships, 5);
        
        printf( "Place your 4 space ship\n" );
        placement2(map, ships, 4);
        
        printf( "Place your 3 space ship\n" );
        placement2(map, ships, 3);
        
        printf( "Place your second 3 space ship\n" );
        placement2(map, ships, 3);
        
        printf( "Place your 2 space ship\n" );
        placement2(map, ships, 2);
        }
        
        void placement2(int map[][8], int ships[][8], int num)
        {
        int y;
        int x;
        char voh;
        
        int i;
        int check = 0;
        
        graph(map);
        
        printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
        
        printf( "Row:" );
        y = choose();
        
        printf( "Column:" );
        x = choose();
        
        printf( "Vertical or Horizontal? (V/H):" );
        scanf( " %c", &voh ); /* BREAKS DOWN HERE */
        
        if(voh == 'V' || voh == 'v'){
        if(y + num >= 8){
        printf( "Goes off the grid. Retry.\n" );
        placement2(map, ships, num);
        }
        else{
        for(i = y; i < y + num; i++){
        if(ships[i][x] == 1)
        check = 1;
        }
        if(check == 1){
        printf( "Intersects another ship. Retry.\n" );
        placement2(map, ships, num);
        }
        else{
        for(i = y; i < y + num; i++){
        map[i][x] = 2;
        ships[i][x] = 1;
        }
        }
        }
        }
        
        else if(voh == 'H' || voh == 'h'){
        if(num + x >= 8){
        printf( "Goes off the grid. Retry.\n" );
        placement2(map, ships, num);
        }
        else{
        for(i = x; i < x + num; i++){
        if(ships[y][i] == 1)
        check = 1;
        }
        if(check == 1){
        printf( "Intersects another ship. Retry.\n" );
        placement2(map, ships, num);
        }
        else{
        for(i = x; i < x + num; i++){
        map[y][i] = 2;
        ships[y][i] = 1;
        }
        }
        }
        }
        
        else{
        printf( "Invalid. Retry.\n" );
        placement2(map, ships, num);
        }
        }
        /* SOME KIND OF SHIP PLACEMENT HERE */
        
        
        int y;
        int x;
        
        int gamestatus = 1;
        
        while(gamestatus == 1){
        
        printf( "\nPlayer 1's turn\n" );
        
        graph(grid2);
        
        printf( "Row:" );
        y = choose();
        
        printf( "Column:" );
        x = choose();
        
        shoot(grid2, ships2, y - 1, x - 1, &gamestatus);
        
        if(gamestatus == 0){
        printf( "PLAYER 1 WINS!\n" );
        break;
        }
        
        printf( "\nPlayer 2's turn.\n" );
        
        graph(grid1);
        
        printf( "Row:" );
        y = choose();
        
        printf( "Column:" );
        x = choose();
        
        shoot(grid1, ships1, y - 1, x - 1, &gamestatus);
        
        if(gamestatus == 0)
        printf( "PLAYER 2 WINS!\n" );
        
        else
        printf( "\nNext round.\n" );
        }
        
        return 0;
        }}

  8. #8
    Registered User
    Join Date
    Dec 2010
    Location
    Australia
    Posts
    8
    When I compiled the code on my computer, the majority of the errors were relating to an undefined "placement2" function.

    You seem to have an unused function declaration:

    Code:
    void placement(int ships[][12]); //Uhm I cant see a definition for this???
    void shoot(int grid[][12], int ships[][12], int y, int x, int *game);
    So either change the unknown function declaration, or add the line:

    Code:
    void placement(int ships[][12]); //If you dont intend on using this...remove it...
    void shoot(int grid[][12], int ships[][12], int y, int x, int *game);
    void placement2(int map[][8], int ships[][8], int num); //NEW LINE TO DECLARE THE FUNCTION
    Seriously, you need to format your code. My compiler is telling me that a void function is returning a value, and I am not going to trawl through that code to find the mucked up bracket. Also, on line 21, you are trying to make an integer equal a function...bad!

    Code:
    num = choose; //Won't work
    Tidy up your code, and your problems won't seem as bad.

  9. #9
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    i try to format the code but i guess the forum disables my formatting. i did the corrections that you showed me i feel like a silly one for missing that but now i tried to run it again and it says :

    In function `void shoot(int (*)[12], int (*)[12], int, int, int*)':
    line 143 a function-definition is not allowed here before '{' token
    line 143 expected `,' or `;' before '{' token
    line 261 return-statement with a value, in function returning 'void'











    Code:
    #include <stdio.h>
     #include <iostream>
     #include <iomanip>
    
    using namespace std;
    
    void shoot( int grid[][ 12 ], int ships[][ 12 ], int y, int x, int *game );
    void placement2( int map[][ 8 ], int ships[][ 8 ], int num );
    
    int choose()
        {
    
        int num;
        int ships;
        int y;
        int x;
    
        scanf( "%d", &num );
    
        if ( num < 1 || num > 12 )
            {
            printf( "Invalid choice, choose again.\n" );
    
            }
    
        return num;
        }
    
    void graph( int grid[][ 12 ] )
        {
        int i;
        int j;
    
        printf( " 123456789101112\n" );
    
        for ( i = 0; i < 12; i++ )
            {
            printf( "%d", i );
    
            for ( j = 0; j < 12; j++ )
                {
    
                if ( grid[ i ][ j ] == 0 )
                    printf( " " );
                else if ( grid[ i ][ j ] == 1 )
                    printf( "X" );
                else if ( grid[ i ][ j ] == 2 )
                    printf( "O" );
                }
    
            printf( "\n" );
            }
    
        printf( "\n" );
        }
    
    void shoot( int grid[][ 12 ], int ships[][ 12 ], int y, int x, int *game )
        {
        if ( grid[ y ][ x ] == 1 || grid[ y ][ x ] == 2 )
            {
            printf( "This space has already been chosen, choose another space\n" );
    
            printf( "Row:" );
    
    
            printf( "Column:" );
            x = choose();
    
            shoot( grid, ships, y, x, game );
            }
    
        else
            {
            if ( ships[ y ][ x ] == 1 )
                {
                printf( "HIT!\n" );
                grid[ y ][ x ] = 1;
                ships[ y ][ x ] = 0;
                }
            else if ( ships[ y ][ x ] == 0 )
                {
                printf( "Miss.\n" );
                grid[ y ][ x ] = 2;
                }
            }
    
        int k;
        int l;
        int check = 0;
    
        for ( k = 0; k < 8; k++ )
            {
            for ( l = 0; l < 8; l++ )
                {
    
                if ( ships[ k ][ l ] == 1 )
                    check = 1;
                }
            }
    
        if ( check == 1 )
            * game = 0;
    
    
    
    
            {
            /* Grid is what the players see, ships is where the ships are */
            /* Player 1 ships */
            int grid1[ 12 ][ 12 ] = {{0}, {0}};
            int ships1[ 12 ][ 12 ] = {{0}, {0}};
    
            /* Player 2 ships */
            int grid2[ 12 ][ 12 ] = {{0}, {0}};
            int ships2[ 12 ][ 12 ] = {{0}, {0}};
    
    
    
            void placement2( int map[][ 12 ], int ships[][ 12 ], int num );
    
            void placement( int ships[][ 12 ] );
                {
                int map[ 12 ][ 12 ] = {0};
    
                printf( "Place your 5 space ship\n" );
                placement2( map, ships, 5 );
    
                printf( "Place your 4 space ship\n" );
                placement2( map, ships, 4 );
    
                printf( "Place your 3 space ship\n" );
                placement2( map, ships, 3 );
    
                printf( "Place your second 3 space ship\n" );
                placement2( map, ships, 3 );
    
                printf( "Place your 2 space ship\n" );
                placement2( map, ships, 2 );
                }
    
            void placement2( int map[][ 12 ], int ships[][ 12 ], int num )
                {
                int y;
                int x;
                char voh;
    
                int i;
                int check = 0;
    
                graph( map );
    
                printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                printf( "Vertical or Horizontal? (V/H):" );
                scanf( " %c", &voh ); /* BREAKS DOWN HERE */
    
                if ( voh == 'V' || voh == 'v' )
                    {
                    if ( y + num >= 12 )
                        {
                        printf( "Goes off the grid. Retry.\n" );
                        placement2( map, ships, num );
                        }
                    else
                        {
                        for ( i = y; i < y + num; i++ )
                            {
                            if ( ships[ i ][ x ] == 1 )
                                check = 1;
                            }
    
                        if ( check == 1 )
                            {
                            printf( "Intersects another ship. Retry.\n" );
                            placement2( map, ships, num );
                            }
                        else
                            {
                            for ( i = y; i < y + num; i++ )
                                {
                                map[ i ][ x ] = 2;
                                ships[ i ][ x ] = 1;
                                }
                            }
                        }
                    }
    
                else if ( voh == 'H' || voh == 'h' )
                    {
                    if ( num + x >= 12 )
                        {
                        printf( "Goes off the grid. Retry.\n" );
                        placement2( map, ships, num );
                        }
                    else
                        {
                        for ( i = x; i < x + num; i++ )
                            {
                            if ( ships[ y ][ i ] == 1 )
                                check = 1;
                            }
    
                        if ( check == 1 )
                            {
                            printf( "Intersects another ship. Retry.\n" );
                            placement2( map, ships, num );
                            }
                        else
                            {
                            for ( i = x; i < x + num; i++ )
                                {
                                map[ y ][ i ] = 2;
                                ships[ y ][ i ] = 1;
                                }
                            }
                        }
                    }
    
                else
                    {
                    printf( "Invalid. Retry.\n" );
                    placement2( map, ships, num );
                    }
                }
    
            /* SOME KIND OF SHIP PLACEMENT HERE */
    
    
            int y;
            int x;
    
            int gamestatus = 1;
    
            while ( gamestatus == 1 )
                {
    
                printf( "\nPlayer 1's turn\n" );
    
                graph( grid2 );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                shoot( grid2, ships2, y - 1, x - 1, &gamestatus );
    
                if ( gamestatus == 0 )
                    {
                    printf( "PLAYER 1 WINS!\n" );
                    break;
                    }
    
                printf( "\nPlayer 2's turn.\n" );
    
                graph( grid1 );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                shoot( grid1, ships1, y - 1, x - 1, &gamestatus );
    
                if ( gamestatus == 0 )
                    printf( "PLAYER 2 WINS!\n" );
    
                else
                    printf( "\nNext round.\n" );
                }
    
            return 0;
            }
        }

  10. #10
    Registered User
    Join Date
    Dec 2010
    Location
    Australia
    Posts
    8
    line 261 return-statement with a value, in function returning 'void'
    I removed the return 0 from your code. The void function does not need to return a value (as I said in my previous post).

    line 143 a function-definition is not allowed here before '{' token
    We found the missing function declaration from before. Function delcarations (if they are in the same file) must occur at the top of the code, above everything else. You can not declare a function within a function. I was getting numerous errors regarding a conversion between two array types (due to an apparent change to the parameter list of the "placement2" definition, so I fixed that).

    Also, the "placement" function had a semicolon after the function name, resulting in a local declaration error. I removed this, and added a declaration to the top of the code.

    More errors just kept popping up...

    Code:
     if ( check == 1 )
            * game = 0;
    	{
    You are getting more local function definition errors due to this section of code. Please go through your code from top to bottom and make sure you are encompassing what you need within the appropriate brackets. I can not fix this, as it is logic based and is up to you.

    Make sure that you are not writing function definitions (edit*) WITHIN FUNCTIONS! That is what is causing the errors with regards to local declarations. Make sure all functions end with a } before a new function starts!

    Here is the modified code. The rest is up to you, good luck:

    Code:
    #include <stdio.h>
     #include <iostream>
     #include <iomanip>
    
    using namespace std;
    
    void shoot( int grid[][ 12 ], int ships[][ 12 ], int y, int x, int *game );
    //void placement2( int map[][ 8 ], int ships[][ 8 ], int num );
    void placement2( int map[][ 12 ], int ships[][ 12 ], int num );
    void placement( int ships[][ 12 ] );
    
    int choose()
        {
    
        int num;
        int ships;
        int y;
        int x;
    
        scanf( "%d", &num );
    
        if ( num < 1 || num > 12 )
            {
            printf( "Invalid choice, choose again.\n" );
    
            }
    
        return num;
        }
    
    void graph( int grid[][ 12 ] )
        {
        int i;
        int j;
    
        printf( " 123456789101112\n" );
    
        for ( i = 0; i < 12; i++ )
            {
            printf( "%d", i );
    
            for ( j = 0; j < 12; j++ )
                {
    
                if ( grid[ i ][ j ] == 0 )
                    printf( " " );
                else if ( grid[ i ][ j ] == 1 )
                    printf( "X" );
                else if ( grid[ i ][ j ] == 2 )
                    printf( "O" );
                }
    
            printf( "\n" );
            }
    
        printf( "\n" );
        }
    
    void shoot( int grid[][ 12 ], int ships[][ 12 ], int y, int x, int *game )
        {
        if ( grid[ y ][ x ] == 1 || grid[ y ][ x ] == 2 )
            {
            printf( "This space has already been chosen, choose another space\n" );
    
            printf( "Row:" );
    
    
            printf( "Column:" );
            x = choose();
    
            shoot( grid, ships, y, x, game );
            }
    
        else
            {
            if ( ships[ y ][ x ] == 1 )
                {
                printf( "HIT!\n" );
                grid[ y ][ x ] = 1;
                ships[ y ][ x ] = 0;
                }
            else if ( ships[ y ][ x ] == 0 )
                {
                printf( "Miss.\n" );
                grid[ y ][ x ] = 2;
                }
            }
    
        int k;
        int l;
        int check = 0;
    
        for ( k = 0; k < 8; k++ )
            {
            for ( l = 0; l < 8; l++ )
                {
    
                if ( ships[ k ][ l ] == 1 )
                    check = 1;
                }
            }
    
        if ( check == 1 )
            * game = 0;
    	{
            /* Grid is what the players see, ships is where the ships are */
            /* Player 1 ships */
            int grid1[ 12 ][ 12 ] = {{0}, {0}};
            int ships1[ 12 ][ 12 ] = {{0}, {0}};
    
            /* Player 2 ships */
            int grid2[ 12 ][ 12 ] = {{0}, {0}};
            int ships2[ 12 ][ 12 ] = {{0}, {0}};
    
    
    
                void placement( int ships[][ 12 ] )
                {
                int map[ 12 ][ 12 ] = {0};
    
                printf( "Place your 5 space ship\n" );
                placement2( map, ships, 5 );
    
                printf( "Place your 4 space ship\n" );
                placement2( map, ships, 4 );
    
                printf( "Place your 3 space ship\n" );
                placement2( map, ships, 3 );
    
                printf( "Place your second 3 space ship\n" );
                placement2( map, ships, 3 );
    
                printf( "Place your 2 space ship\n" );
                placement2( map, ships, 2 );
                }
    
            void placement2( int map[][ 12 ], int ships[][ 12 ], int num )
                {
                int y;
                int x;
                char voh;
    
                int i;
                int check = 0;
    
                graph( map );
    
                printf( "Choose a point on the grid. Note: ships will go down or to the right of this point.\n" );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                printf( "Vertical or Horizontal? (V/H):" );
                scanf( " %c", &voh ); /* BREAKS DOWN HERE */
    
                if ( voh == 'V' || voh == 'v' )
                    {
                    if ( y + num >= 12 )
                        {
                        printf( "Goes off the grid. Retry.\n" );
                        placement2( map, ships, num );
                        }
                    else
                        {
                        for ( i = y; i < y + num; i++ )
                            {
                            if ( ships[ i ][ x ] == 1 )
                                check = 1;
                            }
    
                        if ( check == 1 )
                            {
                            printf( "Intersects another ship. Retry.\n" );
                            placement2( map, ships, num );
                            }
                        else
                            {
                            for ( i = y; i < y + num; i++ )
                                {
                                map[ i ][ x ] = 2;
                                ships[ i ][ x ] = 1;
                                }
                            }
                        }
                    }
    
                else if ( voh == 'H' || voh == 'h' )
                    {
                    if ( num + x >= 12 )
                        {
                        printf( "Goes off the grid. Retry.\n" );
                        placement2( map, ships, num );
                        }
                    else
                        {
                        for ( i = x; i < x + num; i++ )
                            {
                            if ( ships[ y ][ i ] == 1 )
                                check = 1;
                            }
    
                        if ( check == 1 )
                            {
                            printf( "Intersects another ship. Retry.\n" );
                            placement2( map, ships, num );
                            }
                        else
                            {
                            for ( i = x; i < x + num; i++ )
                                {
                                map[ y ][ i ] = 2;
                                ships[ y ][ i ] = 1;
                                }
                            }
                        }
                    }
    
                else
                    {
                    printf( "Invalid. Retry.\n" );
                    placement2( map, ships, num );
                    }
                }
    
            /* SOME KIND OF SHIP PLACEMENT HERE */
    
    
            int y;
            int x;
    
            int gamestatus = 1;
    
            while ( gamestatus == 1 )
                {
    
                printf( "\nPlayer 1's turn\n" );
    
                graph( grid2 );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                shoot( grid2, ships2, y - 1, x - 1, &gamestatus );
    
                if ( gamestatus == 0 )
                    {
                    printf( "PLAYER 1 WINS!\n" );
                    break;
                    }
    
                printf( "\nPlayer 2's turn.\n" );
    
                graph( grid1 );
    
                printf( "Row:" );
                y = choose();
    
                printf( "Column:" );
                x = choose();
    
                shoot( grid1, ships1, y - 1, x - 1, &gamestatus );
    
                if ( gamestatus == 0 )
                    printf( "PLAYER 2 WINS!\n" );
    
                else
                    printf( "\nNext round.\n" );
                }
            }
        }

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    ok and another thing is that im missing the rest of my shoot method but idk how i would write it out im confused

  12. #12
    Registered User
    Join Date
    Dec 2010
    Location
    Australia
    Posts
    8
    Writing the rest of your shoot method is something that the assignment question should tell you how to do. We can't be given a lump of code and be expected to complete it, without knowing the game mechanics :\ I still haven't read the code enough to get a logical understanding of how it works, to even guess how to complete it.

    Since your initial approach to finishing the function is primarily logic based (as opposed to syntax based), it may be more appropriate for you to consult your lecturer directly. They will be able to help you have a go at writing an answer; however, come back to us if you have any further syntactic errors.

    You would be surprised at how much information lecturers will give about an assignment if you just go and ask them; don't be afraid, they are there to help!

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    12
    all i want to do is find a way to randomly place ships on the board to that the two player can guess the coordinates. its a simple battleship game

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question on battleship program.
    By newbc in forum C Programming
    Replies: 0
    Last Post: 12-07-2010, 08:32 AM
  2. Battleship program
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 04-07-2008, 09:02 AM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM