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!