2D Arrays Application

This is a discussion on 2D Arrays Application within the C Programming forums, part of the General Programming Boards category; Hello, I am making a program that lets you "walk" you character by entering the coordinates. The map's border would ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    2D Arrays Application

    Hello, I am making a program that lets you "walk" you character by entering the coordinates. The map's border would be the number 8 and the path would be the number 0 and my character would be the number 1. Note that the character cannot step on the border. I think I'm more than halfway on this program. Am I going in the right direction? I mean, am I missing something? I just need to do the "move up, down, left, right codes". Here's the code.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void setMap(int num_row, int num_col);
    void printMap(int map[100][100], int num_row, int num_col);
    void playGame(int map[100][100], int num_row, int num_col);
    
    main()
    {
        int map[100][100];
        char name[50];
        int num_row, num_col, choice;
    
        printf("Enter name: ");
        scanf("%s", name);
        printf("Hello %s!\n", name);
    
        do
        {
            printf("[1] Game Start\n[2] Logout\n");
            printf("Enter choice: ");
            scanf("%d", &choice);
        
            switch(choice)
            {
                case 1: printf("Enter number of rows: ");
                           scanf("%d", &num_row);
                           printf("Enter number of columns: ");
                           scanf("%d", &num_col);
                           setMap(num_row, num_col);
                           break;
                
                case 2: printf("Bye!\n");
                           getchar();
                           getchar();
                           break;
        
                default: printf("Error! Choice invalid!\n");
            }
        } while(choice!=2);
    }
    
    void setMap(int num_row, int num_col)
    {
        int i, j;
        int map[100][100];
    
        for(i=0; i<1; i++)
        {
            for(j=0; j<num_col; j++)
            {
                map[i][j]=8;
            }
        }
    
        for(j=0; j<1; j++)
        {
            for(i=0; i<num_row; i++)
            {
                map[i][j]=8;
            }
        }
    
            for(i=num_row-1; i<num_row; i++)
        {
            for(j=0; j<num_col; j++)
            {
                map[i][j]=8;
            }
        }
    
        for(j=num_col-1; j<num_col; j++)
        {
            for(i=0; i<num_row; i++)
            {
                map[i][j]=8;
            }
        }
    
        for(i=0; i<num_row; i++)
        {
            for(j=0; j<num_col; j++)
            {
                if(map[i][j]!=8)
                {
                    map[i][j]=0;
                }
            }
        }
        
        printMap(map, num_row, num_col);
        
    }
    
    void printMap(int map[100][100], int num_row, int num_col)
    {
        int i, j;
    
        for (i=0; i<num_row; i++)
        
        {
            for(j=0; j<num_col; j++)
            {
                printf("%d ", map[i][j]);
            }
            putchar('\n');
        }
        playGame(map, num_row, num_col);
    }
    
    void playGame(int map[100][100], int num_row, int num_col)
    {
        int i, j, a, b, choice2;
        char direction[10];
        
        do
        {
            printf("Plant the character (row col): ");
            scanf("%d %d", &a, &b);
            if(i==0 || i==num_row-1 || j==0 || j==num_col-1)
            {
                printf("You cannot plant your character there.\n");
            }
            else
            {
                for (i=0; i<num_row; i++)
                {
                        for(j=0; j<num_col; j++)
                       {
                        if(i==a && j==b)
                        {
                            map[i][j]=1;
                        }
    
                        printf("%d ", map[i][j]);
                    }
                    putchar('\n');
                }
            }
        } while(i==0 || i==num_row-1 || j==0 || j==num_col-1);
        
        printf("[1] Move Player\n[2] Sign Out\n");
        printf("Enter choice: ");
        scanf("%d", &choice2);
        
        switch(choice2)
        {
            case 1: printf("Where do you want to go (up, down, left, right)? ");
                    scanf("%s", direction);
                    if(strcmp(direction, "up")==0)
                    {
                        printf("down\n");
                        /*codes for moving the character up.....*/
                    }
                    else if(strcmp(direction, "down")==0)
                    {
                        printf("down\n");
                        /*codes for moving the character down.....*/
                    }
                    else if(strcmp(direction, "left")==0)
                    {
                        printf("left\n");
                        /*codes for moving the character left.....*/
                    }
                    else if(strcmp(direction, "right")==0)
                    {
                        printf("down\n");
                        /*codes for moving the character up.....*/
                    }
    
            case 2: printf("Bye!\n");
        }
        
        getchar();
        getchar();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What are you having problems with exactly?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    This code works well but I made it as a chain reaction functions by calling the next function inside the setMap function and so on(function in a function in a function--FUNCTIONCEPTION!). I want the functions to be out of each other.
    When I modify this part:
    Code:
    switch(choice)        {
                case 1: printf("Enter number of rows: ");
                           scanf("%d", &num_row);
                           printf("Enter number of columns: ");
                           scanf("%d", &num_col);
                           setMap(num_row, num_col);
                           break;
    to this:
    Code:
    switch(choice)        {
                case 1: printf("Enter number of rows: ");
                           scanf("%d", &num_row);
                           printf("Enter number of columns: ");
                           scanf("%d", &num_col);
    
                           setMap(num_row, num_col);
                           printMap(map, num_row, num_col);
                           playGame(map, num_row, num_col);
                           
    
    
                           break;
    assuming the user inputs rows and columns, the output shows:
    0 0 0 0 0
    0 1 0 0 0
    0 0 0 0 0
    0 0 0 0 0

    The correct output should be(also the output of the 'functionception'):
    8 8 8 8 8
    8 1 0 0 8
    8 0 0 0 8
    8 8 8 8 8

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    you need to pass 'map' to setMap. setMap is initializing a local copy of map, not the one from your main

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    You, sir, are a genius. Thank you. Now, I just need to do the 'move' codes. I'll give you an update later.

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    Code:
    
    #include <stdio.h>
    #include <string.h>
    
    
    void setMap(int map[100][100], int num_row, int num_col); /*prototype*/
    void printMap(int map[100][100], int num_row, int num_col);
    void playGame(int map[100][100], int num_row, int num_col);
    
    
    main()
    {
        int map[100][100]; /*variable declaraion*/
        char name[50];
        int num_row, num_col, choice;
    
    
        printf("Enter name: "); /*input name*/
        scanf("%s", name);
        printf("Hello %s!\n", name);
    
    
        do
        {
            printf("[1] Game Start\n[2] Logout\n"); /*menu*/
            printf("Enter choice: ");
            scanf("%d", &choice);
    
    
            switch(choice)
            {
                case 1: do /*loop until num_row>=5*/
                        {
                            printf("Enter number of rows(greater than or equal to 5)): ");
                                scanf("%d", &num_row);
                                if(num_row<5)
                                {
                                printf("Must be greater than or equal to 5\n");
                                }
                        } while(num_row<5);
                        do /*loop until num_col>=5*/
                        {
                            printf("Enter number of columns(greater than or equal to 5)): ");
                            scanf("%d", &num_col);
                            if(num_col<5)
                            {
                                printf("Must be greater than of equal to 5\n");
                            }
                        } while(num_col<5);
                        setMap(map, num_row, num_col); /*credits to user 'dmh2000' of Cboard for pointing the error here*/
                        printMap(map, num_row, num_col);
                        playGame(map, num_row, num_col);
                        break;
    
    
                case 2: printf("Bye!\n");
                        break;
    
    
                default: printf("Error! Choice invalid!\n");
            }
        } while(choice!=2);
    }
    
    
    void setMap(int map[100][100], int num_row, int num_col)
    {
        int i, j;
        for(i=0; i<1; i++) /*border on first row*/
        {
            for(j=0; j<num_col; j++)
            {
                map[i][j]=8;
            }
        }
    
    
        for(j=0; j<1; j++) /*border on first column*/
        {
            for(i=0; i<num_row; i++)
            {
                map[i][j]=8;
            }
        }
    
    
        for(i=num_row-1; i<num_row; i++) /*border on last row*/
        {
            for(j=0; j<num_col; j++)
            {
                map[i][j]=8;
            }
        }
    
    
        for(j=num_col-1; j<num_col; j++) /*border on last column*/
        {
            for(i=0; i<num_row; i++)
            {
                map[i][j]=8;
            }
        }
    
    
        for(i=0; i<num_row; i++) /*set 0 to walkable path*/
        {
            for(j=0; j<num_col; j++)
            {
                if(map[i][j]!=8)
                {
                    map[i][j]=0;
                }
            }
        }
    }
    
    
    void printMap(int map[100][100], int num_row, int num_col)
    {
        int i, j;
    
    
        for (i=0; i<num_row; i++) /*print map*/
        {
            for(j=0; j<num_col; j++)
            {
                printf("%d ", map[i][j]);
            }
            putchar('\n');
        }
    }
    
    
    void playGame(int map[100][100], int num_row, int num_col)
    {
        int i, j, a, b, choice2; /*variable declaration*/
        char direction[10];
    
    
        do /*loop if coordinates invalid*/
        {
            printf("Plant the character (row col): "); /*starting point*/
            scanf("%d %d", &a, &b);
            if(a==0 || a==num_row-1 || a<0 || a>num_row-1 || b==0 || b<0 || b>num_col-1 || b==num_col-1) /*borders and coordinates not in map*/
            {
                printf("You cannot plant your character there.\n");
            }
            else
            {
                for (i=0; i<num_row; i++)
                {
                        for(j=0; j<num_col; j++)
                       {
                        if(i==a && j==b) /*plant character in (a,b)*/
                        {
                            map[i][j]=1;
                        }
    
    
                        printf("%d ", map[i][j]);
                    }
                    putchar('\n');
                }
            }
        } while(a==0 || a==num_row-1 || a<0 || a>num_row-1 || b==0 || b<0 || b>num_col-1 || b==num_col-1);
    
    
        do
        {
            printf("[1] Move Player\n[2] Sign Out\n"); /*menu*/
            printf("Enter choice: ");
            scanf("%d", &choice2);
    
    
            switch(choice2)
            {
                case 1: printf("Where do you want to go (up, down, left, right)? ");
                            scanf("%s", direction);
                            if(strcmp(direction, "up")==0) /*check for input*/
                        {
                            if(a==1) /*border*/
                            {
                                printf("Obstacle detected. Can't move up.\n");
                            }
                            else
                            {
                                   for (i=0; i<num_row; i++)
                                {
                                        for(j=0; j<num_col; j++)
                                       {
                                        if(i==a-1 && j==b) /*set 1 to new coordinate*/
                                        {
                                            map[i][j]=1;
                                        }
                                        map[a][b]=0; /*reset (a,b) to 0*/
                                        printf("%d ", map[i][j]);
                                    }
                                    putchar('\n');
                                }
                                a--;  /*set a to the new value of a for next move*/
                            }
                        }
                        else if(strcmp(direction, "down")==0) /*check for input*/
                        {
                            if(a==num_row-2) /*border*/
                            {
                                printf("Obstacle detected. Can't move down.\n");
                            }
                            else
                            {
                                   for (i=0; i<num_row; i++)
                                {
                                        for(j=0; j<num_col; j++)
                                       {
                                        if(i==a+1 && j==b) /*set 1 to new coordinate*/
                                        {
                                            map[i][j]=1;
                                        }
                                        map[a][b]=0; /*reset (a,b) to 0*/
                                        printf("%d ", map[i][j]);
                                    }
                                    putchar('\n');
                                }
                                a++; /*set a to the new value of a for next move*/
                            }
                        }
                        else if(strcmp(direction, "left")==0) /*check for input*/
                        {
                            if(b==1) /*border*/
                            {
                                printf("Obstacle detected. Can't move left.\n");
                            }
                            else
                            {
                                   for (i=0; i<num_row; i++)
                                {
                                        for(j=0; j<num_col; j++)
                                       {
                                        if(i==a && j==b-1) /*set 1 to new coordinate*/
                                        {
                                            map[i][j]=1;
                                        }
                                        map[a][b]=0; /*reset (a,b) to 0*/
                                        printf("%d ", map[i][j]);
                                    }
                                    putchar('\n');
                                }
                                b--;  /*set b to the new value of a for next move*/
                            }
                        }
                        else if(strcmp(direction, "right")==0) /*check for input*/
                        {
                            if(b==num_col-2)
                            {
                                printf("Obstacle detected. Can't move right.\n");
                            }
                            else
                            {
                                   for (i=0; i<num_row; i++)
                                {
                                        for(j=0; j<num_col; j++)
                                       {
                                        if(i==a && j==b+1) /*set 1 to new coordinate*/
                                        {
                                            map[i][j]=1;
                                        }
                                        map[a][b]=0; /*reset (a,b)*/
                                        printf("%d ", map[i][j]);
                                    }
                                    putchar('\n');
                                }
                                b++;  /*set b to the new value of b for next move*/
                            }
                        }
                        break;
    
    
                case 2: printf("Bye!\n");
                        break;
                default: printf("Error! Invalid choice!\n");
            }
        } while(choice2!=2);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and their Application to image processing
    By quintenmater in forum C Programming
    Replies: 7
    Last Post: 12-10-2010, 02:10 PM
  2. Replies: 1
    Last Post: 07-03-2010, 02:18 PM
  3. Application A interacting with application B
    By Dante Wingates in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2010, 09:01 AM
  4. Convirt my C program to a GUI application/Web Application
    By kapil1089thekin in forum C Programming
    Replies: 6
    Last Post: 07-21-2008, 02:43 AM
  5. Windows form application and Windows application
    By |Wiz| in forum Windows Programming
    Replies: 5
    Last Post: 10-01-2005, 05:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21