⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 boggle.c

📁 操作系统SunOS 4.1.3版本的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
#ifndef lintstatic	char sccsid[] = "@(#)boggle.c 1.1 92/07/30 Copyr 1985 Sun Micro";#endif/* * Copyright (c) 1985 by Sun Microsystems, Inc. *//* * main.c: main routine and general windows routines */#include <suntool/tool_hs.h>#include <suntool/menu.h>#include <suntool/wmgr.h>#include <sunwindow/cms.h>#include <sunwindow/win_ioctl.h>#include <sys/wait.h>#include <sys/ioctl.h>#include <ctype.h>#include <stdio.h>#include "defs.h"#include "images.h"static short icon_data[256] = {#include <images/boggle.icon>};mpr_static(boggle_pr, 64, 64, 1, icon_data);struct icon boggle_icon = {	TOOL_ICONWIDTH, TOOL_ICONHEIGHT, 0,	{0, 0, TOOL_ICONWIDTH, TOOL_ICONHEIGHT},	&boggle_pr, {0, 0, 0, 0}, 0, 0, 0};struct timeval		flashdelay = {0, 10000};static			bog_sigwinch(),			bog_selected(),			sigwinch_handler();static char		tool_name[] = "boggletool";#define DEFDICT	"/usr/games/boggledict"		/* dictionary */char board[16];int setupboard = 0;char *cubeletters[16] = {	/* the boggle cubes */	"forixb", "moqabj", "gurilw", "setupl",	"cmpdae", "acitao", "slcrae", "romash",	"nodesw", "hefiye", "onudtk", "tevign",	"anedvz", "pinesh", "abilyt", "gkyleu"};#define BOG_RESTARTTIMER	(caddr_t)1	/* restart the timer */#define BOG_EXIT		(caddr_t)2	/* quit bogtool */#define BOG_RESTARTGAME		(caddr_t)3	/* restart the game */#define BOG_GIVEUP		(caddr_t)4	/* human gives up */struct menuitem bogmenu_items[] = {	MENU_IMAGESTRING,	"Restart Game",		BOG_RESTARTGAME,	MENU_IMAGESTRING,	"Restart Timer",	BOG_RESTARTTIMER,	MENU_IMAGESTRING,	"Give Up",		BOG_GIVEUP,	MENU_IMAGESTRING,	"Quit",			BOG_EXIT};struct menu	b_menu = {	MENU_IMAGESTRING, "Boggle",	sizeof(bogmenu_items) / sizeof(struct menuitem),	bogmenu_items, NULL, NULL};struct menu *bog_menu = &b_menu;boggletool_main(argc, argv)int argc;char **argv;{	char **tool_attrs = NULL, *getenv();	int quit();	long atol();	extern sand_delay;	if (tool_parse_all(&argc, argv, &tool_attrs, tool_name) == -1) {		tool_usage(tool_name);		exit(1);	}	dictionary = DEFDICT;	while (--argc) {		++argv;		if (**argv == '+') {			while (*(*argv)++ == '+')				reuse++;		} else if (isdigit(**argv)) {			sand_delay = compute_delay(atol(*argv));			if (sand_delay <= 0) {				fprintf(stderr, "Playing time cannot be negative!\n");				exit(1);			}		} else if (isalpha(**argv)) {			if (strlen(*argv) != 16)				usage();			strncpy(board, *argv, 16);			setupboard = 1;		} else if (**argv == '-') {			if (*(++(*argv)) == 'd') {				if (*(++(*argv)) != '\0') {					dictionary = *argv;				} else if (argc > 1) {					dictionary = *(++argv);					argc--;				} else {					usage();				}			} else {				usage();			}		} else {			usage();		}	}	tool = tool_make(WIN_NAME_STRIPE, 1,			WIN_LABEL, tool_name,			WIN_ATTR_LIST, tool_attrs,			WIN_ICON, &boggle_icon, 0);	if (tool == NULL) {		fprintf(stderr, "Couldn't create tool!\n");		exit(1);	}	tool_free_attribute_list(tool_attrs);	state = INSTRUCTIONS;	initboggle();	bogsw = tool_createsubwindow(tool, "", -1, -1);	initbogsw();	signal(SIGWINCH, sigwinch_handler);	signal(SIGINT, quit);	signal(SIGQUIT, quit);	signal(SIGTERM, quit);	tool_install(tool);	tool_select(tool, 0);	signal(SIGWINCH, SIG_IGN);	tool_destroy(tool);	killchild();	exit(0);}usage(){	fprintf(stderr, "Usage: bogtool [+[+]] [playing-time-in-minutes] [16-letter-string] -d dictionary\n");	exit(1);}killchild(){	if (childpid > 0) {		kill(childpid, SIGINT);		while (wait(0) > 0)			;		childpid = 0;		close(pfd[0]);		close(pfd[1]);	}}staticquit(){	tool_done(tool);}#define TOOL_ADDWIDTH	(2 * tool_borderwidth(tool))#define TOOL_ADDHEIGHT	(tool_borderwidth(tool) + tool_stripeheight(tool))staticsigwinch_handler(){	int miny;	struct rect r;	if ((tool->tl_flags & TOOL_ICONIC) == 0 && (win_getuserflags(tool->tl_windowfd) & WMGR_ICONIC) == 0) {		miny = DISPLAY_BOTTOM;		win_getrect(tool->tl_windowfd, &r);		if (miny % fontheight != 0)			miny += fontheight - (miny % fontheight);		if (r.r_width < DISPLAY_RIGHT + TOOL_ADDWIDTH) {			if (r.r_height < DISPLAY_BOTTOM + miny + fontheight + TOOL_ADDHEIGHT) {				r.r_width = DISPLAY_RIGHT + BOARD_SPACING + 10 * fontwidth + TOOL_ADDWIDTH;				r.r_height = DISPLAY_BOTTOM + BOARD_SPACING + TOOL_ADDHEIGHT;				win_setrect(tool->tl_windowfd, &r);				return;			} else {				r.r_width = DISPLAY_RIGHT + TOOL_ADDWIDTH;				win_setrect(tool->tl_windowfd, &r);				return;			}		} else if (r.r_height < DISPLAY_BOTTOM + TOOL_ADDHEIGHT) {			if (r.r_width < DISPLAY_RIGHT + BOARD_SPACING + 10 * fontwidth + TOOL_ADDWIDTH) {				r.r_width = DISPLAY_RIGHT + BOARD_SPACING + 10 * fontwidth + TOOL_ADDWIDTH;				r.r_height = DISPLAY_BOTTOM + BOARD_SPACING + TOOL_ADDHEIGHT;				win_setrect(tool->tl_windowfd, &r);				return;			} else {				r.r_height = DISPLAY_BOTTOM + BOARD_SPACING + TOOL_ADDHEIGHT;				win_setrect(tool->tl_windowfd, &r);				return;			}		}	}	tool_sigwinch(tool);}initbogsw(){	struct inputmask im;	register x, y, triwidth, triheight;	bogwin = pw_open(bogsw->ts_windowfd);	bogsw->ts_io.tio_handlesigwinch = bog_sigwinch;	bogsw->ts_io.tio_selected = bog_selected;	input_imnull(&im);	win_setinputcodebit(&im, MS_LEFT);	win_setinputcodebit(&im, MS_MIDDLE);	win_setinputcodebit(&im, MS_RIGHT);	win_setinputcodebit(&im, LOC_MOVEWHILEBUTDOWN);	win_setinputcodebit(&im, LOC_WINEXIT);	im.im_flags |= IM_ASCII;	im.im_flags |= IM_NEGEVENT;	win_setinputmask(bogsw->ts_windowfd, &im, 0, WIN_NULLLINK);	bogwin->pw_prretained = NULL;	if ((bogfont = pw_pfsysopen()) == NULL) {		fprintf(stderr, "Couldn't open font!\n");		exit(1);	}	tri_left_pr = mem_create(fontwidth, fontheight, 1);	tri_right_pr = mem_create(fontwidth, fontheight, 1);	triheight = make_even(0.625 * (double)fontheight);	triwidth = triheight / 2;	for (x = 0; x < triwidth; x++) {		for (y = x; y <= triheight - x; y++) {			pr_put(tri_right_pr, x + 1, y + (fontheight - triheight) / 2, 1);			pr_put(tri_left_pr, triwidth - x, y + (fontheight - triheight) / 2, 1);		}	}}make_even(c)double c;{	int lower;	lower = (int) c;	if (lower % 2 != 0)		lower--;	if (c - (double)lower <= (double)(lower + 2) - c) {		return(lower);	} else {		return(lower + 2);	}}startdisplay(){	freehumanwords();	cwordlen = 0;	scrolled = 0;}startboggle(draw)int draw;{	genboard();	startdisplay();	starttimer();	startwordfind();	if (draw)		drawbog();}staticbog_sigwinch(){	struct rect r;	win_getsize(bogsw->ts_windowfd, &r);	if (!rect_equal(&r, &bogrect)) {		pw_damaged(bogwin);		pw_donedamaged(bogwin);		bogwidth = r.r_width;		bogheight = r.r_height;		if (bogwin->pw_prretained != NULL)			pr_destroy(bogwin->pw_prretained);		bogwin->pw_prretained = mem_create(bogwidth, bogheight, 1);		switch (state) {		case INSTRUCTIONS:			askinstr();			break;		case CONFIRM_INSTR:			printinstr();			break;		case PLAYING:			drawbog();			break;		case GAMEOVER:			pw_write(bogwin, 0, 0, bogwidth, bogheight, PIX_SRC, 0, 0, 0);			drawboard();			drawtimer();			displaylists();			break;		}	} else {		pw_damaged(bogwin);		pw_repairretained(bogwin);		pw_donedamaged(bogwin);	}}staticbog_selected(data, ibits, obits, ebits, timer)caddr_t data;int *ibits, *obits, *ebits;struct timeval **timer;{	struct inputevent ie;	if (*ibits & (1 << bogsw->ts_windowfd)) {		if (input_readevent(bogsw->ts_windowfd, &ie) < 0) {			fprintf(stderr, "input_readevent failed!\n");			exit(1);		}		process_input(&ie);	}	if (childactive && state == PLAYING) {		if (*ibits && (1 << fromchild)) {			if (!readchild()) {				close(pfd[0]);				close(pfd[1]);				childactive = 0;			}		}	}	if (state == PLAYING) {		if (movesand()) {			stoptimer = 1;			timeisup(1);		}	}	if (stoptimer) {		timer = NULL;		stoptimer = 0;	}	*ibits = *obits = *ebits = 0;	if (setchildbits) {		childactive++;		setchildbits = 0;	}	if (childactive && state == PLAYING)		*ibits |= (1 << fromchild);	*ibits |= (1 << bogsw->ts_windowfd);}process_input(ie)struct inputevent *ie;{	int x, y;	char *word, *mouseonword();	if (isascii(ie->ie_code)) {		if (win_inputnegevent(ie))			return;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -