ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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
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.
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
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!
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.
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
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
Some hopefully helpful thoughts of my own, not intended to answer your questions for others...
Quote:
Originally Posted by however
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
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!).
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 (
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.