hangmen

MAIN()

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include «hangman.h»

int main (void)
{
// проверка is_word_guessed
printf(«%d\n», is_word_guessed(«secret», «aeiou»)); // 0
printf(«%d\n», is_word_guessed(«hello», «aeihoul»)); // 1
printf(«\n»);

// проверка get_guessed_word
char result[30];
get_guessed_word(«container», «arpstxgoieyu», result); // _o_tai_er
printf(«%s\n\n», result);

// проверка get_available_letters
char available_letters[27] = «abcdefghijklmnopqrstuvwxyz»;
get_available_letters(«acejqz», available_letters); // bdfghiklmnoprstuvwxy
printf(«%s\n\n», available_letters);

// проверка get_word
srand( (unsigned int)time(NULL)/2 );
char secret[30];
int error = get_word(secret);
if (error == 1) return error;
//printf(«secret: %s\n», secret);

// проверка hangman
hangman(secret);

return error;
}

hangman.C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include «hangman.h»

/* определяет угадано ли слово */
int is_word_guessed(const char secret[], const char letters_guessed[])
{
int i, result = 1;
for (i=0; secret[i] && result; i++)
{
result = ( strchr(letters_guessed, secret[i]) == NULL )? 0 : 1;
// result = 0;
// for (int k=0; letters_guessed[k] && !result; k++)
// if (secret[i] == letters_guessed[k]) result = 1;
}
return result;
}

/* обновляет слово на экране */
void get_guessed_word(const char secret[], const char letters_guessed[], char guessed_word[])
{
int i;
for (i=0; secret[i]; i++)
{
if ( strchr(letters_guessed, secret[i]) == NULL )
{
guessed_word[i] = '_';
}
else
{
guessed_word[i] = secret[i];
}
}
guessed_word[i] = '\0';
}

/* список букв, которые ещё не были использованы для отгадавания */
void get_available_letters(const char letters_guessed[], char available_letters[])
{
int i, k, end = 'z'-'a'+2;
for (i=0; i
{
if ( strchr(letters_guessed, available_letters[i]) != NULL )
{
for (k=i+1; k
available_letters[k-1] = available_letters[k];
}
}
}

/* содержит в себе функционал самой игры */
void hangman(const char secret[])
{
// эту функцию необходимо написать
printf(«Wellcome to hangman\n»);
printf(«secret: %s\n», secret);
}

/* случайное слово из словаря */
int get_word(char secret[]){
// check if file exists first and is readable
FILE *fp = fopen(WORDLIST_FILENAME, «rb»);
if( fp == NULL ){
fprintf(stderr, «No such file or directory: %s\n», WORDLIST_FILENAME);
return 1;
}

// get the filesize first
struct stat st;
stat(wordlist_filename, &st);
long int size = st.st_size;

do{
// generate random number between 0 and filesize
long int random = (rand() % size) + 1;
// seek to the random position of file
fseek(fp, random, SEEK_SET);
// get next word in row ;)
int result = fscanf(fp, «%*s %20s», secret);
if( result != EOF )
break;
}while(1);

fclose(fp);
return 0;
}

hangman.H

#ifndef _hungman_h
#define _hungman_h

#define WORDLIST_FILENAME «words.txt»

/**
* Function detects, whether player guessed the secret word
* Based on the letters player used for guessing, this function detects,
* if it is possible to construct (and guess) the secret word from this set.
* If it is possible, function returns 1. If not, returns 0.
* @param secret the secret word lowercase
* @param letters_guessed the lowercase letters player already used in his guessing
* @return 1, if word is guess; 0 otherwise.
*/
int is_word_guessed(const char secret[], const char letters_guessed[]);

/**
* Function returns string representing the guessed word
* This string contains visible letters, which were guessed successfuly
* by player at their correct positions. If there are still some hidden
* letters, the character '_' will be used for their representation.
* @param secret the secret word lowercase
* @param letters_guessed the lowercase letters player already used in his guessing
* @param guessed_word the constructed string as result of this function (output parameter)
*/
void get_guessed_word(const char secret[], const char letters_guessed[], char guessed_word[]);

/**
* Returns all letters, which were not used for guessing
* Function returns set of lowercase letters sorted in alphabetical order. This
* set will contain all letters from the alphabet, but it ommits those, which
* were already guessed.
* @param letters_guessed the lowercase letters player already used in his guessing
* @param available_letters set of lowercase not yet used letters
*/
void get_available_letters(const char letters_guessed[], char available_letters[]);

/**
* Starts interactive hangman game
*
* @param secret the secret word lowercase
*/
void hangman(const char secret[]);

/**
* Returns the random word
* Function opens the file with all the words and reads randomly one of them
* into the buffer pointed by secret. Word consists of lowercase letters.
* If there is some error with reading the file, 1 is returned.
* Don’t forget to initialize the random numbers in your main() function will srand() call!
* Otherwise (everything is ok), 0 is returned.
* @param secret buffer, where random word will be read
* @return status code
*/
int get_word(char secret[]);

#endif

Не могли б вы помочь исправить мне код, и соединить его

и можете исправить нулёвый терминант массива

187187 открытий
1 комментарий

Комментарий недоступен

Ответить