Advertisement
Guest User

2D Grid

a guest
Jul 8th, 2010
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. /*
  2. * Use A, S, D, and W to move the icon
  3. * across the grid.
  4. */
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <conio.h>
  8. #define width 10
  9. void printmap(char grid[][width],int h,int w);
  10. void fill(char grid[][width],int h,int w,char c);
  11.  
  12. char map[10][width];
  13.  
  14. int main() {
  15.    fill(map,10,10,'.');
  16.    map[1][1]='0';
  17.    printmap(map,10,10);
  18.    int x=1,y=1;
  19.    while (true) {
  20.       map[x][y]='.';
  21.       int i;
  22.       while (i==0) {
  23.          i=kbhit();
  24.       }
  25.       char c=getch();
  26.       switch (c) {
  27.          case 97: //ascii a
  28.             if (y==1) {y=10;} else {y--;}
  29.             break;
  30.          case 115: //ascii s
  31.             if (x==10) {x=1;} else {x++;}
  32.             break;
  33.          case 100: //ascii d
  34.             if (y==10) {y=1;} else {y++;}
  35.             break;
  36.          case 119: //ascii w
  37.             if (x==1) {x=10;} else {x--;}
  38.             break;
  39.       }
  40.       map[x][y]='0';
  41.       system("cls");
  42.       printmap(map,10,10);
  43.    }
  44.    system("pause>nul");
  45.    return 0;
  46. }
  47.  
  48. void printmap(char grid[][width],int h,int w) {
  49.    for (int i=1;i<=h;i++) {
  50.       for (int j=1;j<=w;j++) {
  51.          printf("%c",grid[i][j]);
  52.       }
  53.       printf("\n");
  54.    }
  55. }
  56. void fill(char grid[][width],int h,int w,char c) {
  57.    for (int i=1;i<=h;i++) {
  58.       for (int j=1;j<=w;j++) {
  59.          grid[i][j]=c;
  60.       }
  61.    }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement