简体   繁体   中英

How to modify vector of a vector from 3x3 to 4x4 using push_back and insert in c++?

A beginner to C++ programming here!

I'm working on a project of coding a two-player tic-tac-toe game. My grid is a 2D vector of a vector size 3x3 filled with dots in the beginning. The program asks x- and y-coordinates from the user and replaces the dot with the X or 0.

Now, my task is to increase the grid size in two-ways, for example when given coordinates x or y is greater by one than the size of the game board the board will expand to the right and downwards or if given coordinates for example x=2 and y=0, the board should expand left and upwards.

Here is how I tried to approach the situation, I made two functions:

Here you have my vector of a vector:

int grid_size = 3;
vector< vector<char>>grid(grid_size, vector<char>(grid_size, '.'));

First one tries to deal with the down and right expansion:

void expand_grid_down_right(vector<vector<char>>&grid){
//Let's make some helpers
vector<char> grid_row;
vector<char> grid_col;
//Adds new row but not column
for(int i = 0; i < grid.size();i++){
            grid_row.push_back('+');

    }
    vector<char> new_line(grid_row.size(), '+');
    grid.push_back(new_line);



   for(unsigned int i = 0; i < grid.size(); i++){
     cout << (i+1)%10 << ' ';

   }
   cout << endl;
   //Draws the grid with three rows of dots and one row of plus-signs
   //Column numbered with four appears blank?
   //If I try to touch this with at(i).at(j) it gives out of bounds error
   for (auto &grid_row : grid) {

          for (auto &cell : grid_row) {

              cout << cell << ' ';
          }

          cout << endl;
      }
   //But this prints it as one column?
      for(int i=0; i<grid.size();i++){
          for(int j=0; j<grid[i].size(); j++){
              cout << grid[i][j] << endl;
          }
      }
      //I want to at to the new column the char = '.' but not sure what this does?
          for(int i=0; i<grid.size();i++){
              for(int j=0; j<grid[i].size(); j++){
                   grid.at(i).at(j) = '.';
              }
          }
}

Second one tries to deal with the up and left expansion:

void expand_grid_up_left(vector<vector<char>>&grid){
unsigned int dimension = grid.size();
vector<char> grid_col;
//Add new row of plus-signs to the top row but does not shift
//So from 3x3 to 4x3 but how to get it 4x4?
for (int i=0; i<dimension; i++){
    grid_col.insert(grid_col.begin(),'+');
}
vector<char> line(grid_col.size(), '+');
grid.insert(grid.begin(), line);
for (auto &grid_col : grid) {

      for (auto &cell : grid_col) {

          cout << cell << ' ';
      }

      cout << endl;
  }
}

But neither of them work in the way they are supposed to. I am using the plus-signs to see where the new lines are added. The down and right-expansion produces for rows and it draws an empty column 4. If I try to access this with index by index loop it just draws a single column with all of the dots and plus signs. The one that is supposed to expand left and up only puts a row up but doesn't go to left.

Does the empty column 4 exist and how do I make them work so that the expansion becomes from 3x3 to 4x4 or in broader sense NxN?

The issue with the code you've got is that you create grid_row and grid_col but don't actually use them. What if you tried something like this for the start of expand_grid_down_right :

void expand_grid_down_right(vector<vector<char>> &grid) {
  // first add the new row
  std::vector<char> new_row{grid.size(), '+'};
  grid.push_back(std::move(new_row));

  // then add the new column
  for (vector<char>& row : grid) {
    row.push_back('+');
  }

  ...

In order to print correctly you need to make sure your second loop is the same as the first loop:

  for (auto &grid_row : grid) {
    for (auto &cell : grid_row) {
      cout << cell << ' '; // no endl here
    }
    cout << endl;          // endl here
  }

Which would end up looking like this:

  // But this prints it as one column?
  for (int i = 0; i < grid.size(); i++) {
    for (int j = 0; j < grid[i].size(); j++) {
      cout << grid[i][j] << ' ';
    }
    cout << endl;
  }

Adding to the top left should be very similar, but (similar to what you've got) you'd want to insert at the start rather than push_back for all the vector operations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM