Thread: self-taught newbie searching for help

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    6

    self-taught newbie searching for help

    Hello everyone,
    I have been trying to learn C and C++ programming for a while now, by watching youtube tutorials mostly. Recently, i came across a crossword tutorial to be completed in C and I am not sure what i am doing wrong.
    Firstly, i have to say that the info wasn't very clear on what the user was required to do. Most of the code was given and to be completed/corrected by the C programmer, so i added a main menu to make it easier to follow, with the following options: 1. Start a New Puzzle, 2. Solve the Puzzle and 3. Exit.
    I managed (i think to have option 2 and 3 to work (almost) as expected but i can't get a new puzzle to start.
    Also, the putHorizontalWord section of the code works (it was given) but inverting
    Code:
    [rRow][rCol + i]
    to
    Code:
    [rRow + i][rCol]
    or
    Code:
    [rRow + i][rCol + i]
    does not work for vertical and diagonal words
    What am i doing wrong?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.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 = 1;
            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
            {
                // put word diagonal
            }
        }
    }
    
    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. Solve Puzzle\n");
            printf("3. Exit\n");
            menuChoice = getchar();
    
            switch (menuChoice)
            {
                case '1': createNewPuzzle(); break;
                case '2': displayPuzzel(); break;
            }
    
        } while (menuChoice != '3');
    }
    
    int main()
    {
        srand(time(NULL));
    
        createBlankPuzzle();
        displayPuzzel();
        fillPuzzleWithWords();
    
        mainMenu();
        getchar();
    
        printf("Thank you for playing today! :)\nGood Bye");
        return 0;
    }
    Any help would be highly appreciated!

    p.s.: i am using KDevelop on a Linux machine and a lot of commands, apparently, accepted on VSCode, show as building error in KDevelop i.e.: gets instead of fget(name, size, stdin) and scanf_s instead of scanf
    Last edited by however; 12-14-2022 at 03:32 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 10 Reasons Why Self-Taught Engineers are the Best in the World
    By anthonyemuobo in forum General Discussions
    Replies: 23
    Last Post: 12-08-2015, 12:28 AM
  2. who is self taught among u ppl?
    By Kuro Tensai in forum Tech Board
    Replies: 40
    Last Post: 02-26-2013, 10:55 AM
  3. who is self taught among u ppl?
    By Kuro Tensai in forum C++ Programming
    Replies: 29
    Last Post: 02-17-2013, 06:15 AM
  4. Anyone purely self-taught?
    By guitarscn in forum A Brief History of Cprogramming.com
    Replies: 103
    Last Post: 03-15-2008, 04:58 AM
  5. Will the upcoming C++ standard change how Programming languages are taught
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-14-2008, 01:07 PM

Tags for this Thread