18
figures.h
Normal file
18
figures.h
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
const char base_figure[6][8] = {{"======="},\
|
||||||
|
{"I "},\
|
||||||
|
{"I "},\
|
||||||
|
{"I "},\
|
||||||
|
{"I "},\
|
||||||
|
{"I\\ "}};
|
||||||
|
|
||||||
|
const char figure[6][5] = {{" O "},\
|
||||||
|
{" I "},\
|
||||||
|
{"\\I "},\
|
||||||
|
{"\\I/"},\
|
||||||
|
{"/ "},\
|
||||||
|
{"/ \\"}};
|
||||||
|
|
||||||
|
|
||||||
|
/* This array represents the layers where the strings
|
||||||
|
of the current stage(array index) should be printed*/
|
||||||
|
const int stages[] = {0, 1, 1, 1, 2, 2};
|
62
hangman.c
62
hangman.c
@@ -1,4 +1,10 @@
|
|||||||
#include <ncurses.h>
|
#ifdef _WIN32 //For 32 and 64 bits
|
||||||
|
/* pdcurses include */
|
||||||
|
#include <curses.h>
|
||||||
|
#else
|
||||||
|
/* Linux ncurses include */
|
||||||
|
#include <ncurses.h>
|
||||||
|
#endif
|
||||||
#include "hangman.h"
|
#include "hangman.h"
|
||||||
#include <stdlib.h> //atexit
|
#include <stdlib.h> //atexit
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -7,6 +13,11 @@
|
|||||||
#include <time.h> //time(null)
|
#include <time.h> //time(null)
|
||||||
#include "prng.h"
|
#include "prng.h"
|
||||||
#include <unistd.h> //usleep
|
#include <unistd.h> //usleep
|
||||||
|
#include "figures.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
@@ -72,9 +83,9 @@ int main(int argc, char **argv) {
|
|||||||
/* start game */
|
/* start game */
|
||||||
initGuessWord(gs, filename);
|
initGuessWord(gs, filename);
|
||||||
initAlphabet(gs);
|
initAlphabet(gs);
|
||||||
drawAlphabet(gs);
|
drawAlphabet(gs, 0);
|
||||||
|
drawFigure(gs);
|
||||||
startGame(gs);
|
startGame(gs);
|
||||||
|
|
||||||
|
|
||||||
getch();
|
getch();
|
||||||
free(gs);
|
free(gs);
|
||||||
@@ -152,30 +163,32 @@ void initGuessWord(game_state *gs, char *filename) {
|
|||||||
|
|
||||||
void initAlphabet(game_state *gs) {
|
void initAlphabet(game_state *gs) {
|
||||||
int hasNumber = 0;
|
int hasNumber = 0;
|
||||||
int i, j;
|
int i;
|
||||||
for (i = 0; i < MAXWORDLENGTH; i++) {
|
for (i = 0; i < MAXWORDLENGTH; i++) {
|
||||||
if (isdigit(gs->guessWord[i]) == 0) {
|
if (isdigit(gs->guessWord[i]) == 1) {
|
||||||
hasNumber = 1;
|
hasNumber = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
gs->alphabet = malloc(sizeof(char) * hasNumber ? ALPHABET : ALPHABET_NUM);
|
gs->alphabet = malloc(sizeof(char) * hasNumber ? ALPHABET : ALPHABET_NUM);
|
||||||
|
strcpy(gs->alphabet, "abcdefghijklmnopqrstuvwxyz");
|
||||||
for (i = 'a', j = 0; i <= 'z'; i++, j++) {
|
|
||||||
gs->alphabet[j] = i;
|
|
||||||
}
|
|
||||||
if (hasNumber) {
|
if (hasNumber) {
|
||||||
j++;
|
strcat(gs->alphabet, "1234567890");
|
||||||
for (i = 0; i < 10; i++, j++) {
|
|
||||||
gs->alphabet[j] = i;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawAlphabet(game_state *gs) {
|
void drawAlphabet(game_state *gs, char usedchar) {
|
||||||
int start = centerDiff(gs->centerx, gs->alphabet);
|
int start = centerDiff(gs->centerx, gs->alphabet);
|
||||||
|
int i;
|
||||||
|
if (usedchar != 0) { /* 0 is used for initialization */
|
||||||
|
for (i = 0; i < (int)strlen(gs->alphabet); i++) {
|
||||||
|
if (gs->alphabet[i] == usedchar) {
|
||||||
|
gs->alphabet[i] = '_';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
mvprintw(1, start, "%s", gs->alphabet);
|
mvprintw(1, start, "%s", gs->alphabet);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,6 +260,7 @@ int fillCurrentWord(game_state *gs, char validchar) {
|
|||||||
gs->currentWord[i] = validchar;
|
gs->currentWord[i] = validchar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
drawAlphabet(gs, validchar);
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -263,7 +277,8 @@ int stackWrongCharacter(game_state *gs, char wrongchar) {
|
|||||||
|
|
||||||
if (!alreadyUsed) {
|
if (!alreadyUsed) {
|
||||||
gs->wrongCharacters[strlen(gs->wrongCharacters)] = wrongchar;
|
gs->wrongCharacters[strlen(gs->wrongCharacters)] = wrongchar;
|
||||||
return 1;
|
drawAlphabet(gs, wrongchar);
|
||||||
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -373,5 +388,18 @@ void trollHitScreen(game_state *gs, int hits) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void drawFigure(game_state *gs) {
|
||||||
|
int row = 6;
|
||||||
|
int length = 10;
|
||||||
|
int i;
|
||||||
|
static init = 0;
|
||||||
|
if (init) {
|
||||||
|
for (i = 0; i < row; i++) {
|
||||||
|
mvprintw(i, gs->maxx - length - 1, "%s", base_figure[i]);
|
||||||
|
}
|
||||||
|
init = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -33,7 +33,7 @@ void updateScreen(game_state *gs);
|
|||||||
void initCoordinates(game_state *gs);
|
void initCoordinates(game_state *gs);
|
||||||
void initGuessWord(game_state *gs, char *filename);
|
void initGuessWord(game_state *gs, char *filename);
|
||||||
void initAlphabet(game_state *gs);
|
void initAlphabet(game_state *gs);
|
||||||
void drawAlphabet(game_state *gs);
|
void drawAlphabet(game_state *gs, char usedchar);
|
||||||
void drawGuessWord(game_state *gs);
|
void drawGuessWord(game_state *gs);
|
||||||
int playerInput(game_state *gs);
|
int playerInput(game_state *gs);
|
||||||
int fillCurrentWord(game_state *gs, char validchar);
|
int fillCurrentWord(game_state *gs, char validchar);
|
||||||
@@ -47,3 +47,4 @@ void showHelp(game_state *gs);
|
|||||||
int centerDiff(int coordinate, char *str);
|
int centerDiff(int coordinate, char *str);
|
||||||
void readRandomLine(char *file, char *result);
|
void readRandomLine(char *file, char *result);
|
||||||
void trollHitScreen(game_state *gs, int hits);
|
void trollHitScreen(game_state *gs, int hits);
|
||||||
|
void drawFigure(game_state *gs);
|
20
makefile_mingw
Normal file
20
makefile_mingw
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#Infos: http://www.ijon.de/comp/tutorials/makefile.html
|
||||||
|
|
||||||
|
|
||||||
|
VERSION = 1.0
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -Wall -Wextra -O3 -D_REENTRANT -DVERSION=\"$(VERSION)\"
|
||||||
|
#LDFLAGS = -lm -lpthread `gtk-config --cflags` `gtk-config --libs` -lgthread
|
||||||
|
LDFLAGS = -lpdcurses
|
||||||
|
|
||||||
|
OBJ = hangman.o prng.o
|
||||||
|
|
||||||
|
all: $(OBJ)
|
||||||
|
$(CC) $(CFLAGS) -o hangman $(OBJ) $(LDFLAGS)
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) $(CFLAGS) -c $<
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -r *.o
|
Reference in New Issue
Block a user