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

📄 canfield.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
	dlength = length[des] + 1;	slength = length[sour];	if (slength == tabrow)		printcard(pilemap[des], dlength, tableau[sour]);	else 		while (slength != tabrow - 1) {			tempcard = tableau[sour];			for (i=1; i<=slength-tabrow; i++)			    tempcard = tempcard->next;			printcard(pilemap[des], dlength, tempcard);			slength--;			dlength++;		}}/* * procedure to move from the tableau to the tableau */tabtotab(sour, des)	register int sour, des;{	struct cardtype *temp;	if (notempty(tableau[sour])) {		if (tabok(bottom[sour], des)) {			tabprint(sour, des);			temp = bottom[sour];			bottom[sour] = NIL;			if (bottom[des] == NIL)				bottom[des] = temp;			temp->next = tableau[des];			tableau[des] = tableau[sour];			tableau[sour] = NIL;			length[des] = length[des] + (length[sour] - (tabrow - 1));			length[sour] = tabrow - 1;			timesthru = 0;		} else 			destinerror();	}}/* * functions to see if the card can go onto the foundation */boolrankhigher(cp, let)	struct cardtype *cp;{	if (found[let]->rank == King)		if (cp->rank == Ace)			return(TRUE);		else 			return(FALSE);	else if (cp->rank - 1 == found[let]->rank)		return(TRUE);	else 		return(FALSE);}/* * function to determine if two cards are the same suit */samesuit(cp, let)	struct cardtype *cp;{	if (cp->suit == found[let]->suit)		return (TRUE);	else 		return (FALSE);}/* * procedure to move a card to the correct foundation pile */movetofound(cp, source)	struct cardtype **cp;{	tempbase = 0;	mtfdone = FALSE;	if (notempty(*cp)) {		do {			if (found[tempbase] != NIL)				if (rankhigher(*cp, tempbase)				    && samesuit(*cp, tempbase)) {					if (*cp == stock)						mtforigin = stk;					else if (*cp == talon)						mtforigin = tal;					else						mtforigin = tab;					transit(cp, &found[tempbase]);					printcard(pilemap[tempbase],						foundrow, found[tempbase]);					timesthru = 0;					if (mtforigin == stk) {						usedstock();						printcard(stockcol, stockrow, stock);					} else if (mtforigin == tal) {						usedtalon();						printcard(taloncol, talonrow, talon);					} else {						removecard(pilemap[source], length[source]);						length[source]--;					}					cardsoff++;					if (infullgame) {						this.wins += valuepercardup;						game.wins += valuepercardup;						total.wins += valuepercardup;					}					mtfdone = TRUE;				} else					tempbase++;			else 				tempbase++;		} while ((tempbase != 4) && !mtfdone);		if (!mtfdone)			destinerror();	}}/* * procedure to get a command */getcmd(row, col, cp)	int row, col;	char *cp;{	char cmd[2], ch;	int i;	i = 0;	move(row, col);	printw("%-24s", cp);	col += 1 + strlen(cp);	move(row, col);	refresh();	do {		ch = getch() & 0177;		if (ch >= 'A' && ch <= 'Z')			ch += ('a' - 'A');		if (ch == '\f') {			wrefresh(curscr);			refresh();		} else if (i >= 2 && ch != erasechar() && ch != killchar()) {			if (ch != '\n' && ch != '\r' && ch != ' ')				write(1, "\007", 1);		} else if (ch == erasechar() && i > 0) {			printw("\b \b");			refresh();			i--;		} else if (ch == killchar() && i > 0) {			while (i > 0) {				printw("\b \b");				i--;			}			refresh();		} else if (ch == '\032') {	/* Control-Z */			suspend();			move(row, col + i);			refresh();		} else if (isprint(ch)) {			cmd[i++] = ch;			addch(ch);			refresh();		}	} while (ch != '\n' && ch != '\r' && ch != ' ');	srcpile = cmd[0];	destpile = cmd[1];}/* * Suspend the game (shell escape if no process control on system) */suspend(){#ifndef SIGTSTP	char *sh;#endif	updatebettinginfo();	move(21, 0);	refresh();	if (dbfd != -1) {		lseek(dbfd, uid * sizeof(struct betinfo), 0);		write(dbfd, (char *)&total, sizeof(total));	}	kill(getpid(), SIGTSTP);	raw();	noecho();}/* * procedure to evaluate and make the specific moves */movecard(){	int source, dest;	char osrcpile, odestpile;	done = FALSE;	errmsg = FALSE;	do {		if (talon == NIL && hand != NIL)			movetotalon();		if (cardsoff == 52) {			refresh();			srcpile = 'q';		} else if (!startedgame) {			move(msgrow, msgcol);			errmsg = TRUE;			switch (34 - taloncnt - cinhand) {			default:				errmsg = FALSE;				break;			case 1:				printw("One card used from talon  ");				break;			case 2:				printw("Two cards used from talon ");				break;			case 3:				printw(">3< cards used from talon ");				break;			}			getcmd(moverow, movecol, "Move:");		} else			getcmd(moverow, movecol, "Move:");		clearmsg();		if (srcpile >= '1' && srcpile <= '4')			source = (int) (srcpile - '1');		if (destpile >= '1' && destpile <= '4')			dest = (int) (destpile - '1');		if (!startedgame &&		    (srcpile == 't' || srcpile == 's' || srcpile == 'h' ||		     srcpile == '1' || srcpile == '2' || srcpile == '3' ||		     srcpile == '4')) {			startedgame = TRUE;			osrcpile = srcpile;			odestpile = destpile;			if (status != BETTINGBOX)				srcpile = 'y';			else do {				getcmd(moverow, movecol, "Inspect game?");			} while (srcpile != 'y' && srcpile != 'n');			if (srcpile == 'n') {				srcpile = 'q';			} else {				this.inspection += costofinspection;				game.inspection += costofinspection;				total.inspection += costofinspection;				srcpile = osrcpile;				destpile = odestpile;			}		}		switch (srcpile) {			case 't':				if (destpile == 'f' || destpile == 'F')					movetofound(&talon, source);				else if (destpile >= '1' && destpile <= '4')					simpletableau(&talon, dest);				else					dumberror();				break;			case 's':				if (destpile == 'f' || destpile == 'F')					movetofound(&stock, source);				else if (destpile >= '1' && destpile <= '4')					simpletableau(&stock, dest);				else dumberror();				break;			case 'h':				if (destpile != 't' && destpile != 'T') {					dumberror();					break;				}				if (infullgame) {					movetotalon();					break;				}				if (status == BETTINGBOX) {					do {						getcmd(moverow, movecol,							"Buy game?");					} while (srcpile != 'y' &&						 srcpile != 'n');					if (srcpile == 'n') {						showcards();						done = TRUE;						break;					}				}				infullgame = TRUE;				this.wins += valuepercardup * cardsoff;				game.wins += valuepercardup * cardsoff;				total.wins += valuepercardup * cardsoff;				this.game += costofgame;				game.game += costofgame;				total.game += costofgame;				movetotalon();				break;			case 'q':				showcards();				done = TRUE;				break;			case 'b':				printtopbettingbox();				printbottombettingbox();				status = BETTINGBOX;				break;			case 'x':				clearabovemovebox();				clearbelowmovebox();				status = NOBOX;				break;			case 'i':				printtopinstructions();				printbottominstructions();				status = INSTRUCTIONBOX;				break;			case 'c':				Cflag = !Cflag;				if (Cflag)					showstat();				else					clearstat();				break;			case '1': case '2': case '3': case '4':				if (destpile == 'f' || destpile == 'F')					movetofound(&tableau[source], source);				else if (destpile >= '1' && destpile <= '4')					tabtotab(source, dest);				else dumberror();				break;			default:				dumberror();		}		fndbase(&stock, stockcol, stockrow);		fndbase(&talon, taloncol, talonrow);		updatebettinginfo();	} while (!done);}char *basicinstructions[] = {	"Here are brief instuctions to the game of Canfield:\n\n",	"     If you have never played solitaire before, it is recom-\n",	"mended  that  you  consult  a solitaire instruction book. In\n",	"Canfield, tableau cards may be built on each other  downward\n",	"in  alternate colors. An entire pile must be moved as a unit\n",	"in building. Top cards of the piles are available to be able\n",	"to be played on foundations, but never into empty spaces.\n\n",	"     Spaces must be filled from the stock. The top  card  of\n",	"the  stock  also is available to be played on foundations or\n",	"built on tableau piles. After the stock  is  exhausted,  ta-\n",	"bleau spaces may be filled from the talon and the player may\n",	"keep them open until he wishes to use them.\n\n",	"     Cards are dealt from the hand to the  talon  by  threes\n",	"and  this  repeats until there are no more cards in the hand\n",	"or the player quits. To have cards dealt onto the talon  the\n",	"player  types  'ht'  for his move. Foundation base cards are\n",	"also automatically moved to the foundation when they  become\n",	"available.\n\n",	"push any key when you are finished: ",	0 };char *bettinginstructions[] = {	"     The rules for betting are  somewhat  less  strict  than\n",	"those  used in the official version of the game. The initial\n",	"deal costs $13. You may quit at this point  or  inspect  the\n",	"game.  Inspection  costs  $13 and allows you to make as many\n",	"moves as is possible without moving any cards from your hand\n",	"to  the  talon.  (the initial deal places three cards on the\n",	"talon; if all these cards are  used,  three  more  are  made\n",	"available)  Finally, if the game seems interesting, you must\n",	"pay the final installment of $26.  At  this  point  you  are\n",	"credited  at the rate of $5 for each card on the foundation;\n",	"as the game progresses you are credited  with  $5  for  each\n",	"card  that is moved to the foundation.  Each run through the\n",	"hand after the first costs $5.  The  card  counting  feature\n",	"costs  $1  for  each unknown card that is identified. If the\n",	"information is toggled on, you are only  charged  for  cards\n",	"that  became  visible  since it was last turned on. Thus the\n",	"maximum cost of information is $34.  Playing time is charged\n",	"at a rate of $1 per minute.\n\n",	"push any key when you are finished: ",	0 };/* * procedure to printout instructions */instruct(){	register char **cp;	move(originrow, origincol);	printw("This is the game of solitaire called Canfield.  Do\n");	printw("you want instructions for the game?");	do {		getcmd(originrow + 3, origincol, "y or n?");	} while (srcpile != 'y' && srcpile != 'n');	if (srcpile == 'n')		return;	clear();	for (cp = basicinstructions; *cp != 0; cp++)		printw(*cp);	refresh();	getch();	clear();	move(originrow, origincol);	printw("Do you want instructions for betting?");	do {		getcmd(originrow + 2, origincol, "y or n?");	} while (srcpile != 'y' && srcpile != 'n');	if (srcpile == 'n')		return;	clear();	for (cp = bettinginstructions; *cp != 0; cp++)		printw(*cp);	refresh();	getch();}/* * procedure to initialize the game */initall(){	int i;	srandom(getpid());	time(&acctstart);	initdeck(deck);	uid = getuid();	if (uid < 0)		uid = 0;	dbfd = open(_PATH_SCORE, 2);	if (dbfd < 0)		return;	i = lseek(dbfd, uid * sizeof(struct betinfo), 0);	if (i < 0) {		close(dbfd);		dbfd = -1;		return;	}	i = read(dbfd, (char *)&total, sizeof(total));	if (i < 0) {		close(dbfd);		dbfd = -1;		return;	}}/* * procedure to end the game */boolfinish(){	int row, col;	if (cardsoff == 52) {		getcmd(moverow, movecol, "Hit return to exit");		clear();		refresh();		move(originrow, origincol);		printw("CONGRATULATIONS!\n");		printw("You won the game. That is a feat to be proud of.\n");		row = originrow + 5;		col = origincol;	} else {		move(msgrow, msgcol);		printw("You got %d card", cardsoff);		if (cardsoff > 1)			printw("s");		printw(" off    ");		move(msgrow, msgcol);		row = moverow;		col = movecol;	}	do {		getcmd(row, col, "Play again (y or n)?");	} while (srcpile != 'y' && srcpile != 'n');	errmsg = TRUE;	clearmsg();	if (srcpile == 'y')		return (FALSE);	else		return (TRUE);}/* * procedure to clean up and exit */voidcleanup(){	total.thinktime += 1;	status = NOBOX;	updatebettinginfo();	if (dbfd != -1) {		lseek(dbfd, uid * sizeof(struct betinfo), 0);		write(dbfd, (char *)&total, sizeof(total));		close(dbfd);	}	clear();	move(22,0);	refresh();	endwin();	exit(0);	/* NOTREACHED */}/* * Field an interrupt. */voidaskquit(){	move(msgrow, msgcol);	printw("Really wish to quit?    ");	do {		getcmd(moverow, movecol, "y or n?");	} while (srcpile != 'y' && srcpile != 'n');	clearmsg();	if (srcpile == 'y')		cleanup();	signal(SIGINT, askquit);}/* * Can you tell that this used to be a Pascal program? */main(argc, argv)	int argc;	char *argv[];{#ifdef MAXLOAD	double vec[3];	loadav(vec);	if (vec[2] >= MAXLOAD) {		puts("The system load is too high.  Try again later.");		exit(0);	}#endif	signal(SIGINT, askquit);	signal(SIGHUP, cleanup);	signal(SIGTERM, cleanup);	initscr();	raw();	noecho();	initall();	instruct();	makeboard();	for (;;) {		startgame();		movecard();		if (finish())			break;		if (cardsoff == 52)			makeboard();		else			cleanupboard();	}	cleanup();	/* NOTREACHED */}

⌨️ 快捷键说明

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