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