- Dev C++ Source Code
- How To Code Hangman
- Hangman Source Code Java
- Hangman C++ Program
- Hangman Source Code Dev C Pdf
$ begingroup $ In general 'Data Driven Programming' makes your code more robust (As there is less repeated code). It makes your code easier to modify (you can update the data without changing the program (so no re-compile)). But DDP is a huge subject and I am just brushing the surface. $ endgroup $ – Martin York Aug 4 '16 at 16:12. Lately I have written the game Hangman in many different languages (C, JavaScript, Java and PHP so far). Here is the code snippet for PHP! It uses a words file, on the bottom of this post you will see a small example of a few words. The source code for other languages can be found here on DaniWeb and on my new website:. C projects for beginners. Use these project as sample code for making board game program like Tic-Tac-Toe, Snake and ladder, Hangman. Data handling projects using object oriented design Bank, Library and Student database projects for project idea.
Dev C++ Source Code
![Hangman Source Code Dev C++ Hangman Source Code Dev C++](https://moodle.org/pluginfile.php/154/mod_forum/attachment/1605171/capture.png?forcedownload=1)
How To Code Hangman
Hangman Source Code Java
hangman.c
Hangman C++ Program
#include<stdio.h> |
#include<stdlib.h> |
#include<time.h> |
#include<string.h> |
#include<ctype.h> |
typedefstruct Game { |
struct { |
char body[7]; |
int errors; |
} gallow; |
constchar* const correct_word; |
char* const hidden_word; |
} Game; |
staticconstchar* const words[] = { 'control', 'television', 'computer' }; |
staticintinit_game(Game* const game) |
{ |
srand(time(NULL)); |
*game = (Game) { .gallow.body = {'0'}, .gallow.errors = 0 }; |
constint word_idx = rand() % (sizeof(words)/sizeof(constchar* const)); |
*((constchar**)&game->correct_word) = words[word_idx]; |
constsize_t word_size = sizeof(char) * strlen(game->correct_word); |
*((char**)&game->hidden_word) = malloc(word_size + 1); |
if (game->hidden_wordNULL) { |
perror('failed to allocate memory: '); |
return -1; |
} |
memset(game->hidden_word, '_', word_size); |
game->hidden_word[word_size] = '0'; |
return0; |
} |
staticvoidfree_game(const Game* const game) |
{ |
free(game->hidden_word); |
} |
staticintincrement_gallow(Game* const game) |
{ |
char* const body = game->gallow.body; |
switch(++game->gallow.errors) { |
case6: body[6] = ''; break; |
case5: body[5] = '/'; break; |
case4: body[4] = ''; break; |
case3: body[3] = '|'; break; |
case2: body[2] = '/'; break; |
case1: body[1] = ')', body[0] = '('; break; |
} |
return game->gallow.errors < 6 ? 0 : -1; |
} |
staticvoiddraw_game(const Game* const game) |
{ |
#ifdef _WIN32 |
system('cls'); |
#else |
system('clear'); |
#endif |
constchar* const body = game->gallow.body; |
printf(' _________n' |
'| |n' |
'| %c%cn' |
'| %c%c%cn' |
'| %c%cn' |
'| n' |
'| ', body[0], body[1], body[2], |
body[3], body[4], body[5], body[6]); |
constchar* c = game->hidden_word; |
while (*c != '0') |
printf('%c', *c++); // couldn't resist |
putchar('n'); |
} |
staticintupdate_game(Game* const game) |
{ |
printf('Enter a letter: '); |
constchar input = tolower(getchar()); |
while (getchar() != 'n') {} |
constchar* correct = game->correct_word; |
char* const hidden = game->hidden_word; |
unsignedchar found = 0; |
for (size_t i = 0; correct[i] != '0'; ++i) { |
if (correct[i] input) { |
hidden[i] = input; |
found = 1; |
} |
} |
if (!found && increment_gallow(game) != 0) { |
draw_game(game); |
printf('######## you lose! ############n'); |
return1; |
} elseif (found && strcmp(game->correct_word, game->hidden_word) 0) { |
draw_game(game); |
printf('######### you win! ############n'); |
return1; |
} |
return0; |
} |
intmain() |
{ |
Game game; |
if (init_game(&game) != 0) |
return EXIT_FAILURE; |
do |
draw_game(&game); |
while (update_game(&game) 0); |
free_game(&game); |
return EXIT_SUCCESS; |
} |
Hangman Source Code Dev C Pdf
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment