LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2023, 02:08 AM   #16
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,594

Rep: Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629Reputation: 6629

just wanted to repeat myself, if you wish to understand what's going on use a debugger and you can see how those lines actually work one by one, step by step.
(you will be able to follow a loop, inspect all of your variables)
 
Old 01-13-2023, 03:01 AM   #17
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,005
Blog Entries: 23

Rep: Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967
I too am not sure that I fully understand how you intend the program to work, but taking the problem of filling the grid with random letters except where the four words have been entered...

One way is as michaelk has suggested:

Quote:
Originally Posted by michaelk View Post
If you change the createNewPuzzle to only place random characters where an element has a * that just fix your problems.
Another might be to just fill the entire puzzle with random letters first, instead of asterisks. That should be just a simple loop-within-a-loop as you have already written. Then place the words, thus overwriting the random letters in the word positions. This way there is no need to test whether a position has already been filled and the result is the same.

To prevent wrapping words all you need to do is test the random starting row or column against the length of the word you want to place. If the start position plus word length is greater than the corresponding width or height try again until you get a position far enough from the corresponding edge.

There is also the problem that one word might overwrite another... but for another post after we decide how to solve the above.

Last edited by astrogeek; 01-13-2023 at 03:02 AM.
 
Old 01-13-2023, 03:26 AM   #18
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post
Great advice from astrogeek


The key is divide-and-conquer ie write only one fn and get that working, then put that aside in a file and work on the next fn and so on.
Finally start adding the fns one by one to the main program file and ensure each new addition works in harmony with the existing code before bringing in the next fn.

This works for any language. In general do not try to write and then debug an entire program in one go, it gets exponentially harder as the num of lines increases.
Thank you for the hint.
When i run/debug each function or the whole program I get no error,whether I am in debugging mode or not.

Do i have a different KDevelop from what everyone else has? I attached a scrnshot of my KDevelop while debugging the program and the a scrnshot of the terminal window showing the output. Nothing else was shown; no error or warnings msgs.


Quote:
I too am not sure that I fully understand how you intend the program to work, but taking the problem of filling the grid with random letters except where the four words have been entered...

One way is as michaelk has suggested:

Quote:
Originally Posted by michaelk View Post
If you change the createNewPuzzle to only place random characters where an element has a * that just fix your problems.
Another might be to just fill the entire puzzle with random letters first, instead of asterisks. That should be just a simple loop-within-a-loop as you have already written. Then place the words, thus overwriting the random letters in the word positions. This way there is no need to test whether a position has already been filled and the result is the same.

this is how I am trying to fill the grid with random letter in places where there is an asterisk, but it won't work.
Code:
//Create a grid filled with random characters
void createNewPuzzle()
{
    int i , j, ok;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            ok = 1;
            if (j== '*')
            {
            puzzle [i][j] = getRandomCharacter();
            }
            else
            {
                ok = 0;
            }
        }
    }
}
I have tried to fill the grid with random letter and then place the fourWords but it breaks everything; this is the function i changed
Code:
void createBlankPuzzle()
{
    int i , j;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            puzzle [i][j] = 'getRandomCharacter';
        }
    }
}
Attached Thumbnails
Click image for larger version

Name:	scrnshot_1.jpg
Views:	5
Size:	63.9 KB
ID:	40221   Click image for larger version

Name:	scrnshot_2.jpg
Views:	5
Size:	155.7 KB
ID:	40222  
 
Old 01-13-2023, 04:08 AM   #19
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,005
Blog Entries: 23

Rep: Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967
The reason that breaks everything, by which I take it the program probably goes off into la-la-land, is because the functions putHorizzontalWord() and friends all go into an endless loop.

You need to rethink the loop tests for those three functions. They all test for the presence of an asterisk in the inner loop (which it never finds after random letter are set), else the outer loop test is set to zero, which results in the endless loop.

You will at least need to handle that differently for asterisks and random letters, but would probably do better to rethink how those work. I'll have a closer look tomorrow - all done for tonight!
 
Old 01-13-2023, 05:58 AM   #20
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,428
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
getchar doesn't work the way you expect it (meaning: it is line-buffered, you don't get keystrokes individually)
Find attached a modified versions.
Attached Files
File Type: txt crosswords.c.txt (6.1 KB, 7 views)

Last edited by NevemTeve; 01-13-2023 at 06:15 AM.
 
1 members found this post helpful.
Old 01-13-2023, 07:00 AM   #21
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,913

Rep: Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268Reputation: 5268
Code:
 ok = 1;
 if (j== '*')
 {
    puzzle [i][j] = getRandomCharacter();
 }
 else
 {
   ok = 0;
 }
You are checking the index and not the contents of the array at that index.

Code:
if ( puzzle[i][j]== '*' ) puzzle [i][j] = getRandomCharacter();
I had thought about filling the array first with random characters but that would make determining if an already placed word was being overwritten much harder to determine.
 
1 members found this post helpful.
Old 01-13-2023, 01:01 PM   #22
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
getchar doesn't work the way you expect it (meaning: it is line-buffered, you don't get keystrokes individually)
Find attached a modified versions.
Much more sophisti'code'd than me however, if I may ask: what IDE are you using? Asking bcos on my KDevelop your code was buggier than mine and I still had only asterisks with no letters. Now, I am starting to wonder whether I should to install and work on a different IDE.

Quote:
Originally Posted by michaelk
You are checking the index and not the contents of the array at that index.
I appreciate your input though it sounds like arabic to me and therefore wouldn't know how to do either. I guess in a few month i will be ablew to answer with the same 'savvy' terms.

But really, thank you for the effort; I understand how frustrating and boring it must be to try walk normally with someone who wears no shoes
 
Old 01-13-2023, 02:36 PM   #23
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,428
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
I don't know any IDE, but if you get an error message you need help with, do quote it.
Also I suggest the following changes:
Code:
void putHorizzontalWord(const char *word)
{
    int rRow, rCol , ok , i;
    int wlen= strlen(word);

    do {
        rRow = rand() % (11-wlen);
        rCol = rand() % 10;
        for (i=0, ok= 1; ok && i<wlen; i++) {
            ok= puzzle[rRow][rCol+i]=='*';
        }
    } while (!ok);

    for (i= 0;i<wlen; i++) {
        puzzle[rRow][rCol+i]= word[i];
    }
}

void putVerticalWord(const char *word)
{
    int rRow, rCol, ok, i;
    int wlen= strlen(word);

    do {
        rRow = rand() % 10;
        rCol = rand() % (11-wlen);
        for (i=0, ok= 1; ok && i<wlen; i++) {
            ok= puzzle[rRow+i][rCol]=='*';
        }
    } while (!ok);

    for (i= 0;i<wlen; i++) {
        puzzle[rRow+i][rCol]= word[i];
    }
}

void putDiagonalWord(const char *word)
{
    int rRow, rCol , ok , i;
    int wlen= strlen(word);

    do {
        rRow = rand() % (11-wlen);
        rCol = rand() % (11-wlen);
        for (i=0, ok= 1; ok && i<wlen; i++) {
            ok= puzzle[rRow+i][rCol+i]=='*';
        }
    } while (!ok);

    for (i= 0;i<wlen; i++) {
        puzzle[rRow+i][rCol+i]= word[i];
    }
}

Last edited by NevemTeve; 01-13-2023 at 02:38 PM.
 
1 members found this post helpful.
Old 01-13-2023, 03:01 PM   #24
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
I don't know any IDE, but if you get an error message you need help with, do quote it.
Also I suggest the following changes:
Code:
void putHorizzontalWord(const char *word)
{
    int rRow, rCol , ok , i;
    int wlen= strlen(word);

    do {
        rRow = rand() % (11-wlen);
        rCol = rand() % 10;
        for (i=0, ok= 1; ok && i<wlen; i++) {
            ok= puzzle[rRow][rCol+i]=='*';
        }
    } while (!ok);

    for (i= 0;i<wlen; i++) {
        puzzle[rRow][rCol+i]= word[i];
    }
}

void putVerticalWord(const char *word)
{
    int rRow, rCol, ok, i;
    int wlen= strlen(word);

    do {
        rRow = rand() % 10;
        rCol = rand() % (11-wlen);
        for (i=0, ok= 1; ok && i<wlen; i++) {
            ok= puzzle[rRow+i][rCol]=='*';
        }
    } while (!ok);

    for (i= 0;i<wlen; i++) {
        puzzle[rRow+i][rCol]= word[i];
    }
}

void putDiagonalWord(const char *word)
{
    int rRow, rCol , ok , i;
    int wlen= strlen(word);

    do {
        rRow = rand() % (11-wlen);
        rCol = rand() % (11-wlen);
        for (i=0, ok= 1; ok && i<wlen; i++) {
            ok= puzzle[rRow+i][rCol+i]=='*';
        }
    } while (!ok);

    for (i= 0;i<wlen; i++) {
        puzzle[rRow+i][rCol+i]= word[i];
    }
}
thank you for the effort.

I changed the code (putHorizontal/Vertical...) you suggested and it made no real changes to the existing output. Words still get wrapped; and that would not be a problem if i manage to fill the grid with random letters which unfortunately your previous file did not do. The frustrating thing is that I DO NOT get any error msgs. The program compiles OK and then gets executed (when i say buggier, maybe a wrong word, I mean the way it executes. Your file has random number as return, instead of the menuChoice and the actual menu disappeared after the first choice so, if the player wished to start-a-new/exit the game while running, couldnt.

edit: it's not the KDevelop. I run the same code on Geany and it was identical output

Last edited by however; 01-13-2023 at 03:10 PM.
 
Old 01-13-2023, 03:11 PM   #25
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,428
Blog Entries: 1

Rep: Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679Reputation: 1679
I think you should first put the four words into the grid, then replace the remaining stars with random characters. Suggested change:
Code:
void fillPuzzleWithWords()
{
    int i, j, 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]);
        }
    }
    for (i= 0; i<10; ++i) {
        for (j= 0; j<10; ++j) {
            if (puzzle[i][j]=='*') {
                puzzle[i][j]= getRandomCharacter();
            }
        }
    }
}
 
2 members found this post helpful.
Old 01-13-2023, 03:19 PM   #26
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
I think you should first put the four words into the grid, then replace the remaining stars with random characters. Suggested change:
Code:
void fillPuzzleWithWords()
{
    int i, j, 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]);
        }
    }
    for (i= 0; i<10; ++i) {
        for (j= 0; j<10; ++j) {
            if (puzzle[i][j]=='*') {
                puzzle[i][j]= getRandomCharacter();
            }
        }
    }
}
YES! That did the job!

I will try to find the words now!
 
Old 01-13-2023, 03:37 PM   #27
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,005
Blog Entries: 23

Rep: Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967Reputation: 3967
Some hopefully helpful thoughts of my own, not intended to answer your questions for others...

Quote:
Originally Posted by however View Post
Much more sophisti'code'd than me however, if I may ask: what IDE are you using? Asking bcos on my KDevelop your code was buggier than mine and I still had only asterisks with no letters. Now, I am starting to wonder whether I should to install and work on a different IDE.
This is why I generally discourage use of IDEs for learning programming, C in particular. You are learning, obviously, so you don't really know where the language ends and the IDE features begin, which adds some uncertainty at best, confusion in many cases. My personal recommendation is to remove the IDE from the picture and learn the language unimpeded using a simple shell and text editor. Then introduce an IDE later when you have gained some knowledge of the language and can more clearly understand what the IDE actually does for you.

Quote:
Originally Posted by however View Post
I appreciate your input though it sounds like arabic to me and therefore wouldn't know how to do either. I guess in a few month i will be ablew to answer with the same 'savvy' terms.

But really, thank you for the effort; I understand how frustrating and boring it must be to try walk normally with someone who wears no shoes
This illustrates the point I tried to make in my initial post about the need to communicate by concepts rather than by code. It isn't about being saavy, it is about understanding and communicating the ideas (meaning) as opposed to the notation (code). The thing michaelk was telling you is that this...

Code:
if (j== '*')
...checking the index, does not accomplish what you are actually trying to do, which is check the contents of the array at the indexed location...

Code:
if ( puzzle[i][j]== '*' )
... a common enough programming error but a concept you need to understand intuitively.

Keep at it, and use the non-code answers provided by others to learn in human language the ideas captured by the code syntax. Actaully easier than learning Arabic (believe me, I know!).

Good learning and good luck!
 
1 members found this post helpful.
Old 01-13-2023, 03:43 PM   #28
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
So, the job is done, except now that I don't know what words to look for.

I have tried to add
Code:
printf("%n\n" , draws);
in the getFourRandomWords function but i remembered that said function only initiate the randomization

So, i created a small function
Code:
// word to be displayed/searched by the user (I CAN'T GET THIS PART WO WORK)
void displayWords(){
    int words = (rand() % 4);
    printf("%d\n " , words);
}
but i must be missing something because it doesn't seem to be called

is there a tricky way i can add a printf of the random words either in a function of its own? or included in the getFourRandomWords function?

Thanks again

Last edited by however; 01-13-2023 at 04:00 PM.
 
Old 01-13-2023, 03:48 PM   #29
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
Some hopefully helpful thoughts of my own, not intended to answer your questions for others...



This is why I generally discourage use of IDEs for learning programming, C in particular. You are learning, obviously, so you don't really know where the language ends and the IDE features begin, which adds some uncertainty at best, confusion in many cases. My personal recommendation is to remove the IDE from the picture and learn the language unimpeded using a simple shell and text editor. Then introduce an IDE later when you have gained some knowledge of the language and can more clearly understand what the IDE actually does for you.



This illustrates the point I tried to make in my initial post about the need to communicate by concepts rather than by code. It isn't about being saavy, it is about understanding and communicating the ideas (meaning) as opposed to the notation (code). The thing michaelk was telling you is that this...

Code:
if (j== '*')
...checking the index, does not accomplish what you are actually trying to do, which is check the contents of the array at the indexed location...

Code:
if ( puzzle[i][j]== '*' )
... a common enough programming error but a concept you need to understand intuitively.

Keep at it, and use the non-code answers provided by others to learn in human language the ideas captured by the code syntax. Actaully easier than learning Arabic (believe me, I know!).

Good learning and good luck!
Although frightening it's quite helpful advice. I thought that by using an IDE i would make some shortcuts but I was not aware of such beginner's obstacles Machine thinking; (trying to jog before learning to walk).
Thanks

edit: I started this project in Nov/22; a thing to consider is, that i will probably park this project in the next few days as life duties call and, return to it in a couple/few weeks (

Last edited by however; 01-13-2023 at 03:51 PM.
 
Old 01-13-2023, 04:03 PM   #30
however
Member
 
Registered: Jan 2019
Distribution: slackware current
Posts: 365

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
I think you should first put the four words into the grid, then replace the remaining stars with random characters. Suggested change:
Code:
void fillPuzzleWithWords()
{
    int i, j, 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]);
        }
    }
    for (i= 0; i<10; ++i) {
        for (j= 0; j<10; ++j) {
            if (puzzle[i][j]=='*') {
                puzzle[i][j]= getRandomCharacter();
            }
        }
    }
}
I have the feeling that no words are inserted. Now the entire grid is filled with random characters. I had done something similar with this
Code:
//Create a grid filled with random characters
void createNewPuzzle()
{
    int i , j, ok;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            ok = 1;
            if (j== '*')
            {
            puzzle [i][j] = getRandomCharacter();
            }
            else
            {
                ok = 0;
            }
        }
    }
}
edit: or maybe not! i am not sure anymore! out of 7 executions I found 2 words. Coincidence?

Last edited by however; 01-13-2023 at 04:11 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
to grep 100 letters out of 500 letters on certain criteria l_ravi69 Programming 3 10-13-2013 12:37 PM
Can't stop fast typing double letters from "skipping" one of the letters. Lola Kews Ubuntu 3 04-20-2013 04:21 PM
Random device letters assigned on Ubuntu 8.04? brianpbarnes Linux - Hardware 1 12-04-2008 07:26 PM
random letters pixellany Programming 15 07-14-2007 09:05 AM
Broken Fedora Version? (National letters get messed up, random times) blackman890 Fedora 1 06-27-2005 08:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration