Pode ser inserido diretamente no código, construindo o tabuleiro em ascii ou criando o tabuleiro em um arquivo de texto, usando números referentes a caracteres do ascii, e depois faz a leitura deste arquivo.
Podemos utilizar o código abaixo:
001
#include <stdio.h> // Allows printf, ...<font></font>
002
#include <string.h><font></font>
003
#include <stdlib.h> // Allows malloc, ...<font></font>
004
#include <errno.h> // Allows errno<font></font>
005
<font></font>
006
char** createArray(int rows, int cols);<font></font>
007
void fillArray(char** myArray, int rows, int cols);<font></font>
008
void printArray(char** myArray, int rows, int cols);<font></font>
009
void deleteArray(char** myArray, int rows, int cols);<font></font>
010
<font></font>
011
int main(void) {<font></font>
012
<font></font>
013
char filename[256];<font></font>
014
FILE *fp = NULL;<font></font>
015
printf("CROSSWORD PUZZLE GAME \n\n");<font></font>
016
printf("Enter game filename to load: ");<font></font>
017
scanf("%255s",filename);<font></font>
018
fp = fopen(filename, "r");<font></font>
019
<font></font>
020
printf("Loading game file %s",filename);<font></font>
021
printf("\n");<font></font>
022
<font></font>
023
int rows, cols, numclues;<font></font>
024
fscanf(fp,"%i %i %i\n", &rows, &cols, &numclues);<font></font>
025
<font></font>
026
printf("Game is %i",rows);<font></font>
027
printf(" x %i", cols);<font></font>
028
printf(" with %i", numclues);<font></font>
029
printf(" words \n");<font></font>
030
<font></font>
031
char** puzzle;<font></font>
032
puzzle = createArray(rows, cols);<font></font>
033
fillArray(puzzle, rows, cols);<font></font>
034
printArray(puzzle, rows, cols);<font></font>
035
//deleteArray(myArray, ROWS, COLS);<font></font>
036
<font></font>
037
char HV;<font></font>
038
int srow, scol;<font></font>
039
char word[256];<font></font>
040
char clue[256];<font></font>
041
fscanf(fp,"%s %i %i %255s %255[^\n]", &HV, &srow, &scol, word, clue);<font></font>
042
printf("Orientation: %c \n", HV);<font></font>
043
printf("Starting Row: %i \n", srow);<font></font>
044
printf("Starting Column: %i \n", scol);<font></font>
045
printf("Word: %s \n", word);<font></font>
046
printf("Clue: %s \n", clue);<font></font>
047
<font></font>
048
fclose(fp);<font></font>
049
return EXIT_SUCCESS;<font></font>
050
}<font></font>
051
<font></font>
052
char** createArray(int rows, int cols) {<font></font>
053
char **myArray;<font></font>
054
<font></font>
055
// Allocate a 1xROWS array to hold pointers to more arrays<font></font>
056
myArray = calloc(rows, sizeof(int *));<font></font>
057
if (myArray == NULL) {<font></font>
058
printf("FATAL ERROR: out of memory: %s\n", strerror(errno));<font></font>
059
exit(EXIT_FAILURE);<font></font>
060
}<font></font>
061
<font></font>
062
// Allocate each row in that column<font></font>
063
for (int i = 0; i < rows; i++) {<font></font>
064
myArray[i] = calloc(cols, sizeof(int));<font></font>
065
if (myArray[i] == NULL) {<font></font>
066
printf("FATAL ERROR: out of memory: %s\n", strerror(errno));<font></font>
067
exit(EXIT_FAILURE);<font></font>
068
}<font></font>
069
}<font></font>
070
<font></font>
071
return myArray;<font></font>
072
}<font></font>
073
<font></font>
074
void fillArray(char** myArray, int rows, int cols) {<font></font>
075
printf("Current Puzzle: \n");<font></font>
076
<font></font>
077
int count = 1;<font></font>
078
<font></font>
079
for (int i = 0; i < rows; i++) {<font></font>
080
for (int j = 0; j < cols; j++) {<font></font>
081
myArray[i][j] = count;<font></font>
082
count++;<font></font>
083
}<font></font>
084
}<font></font>
085
<font></font>
086
return;<font></font>
087
}<font></font>
088
<font></font>
089
void printArray(char** myArray, int rows, int cols) {<font></font>
090
for (int i = 0; i < rows; i++) {<font></font>
091
for (int j = 0; j < cols; j++) {<font></font>
092
printf("%i ", myArray[i][j]);<font></font>
093
}<font></font>
094
printf("\n");<font></font>
095
}<font></font>
096
}<font></font>
097
<font></font>
098
void deleteArray(char** myArray, int rows, int cols) {<font></font>
099
for (int i = 0; i < rows; i++) {<font></font>
100
for (int j = 0; j < cols; j++) {<font></font>
101
free(myArray[i]);<font></font>
102
}<font></font>
103
free(myArray);<font></font>
104
}<font></font>
105
<font></font>
106
return;<font></font>
107
}<font></font>
108
<font></font>
Para escrever sua resposta aqui, entre ou crie uma conta
Compartilhar