Hangman
This commit is contained in:
parent
1ca313d046
commit
1e3a80766b
40
src/dynarray.c
Normal file
40
src/dynarray.c
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include "dynarray.h"
|
||||||
|
|
||||||
|
struct dynarray create_dynarray() {
|
||||||
|
struct dynarray d;
|
||||||
|
d.index = 0;
|
||||||
|
d.capacity = 128;
|
||||||
|
d.buffer = malloc(d.capacity);
|
||||||
|
memset(d.buffer, 0, d.capacity);
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
void append(struct dynarray* d, char c) {
|
||||||
|
d->buffer[d->index++] = c;
|
||||||
|
if (d->index == d->capacity) {
|
||||||
|
d->buffer = realloc(d->buffer, d->capacity *= 2);
|
||||||
|
// Clear the rest of the buffer
|
||||||
|
memset(&d->buffer[d->index], 0, d->capacity - d->index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pop(struct dynarray* d) {
|
||||||
|
if (!d->index)
|
||||||
|
return;
|
||||||
|
|
||||||
|
d->buffer[--d->index] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void read_line(struct dynarray* d) {
|
||||||
|
char c;
|
||||||
|
while (1) {
|
||||||
|
read(0, &c, 1);
|
||||||
|
if (c == '\n')
|
||||||
|
break;
|
||||||
|
else if (c == '\b') {
|
||||||
|
pop(d);
|
||||||
|
write(1, &c, 1);
|
||||||
|
} else
|
||||||
|
append(d, c);
|
||||||
|
}
|
||||||
|
}
|
21
src/dynarray.h
Normal file
21
src/dynarray.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#ifndef DYNARRAY_H
|
||||||
|
#define DYNARRAY_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
struct dynarray {
|
||||||
|
char* buffer;
|
||||||
|
int index;
|
||||||
|
int capacity;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct dynarray create_dynarray();
|
||||||
|
void append(struct dynarray* d, char c);
|
||||||
|
void pop(struct dynarray* d);
|
||||||
|
|
||||||
|
void read_line(struct dynarray* d);
|
||||||
|
|
||||||
|
#endif
|
115
src/main.c
115
src/main.c
@ -1,5 +1,118 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "dynarray.h"
|
||||||
|
|
||||||
|
char* hangman_stages[6] = {
|
||||||
|
" /-----\\ \n"
|
||||||
|
" | | \n"
|
||||||
|
" | \n"
|
||||||
|
" | \n"
|
||||||
|
" | \n"
|
||||||
|
" | \n"
|
||||||
|
"============\n",
|
||||||
|
" /-----\\ \n"
|
||||||
|
" | | \n"
|
||||||
|
" O | \n"
|
||||||
|
" | \n"
|
||||||
|
" | \n"
|
||||||
|
" | \n"
|
||||||
|
"============\n",
|
||||||
|
" /-----\\ \n"
|
||||||
|
" | | \n"
|
||||||
|
" O | \n"
|
||||||
|
" + | \n"
|
||||||
|
" | \n"
|
||||||
|
" | \n"
|
||||||
|
"============\n",
|
||||||
|
" /-----\\ \n"
|
||||||
|
" | | \n"
|
||||||
|
" O | \n"
|
||||||
|
" /+ | \n"
|
||||||
|
" | \n"
|
||||||
|
" | \n"
|
||||||
|
"============\n",
|
||||||
|
" /-----\\ \n"
|
||||||
|
" | | \n"
|
||||||
|
" O | \n"
|
||||||
|
" /+\\ | \n"
|
||||||
|
" | \n"
|
||||||
|
" | \n"
|
||||||
|
"============\n",
|
||||||
|
" /-----\\ \n"
|
||||||
|
" | | \n"
|
||||||
|
" O | \n"
|
||||||
|
" /+\\ | \n"
|
||||||
|
" ^ | \n"
|
||||||
|
" | \n"
|
||||||
|
"============\n",
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
printf("Hello, World!\n");
|
int letters_guessed[26] = {0};
|
||||||
|
struct dynarray correct_word = create_dynarray();
|
||||||
|
|
||||||
|
printf("Please enter the word(s) that should be guessed: ");
|
||||||
|
fflush(stdout);
|
||||||
|
read_line(&correct_word);
|
||||||
|
|
||||||
|
for (int i=0; i<correct_word.index; i++) {
|
||||||
|
if (correct_word.buffer[i] != 32) {
|
||||||
|
char cn = correct_word.buffer[i] & ~32;
|
||||||
|
if (cn < 0x41 || cn > 0x5a) {
|
||||||
|
printf("Invalid character `%c` in input!\n", correct_word.buffer[i]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int stage = 0;
|
||||||
|
while (stage != 5) {
|
||||||
|
printf("Guesses: ");
|
||||||
|
for (int i=0; i<26; i++)
|
||||||
|
printf(letters_guessed[i]?" %c ":"", 0x41+i);
|
||||||
|
printf("\n");
|
||||||
|
int correct = 0;
|
||||||
|
for (int i=0; i<correct_word.index; i++)
|
||||||
|
printf(correct_word.buffer[i]==32?(correct++, " / "):(letters_guessed[correct_word.buffer[i]-0x41]?(correct++, " %c "):" _ "), correct_word.buffer[i]);
|
||||||
|
if (correct == correct_word.index)
|
||||||
|
break;
|
||||||
|
printf("\n");
|
||||||
|
printf(hangman_stages[stage]);
|
||||||
|
|
||||||
|
char c;
|
||||||
|
int cn;
|
||||||
|
while (1) {
|
||||||
|
printf("Guess a letter: ");
|
||||||
|
do c = getchar(); while (c == '\n');
|
||||||
|
c &= ~32;
|
||||||
|
if (c < 0x41 || c > 0x5a) {
|
||||||
|
printf("\nPlease enter a valid letter!\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
cn = c - 0x41;
|
||||||
|
if (!letters_guessed[cn]) {
|
||||||
|
letters_guessed[cn] = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
printf("\nLetter already guessed!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine if the letter is in the string
|
||||||
|
|
||||||
|
int found = 0;
|
||||||
|
for (int i=0; i<correct_word.index; i++)
|
||||||
|
if (correct_word.buffer[i] == c)
|
||||||
|
found = 1;
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
stage++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stage == 5) {
|
||||||
|
printf(hangman_stages[stage]);
|
||||||
|
printf("You lost!\nThe correct word was: %s\n", correct_word.buffer);
|
||||||
|
} else
|
||||||
|
printf("You win!\n");
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user