So here's what I'm trying to do with my program:
-fill a list of words;
-fill the matrix with the words on the list, however, when choosing the initial position where the word is going to be inserted, it needs to be done randomly. I did it with random_suffle.
#include <iostream>
#include <fstream>
#include <list>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
using namespace std;
struct mPair {
char letter;
bool marked = false;
};
int main(){
// matrix size
int size;
cout << "Matrix size: ";
cin >> size;
// declaring matrix
mPair* matriz;
matriz = new mPair[size*size];
//matrix positions
int i, j, position;
int word_length = 0;
int status = 0;
vector<int> v;
for(int x = 0; x < size*size; x++) v.push_back(x);
vector<int>::iterator it;
list<string> l;
string line;
list<string>::iterator it3;
// filling the list
l.push_back("my");
l.push_back("book");
l.push_back("moon");
l.push_back("lulululu");
l.push_back("constitutional");
l.push_back("lalalay");
l.push_back("lure");
l.push_back("memory");
for(it3 = l.begin(); it3 != l.end(); ++it3){
line = *it3;
// checking if the word can fit inside the matrix
if(line.length() <= size){
// choosing a random position
random_shuffle (v.begin(), v.end());
for(it = v.begin(); it != v.end(); ++it){
i = (*it)%size; //cols
j = (*it)/size; //rows
position = *it;
// checking to see if the word fits from left to right, its starting point being (i,j)
if(line.length() <= (size-i)){
// here we'll check if the space where the word is being inserted is free
while(position <= line.length()){
//it the position is marked true, that means there's already a letter in there
if(matriz[position].marked == true){
// so we're checking to see if the letter that is already there is equal to the current letter of string line
if(matriz[position].letter == line[word_length]){
status = 0;
}else{
status = 1;
}
}else{
status = 0;
}
// incrementing position variable, since we're checking horizontally
position++;
word_length++;
}
//filling the matrix
if(status = 0){
position = *it;
word_length = 0;
for(int i = 0; i < line.length(); i++){
matriz[position].letter = line[word_length];
matriz[position].marked = true;
position++;
word_length++;
}
}
}
/*
// checking to see if the word fits from right to left, its starting point being (i,j)
}else if(line.length() <= (i+1)){
// checking to see if the word fits top to bottom, its starting point being (i,j)
}else if(line.length() <= (size-j)){
// checking to see if the word fits bottom to top, its starting point being (i,j)
}else if(line.length() <= (j+1)){
}
*/
}
}
}
for(int i = 0; i < size*size; i++){
cout << "(i,j) = (" << i%size << "," << i/size << ") = position " << i << " = " << matriz[i].letter << endl;
}
}
When I print the letter on the matrix, something like this comes out:
Matrix size: 6
(i,j) = (0,0) = position 0 = ░
(i,j) = (1,0) = position 1 =
(i,j) = (2,0) = position 2 = └
(i,j) = (3,0) = position 3 =
(i,j) = (4,0) = position 4 =
(i,j) = (5,0) = position 5 =
(i,j) = (0,1) = position 6 =
(i,j) = (1,1) = position 7 =
(i,j) = (2,1) = position 8 = ╚
(i,j) = (3,1) = position 9 =
(i,j) = (4,1) = position 10 = ░
(i,j) = (5,1) = position 11 =
(i,j) = (0,2) = position 12 =
(i,j) = (1,2) = position 13 =
(i,j) = (2,2) = position 14 =
(i,j) = (3,2) = position 15 =
(i,j) = (4,2) = position 16 = α
(i,j) = (5,2) = position 17 =
(i,j) = (0,3) = position 18 = á
(i,j) = (1,3) = position 19 =
(i,j) = (2,3) = position 20 =
(i,j) = (3,3) = position 21 =
(i,j) = (4,3) = position 22 =
(i,j) = (5,3) = position 23 =
(i,j) = (0,4) = position 24 =
(i,j) = (1,4) = position 25 =
(i,j) = (2,4) = position 26 =
(i,j) = (3,4) = position 27 =
(i,j) = (4,4) = position 28 =
(i,j) = (5,4) = position 29 =
(i,j) = (0,5) = position 30 =
(i,j) = (1,5) = position 31 =
(i,j) = (2,5) = position 32 =
(i,j) = (3,5) = position 33 =
(i,j) = (4,5) = position 34 =
(i,j) = (5,5) = position 35 =
I'm not sure what is wrong with my code
Aucun commentaire:
Enregistrer un commentaire