Thread: Single put routine to avoid overlapping words during iteration

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    6

    Single put routine to avoid overlapping words during iteration

    I am trying to have a simple loop for random words that may be placed horizontally and/or vertically and/or diagonally depending on the allowed space and wordLength. Below, is what I came up with but there is significant overlapping and broken words. Is there a way to have the loop in a single 'put' routine for all direction?
    All my attempts have resulted in buggier outcomes. Thanks


    Code:
     //Check horizontal orientation and place the word if available spaces allow it
    void putHorizzontalWord(char word[10])
    {
        int rRow, rCol , ok , i;
    
        do
        {
            rRow = rand() % 10;
            rCol = rand() % 10;
    
            ok = 1;
            if(rCol + strlen(word) < 10)
            {
                for(i = 0;i < strlen(word);i++)
                {
                    if(puzzle[rRow][rCol + i] == '*' ||
                        puzzle[rRow][rCol + i] == word[i])
                    {
                        puzzle[rRow][rCol + i] = word[i];
                    }
                    else
                    {
                        ok = 0;
                    }
                }
            }
            else
            {
                ok = 0;
            }
        }
        while(ok == 0);
    }
    
    //Check vertical orientation and place the word if available spaces allow it
    void putVerticalWord(char word[10]) //this, doesn't seem to work'
    {
        int rRow, rCol , ok , i;
    
        do
        {
            rRow = rand() % 10;
            rCol = rand() % 10;
    
            ok = 0;
            if(rRow + strlen(word) < 10)
            {
                for(i = 0;i < strlen(word);i++)
                {
                    if(puzzle[rRow + i][rCol] == '*' ||
                        puzzle[rRow + i][rCol] == word[i])
                    {
                        puzzle[rRow + i][rCol] = word[i];
                    }
                    else
                    {
                        ok = 1;
                    }
                }
            }
            else
            {
                ok = 1;
            }
        }
        while(ok == 1);
    }
    
    //Check diagonal orientation and place the word if available spaces allow it
    void putDiagonalWord(char word[10]) //this, doesn't seem to work'
    {
        int rRow, rCol , ok , i;
    
        do
        {
            rRow = rand() % 10;
            rCol = rand() % 10;
    
            ok = 2;
            if(rRow + strlen(word) < 10)
            {
                for(i = 0;i < strlen(word);i++)
                {
                    if(puzzle[rRow + i][rCol + i] == '*' ||
                        puzzle[rRow + i][rCol + i] == word[i])
                    {
                        puzzle[rRow + i][rCol + i] = word[i];
                    }
                    else
                    {
                        ok = 2;
                    }
                }
            }
            else
            {
                ok = 1;
            }
        }
        while(ok == 0);
    }
    
    void fillPuzzleWithWords()
    {
        int i , orientation;
        getFourRandomWords();
    
        for(i=0; i<4; i++)
        {
            orientation = rand() % 3;
            if(orientation == 0)
            {
                putHorizzontalWord(fourWords[i]);
            }
            else if(orientation == 1)
            {
                putVerticalWord(fourWords[i]);
            }
            else
            {
                putDiagonalWord(fourWords[i]);
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Sep 2022
    Posts
    55
    If you really want to check if you have enough space you need to perform an initial iteration to verify that all characters that you want to overwrite are still asterisks.
    What is the reason for having only a single put() function? do you want to write into whatever direction the word fits?

  3. #3
    Registered User
    Join Date
    Dec 2022
    Posts
    6
    Quote Originally Posted by aGerman View Post
    If you really want to check if you have enough space you need to perform an initial iteration to verify that all characters that you want to overwrite are still asterisks.
    What is the reason for having only a single put() function? do you want to write into whatever direction the word fits?
    Hi and thanks for your reply.
    Yes i would like to insert a word in to any direction it fits and I thought that having only one put() function would solve it, but i am not sure anymore as I am new to C

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Here's something to study.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define GRID_W  10
    #define GRID_H  10
    
    int isValidPos(int row, int col) {
        return row >= 0 && row < GRID_H &&
               col >= 0 && col < GRID_W;
    }
    
    int allowed(char grid[GRID_H][GRID_W], int row, int col, int deltar, int deltac, const char *word) {
        int result = 1;
        while ( result && *word ) {
            result = isValidPos(row, col);              // it's in the grid
            result = result && (grid[row][col] == ' ' || grid[row][col] == *word);   // the cell is free, or matches
            if ( result ) {
                row += deltar;  // next pos in the chosen dir
                col += deltac;  
                word++;         // next char
            }
        }
        return result;
    }
    
    void addWord(char grid[GRID_H][GRID_W], int row, int col, int deltar, int deltac, const char *word) {
        if ( allowed(grid, row, col, deltar, deltac, word) ) {
            while ( *word ) {
                grid[row][col] = *word;
                row += deltar;  // next pos in the chosen dir
                col += deltac;  
                word++;         // next char
            }
        }
    }
    
    void initGrid(char grid[GRID_H][GRID_W]) {
        for ( int r = 0 ; r < GRID_H ; r++ ) {
            for ( int c = 0 ; c < GRID_W ; c++ ) {
                grid[r][c] = ' ';
            }
        }
    }
    
    void printGrid(const char grid[GRID_H][GRID_W]) {
        for ( int r = 0 ; r < GRID_H ; r++ ) {
            for ( int c = 0 ; c < GRID_W ; c++ ) {
                putchar(grid[r][c]);
            }
            putchar('\n');
        }
    }
    
    int main ()
    {
        char grid[GRID_H][GRID_W];
        initGrid(grid);
        addWord(grid, 0, 0, 1, 1, "hello"); // diagonal hello
        addWord(grid, 4, 3, 0, 1, "world"); // horizontal world through end of hello
        addWord(grid, 9, 9, -1, -1, "bye");
        printGrid(grid);
        return 0;
    }
    
    
    $ gcc foo.c
    $ ./a.out 
    h         
     e        
      l       
       l      
       world  
              
              
           e  
            y 
             b
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2022
    Posts
    6
    <Salem>
    thank you for your (supposedly) elegant hint but I am not sure how or where to implement it.
    I think for now I will leave my code as it is; it's a little buggier, meaning that every 3-4 runs the words break but, at least it works.
    When i tried you add your code to the rest, it completely broke the program.
    Obviously it's not personal, it's just me; I simply don't know how.
    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting and separating single words in a text file
    By mrafay in forum C Programming
    Replies: 12
    Last Post: 01-05-2011, 09:08 AM
  2. Replies: 7
    Last Post: 10-01-2010, 04:09 PM
  3. Read single words seperated by white space in as strings
    By steals10304 in forum C Programming
    Replies: 2
    Last Post: 11-02-2009, 04:05 PM
  4. Split string up into single words
    By Todd88 in forum C++ Programming
    Replies: 21
    Last Post: 12-05-2008, 04:12 AM
  5. Reading a File and returning single words
    By kapri in forum C++ Programming
    Replies: 5
    Last Post: 05-11-2005, 05:13 AM

Tags for this Thread