30/09/2018, 17:00

[code] game pluzzle trong c

#include <stdio.h>
#include <string.h>
#include <ctype.h>
 
#define UP -1
#define DOWN 1
#define RIGHT 1
#define LEFT -1
 
typedef struct
{
        int upDown;
        int leftRight;
        char orient_id[30];
}orientation_t;
 
int searchWord(char word[], char puzzle[][10], orientation_t *orient, int *row, int *col); 
int searchRestOfWord(char word[], char puzzle[][10], orientation_t orient, int row, int col, int wordIndex); 
void readPuzzle(char fileName[], char puzzle[][10]);
void printPuzzle(char puzzle[][10]);
 
int main(int argc, char *argv[])
{
 
        FILE *words;
        int end, row = 0, col = 0;
        char currentWord[30];
        char puzzle[10][10];
        orientation_t orient;
       
        if(argc == 3)
        {
                orient.upDown = 0;
                orient.leftRight = 0;
                readPuzzle(argv[1], puzzle);   
                printPuzzle(puzzle);
                words = fopen(argv[2], "r");
 
                end = fscanf(words, "%s", currentWord);
                while(end != EOF)
                {
                        if(searchWord(currentWord, puzzle, &orient, &row, &col) == 1)
                        {
                                /* found: do stuff */
                                printf("%s: (%s) row: %d column: %d
", currentWord, orient.orient_id, row, col);
                               
                        }
                        else
                        {
                                /* not found: do stuff */
                                printf("%s: word not found
", currentWord);
                        }
                        end = fscanf(words, "%s", currentWord);
                }
        }
        else
        {
                printf("not file
");
        }
       
        return 0;
}
 
 

int searchWord(char word[], char puzzle[][10], orientation_t *orient, int *row, int *col)
{
 
        /* search for first letter */
 
        int i, j;
 
        for(i = 0; i<10; i++)
        {
                for(j = 0; j<10; j++)
                {
                        if(puzzle[i][j] == word[0])
                        {
 
                                /* first letter found, search for second letter */
                                if(j!=9)
                                {
                                        if(puzzle[i][j+1] == word[1])
                                        {
                                                orient->upDown = 0;
                                                orient->leftRight = RIGHT;
                                                strcpy(orient->orient_id, "FORWARD");
                                                if(searchRestOfWord(word, puzzle, *orient, i, j+1, 2) == 1)
                                                {
                                                        *row = i;
                                                        *col = j;
                                                        return 1;
                                                }
                                        }
                                }
 
                                if(j!=0)
                                {
                                        if(puzzle[i][j-1] == word[1])
                                        {      
                                                orient->upDown = 0;
                                                orient->leftRight = LEFT;
                                                strcpy(orient->orient_id, "BACKWARD");
                                                if(searchRestOfWord(word, puzzle, *orient, i, j-1, 2) == 1)
                                                {
                                                        *row = i;
                                                        *col = j;
                                                        return 1;
                                                }
                                        }
                                }
 
                                if(i!=9)
                                {
                                        if(puzzle[i+1][j] == word[1])
                                        {      
                                                orient->upDown = DOWN;
                                                orient->leftRight = 0;
                                                strcpy(orient->orient_id, "DOWN");
                                                if(searchRestOfWord(word, puzzle, *orient, i+1, j, 2) == 1)
                                                {
                                                        *row = i;
                                                        *col = j;
                                                        return 1;
                                                }
                                        }
                                }
 
                                if(i!=0)
                                {
                                        if(puzzle[i-1][j] == word[1])
                                        {      
                                                orient->upDown = UP;
                                                orient->leftRight = 0;
                                                strcpy(orient->orient_id, "UP");
                                                if(searchRestOfWord(word, puzzle, *orient, i-1, j, 2) == 1)
                                                {
                                                        *row = i;
                                                        *col = j;
                                                        return 1;
                                                }
                                        }
                                }
 
                                if(j != 9 && i != 0)
                                {
                                        if(puzzle[i-1][j+1] == word[1])
                                        {      
                                                orient->upDown = UP;
                                                orient->leftRight = RIGHT;
                                                strcpy(orient->orient_id, "UP-RIGHT");
                                                if(searchRestOfWord(word, puzzle, *orient, i-1, j+1, 2) == 1)
                                                {
                                                        *row = i;
                                                        *col = j;
                                                        return 1;
                                                }
                                        }
                                }
 
                                if(j != 9 && i != 9)
                                {
                                        if(puzzle[i+1][j+1] == word[1])
                                        {      
                                                orient->upDown = DOWN;
                                                orient->leftRight = RIGHT;
                                                strcpy(orient->orient_id, "DOWN-RIGHT");
                                                if(searchRestOfWord(word, puzzle, *orient, i+1, j+1, 2) == 1)
                                                {
                                                        *row = i;
                                                        *col = j;
                                                        return 1;
                                                }
                                        }
                                }
 
                                if(j != 0 && i != 0)
                                {
                                        if(puzzle[i-1][j-1] == word[1])
                                        {      
                                                orient->upDown = UP;
                                                orient->leftRight = LEFT;
                                                strcpy(orient->orient_id, "UP-LEFT");
                                                if(searchRestOfWord(word, puzzle, *orient, i-1, j-1, 2) == 1)
                                                {
                                                        *row = i;
                                                        *col = j;
                                                        return 1;
                                                }
                                        }
                                }
 
                                if(j != 0 && i != 9)
                                {
                                        if(puzzle[i+1][j-1] == word[1])
                                        {      
                                                orient->upDown = DOWN;
                                                orient->leftRight = LEFT;
                                                strcpy(orient->orient_id, "DOWN-LEFT");
                                                if(searchRestOfWord(word, puzzle, *orient, i+1, j-1, 2) == 1)
                                                {
                                                        *row = i;
                                                        *col = j;
                                                        return 1;
                                                }
                                        }
                                }
                        }
                }
        }
       
        return 0;
}
 

int searchRestOfWord(char word[], char puzzle[][10], orientation_t orient, int row, int col, int wordIndex)
{
        if(word[wordIndex] == puzzle[row+orient.upDown][col+orient.leftRight]) /* if next letter matches */
        {
                if(wordIndex == strlen(word)-1)
                {
                        return 1;
                }
                else
                {
                        return searchRestOfWord(word, puzzle, orient, row+orient.upDown, col+orient.leftRight, wordIndex+1);
                }
        }
        else
        {

                return 0;
       
        }
}
 

void readPuzzle(char fileName[], char puzzle[][10])
{
 
        FILE *in;
        int i = 0, j = 0, end;
        char c;
       
        in = fopen(fileName, "r");
        end = fscanf(in, "%c", &c);    
 
        while(end != EOF && i < 10)
        {
                if(c == '
')
                {
                        i++;
                        j = 0;
                }
                else if(isalpha(c))
                {
                        puzzle[i][j] = c;
                        j++;
                }
 
                end = fscanf(in, "%c", &c);
        }
 
        fclose(in);
}
 
void printPuzzle(char puzzle[][10])
{
        int i, j;
 
        printf("Puzzle:
");
        for(i = 0; i < 10; i++)
        {
                for(j = 0; j < 10; j++)
                {
                        printf("%c", puzzle[i][j]);
                }
                printf("
");
        }
}



Khai Nguyen Dinh viết 19:06 ngày 30/09/2018

mình đọc code này trên mạng nhưng không biết chạy như thế nào?giúp mình với

Trịnh Minh Cường viết 19:07 ngày 30/09/2018

mình nhìn k rõ sorry

Khai Nguyen Dinh viết 19:03 ngày 30/09/2018

bạn hỏi nhưng mình chả biết chạy như thế nào cả hic

Bài liên quan
0