我開發了一個簡單的填字遊戲生成器,其中重要的一點是我已經使用了一個數組作爲網格。我現在正在處理的是設置它,以便用戶可以選擇他們想要使用的網格大小。對於數組我不能這樣做,所以我想改變我的代碼來處理矢量。我遇到了困難,並嘗試了幾件不同的事情,但無法正常工作:從陣列移植代碼到矢量
// std::vector<std::vector<char> > wordsearch;
// std::vector<std::vector<char *> > wordsearch;
char wordsearch [10][11] = {0};
第三行是與代碼正常工作的數組。第一行導致程序崩潰和第二線運行良好,但抱怨「從焦炭無效轉換爲char *,我追加字符的載體。
我也試過
// std::vector<std::vector<char> > wordsearch_vector(wordsearch, wordsearch + sizeof(wordsearch)/sizeof(char));
但抱怨炭在我的其他嘗試中,我試圖編寫函數來將數組轉換爲向量,但是數組必須在其維度中進行數字定義,沒有變量,或者未指定給用戶以供後來定義,例如: std :: vector > array_convertto_vector(array a);(array undefined)
任何建議?這裏是我的代碼:
#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<<" ";
}
}
}
}
你必須取消對向量部分和註釋掉三行我單挑做,如果我沒有使代碼很清楚它的工作陣列部分。
Theres是我用文字裝入文件的一部分,所以我不得不取消註釋該部分。如果它仍然沒有編譯,因爲它讓我知道,我會重新發佈一個希望工作的代碼版本。並感謝您的幫助!
恕我直言,你太連接到初始化向量單詞搜索C思維。當從低級語言轉換爲面向對象時,您需要改變您的範例。換句話說,你爲什麼不堅持列出? –
lpapp
這讓我笑了起來,因爲我從來沒有做過C,事實上我對C++來說是比較新的。你也是什麼意思列表?我更喜歡矢量,因爲如果用戶想要,我可以動態改變其大小,並且它與我熟悉的數組類似。我還不知道名單。而且我一直在處理一個角色,所以與字符串相反的字符看起來很自然。 –
user2671156
長* 216行的'main()'*如果你把它分解成函數,你會得到更容易處理的東西。 – Beta