Thread: 2d arrays, setting values doesnt work

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    15

    2d arrays, setting values doesnt work

    im doing a battleship game program for my project.

    here's my code so far
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    int main_menu(char board1[10][10],char board2[10][10]){
        int choice;
        printf(" \n_____________\n");
        printf("|--Main Menu--|\n");
        printf("|             |\n");
    	printf("|[1]Game Start|\n");            //print main menu
    	printf("|[2]Logout    |\n");
        printf(" ~~~~~~~~~~~~~\n\n");
    
    
        do{
            printf("Enter Choice: ");
            scanf("%d",&choice);
            if (choice==2) {
                return 0;
            }
            else if (choice!=1){
                printf("Choice invalid!\n");
            }
        }while(choice!=2 && choice!=1);
        do{
        printf("\n[1]Single Player\n");
        printf("[2]Multiplayer\n");
        printf("[3]Go back\n");
        scanf("%d", &choice);
           switch(choice){
                case 1:
                    setBoard1(board1,board2);
                    printBoard1(board1,board2);
                    setShip(board1,board2);
                    break;
                case 2:
                    return 0;
                    break;
                case 3:
                    main_menu(board1,board2);
                    break;
                default:printf("Choice invalid!");
                    break;
           }
        }while(choice!=3);
    }
    
    
    int setBoard1(char board1[10][10], char board2[10][10]){
        int row,col;
        for (row=0;row<10;row++){
            for(col=0;col<10;col++){
                board1[row][col]=(char)247;
                board2[row][col]=(char)247;
            }
        }
    }
    int printBoard1(char board1[10][10],char board2[10][10]){
        int row,col;
        printf("             Player 1                            Player 2\n\n");
    for(row=0;row<10;row++){
            printf("%c",(char)row+65);
            for(col=0;col<10;col++){
                printf("%3c",board1[row][col]);
            }
            printf("  |||");
            for(col=0;col<10;col++){
                printf("%3c",board2[row][col]);
            }
            printf("  %c",(char)row+65);
            printf("\n\n");
        }
        printf(" ");
        for(col=1;col<=10;col++){
            printf("%3d",col);
        }
        printf("     ");
        for(col=1;col<=10;col++){
            printf("%3d",col);
        }
        putchar('\n');
    
    
    }
    int setShip(char board1[10][10],char board2[10][10]){
        int i,j,choice;
        char row, direction[5];
        int col;
    
    
            printf("    #       ship         size");
            printf("\n[1] x1 Aircraft Carrier   5\n");
            printf("[2] x1 Battleship         4\n");
            printf("[3] x1 Cruiser            3\n");
            printf("[4] x2 Destroyer          2\n");
            printf("[5] x2 Cruiser            1\n\n");
            printf("Choose a ship:");
            scanf("%d",&choice);
            switch (choice){
    
    
                case 1:
                    printf("-- Aircraft Carrier --\n");
                    printf("Place Head(row,col): ");
                    printf("row: ");
                    scanf("%c",&row);
                    getchar();
                    printf("col: ");
                    scanf("%d",&col);
                break;
                case 2:
                break;
                case 3:
                break;
                case 4:
                break;
                case 5:
                break;
                default:
                break;
            }
            printf("From head (up,down,left,right): ");
            scanf("%s", direction);
            printf("%s", direction);
            if(strcmp(direction,"up")==0){
                if (choice==1){
                    for(i=0;i<5;i++){
                        board1[(int)row-65][col-1]= 'A';
                        row--;
                    }
                }
                printBoard1(board1,board2);
            }
    }
    main(){
        char board1[10][10];
        char board2[10][10];
        printf("Hello! Welcome to Battleship C!\n");
        printf("What would you like to do?\n\n");
        if (main_menu(board1,board2)==0){
            return 0;
        }
    }
    my problem is in this function below
    Code:
    int setShip(char board1[10][10],char board2[10][10]){    int i,j,choice;
        char row, direction[5];
        int col;
    
    
            printf("    #       ship         size");
            printf("\n[1] x1 Aircraft Carrier   5\n");
            printf("[2] x1 Battleship         4\n");
            printf("[3] x1 Cruiser            3\n");
            printf("[4] x2 Destroyer          2\n");
            printf("[5] x2 Cruiser            1\n\n");
            printf("Choose a ship:");
            scanf("%d",&choice);
            switch (choice){
    
    
                case 1:
                    printf("-- Aircraft Carrier --\n");
                    printf("Place Head(row,col): ");
                    printf("row: ");
                    scanf("%c",&row);
                    getchar();
                    printf("col: ");
                    scanf("%d",&col);
                break;
                case 2:
                break;
                case 3:
                break;
                case 4:
                break;
                case 5:
                break;
                default:
                break;
            }
            printf("From head (up,down,left,right): ");
            scanf("%s", direction);
            printf("%s", direction);
            if(strcmp(direction,"up")==0){
                if (choice==1){
                    for(i=0;i<5;i++){
                        board1[(int)row-65][col-1]= 'A';
                        row--;
                    }
                }
                printBoard1(board1,board2);
            }
    }
    then when i print it the values doesnt change to 'A'... why?
    any help is appreciated

    i cannot set the values to 'A' and print it..

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You don't even use your counter in the for loop, line #42.

    EDIT: Never mind, I didn't see that you're decrementing "row".

    EDIT^2: Wait, what is this?
    Code:
    board1[(int)row-65][col-1]= 'A';
    "row-65"? Why?!

    EDIT^3: Oh, it's a character. See, things get confusing when you replace well defined symbols( 'A') with magic numbers(65) ...
    Last edited by GReaper; 02-18-2012 at 11:11 PM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    i want to set value 'A' from the user's initial input and to the 4 units above it

    EDIT: sorry for no documentation... just started writing this today O_O.
    Last edited by Isnalla; 02-18-2012 at 11:05 PM.

  4. #4
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    Quote Originally Posted by GReaper View Post
    You don't even use your counter in the for loop, line #42.

    EDIT: Never mind, I didn't see that you're decrementing "row".

    EDIT^2: Wait, what is this?
    Code:
    board1[(int)row-65][col-1]= 'A';

    "row-65"? Why?!
    because a standard battleship game has characters for row and integers for the column,
    so when the user inputs J 5 it would be like 9 4 (J in ASCII is 74.. -65 it will be 9)..
    i dont know if that logic is right O_O.. seems right to me.. please correct me if im wrong.

    EDIT: then it would be like board1[9][4]='A'; and 4 units above it would be 'A' too. that's what i would want to do..

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    Quote Originally Posted by GReaper View Post
    EDIT^3: Oh, it's a character. See, things get confusing when you replace well defined symbols( 'A') with magic numbers(65) ...
    oh. so can i do something like this instead?

    board1[(int)row-A][col-1]='A' ??

    or board1[(int)row-(int)'A'][col-1]='A' ??

    or neither... *sigh* i'm currently studying basic C
    and i just learned typecasting the other day.. so
    i guess i still have a lot to learn.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You don't need a cast at all. The character is promoted to integer, behind the scenes, while your code retains its readability.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    ^ it compiled.. . but it still doesnt print those values.. can you see anything wrong? or should i add something ...

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Post your whole code please, a test-drive will reveal the root of the problem.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    just removed the typecast at the setShip function. i hope i did right

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    
    
    int main_menu(char board1[10][10],char board2[10][10]){
        int choice;
        printf(" \n_____________\n");
        printf("|--Main Menu--|\n");
        printf("|             |\n");
    	printf("|[1]Game Start|\n");            //print main menu
    	printf("|[2]Logout    |\n");
        printf(" ~~~~~~~~~~~~~\n\n");
    
    
        do{
            printf("Enter Choice: ");
            scanf("%d",&choice);
            if (choice==2) {
                return 0;
            }
            else if (choice!=1){
                printf("Choice invalid!\n");
            }
        }while(choice!=2 && choice!=1);
        do{
        printf("\n[1]Single Player\n");
        printf("[2]Multiplayer\n");
        printf("[3]Go back\n");
        scanf("%d", &choice);
           switch(choice){
                case 1:
                    setBoard1(board1,board2);
                    printBoard1(board1,board2);
                    setShip(board1,board2);
                    break;
                case 2:
                    return 0;
                    break;
                case 3:
                    main_menu(board1,board2);
                    break;
                default:printf("Choice invalid!");
                    break;
           }
        }while(choice!=3);
    }
    
    
    int setBoard1(char board1[10][10], char board2[10][10]){
        int row,col;
        for (row=0;row<10;row++){
            for(col=0;col<10;col++){
                board1[row][col]=(char)247;
                board2[row][col]=(char)247;
            }
        }
    }
    int printBoard1(char board1[10][10],char board2[10][10]){
        int row,col;
        printf("             Player 1                            Player 2\n\n");
    for(row=0;row<10;row++){
            printf("%c",(char)row+65);
            for(col=0;col<10;col++){
                printf("%3c",board1[row][col]);
            }
            printf("  |||");
            for(col=0;col<10;col++){
                printf("%3c",board2[row][col]);
            }
            printf("  %c",(char)row+65);
            printf("\n\n");
        }
        printf(" ");
        for(col=1;col<=10;col++){
            printf("%3d",col);
        }
        printf("     ");
        for(col=1;col<=10;col++){
            printf("%3d",col);
        }
        putchar('\n');
    
    
    }
    int setShip(char board1[10][10],char board2[10][10]){
        int i,j,choice;
        char row, direction[5];
        int col;
    
    
            printf("    #       ship         size");
            printf("\n[1] x1 Aircraft Carrier   5\n");
            printf("[2] x1 Battleship         4\n");
            printf("[3] x1 Cruiser            3\n");
            printf("[4] x2 Destroyer          2\n");
            printf("[5] x2 Cruiser            1\n\n");
            printf("Choose a ship:");
            scanf("%d",&choice);
            switch (choice){
    
    
                case 1:
                    printf("-- Aircraft Carrier --\n");
                    printf("Place Head(row,col): ");
                    printf("row: ");
                    scanf("%c",&row);
                    getchar();
                    printf("col: ");
                    scanf("%d",&col);
                break;
                case 2:
                break;
                case 3:
                break;
                case 4:
                break;
                case 5:
                break;
                default:
                break;
            }
            printf("From head (up,down,left,right): ");
            scanf("%s", direction);
            printf("%s", direction);
            if(strcmp(direction,"up")==0){
                if (choice==1){
                    for(i=0;i<5;i++){
                        board1[row-'A'][col-1]= 'A';
                        row--;
                    }
                }
                printBoard1(board1,board2);
            }
    }
    main(){
        char board1[10][10];
        char board2[10][10];
        printf("Hello! Welcome to Battleship C!\n");
        printf("What would you like to do?\n\n");
        if (main_menu(board1,board2)==0){
            return 0;
        }
    }

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I got it. You set the board every single time. Set the board only once. That should fix your problem.

    EDIT: Note that you firstly print the board and then you update it by calling "setShip()". That combined with the fact that you set the board to default values every time, produces this result.
    Last edited by GReaper; 02-19-2012 at 12:18 AM.
    Devoted my life to programming...

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    i got it! did that ^ then put a getchar(); (on line95)below scanf("%d", &choice); (line 94)

    seems like the "enter" character goes to scanf("%c",&row);(line 102) when i input the choice.. >.<

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    15
    many thanks!

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Having the newline char mess up a char scanf() is one of the most common complaints we see on the forum.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesnt this work
    By digdug4life in forum C++ Programming
    Replies: 13
    Last Post: 06-19-2005, 03:22 PM
  2. Why doesnt this work
    By Jotun in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2004, 04:55 PM
  3. How come this doesnt work?
    By correlcj in forum C Programming
    Replies: 2
    Last Post: 07-11-2002, 06:36 PM
  4. why doesnt this work?
    By brad123 in forum C Programming
    Replies: 3
    Last Post: 04-23-2002, 05:26 PM
  5. Why doesnt this work?
    By dougaerb in forum C++ Programming
    Replies: 7
    Last Post: 09-20-2001, 08:51 AM