如何通过从用户处获取n并成对生成数字来制作n * n矩阵的输出文件?

时间:2019-02-09 14:48:23

标签: c++ vector random 2d-vector

目前,我在这样的文本文件中有一个预制的6X6矩阵:

2 6 3 1 0 4
4 2 7 7 2 8
4 7 3 2 5 1
7 6 5 1 1 0
8 4 6 0 0 6
1 3 1 8 3 8

,我编写了一个代码,该代码正在读取我制作的文件。但是我想让用户自己制作一个网格(即3X3或10X10)。然后,它会以类似的方式自动写入文本文件,然后进行读入。这是一个基本的内存比赛纸牌游戏,因此我需要有rand()来生成相等的对,以便在网格中的每个对都找到后游戏就可以结束。非常感谢您的宝贵时间!

/*Here are the snippets of my code*/

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <fstream>
#include <string>
#include <numeric>
#include <limits>

using namespace std;  

//global 2d vectors that are associated with the game
vector<vector<int> > game_grid; 
vector<vector<int> > hidden_grid;
vector <vector<int> > guessed;

void initialize_grid() {
ifstream input_file;
input_file.open("grid.txt");
int num;

if (input_file) {
    for (int i = 0; i < 6; ++i) {
        vector<int> row; // game grid
        vector<int> row2; // hidden grid
        vector<int> row3; // guessed grid
        for (int j = 0; j < 6; ++j) {
            if (input_file >> num)
                row.push_back(num);
            row2.push_back(-1);
            row3.push_back(0);
        }
        game_grid.push_back(row);
        hidden_grid.push_back(row2);
        guessed.push_back(row3);
    }
    cout << "Get is ready, Challenger!" << endl << endl;
}
else {
    cout << "Womp. File open failed!";
}
return;
}

void print_grid() {
cout << "Game  grid" << endl;
cout << " -------------------------" << endl;
for (int i = 0; i < 6; ++i) {
    cout << " | ";
    for (int j = 0; j < 6; ++j) {
        cout << game_grid[i][j] << " | ";
    }
    cout << endl << " -------------------------" << endl;
}
cout << endl;
}

void print_hidden_grid(int r1 = -1, int r2 = -1, int c1 = -1, int c2 = -1) {
cout << "Attempt:" << endl;
if (r1 != -1) {
    hidden_grid[r1][c1] = game_grid[r1][c1];
}
if (r2 != -1) {
    hidden_grid[r2][c2] = game_grid[r2][c2];
}
for (int i = 0; i < 6; ++i) {
    cout << " | ";
    for (int j = 0; j < 6; ++j) {
        if (hidden_grid[i][j] > -1)
            cout << hidden_grid[i][j] << " | ";
        else
            cout << "  | ";
    }
    cout << endl << " -------------------------" << endl;
}
cout << endl;

if (r1 != -1) {
    if (game_grid[r1][c1] == game_grid[r2][c2]) {
        guessed[r1][c1] = 1;
        guessed[r2][c2] = 1;
        cout << "You have a match!" << endl << endl;
    }
    else {
        hidden_grid[r1][c1] = -1;
        hidden_grid[r2][c2] = -1;
    }
}
cout << endl << endl;
}

void print_current_grid() {
cout << "Current Grid:" << endl;
cout << " -------------------------" << endl;
for (int i = 0; i < 6; ++i) {
    cout << " | ";
    for (int j = 0; j < 6; ++j) {
        if (hidden_grid[i][j] > -1)
            cout << hidden_grid[i][j] << " | ";
        else
            cout << "  | ";
    }
    cout << endl << " -------------------------" << endl;
}
cout << endl << endl;
}


 .......

1 个答案:

答案 0 :(得分:1)

如果我很了解您想要在读取矩阵时自动检测矩阵的大小?如果是,则可以在initialize_grid中进行类似的操作:

void initialize_grid() {
  ifstream input_file;
  input_file.open("grid.txt");
  int num;

  if (input_file) {
    // detect size
    int size = 0;
    string line;

    if (!getline(input_file, line))
      return;

    istringstream iss(line);

    while (iss >> num)
      size += 1;

    input_file.clear();
    input_file.seekg(0);

    for (int i = 0; i < size; ++i) {
      vector<int> row; // game grid
      vector<int> row2; // hidden grid
      vector<int> row3; // guessed grid
      for (int j = 0; j < size; ++j) {
        if (input_file >> num)
          row.push_back(num);
        row2.push_back(-1);
        row3.push_back(0);
      }
      game_grid.push_back(row);
      hidden_grid.push_back(row2);
      guessed.push_back(row3);
    }
    cout << "Get is ready, Challenger!" << endl << endl;
  }
  else {
    cout << "Womp. File open failed!";
  }
}

,以及用6替换game_grid.size()的其他地方(使用size_t而不是 int 来键入索引)