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

How to make an output file of n*n matrix,by taking n from the user, with the numbers generated in pairs?

本文关键字:数字 那里 用户 获取 何制作 输出 文件      更新时间:2023-10-16

目前我在文本文件中有一个预制的 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 键入索引(