Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: novatus84 en 15 Diciembre 2014, 19:02 pm



Título: Ayuda perfeccionar programa
Publicado por: novatus84 en 15 Diciembre 2014, 19:02 pm
Buenas necesito un empujón para perfeccionar programa, saco las letras en horizontal y vertical en la matriz pero lo tengo un poco guarrete, necesito que no me pise las palabras mostradas y que muestre todas bien mezcladas en horizontal y vertical.
Gracias de antemano, saludos!!
Código
  1. #include <vector>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <random>
  9.  
  10. using namespace std;
  11.  
  12. string filename = "words.txt";
  13. ifstream ifile {filename};
  14. void error(const string&);
  15.  
  16. vector<string> load_vector(const string& filename) {
  17.  
  18.    vector<string> vi;
  19.  
  20.    if (!ifile)
  21.        error("load_vector cannot open the file " + filename);
  22.    copy(istream_iterator<string>(ifile), istream_iterator<string>(), back_inserter(vi));
  23.  
  24.    return vi;
  25. }
  26.  
  27. void error(const string& s) {
  28.    cerr << "Error: " << s << endl;
  29.    exit(EXIT_FAILURE);
  30. }
  31.  
  32. void aleatorio () {
  33.    int HEIGHT = 15; //Numero de filas
  34.    int WIDTH = 15; //Numero de columnas
  35.    int wordsize = 0;
  36.    int wordPos = 0;
  37.    char puzzle[HEIGHT][WIDTH]; //Matriz de caracteres
  38.    bool okWord;
  39.    bool horizontal;
  40.    int randColumn;
  41.  
  42.    srand(time(NULL)); //inicializamos semilla
  43.  
  44.    char random; //Carácter aleatorio
  45.    int filarand = 0; //Variable para escoger una fila aleatoria
  46.  
  47.    vector<string> load_vector(const string&);
  48.    vector<string> words = load_vector(filename);
  49.    vector<string> auxWords;
  50.    string enteredWord;
  51.    string aux;
  52.  
  53.    //Inicializamos matriz
  54.    for (int i = 0; i < HEIGHT; i++) {
  55.        for (int j = 0; j < WIDTH; j++) {
  56.            puzzle[i][j] = ' '; //Al inicializar la matriz la rellenamos con espacios
  57.        }
  58.    }
  59.  
  60.    for (int i = 0; i < words.size(); i++) {
  61.        switch (rand() % 2) {
  62.        case 1:
  63.            filarand = rand() % 15; //Escogemos una fila aleatoria
  64.            wordsize = WIDTH - words[i].size() - 1; //Posiciones que nos quedan para escoger columna aleatoria
  65.            wordPos = rand() % wordsize; //Escogemos columna aleatoria para empezar a escribir la palabra
  66.            for (int j = 0; j < words[i].size(); j++) {
  67.                if (words[i].size() < 10) { //Si la palabra es menor de 10 caracteres
  68.                    aux = ""; // Inicializamos la variable con ningún caracter
  69.                    aux = words[i].substr(j,1); //Sustraemos una letra cada vez de la palabra
  70.                    puzzle[filarand][wordPos + j] = aux[0]; //Rellenamos la fila de la matriz con la palabra
  71.                }
  72.            }
  73.            break;
  74.        case 0:
  75.            randColumn = rand() % 15;
  76.            wordsize = HEIGHT - words[i].size() - 1;
  77.            wordPos = rand() % wordsize;
  78.            for (int j = 0; j < words[i].size(); j++) {
  79.                if (words[i].size() < 10) {
  80.                    aux = "";
  81.                    aux = words[i].substr(j,1);
  82.                    puzzle[wordPos + j][randColumn] = aux[0];
  83.                }
  84.            }
  85.            break;
  86.        }
  87.    }
  88.  
  89.    auxWords = words;
  90.  
  91.    for (int i = 0; i < HEIGHT; i++) {          //la llenamos aleatoriamente de letras
  92.        for (int j = 0; j < WIDTH; j++) {
  93.            if (puzzle[i][j] == ' ') {
  94.                random = 'A' + (rand() % 26); //Escogemos un carácter aleatorio
  95.                puzzle[i][j] = random; //Rellenamos la posición con un carácter aleatorio
  96.            }
  97.        }
  98.    }
  99.  
  100.  
  101.    for (int i = 0; i < HEIGHT; i++) { //Mostramos la matriz final
  102.        for (int j = 0; j < WIDTH; j++) {
  103.            cout << puzzle[i][j] ;
  104.            cout << "  ";
  105.        }
  106.        cout << endl;
  107.    }
  108.  
  109.    do {
  110.        cout << "Introduce una palabra: ";
  111.        cin >> enteredWord;
  112.  
  113.        for (int i = 0; i < auxWords.size(); i++) {
  114.            if (enteredWord == auxWords[i]) { //Miramos si la palabra introducida existe en el vector de palabras
  115.                okWord = true;                  // SI LA PALABRA existe borrarla del vector, cuando la borre sale
  116.                auxWords.erase(auxWords.begin() + i);
  117.                break;
  118.            }
  119.            else {                          // si no la encuentra sigue buscando
  120.                okWord = false;
  121.            }
  122.        }
  123.  
  124.        if (okWord == true) {                  // si la a encontrado te dice la palabra
  125.            cout << "Palabra " << enteredWord << " correcta" << endl;
  126.        }
  127.        else if (enteredWord != "FIN") {        //si no la encuetra te dice incorrecta
  128.            cout << "Palabra " << enteredWord << " incorrecta" << endl;
  129.        }
  130.    } while (enteredWord != "FIN");             //Mientras que no escribas FIN no termina programa
  131. }
  132.  
  133.  
  134.  
  135. int main() {
  136.  
  137.    int HEIGHT = 10;
  138.    int WIDTH = 10;
  139.  
  140.    char puzzle[HEIGHT][WIDTH];
  141.  
  142.    srand(time(NULL));
  143.    aleatorio();
  144.    //wordToMatrix(10, 10, puzzle[HEIGHT][WIDTH]);
  145.  
  146.    /*for (int i = 0; i < HEIGHT; i++) {
  147.      for (int j = 0; j < WIDTH; j++) {
  148.      cout << puzzle[i][j] << endl;
  149.      }
  150.      }*/
  151.  
  152.  
  153.  
  154.    return 0;
  155. }
  156.  
  157.