我开发了一个简单的填字游戏生成器,其中重要的一点是我使用了一个数组作为网格。我现在正在做的是设置它,以便用户可以选择他们想要使用的网格大小。使用数组我不能这样做,所以我想更改我的代码以使用向量。我对此有困难并尝试了一些不同的方法,但无法使其正常工作:
// std::vector<std::vector<char> > wordsearch;
// std::vector<std::vector<char *> > wordsearch;
char wordsearch [10][11] = {0};
第三行是与代码一起正常工作的数组。第一行使程序崩溃,第二行运行正常,但抱怨“当我将字符附加到向量时,从 char 到 char* 的转换无效。
我也试过
// std::vector<std::vector<char> > wordsearch_vector(wordsearch, wordsearch + sizeof(wordsearch) / sizeof(char));
但它也抱怨 char 到 char* 的转换。在我的其他尝试中,我尝试编写将数组转换为向量的函数,但数组必须在其维度中以数字方式定义,没有变量或未指定供用户稍后定义,例如:std::vector > array_convertto_vector(array a); (数组未定义)
有什么建议么?这是我的代码:
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <iterator>
using namespace std;
int center_text(string string_word/*add second parameter if setw will vary*/) {
int spaces = 10 - string_word.size();
return spaces / 2;
}
int main() {
srand(time(NULL));
const char* const a_to_z = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
int random_char;
// std::vector<std::vector<char> > wordsearch;
// std::vector<std::vector<char *> > wordsearch;
char wordsearch [10][11] = {0};
int random_choice;
int f_or_b;
int random_word_number;
int temp_i;
int temp_j;
string random_word;
bool flag;
string words_array[] = {"CAT", "HELLO", "GOODBYE", "DOG", "BAT", "NEW", "SAY", "MAY", "DAY", "HAY", "CELLO", "ORANGES", "LINK", "ROBIN"};
vector<string> words_vector (words_array, words_array + sizeof(words_array) / sizeof(string));
string words_found_array[] = {};
vector<string> words_found_vector (words_found_array, words_found_array + sizeof(words_found_array) / sizeof(string));
//vector<string> words_vector;
//vector<string> words_found_vector;
// ifstream myfile("Words.txt");
// copy(istream_iterator<string>(myfile), istream_iterator<string>(),
// back_inserter(words_vector)); //MAKE SURE TO LOAD INTO VECTOR ONLY ONCE, NOT EACH TIME PROGRAM LOADS!!!
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 11; j++) {
random_choice = rand() % 5;
f_or_b = rand() % 2;
random_word_number = -1;
if (words_vector.size() != 0) {
random_word_number = rand() % words_vector.size();
random_word = words_vector[random_word_number];
}
if (j == 10) {
wordsearch[i][j] = '\n';
}
else if (wordsearch[i][j] != '\0') { // prevents overwriting horizontal words, or add to j in else if loop instead of temp_j
continue;
}
else if (random_choice == 1 && random_word.size() < 11-j && words_vector.size() != 0) { //or <= 10-j
flag = false;
for (int x = 0; x < random_word.size(); x++) {
if (wordsearch[i][j+x] == random_word[x] || wordsearch[i][j+x] == '\0') {
flag = true;
}
else {
flag = false;
break;
}
}
temp_j = j;
if (flag == true) {
if (f_or_b == 1) { //reverse string
reverse(random_word.begin(), random_word.end());
}
for (int x = 0; x < random_word.size(); x++) {
wordsearch[i][temp_j] = random_word[x];
temp_j += 1;
}
if (f_or_b == 1) { //reverse back
reverse(random_word.begin(), random_word.end());
}
words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
words_vector.erase(words_vector.begin()+random_word_number);
}
else {
int random_char = rand() % 26 + 0;
wordsearch[i][j] = a_to_z[random_char];
}
}
else if (random_choice == 2 && random_word.size() <= 10-i && words_vector.size() != 0) {
flag = false;
for (int x = 0; x < random_word.size(); x++) {
if (wordsearch[i+x][j] != '\0') {
flag = false;
}
}
for (int x = 0; x < random_word.size(); x++) {
if (wordsearch[i+x][j] == random_word[x] || wordsearch[i+x][j] == '\0') {
flag = true;
}
else {
flag = false;
break;
}
}
temp_i = i;
if (flag == true) {
if (f_or_b == 1) {
reverse(random_word.begin(), random_word.end());
}
for (int x = 0; x < random_word.size(); x++) {
wordsearch[temp_i][j] = random_word[x];
temp_i += 1;
}
if (f_or_b == 1) {
reverse(random_word.begin(), random_word.end());
}
words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
words_vector.erase(words_vector.begin()+random_word_number);
}
else {
int random_char = rand() % 26 + 0;
wordsearch[i][j] = a_to_z[random_char];
}
}
else if (random_choice == 3 && random_word.size() <= 10-i && random_word.size() < 11-j && words_vector.size() != 0) { //or <= 10-j
flag = false;
for (int x = 0; x < random_word.size(); x++) {
if (wordsearch[i+x][j+x] == random_word[x] || wordsearch[i+x][j+x] == '\0') {
flag = true;
}
else {
flag = false;
break;
}
}
temp_i = i;
temp_j = j;
if (flag == true) {
if (f_or_b == 1) {
reverse(random_word.begin(), random_word.end());
}
for (int x = 0; x < random_word.size(); x++) {
wordsearch[temp_i][temp_j] = random_word[x];
temp_i += 1;
temp_j += 1;
}
if (f_or_b == 1) {
reverse(random_word.begin(), random_word.end());
}
words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
words_vector.erase(words_vector.begin()+random_word_number);
}
else {
int random_char = rand() % 26 + 0;
wordsearch[i][j] = a_to_z[random_char];
}
}
else if (random_choice == 4 && random_word.size() <= 10-i && random_word.size() > 11-j && words_vector.size() != 0) { //or >= 10-j
flag = false;
for (int x = 0; x < random_word.size(); x++) {
if (wordsearch[i+x][j-x] == random_word[x] || wordsearch[i+x][j-x] == '\0') {
flag = true;
}
else {
flag = false;
break;
}
}
temp_i = i;
temp_j = j;
if (flag == true) {
for (int x = 0; x < random_word.size(); x++) {
wordsearch[temp_i][temp_j] = random_word[x];
temp_i += 1;
temp_j -= 1;
}
words_found_vector.insert(words_found_vector.begin(),words_vector[random_word_number]);
words_vector.erase(words_vector.begin()+random_word_number);
}
else {
int random_char = rand() % 26 + 0;
wordsearch[i][j] = a_to_z[random_char];
}
}
else {
int random_char = rand() % 26 + 0;
wordsearch[i][j] = a_to_z[random_char];
}
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 11; j++) {
cout<<wordsearch[i][j];
}
}
// std::vector<std::vector<char> > wordsearch_vector;
// std::vector<std::vector<char> > wordsearch_vector(wordsearch, wordsearch + sizeof(wordsearch) / sizeof(char));
random_shuffle(words_found_vector.begin(), words_found_vector.end());
cout<<endl<<"Your words are:"<<endl;
int counter = 0;
int space_value;
for (int x = 0; x < words_found_vector.size(); x++) {
space_value = center_text(words_found_vector[x]);
if (counter == 2) {
for (int y = 0; y < space_value; y++) {
cout<<" ";
}
cout<<words_found_vector[x]<<endl;
counter = 0;
}
else {
for (int y = 0; y < space_value; y++) {
cout<<" ";
}
cout<<words_found_vector[x];
counter += 1;
for (int y = 0; y < space_value; y++) {
cout<<" ";
}
}
}
}
如果我没有使代码足够清晰,您必须取消对向量部分的注释并在我挑出的三行中注释掉数组部分以使其工作。
我在文件中加载了一个包含单词的部分,因此我不得不取消注释该部分。如果它仍然因为它而无法编译,请告诉我,我将重新发布希望工作的代码版本。并感谢您的帮助!