Boomer trying to learn coding in C and C++

Joined
Dec 16, 2022
Messages
6
Reaction score
0
hello folks,
the thread title says it all.
I am trying to write an,apparently, basic code for am, apparently, super simple cross word but I don't seem to get even half of it done. (before you crucify me, please, beware that I do the average of maybe 1hr of coding/week)
C:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <curses.h>

#define ROWS 10
#define COLUMNS 10

char puzzle[ROWS][COLUMNS];

char allWords[20][10] = {"GIRL" , "BOY" , "SHIP" , "CAT" , "FOG" , "KITE" , "BAG" , "STAMP" , "ZOOM" , "JOY", "CAR" , "BUS" , "VAN" , "BOAT" , "BIKE" , "TURBO" , "SCHOOL" , "DOVIC" , "VIRUS" , "STAR"};

char fourWords[4][10];

char getRandomCharacter(void)
{
    int r = (rand() % 26) + 65;
    return (char)r;
}

void getFourRandomWords(void) {
    int draws[4];
    // draw 4 words from the list, no duplicates
    for (int i = 0; i < 4; i++) {
        int n = rand() % (20 - i);
        int j;
        for (j = 4 - i; j < 4 && n >= draws[j]; j++) {
            draws[j - 1] = draws[j];
            n++;
        }
        draws[j - 1] = n;
        strcpy(fourWords[i], allWords[n]);
    }
}

void createBlankPuzzle()
{
    int i , j;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            puzzle [i][j] = '*';
        }
    }
}

void createNewPuzzle()
{
    int i , j;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            puzzle [i][j] = getRandomCharacter();
        }
    }
}

void displayPuzzel()
{
    int i , j , rowNum = 0;
    char x = 'A';

    // First display column names
    printf("  ");
    for(i=0; i<COLUMNS; i++)
    {
        printf("%c ",x + i);
    }
    printf("\n");

    for(i = 0;i < ROWS;i++)
    {
        printf("%d " ,rowNum);
        rowNum++;
        for(j=0; j<COLUMNS; j++)
        {
            printf("%c ",puzzle[i][j]);
        }
        printf("\n");
    }
}

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);
}

void putVerticalWord(char word[10]) //this, doesn't seem to work'
{
    int rRow, rCol , ok , i;

    do
    {
        rRow = rand() % 10;
        rCol = rand() % 10;

        ok = 1;
        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 = 0;
                }
            }
        }
        else
        {
            ok = 0;
        }
    }
    while(ok == 0);
}

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 = 0;
                }
            }
        }
        else
        {
            ok = 0;
        }
    }
    while(ok == 0);
}

void fillPuzzleWithWords()
{
    int i , orientation;
    getFourRandomWords();

    for(i=0; i<4; i++)
    {
        orientation = 0; //rand() % 3; // To generate a random number from 0, 1, & 2
        if(orientation == 0)
        {
            putHorizzontalWord(fourWords[i]);
        }
        else if(orientation == 1)
        {
            putVerticalWord(fourWords[i]);
        }
        else if(orientation == 2)
        {

        }
        else
        {
            putDiagonalWord(fourWords[i]);
        }
    }
}

void mainMenu()
{
    char menuChoice;
    do
    {
        printf("------------------------");
        printf("\nUse coordinate to solve\nthe puzzle; i.e. C3, G3\n");
        printf("------------------------");
        printf("\n~~~ DAILY CROSSWORD ~~~\n");
        printf("1. New game\n");
        printf("2. Change Words\n");
        printf("3. Exit\n");
        menuChoice = getchar();

        switch (menuChoice)
        {
            case '1': createNewPuzzle(); break;
            //case '2': displayPuzzel(); break;
            case '2': getFourRandomWords();break;
        }

    } while (menuChoice != '3');
}

int main()
{
    srand(time(NULL));

    createBlankPuzzle();
    displayPuzzel();
    createNewPuzzle();
    //fillPuzzleWithWords();
    getFourRandomWords();

    mainMenu();
    getchar();
    createNewPuzzle();
    printf("Thank you for playing today! :)\nGood Bye");
    return 0;
}
I think i have got bugs everywhere. the only thing that I manage to get working is the 'Exit' function.
I should choose 4 words from the list (which could be vertically, horizontally or diagonally orientated and mingled in the grid with random characters), print them on screen so the user/player can search them on the grid and allow the user to find them by inputting the word's coordinates by imputing in the keyboard.

I managed to create a blank puzzle and fill it up with random characters but, i don't seem to be able to populate the grid with the four random words, in any orientation.

I thought it could be cool to have a menu' where the user would get 3 or 4 options such as: Play, Change Words, Solve Puzzle and Exit but I found myself at the bottom of a rabbit hole and finally, I only opted for 2 options: 1) Play and 2) Exit. And, I can't even do that.

What am i messing up?

Thanks in advance

p.s.: i am on linux and using KDevelop as IDE
 
Joined
Sep 21, 2022
Messages
115
Reaction score
14
A different "put" routine for each direction is not needed. One routine could do all 8 directions. Less code, less bugs.

The only difference between placing a word one way or another is "change in x" (-1,0,1) and "change in y" (-1,0,1) as the program moves through the word, and those can be arguments.

Selecting a word at random, and then searching for a place it will fit, is one way, but there are easier ways. (With a better chance of interesting overlaps happening.)

Like, select a random place, and then search for a word.

Start with a grid of "?" characters.

(1) Pick a random position, direction, and word length. If valid, copy what's there in the grid, into a string.

"??E???K" for example.

(2) Search a dictionary (just a list of words) file for matches (treat ? as a wildcard).

(3) Select one (if there are any) of the matches at random, and insert it into the grid.

Repeat (1) to (3) for a reasonable amount of time.

(4) Replace remaining ? with random letters.

There is a teach yourself Python book, and it has a free dictionary on its web site.

Here's a grid made from that text file. Step (4) has not been done.

Code:
? S T R O H S ? ? ?
? ? ? ? ? ? ? E ? E
S ? ? E ? ? D ? G N
? O ? O N O K O ? T
? ? L L B T T R ? R
A T H E N I A N O O
? ? ? G S ? ? I ? P
? D U T I F U L L Y
? R T S E H G I H ?
D ? ? ? ? S T A E H

puzzle: 14 words
dictionary: 45333 words
running time: 60 seconds

ATHENIAN
BODE
DRUG
DUTIFULLY
EGOTIST
ENTAIL
ENTROPY
HEATS
HIGHEST
OLEG
PORK
SHORTS
SOLE
THIGH
 
Joined
Dec 16, 2022
Messages
6
Reaction score
0
Thank you for your reply.

Although I am new to coding, I think it would be quite different to apply python coding to a C program, or am i wrong?

And beside, i don't think it would help me much to introduce a second language, even if possible, when i still know nothing about the first.

I have a conceptual idea of the algorithm you number-listed, writing in C creates a hole in my brain

I have made a few changes and have the code to flow as I want it, now however. beside the words horientation issue I also have the problem that I cannot fill the grid with random characters (around the placed/picked words)
C:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <curses.h>

#define ROWS 10
#define COLUMNS 10

//Creating two arrays Columns and Rows
char puzzle[ROWS][COLUMNS];

//List of words from which four will be picked up by the program, with which the user to play will search
char allWords[20][10] = {"GIRL" , "BOY" , "SHIP" , "CAT" , "FOG" , "KITE" , "BAG" , "STAMP" , "ZOOM" , "JOY", "CAR" , "BUS" , "VAN" , "BOAT" , "BIKE" , "TURBO" , "SCHOOL" , "DOVIC" , "VIRUS" , "STAR"};

char fourWords[4][10];

//creates random characters from ASCII starting at 65 (=A)
char getRandomCharacter(void)
{
    int r = (rand() % 26) + 65;
    return (char)r;
}

//pick 4 random words from a list of 20
void getFourRandomWords(void) {
    int draws[4];
    // draw 4 words from the list, no duplicates
    for (int i = 0; i < 4; i++) {
        int n = rand() % (20 - i);
        int j;
        for (j = 4 - i; j < 4 && n >= draws[j]; j++) {
            draws[j - 1] = draws[j];
            n++;
        }
        draws[j - 1] = n;
        strcpy(fourWords[i], allWords[n]);
    }
}

// word to be displayed/searched by the user (I CAN'T GET THIS PART WO WORK)
void displayWords(void){
    int words = (rand() % 4);
    printf("%s\n " ,&words);
}

//Create a grid filled only with asterisks
void createBlankPuzzle()
{
    int i , j;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            puzzle [i][j] = '*';
        }
    }
}
/*
//Create a grid filled with random characters
void createNewPuzzle()
{
    int i , j;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            puzzle [i][j] = getRandomCharacter();
        }
    }
}
*/
//Display the character-populated grid
void displayPuzzle()
{
    int i , j , rowNum = 0;
    char x = 'A';

    // First display column names
    printf("  ");
    for(i=0; i<COLUMNS; i++)
    {
        printf("%c ",x + i);
    }
    printf("\n");

    for(i = 0;i < ROWS;i++)
    {
        printf("%d " ,rowNum);
        rowNum++;
        for(j=0; j<COLUMNS; j++)
        {
            printf("%c ",puzzle[i][j]);
        }
        printf("\n");
    }
}

//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 = 1;
        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 = 0;
                }
            }
        }
        else
        {
            ok = 0;
        }
    }
    while(ok == 0);
}

//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 = 0;
                }
            }
        }
        else
        {
            ok = 0;
        }
    }
    while(ok == 0);
}

void fillPuzzleWithWords()
{


    int i , orientation;
    getFourRandomWords();


    for(i=0; i<4; i++)
    {
        orientation = 0; //rand() % 3; // To generate a random number from 0, 1, & 2
        if(orientation == 0)
        {
            putHorizzontalWord(fourWords[i]);
        }
        else if(orientation == 1)
        {
            putVerticalWord(fourWords[i]);
        }
        else if(orientation == 2)
        {
            putDiagonalWord(fourWords[i]);
        }
    }
}

//Create a user-interactive menu
void mainMenu()
{
    char menuChoice;
    do
    {
        printf("\n~~~ DAILY CROSSWORD ~~~\n");
        printf("1. New game\n");
        printf("2. Exit\n");
        menuChoice = getchar();

        switch (menuChoice)
        {
            case '1': displayPuzzle();
                printf("\n------------------------");
                printf("\nUse coordinate to solve\nthe puzzle; i.e. C3, G3\n");
                printf("------------------------\n");
                printf("\n"); break;
        }

    } while (menuChoice != '2');
}

int main()
{
    srand(time(NULL));

    createBlankPuzzle();
    displayPuzzle();
    fillPuzzleWithWords();

    mainMenu();
    getchar();
    printf("Thank you for playing today! :)\nGood Bye");
    return 0;
}

How could i fill the grid with random letters, once it has been filled with the random words?

Thanks in advance
 
Joined
Sep 21, 2022
Messages
115
Reaction score
14
The reference to the Python book was to make it easy to find a good list of words, via google. Not a suggestion to switch languages.
 
Joined
Nov 24, 2022
Messages
73
Reaction score
7
You ask about filling the grid with random letters?

Well, I wrote some function last week. It's in C# but still I think it may help You. Ale the fun in coding is possibiliy of making Your own tools. This is one of mine.

Code:
// creating string filled with random letters

public string create_random_string(int size)

        {

            int tmp_char;

            string tmp_string;               // new random string

            char[] arr = new char[size];     // temporary array

            Random random_number = new Random();

            for (int u = 0; u < size; u++)

            {

                tmp_char = random_number.Next(66, 111);

                arr[u] = (char)tmp_char;

            }

            tmp_string = new string(arr);    // creating a new string



            return tmp_string;

        }

You may need pointers (*) to convert it to C (change tmp_string to *tmp_string) and use malloc() and free() (sooner or later You will meet those functions - nothing complicated, but gives You supernatural powers xD) or You can use static array. Or You can do do it anyway as long as it works.

Check ascii code, in the loop create random numbers from 0 to 26, add 65 od 97 to get the code number of letter, and put it into Your array.

You can improve it in many ways.

Change it into C and use strcpy() to put into Your array.

I hope that's what You need, and my answer was helpful.
 
Last edited:
Joined
Dec 16, 2022
Messages
6
Reaction score
0
Thank you infinityhost,

perhaps what I should have pointed out at the beginning that I am a complete beginner in coding simple functions (I started with C).
Now you kindly propose to convert your C# (which is completely martian to me) and gave me a few hints.
I will give it a go but i guess it will be like a mechanic trying to carry out a heart surgery blind-fold.
Well, i guess i need to learn and thank you again
 
Joined
Nov 24, 2022
Messages
73
Reaction score
7
Ok. Let me show You what I think is the best for You, which way I think You should go.
1) Variables - at the begining You cannot move, understending how to use variables will make Your hand moveable.
2) if-else statements - this will allow You, to move Your hand in the way You want to.
3) loops - to write code and make it executable You need to operate on huge data of logical blocks repeatedly ,and this is what loops were created for.
4) arrays - You need to know it, to know what is the computer representation of data (how computer see it) and to group objects.
5) pointers - To use computer memory (to store a big information elements in it) You need pointers (and to operate on created objects).
6) functions - some blocks of code will be used many times, but they will be a little bit changed - those blocks is best to have at one place with some name , and this is called function.
7) IO operations - to read data from keyboard,mouse,file etc.
7 steps, and what I wrote before to You, is completly clear (I don't remember how random function works in C).
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top