Home > database >  C - Is there an better way to do this rather than doing all of it manually?
C - Is there an better way to do this rather than doing all of it manually?

Time:03-30

So I am trying to make an text based game in C and I'm not sure if there is an way to automate doing this:

int karte_plans() {
    printf("\n ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬   ▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬ ");
    printf("\n┃----------------------------------------------------------------------------┃ ┃              ┃                            ┃               ┃");  
    printf("\n┃********************************** Karte ***********************************┃ ┃   Pilsēta    ┃      Apskates objekti      ┃   EU Stacija  ┃");  
    printf("\n┃----------------------------------------------------------------------------┃ ┃              ┃                            ┃               ┃");   
    printf("\n┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃ ┃              ┃                            ┃               ┃");
    printf("\n┃           *  /             _/     ^___/   /                          `^    ┃ ┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃");
    printf("\n┃             |           __/              |                             |   ┃ ┃              ┃              ┃             ┃               ┃");  
    printf("\n┃              ^___  ____/          .*    |                              |   ┃ ┃     Rīga     ┃       1      ┃      2      ┃       A       ┃");  
    printf("\n┃                _||                      /     ___________            _/    ┃ ┃              ┃              ┃             ┃               ┃");
    printf("\n┃               / /                       |___/             ^        /       ┃ ┃              ┃              ┃             ┃               ┃");  
    printf("\n┃              /_/                       /                  ^   ___/         ┃ ┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃");  
    printf("\n┃                                       |                    ^_/   |         ┃ ┃              ┃              ┃             ┃               ┃");
    printf("\n┃                    .        *          |                          |_       ┃ ┃    Tukums    ┃       3      ┃      4      ┃       B       ┃");  
    printf("\n┃                __/  |                  |                            `      ┃ ┃              ┃              ┃             ┃               ┃");  
    printf("\n┃             __/      `_                 |                           |      ┃ ┃              ┃              ┃             ┃               ┃");
    printf("\n┃ Ventspils /X            `               |                            |     ┃ ┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃");  
    printf("\n┃          /  |` _   Talsi `_             |                             |    ┃ ┃              ┃              ┃             ┃               ┃");                                                            
    printf("\n┃         |    |  ` X__       `          /                               `_  ┃ ┃    Talsi     ┃       5      ┃      6      ┃       C       ┃");  
    printf("\n┃         |     |      `       `        /   __X Sigulda                    | ┃ ┃              ┃              ┃             ┃               ┃");  
    printf("\n┃       /Kuldīga X      X Tukums` _____/  _/                               | ┃ ┃              ┃              ┃             ┃               ┃");
    printf("\n┃     /           |     `_       _X______/                                  |┃ ┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃");  
    printf("\n┃    |             `     `_     /  Rīga                                    | ┃ ┃              ┃              ┃             ┃               ┃");  
    printf("\n┃   | Liepāja   ____`X______`X_/                                           | ┃ ┃   Ventspils  ┃       7      ┃      8      ┃       D       ┃");
    printf("\n┃   |X_________/  Saldus       Jelgava       ^                              |┃ ┃              ┃              ┃             ┃               ┃");  
    printf("\n┃   |                                      /  ^                            | ┃ ┃              ┃              ┃             ┃               ┃");
    printf("\n┃   |          _________ ___________  ____/     ^___                      |__┃ ┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃");  
    printf("\n┃   |      _/ `         `           ``              `__                 _/  ^┃ ┃              ┃              ┃             ┃               ┃");
    printf("\n┃    |  __/                                            `__             /     ┃ ┃   Liepāja    ┃       9      ┃      10     ┃       E       ┃");  
    printf("\n┃    | /                                                   `___   ___/       ┃ ┃              ┃              ┃             ┃               ┃");  
    printf("\n┃    |'                                                        `|`           ┃ ┃              ┃              ┃             ┃               ┃");
    printf("\n┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃ ┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬┃▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬┃");
    char izv[1][1];

    printf("\nIzvēlies nākamo staciju: ");  
    scanf("%s",izv);  
  
    switchs(izv)  {  
        icases("A")  
            main();  
        break; 
  
        icases("B")       
            palidzība();  
        break;  

        icases("C")  
            main();  
        break; 
  
        icases("D")       
            palidzība();  
        break; 

        icases("E")  
            main();  
        break; 
  
        icases("1")       
            palidzība();  
        break;  

        icases("2")  
            main();  
        break; 
  
        icases("3")       
            palidzība();  
        break;  

        icases("4")  
            main();  
        break; 
  
        icases("5")       
            palidzība();  
        break;  

        icases("6")  
            main();  
        break; 
  
        icases("7")       
            palidzība();  
        break;  

        icases("8")  
            main();  
        break; 
  
        icases("9")       
            palidzība();  
        break;  

        icases("10")       
            palidzība();  
        break;
  
        defaults  
            printf("Kļūdaina ievade, ieraksti jebkuru burtu vai skaitli, kas rādīts tabulā.");
        karte_plans();
        break;  
    } switchs_end;
}

What I am trying to do is allow the player to move only to control points that are 1 square around him so if the player is in the "6" square then he would be able to move to the "3", "4", "B","C", "D", "8", "7" and "5" square like the king in chess and blank out the remaining ones with an asterisk.With an switch statement (The one here is just an slightly modified one for strings) It can be done manually but that would take a long time for all the options and wouldn't be very efficient.

Thank you for your help!

CodePudding user response:

Set up the board as a 2D array. 1 is [0][0], 2 is [1][0], A is [2][0], 3 is [0][1], 6 is [1][2].

Each element would be a struct with information about that square. What it's named. What's in it. Etc.

Find legal moves by adding and subtracting 1 from both x and y and making sure they're within the bounds of the board. You can do this with a for loop.

for(int dx = -1; dx <= 1; dx  ) {
  int x = player_x   dx;
  if(x < 0 || x > max_x) {
    // Out of bounds
    continue;
  }
  for(int dy = -1; dy <= 1; dy  ) {
    int y = player_y   dy;
    if(y < 0 || y > max_y) {
      // Out of bounds
      continue;
    }

    // board[x][y] is legal
  }
}

CodePudding user response:

You could create an array like int array[width][height][symbol]. Then i would design my art in a .txt file and let a method translate it into the array. An other method would than print the whole thing.

  • Related