如何使N * N矩阵的输出文件,通过采取n的用户,在对所产生的数字?

问题描述 投票:0回答:1

目前,我有这样一个文本文件中预制的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或)。然后写入到一个文本文件中自动像类似的方式,然后有一个读入来代替。这是一个基本的存储器卡匹配的游戏,所以我需要有兰特(),其生成对相等所以游戏可以是当在网格中的每个对已发现过。非常感谢您的参与!

/*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;
}


 .......
c++ vector random 2d-vector
1个回答
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键入索引)

© www.soinside.com 2019 - 2024. All rights reserved.