📄 canfield.c
字号:
/* * Copyright (c) 1980, 1993 * The Regents of the University of California. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */#ifndef lintstatic char copyright[] ="@(#) Copyright (c) 1980, 1993\n\ The Regents of the University of California. All rights reserved.\n";#endif /* not lint */#ifndef lintstatic char sccsid[] = "@(#)canfield.c 8.1 (Berkeley) 5/31/93";#endif /* not lint *//* * The canfield program * * Authors: * Originally written: Steve Levine * Converted to use curses and debugged: Steve Feldman * Card counting: Kirk McKusick and Mikey Olson * User interface cleanups: Eric Allman and Kirk McKusick * Betting by Kirk McKusick */#include <sys/types.h>#include <curses.h>#include <ctype.h>#include <signal.h>#include <termios.h>#include "pathnames.h"#define decksize 52#define originrow 0#define origincol 0#define basecol 1#define boxcol 42#define tboxrow 2#define bboxrow 17#define movecol 43#define moverow 16#define msgcol 43#define msgrow 15#define titlecol 30#define titlerow 0#define sidecol 1#define ottlrow 6#define foundcol 11#define foundrow 3#define stockcol 2#define stockrow 8#define fttlcol 10#define fttlrow 1#define taloncol 2#define talonrow 13#define tabrow 8#define ctoprow 21#define cbotrow 23#define cinitcol 14#define cheightcol 1#define cwidthcol 4#define handstatrow 21#define handstatcol 7#define talonstatrow 22#define talonstatcol 7#define stockstatrow 23#define stockstatcol 7#define Ace 1#define Jack 11#define Queen 12#define King 13#define atabcol 11#define btabcol 18#define ctabcol 25#define dtabcol 32#define spades 's'#define clubs 'c'#define hearts 'h'#define diamonds 'd'#define black 'b'#define red 'r'#define stk 1#define tal 2#define tab 3#define INCRHAND(row, col) {\ row -= cheightcol;\ if (row < ctoprow) {\ row = cbotrow;\ col += cwidthcol;\ }\}#define DECRHAND(row, col) {\ row += cheightcol;\ if (row > cbotrow) {\ row = ctoprow;\ col -= cwidthcol;\ }\}struct cardtype { char suit; char color; bool visible; bool paid; int rank; struct cardtype *next;};#define NIL ((struct cardtype *) -1)struct cardtype *deck[decksize];struct cardtype cards[decksize];struct cardtype *bottom[4], *found[4], *tableau[4];struct cardtype *talon, *hand, *stock, *basecard;int length[4];int cardsoff, base, cinhand, taloncnt, stockcnt, timesthru;char suitmap[4] = {spades, clubs, hearts, diamonds};char colormap[4] = {black, black, red, red};char pilemap[4] = {atabcol, btabcol, ctabcol, dtabcol};char srcpile, destpile;int mtforigin, tempbase;int coldcol, cnewcol, coldrow, cnewrow;bool errmsg, done;bool mtfdone, Cflag = FALSE;#define INSTRUCTIONBOX 1#define BETTINGBOX 2#define NOBOX 3int status = INSTRUCTIONBOX;int uid;/* * Basic betting costs */#define costofhand 13#define costofinspection 13#define costofgame 26#define costofrunthroughhand 5#define costofinformation 1#define secondsperdollar 60#define maxtimecharge 3#define valuepercardup 5/* * Variables associated with betting */struct betinfo { long hand; /* cost of dealing hand */ long inspection; /* cost of inspecting hand */ long game; /* cost of buying game */ long runs; /* cost of running through hands */ long information; /* cost of information */ long thinktime; /* cost of thinking time */ long wins; /* total winnings */ long worth; /* net worth after costs */};struct betinfo this, game, total;bool startedgame = FALSE, infullgame = FALSE;time_t acctstart;int dbfd = -1;/* * The following procedures print the board onto the screen using the * addressible cursor. The end of these procedures will also be * separated from the rest of the program. * * procedure to set the move command box */movebox(){ switch (status) { case BETTINGBOX: printtopbettingbox(); break; case NOBOX: clearabovemovebox(); break; case INSTRUCTIONBOX: printtopinstructions(); break; } move(moverow, boxcol); printw("| |"); move(msgrow, boxcol); printw("| |"); switch (status) { case BETTINGBOX: printbottombettingbox(); break; case NOBOX: clearbelowmovebox(); break; case INSTRUCTIONBOX: printbottominstructions(); break; } refresh();}/* * print directions above move box */printtopinstructions(){ move(tboxrow, boxcol); printw("*----------------------------------*"); move(tboxrow + 1, boxcol); printw("| MOVES |"); move(tboxrow + 2, boxcol); printw("|s# = stock to tableau |"); move(tboxrow + 3, boxcol); printw("|sf = stock to foundation |"); move(tboxrow + 4, boxcol); printw("|t# = talon to tableau |"); move(tboxrow + 5, boxcol); printw("|tf = talon to foundation |"); move(tboxrow + 6, boxcol); printw("|## = tableau to tableau |"); move(tboxrow + 7, boxcol); printw("|#f = tableau to foundation |"); move(tboxrow + 8, boxcol); printw("|ht = hand to talon |"); move(tboxrow + 9, boxcol); printw("|c = toggle card counting |"); move(tboxrow + 10, boxcol); printw("|b = present betting information |"); move(tboxrow + 11, boxcol); printw("|q = quit to end the game |"); move(tboxrow + 12, boxcol); printw("|==================================|");}/* * Print the betting box. */printtopbettingbox(){ move(tboxrow, boxcol); printw("*----------------------------------*"); move(tboxrow + 1, boxcol); printw("|Costs Hand Game Total |"); move(tboxrow + 2, boxcol); printw("| Hands |"); move(tboxrow + 3, boxcol); printw("| Inspections |"); move(tboxrow + 4, boxcol); printw("| Games |"); move(tboxrow + 5, boxcol); printw("| Runs |"); move(tboxrow + 6, boxcol); printw("| Information |"); move(tboxrow + 7, boxcol); printw("| Think time |"); move(tboxrow + 8, boxcol); printw("|Total Costs |"); move(tboxrow + 9, boxcol); printw("|Winnings |"); move(tboxrow + 10, boxcol); printw("|Net Worth |"); move(tboxrow + 11, boxcol); printw("|Return |"); move(tboxrow + 12, boxcol); printw("|==================================|");}/* * clear info above move box */clearabovemovebox(){ int i; for (i = 0; i <= 11; i++) { move(tboxrow + i, boxcol); printw(" "); } move(tboxrow + 12, boxcol); printw("*----------------------------------*");}/* * print instructions below move box */printbottominstructions(){ move(bboxrow, boxcol); printw("|Replace # with the number of the |"); move(bboxrow + 1, boxcol); printw("|tableau you want. |"); move(bboxrow + 2, boxcol); printw("*----------------------------------*");}/* * print betting information below move box */printbottombettingbox(){ move(bboxrow, boxcol); printw("|x = toggle information box |"); move(bboxrow + 1, boxcol); printw("|i = list playing instructions |"); move(bboxrow + 2, boxcol); printw("*----------------------------------*");}/* * clear info below move box */clearbelowmovebox(){ int i; move(bboxrow, boxcol); printw("*----------------------------------*"); for (i = 1; i <= 2; i++) { move(bboxrow + i, boxcol); printw(" "); }}/* * procedure to put the board on the screen using addressable cursor */makeboard(){ clear(); refresh(); move(titlerow, titlecol); printw("=-> CANFIELD <-="); move(fttlrow, fttlcol); printw("foundation"); move(foundrow - 1, fttlcol); printw("=---= =---= =---= =---="); move(foundrow, fttlcol); printw("| | | | | | | |"); move(foundrow + 1, fttlcol); printw("=---= =---= =---= =---="); move(ottlrow, sidecol); printw("stock tableau"); move(stockrow - 1, sidecol); printw("=---="); move(stockrow, sidecol); printw("| |"); move(stockrow + 1, sidecol); printw("=---="); move(talonrow - 2, sidecol); printw("talon"); move(talonrow - 1, sidecol); printw("=---="); move(talonrow, sidecol); printw("| |"); move(talonrow + 1, sidecol); printw("=---="); move(tabrow - 1, atabcol); printw("-1- -2- -3- -4-"); movebox();}/* * clean up the board for another game */cleanupboard(){ int cnt, row, col; struct cardtype *ptr; if (Cflag) { clearstat(); for(ptr = stock, row = stockrow; ptr != NIL; ptr = ptr->next, row++) { move(row, sidecol); printw(" "); } move(row, sidecol); printw(" "); move(stockrow + 1, sidecol); printw("=---="); move(talonrow - 2, sidecol); printw("talon"); move(talonrow - 1, sidecol); printw("=---="); move(talonrow + 1, sidecol); printw("=---="); } move(stockrow, sidecol); printw("| |"); move(talonrow, sidecol); printw("| |"); move(foundrow, fttlcol); printw("| | | | | | | |"); for (cnt = 0; cnt < 4; cnt++) { switch(cnt) { case 0: col = atabcol; break; case 1: col = btabcol; break; case 2: col = ctabcol; break; case 3: col = dtabcol; break; } for(ptr = tableau[cnt], row = tabrow; ptr != NIL; ptr = ptr->next, row++) removecard(col, row); }}/* * procedure to create a deck of cards */initdeck(deck) struct cardtype *deck[];{ int i; int scnt; char s; int r; i = 0; for (scnt=0; scnt<4; scnt++) { s = suitmap[scnt]; for (r=Ace; r<=King; r++) { deck[i] = &cards[i]; cards[i].rank = r; cards[i].suit = s; cards[i].color = colormap[scnt]; cards[i].next = NIL; i++; } }}/* * procedure to shuffle the deck */shuffle(deck) struct cardtype *deck[];{ int i,j; struct cardtype *temp; for (i=0; i<decksize; i++) { deck[i]->visible = FALSE; deck[i]->paid = FALSE; } for (i = decksize-1; i>=0; i--) { j = random() % decksize; if (i != j) { temp = deck[i]; deck[i] = deck[j]; deck[j] = temp; } }}/* * procedure to remove the card from the board */removecard(a, b){ move(b, a); printw(" ");}/* * procedure to print the cards on the board */printrank(a, b, cp, inverse) struct cardtype *cp; bool inverse;{ move(b, a); if (cp->rank != 10) addch(' '); if (inverse) standout(); switch (cp->rank) { case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: printw("%d", cp->rank); break; case Ace: addch('A'); break; case Jack: addch('J'); break; case Queen: addch('Q'); break; case King: addch('K'); } if (inverse) standend();}/* * procedure to print out a card */printcard(a, b, cp) int a,b; struct cardtype *cp;{ if (cp == NIL) removecard(a, b); else if (cp->visible == FALSE) { move(b, a); printw(" ? "); } else { bool inverse = (cp->suit == 'd' || cp->suit == 'h'); printrank(a, b, cp, inverse); if (inverse) standout(); addch(cp->suit); if (inverse) standend(); }}/* * procedure to move the top card from one location to the top * of another location. The pointers always point to the top * of the piles. */transit(source, dest) struct cardtype **source, **dest;{ struct cardtype *temp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -