Ported hangman.

This commit is contained in:
Serge Vakulenko
2014-09-08 18:07:59 -07:00
parent 8886a00340
commit aecac2f5d7
17 changed files with 177 additions and 126 deletions

View File

@@ -183,6 +183,8 @@ int wdeleteln (WINDOW *);
void mvcur(int ly, int lx, int y, int x);
void overwrite(WINDOW *win1, WINDOW *win2);
void wclrtobot(WINDOW *win);
int mvprintw(int y, int x, char *fmt, ...);
int mvwprintw(WINDOW *win, int y, int x, char *fmt, ...);
/*
* Used to be in unctrl.h.

View File

@@ -418,6 +418,7 @@ file /games/cfscores
file /games/factor
file /games/fish
file /games/fortune
file /games/hangman
file /games/morse
file /games/number
file /games/ppt
@@ -630,9 +631,12 @@ file /libexec/smlrc
default
filemode 0444
dir /share
dir /share/dict
dir /share/misc
file /share/emg.keys
file /share/re.help
file /share/dict/words
file /share/misc/more.help
#
# Files: /share/example
@@ -725,6 +729,8 @@ file /share/man/cat6/battlestar.0
file /share/man/cat6/bcd.0
file /share/man/cat6/canfield.0
file /share/man/cat6/fish.0
file /share/man/cat6/fortune.0
file /share/man/cat6/hangman.0
file /share/man/cat6/number.0
file /share/man/cat6/rain.0
file /share/man/cat6/rogue.0

View File

@@ -12,9 +12,8 @@ CFLAGS += -Werror -Wall -Os
# Programs that live in subdirectories, and have makefiles of their own.
#
SUBDIR = adventure atc backgammon battlestar boggle btlgammon \
cribbage fortune rogue sail trek
# TODO: hangman hunt mille monop phantasia quiz robots
# sail snake warp
cribbage fortune hangman rogue sail trek
# TODO: mille monop quiz robots snake
# C programs that live in the current directory and do not need
# explicit make lines.

1
src/games/hangman/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
hangman

View File

@@ -1,22 +1,28 @@
OBJS= endgame.o extern.o getguess.o getword.o main.o playgame.o \
prdata.o prman.o prword.o setup.o
CFILES= endgame.c extern.c getguess.c getword.c main.c playgame.c \
prdata.c prman.c prword.c setup.c
HDRS= hangman.h
CFLAGS= -O
LDFLAGS=
SEPFLAG= -i
TOPSRC = $(shell cd ../../..; pwd)
include $(TOPSRC)/target.mk
#include $(TOPSRC)/cross.mk
all: hangman
CFLAGS += -Werror -Wall
tags: $(HDRS) $(CFILES)
ctags $(HDRS) $(CFILES)
OBJS = endgame.o extern.o getguess.o getword.o main.o playgame.o \
prdata.o prman.o prword.o setup.o
MAN = hangman.0
LIBS = -lcurses -ltermcap -lc
install: hangman
install -s hangman $(DESTDIR)/usr/games/hangman
hangman: $(OBJS)
$(CC) ${SEPFLAG} $(LDFLAGS) -o hangman $(OBJS) -lcurses -ltermlib
all: hangman $(MAN)
hangman: $(OBJS)
$(CC) $(LDFLAGS) -o $@.elf $(OBJS) $(LIBS)
$(OBJDUMP) -S $@.elf > $@.dis
$(SIZE) $@.elf
$(ELF2AOUT) $@.elf $@ && rm $@.elf
$(MAN): hangman.6
nroff -man $< > $@
clean:
rm -f $(OBJS) hangman ? core
rm -f *.o core hangman *.0 *.dis
install: all
install hangman $(DESTDIR)/games/
install -m 644 $(MAN) $(DESTDIR)/share/man/cat6/

View File

@@ -1,9 +1,10 @@
# include "hangman.h"
#include "hangman.h"
/*
* endgame:
* Do what's necessary at the end of the game
*/
void
endgame()
{
register char ch;
@@ -24,7 +25,7 @@ endgame()
leaveok(stdscr, FALSE);
refresh();
if ((ch = readch()) == 'n')
die();
die(0);
else if (ch == 'y')
break;
mvaddstr(MESGY + 2, MESGX, "Please type 'y' or 'n'");

View File

@@ -1,4 +1,4 @@
# include "hangman.h"
#include "hangman.h"
bool Guessed[26];

View File

@@ -1,9 +1,41 @@
# include "hangman.h"
#include <unistd.h>
#include "hangman.h"
#ifndef CTRL
# define CTRL(c) (c & 037)
#endif
/*
* readch;
* Read a character from the input
*/
int
readch()
{
register int cnt;
auto char ch;
cnt = 0;
for (;;) {
if (read(0, &ch, sizeof ch) <= 0)
{
if (++cnt > 100)
die(0);
}
else if (ch == CTRL('L')) {
wrefresh(curscr);
mvcur(0, 0, curscr->_cury, curscr->_curx);
}
else
return ch;
}
}
/*
* getguess:
* Get another guess
*/
void
getguess()
{
register int i;
@@ -23,8 +55,8 @@ getguess()
else
break;
}
else if (ch == CTRL(D))
die();
else if (ch == CTRL('D'))
die(0);
else
mvprintw(MESGY, MESGX, "Not a valid guess: '%s'",
unctrl(ch));
@@ -43,28 +75,3 @@ getguess()
if (!correct)
Errors++;
}
/*
* readch;
* Read a character from the input
*/
readch()
{
register int cnt, r;
auto char ch;
cnt = 0;
for (;;) {
if (read(0, &ch, sizeof ch) <= 0)
{
if (++cnt > 100)
die();
}
else if (ch == CTRL(L)) {
wrefresh(curscr);
mvcur(0, 0, curscr->_cury, curscr->_curx);
}
else
return ch;
}
}

View File

@@ -1,15 +1,26 @@
# include "hangman.h"
#include <string.h>
#include <stdlib.h>
#include "hangman.h"
# if pdp11
# define RN (((off_t) rand() << 16) | (off_t) rand())
# else
# define RN rand()
# endif
/*
* abs:
* Return the absolute value of an integer
*/
off_t
offabs(i)
off_t i;
{
if (i < 0)
return -(off_t) i;
else
return (off_t) i;
}
/*
* getword:
* Get a valid word out of the dictionary file
*/
void
getword()
{
register FILE *inf;
@@ -17,7 +28,7 @@ getword()
inf = Dict;
for (;;) {
fseek(inf, abs(RN % Dict_size), 0);
fseek(inf, offabs(random() % Dict_size), 0);
if (fgets(Word, BUFSIZ, inf) == NULL)
continue;
if (fgets(Word, BUFSIZ, inf) == NULL)
@@ -39,17 +50,3 @@ cont: ;
}
*gp = '\0';
}
/*
* abs:
* Return the absolute value of an integer
*/
off_t
abs(i)
off_t i;
{
if (i < 0)
return -(off_t) i;
else
return (off_t) i;
}

View File

@@ -1,26 +1,30 @@
# include <curses.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <ctype.h>
# include <signal.h>
#include <stdio.h>
#include <ctype.h>
#include <curses.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
# define MINLEN 6
# define MAXERRS 7
# define DICT "/usr/dict/words"
# define MESGY 12
# define MESGX 0
# define PROMPTY 11
# define PROMPTX 0
# define KNOWNY 10
# define KNOWNX 1
# define NUMBERY 4
# define NUMBERX (COLS - 1 - 26)
# define AVGY 5
# define AVGX (COLS - 1 - 26)
# define GUESSY 2
# define GUESSX (COLS - 1 - 26)
#define MINLEN 6
#define MAXERRS 7
#ifdef CROSS
# define DICT "/usr/local/share/dict/words"
#else
# define DICT "/share/dict/words"
#endif
#define MESGY 12
#define MESGX 0
#define PROMPTY 11
#define PROMPTX 0
#define KNOWNY 10
#define KNOWNX 1
#define NUMBERY 4
#define NUMBERX (COLS - 1 - 26)
#define AVGY 5
#define AVGX (COLS - 1 - 26)
#define GUESSY 2
#define GUESSX (COLS - 1 - 26)
typedef struct {
short y, x;
@@ -41,6 +45,15 @@ extern FILE *Dict;
extern off_t Dict_size;
int die();
void die(int);
off_t abs();
off_t offabs(off_t);
void prman(void);
void prword(void);
void prdata(void);
void setup(void);
void playgame(void);
void getword(void);
void getguess(void);
void endgame(void);
int readch(void);

View File

@@ -1,9 +1,22 @@
# include "hangman.h"
#include <stdlib.h>
#include "hangman.h"
/*
* die:
* Die properly.
*/
void die(int sig)
{
mvcur(0, COLS - 1, LINES - 1, 0);
endwin();
putchar('\n');
exit(0);
}
/*
* This game written by Ken Arnold.
*/
main()
int main()
{
initscr();
signal(SIGINT, die);
@@ -15,15 +28,3 @@ main()
}
/* NOTREACHED */
}
/*
* die:
* Die properly.
*/
die()
{
mvcur(0, COLS - 1, LINES - 1, 0);
endwin();
putchar('\n');
exit(0);
}

View File

@@ -1,9 +1,11 @@
# include "hangman.h"
#include <strings.h>
#include "hangman.h"
/*
* playgame:
* play a game
*/
void
playgame()
{
register bool *bp;

View File

@@ -1,9 +1,10 @@
# include "hangman.h"
#include "hangman.h"
/*
* prdata:
* Print out the current guesses
*/
void
prdata()
{
register bool *bp;

View File

@@ -1,10 +1,11 @@
# include "hangman.h"
#include "hangman.h"
/*
* prman:
* Print out the man appropriately for the give number
* of incorrect guesses.
*/
void
prman()
{
register int i;

View File

@@ -1,9 +1,10 @@
# include "hangman.h"
#include "hangman.h"
/*
* prword:
* Print out the current state of the word
*/
void
prword()
{
move(KNOWNY, KNOWNX + sizeof "Word: ");

View File

@@ -1,9 +1,13 @@
# include "hangman.h"
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "hangman.h"
/*
* setup:
* Set up the strings on the screen.
*/
void
setup()
{
register char **sp;
@@ -24,7 +28,7 @@ setup()
addstr(*sp);
}
srand(time(NULL) + getpid());
srandom(time(NULL) + getpid());
if ((Dict = fopen(DICT, "r")) == NULL) {
perror(DICT);
endwin();

View File

@@ -11,20 +11,29 @@
*
*/
int
mvprintw(y, x, fmt, args)
reg int y, x;
char *fmt;
int args;
mvprintw(int y, int x, char *fmt, ...)
{
return move(y, x) == OK ? _sprintw(stdscr, fmt, &args) : ERR;
va_list args;
int ret;
if (move(y, x) != OK)
return ERR;
va_start (args, fmt);
ret = _sprintw (stdscr, fmt, args);
va_end (args);
return ret;
}
int
mvwprintw(win, y, x, fmt, args)
reg WINDOW *win;
reg int y, x;
char *fmt;
int args;
mvwprintw(WINDOW *win, int y, int x, char *fmt, ...)
{
return wmove(win, y, x) == OK ? _sprintw(win, fmt, &args) : ERR;
va_list args;
int ret;
if (wmove(win, y, x) != OK)
return ERR;
va_start (args, fmt);
ret = _sprintw (win, fmt, args);
va_end (args);
return ret;
}