c++ - 如何制作一个 n*n 矩阵的输出文件,通过从用户那里获取 n,成对生成数字?

标签 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;
}


 .......

最佳答案

如果我很清楚你想在阅读时自动检测矩阵的大小?如果是,你可以在 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输入索引)

关于c++ - 如何制作一个 n*n 矩阵的输出文件,通过从用户那里获取 n,成对生成数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54607339/

相关文章:

c++ - 从 vector 中删除多个元素 C++

swift - 在屏幕上以随机位置永远移动 Sprite

c++ - 轻量级 boost::bind

c++ - 动态创建的 Activex 控件的事件处理

c++ - std::tmpfile() 没有选择 TMPDIR 位置

c++ - 迭代器和二维 vector

C++简单编写与编译

r - 如何从满足给定条件的向量中随机删除一个元素?

java - 如何使用随机分布的符号填充数组?

Java随机移动文件