samedi 25 juillet 2020

Correcting a grid map display

been trying to solve this for hours but has been futile. Below is a reproducible code

#include <stdio.h>
#include <iomanip>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    int grid_x_length = 6;
    int grid_y_length = 6;
    int y_counter = 6;
    int x_counter = 0;
    
    int xArray[4];
    xArray[0] = 1;
    xArray[1] = 1;
    xArray[2] = 1;
    xArray[3] = 2;
    
    int yArray[4];
    yArray[0] = 1;
    yArray[1] = 2;
    yArray[2] = 3;
    yArray[3] = 1;
    
    for(int row = grid_y_length+2; row >= -1; row--)
    {
        for(int col = 0; col <= grid_x_length+3; col++)
        {
            if((col == 0) && (row == 0) || (col == 0) && (row == grid_y_length+2))
                cout << setw(3) << left << "   ";
            else if ((col == 0) && (row >= 1) && (row < grid_y_length+3))
                cout << setw(3) << left << y_counter--;
            else if ((col == 1) && (row == -1) || (col == grid_x_length+3) && (row == -1))
                cout << setw(3) << left << "   ";
            else if ((col >= 1) && (row == -1))
                cout << setw(3) << left << x_counter++;
            else if ((row == grid_y_length+2) && (col > 0) && (col <= grid_x_length+3)) // top
                cout << setw(3) << left << "#";
            else if ((row == 0) && (col > 0) && (col <= grid_x_length+3)) // bottom
                cout << setw(3) << left << "#";
            else if ((col == 1) && (row > 0) && (row < grid_y_length+2)) // left
                cout << setw(3) << left << "#";
            else if ((col == grid_x_length+3) && (row > 0) && (row < grid_y_length+2)) // right
                cout << setw(3) << left << "#";
            else
                for(int i = 0; i < 4; i++)
                {
                    if (row == yArray[i]+1 && col == xArray[i]+2)
                    {
                        cout << setw(3) << left << "X";
                    }
                }
               
                // attempted attempt to resolve the issue
                //if(row >= 2 && col >= 1)
                //  cout << "   ";

            //  cout << "   "; // use this when you omit the for loop to print perfect map
        }
        cout << endl;    
    }
    return 0;
}

Output using above code

   #  #  #  #  #  #  #  #  #                                                                                                  
6  #  #                                                                                                                       
5  #  #                                                                                                                       
4  #  #                                                                                                                       
3  #  X  #                                                                                                                    
2  #  X  #                                                                                                                    
1  #  X  X  #                                                                                                                 
0  #  #                                                                                                                       
   #  #  #  #  #  #  #  #  #                                                                                                  
   0  1  2  3  4  5  6    

The expected output:

   #  #  #  #  #  #  #  #  #                                                                                                  
6  #                       #      
5  #                       #                                                                                                                       
4  #                       #                                                                                                                       
3  #     X                 #                                                                                                                    
2  #     X                 #                                                                                                                    
1  #     X  X              #                                                                                                                 
0  #                       #                                                                                                                       
   #  #  #  #  #  #  #  #  #                                                                                                  
      0  1  2  3  4  5  6    

The objective is to use the X/Y arrays to pinpoint the coordinates on the map with X. The grid map display perfectly when I omit the for loop in the else block. However once I include it , the map will not display properly because there is no empty spaces. I have attempted to add another if in the else block but this did not produce the expected result (see commented portion in the code).

I have also tried to replace the for loop and putting the conditions in the else if but the X will only print once (the last x,y coordinate from the array).

Does anyone has any suggestion on how to get the expected results?

Aucun commentaire:

Enregistrer un commentaire