Home | History | Annotate | Line # | Download | only in gomoku
bdisp.c revision 1.14
      1  1.14  dholland /*	$NetBSD: bdisp.c,v 1.14 2010/03/29 03:51:55 dholland Exp $	*/
      2   1.4       cgd 
      3   1.1       tls /*
      4   1.1       tls  * Copyright (c) 1994
      5   1.1       tls  *	The Regents of the University of California.  All rights reserved.
      6   1.1       tls  *
      7   1.1       tls  * This code is derived from software contributed to Berkeley by
      8   1.1       tls  * Ralph Campbell.
      9   1.1       tls  *
     10   1.1       tls  * Redistribution and use in source and binary forms, with or without
     11   1.1       tls  * modification, are permitted provided that the following conditions
     12   1.1       tls  * are met:
     13   1.1       tls  * 1. Redistributions of source code must retain the above copyright
     14   1.1       tls  *    notice, this list of conditions and the following disclaimer.
     15   1.1       tls  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1       tls  *    notice, this list of conditions and the following disclaimer in the
     17   1.1       tls  *    documentation and/or other materials provided with the distribution.
     18   1.8       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1       tls  *    may be used to endorse or promote products derived from this software
     20   1.1       tls  *    without specific prior written permission.
     21   1.1       tls  *
     22   1.1       tls  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1       tls  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1       tls  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1       tls  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1       tls  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1       tls  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1       tls  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1       tls  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1       tls  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1       tls  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1       tls  * SUCH DAMAGE.
     33   1.1       tls  */
     34   1.1       tls 
     35   1.5     lukem #include <sys/cdefs.h>
     36   1.1       tls #ifndef lint
     37   1.2       tls #if 0
     38   1.1       tls static char sccsid[] = "@(#)bdisp.c	8.2 (Berkeley) 5/3/95";
     39   1.2       tls #else
     40  1.14  dholland __RCSID("$NetBSD: bdisp.c,v 1.14 2010/03/29 03:51:55 dholland Exp $");
     41   1.2       tls #endif
     42   1.1       tls #endif /* not lint */
     43   1.1       tls 
     44   1.5     lukem #include <curses.h>
     45   1.5     lukem #include <string.h>
     46   1.9  drochner #include <stdlib.h>
     47  1.13  dholland #include <err.h>
     48   1.1       tls #include "gomoku.h"
     49   1.1       tls 
     50   1.1       tls #define	SCRNH		24		/* assume 24 lines for the moment */
     51   1.1       tls #define	SCRNW		80		/* assume 80 chars for the moment */
     52   1.1       tls 
     53   1.1       tls static	int	lastline;
     54   1.1       tls static	char	pcolor[] = "*O.?";
     55   1.1       tls 
     56   1.7  christos extern int interactive;
     57   1.7  christos extern char *plyr[];
     58   1.7  christos 
     59   1.1       tls /*
     60   1.1       tls  * Initialize screen display.
     61   1.1       tls  */
     62   1.5     lukem void
     63  1.10  dholland cursinit(void)
     64   1.1       tls {
     65   1.1       tls 
     66   1.9  drochner 	if (!initscr()) {
     67  1.13  dholland 		errx(EXIT_FAILURE, "Couldn't initialize screen");
     68   1.9  drochner 	}
     69  1.14  dholland 	if ((LINES < SCRNH) || (COLS < SCRNW)) {
     70  1.14  dholland 		errx(EXIT_FAILURE, "Screen too small (need %d%xd)",
     71  1.14  dholland 		    SCRNW, SCRNH);
     72  1.14  dholland 	}
     73  1.14  dholland 	keypad(stdscr, TRUE);
     74  1.14  dholland 	nonl();
     75   1.1       tls 	noecho();
     76   1.1       tls 	cbreak();
     77  1.14  dholland 	leaveok(stdscr, FALSE);
     78  1.14  dholland 
     79  1.14  dholland #if 0 /* no mouse support in netbsd curses yet */
     80  1.14  dholland 	mousemask(BUTTON1_CLICKED, NULL);
     81  1.14  dholland #endif
     82   1.1       tls }
     83   1.1       tls 
     84   1.1       tls /*
     85   1.1       tls  * Restore screen display.
     86   1.1       tls  */
     87   1.5     lukem void
     88  1.10  dholland cursfini(void)
     89   1.1       tls {
     90   1.1       tls 
     91  1.14  dholland 	move(BSZ4, 0);
     92   1.1       tls 	clrtoeol();
     93   1.1       tls 	refresh();
     94  1.14  dholland 	echo();
     95   1.1       tls 	endwin();
     96   1.1       tls }
     97   1.1       tls 
     98   1.1       tls /*
     99   1.1       tls  * Initialize board display.
    100   1.1       tls  */
    101   1.5     lukem void
    102  1.10  dholland bdisp_init(void)
    103   1.1       tls {
    104   1.5     lukem 	int i, j;
    105   1.1       tls 
    106   1.1       tls 	/* top border */
    107   1.1       tls 	for (i = 1; i < BSZ1; i++) {
    108   1.1       tls 		move(0, 2 * i + 1);
    109   1.1       tls 		addch(letters[i]);
    110   1.1       tls 	}
    111   1.1       tls 	/* left and right edges */
    112   1.1       tls 	for (j = BSZ1; --j > 0; ) {
    113   1.1       tls 		move(20 - j, 0);
    114   1.1       tls 		printw("%2d ", j);
    115   1.1       tls 		move(20 - j, 2 * BSZ1 + 1);
    116   1.1       tls 		printw("%d ", j);
    117   1.1       tls 	}
    118   1.1       tls 	/* bottom border */
    119   1.1       tls 	for (i = 1; i < BSZ1; i++) {
    120   1.1       tls 		move(20, 2 * i + 1);
    121   1.1       tls 		addch(letters[i]);
    122   1.1       tls 	}
    123   1.1       tls 	bdwho(0);
    124   1.1       tls 	move(0, 47);
    125   1.1       tls 	addstr("#  black  white");
    126   1.1       tls 	lastline = 0;
    127   1.1       tls 	bdisp();
    128   1.1       tls }
    129   1.1       tls 
    130   1.1       tls /*
    131   1.1       tls  * Update who is playing whom.
    132   1.1       tls  */
    133   1.5     lukem void
    134  1.10  dholland bdwho(int update)
    135   1.1       tls {
    136  1.14  dholland 	int i, j;
    137   1.1       tls 
    138   1.1       tls 	move(21, 0);
    139  1.14  dholland         printw("                                              ");
    140  1.14  dholland 	i = strlen(plyr[BLACK]);
    141  1.14  dholland 	j = strlen(plyr[WHITE]);
    142  1.14  dholland 	if (i + j <= 20) {
    143  1.14  dholland 		move(21, 10 - (i+j)/2);
    144  1.14  dholland 		printw("BLACK/%s (*) vs. WHITE/%s (O)",
    145  1.14  dholland 		    plyr[BLACK], plyr[WHITE]);
    146  1.14  dholland 	} else {
    147  1.14  dholland 		move(21, 0);
    148  1.14  dholland 		if (i <= 10) {
    149  1.14  dholland 			j = 20 - i;
    150  1.14  dholland 		} else if (j <= 10) {
    151  1.14  dholland 			i = 20 - j;
    152  1.14  dholland 		} else {
    153  1.14  dholland 			i = j = 10;
    154  1.14  dholland 		}
    155  1.14  dholland 		printw("BLACK/%.*s (*) vs. WHITE/%.*s (O)",
    156  1.14  dholland 		    i, plyr[BLACK], j, plyr[WHITE]);
    157  1.14  dholland 	}
    158   1.1       tls 	if (update)
    159   1.1       tls 		refresh();
    160   1.1       tls }
    161   1.1       tls 
    162   1.1       tls /*
    163   1.1       tls  * Update the board display after a move.
    164   1.1       tls  */
    165   1.5     lukem void
    166  1.10  dholland bdisp(void)
    167   1.1       tls {
    168   1.5     lukem 	int i, j, c;
    169   1.5     lukem 	struct spotstr *sp;
    170   1.1       tls 
    171   1.1       tls 	for (j = BSZ1; --j > 0; ) {
    172   1.1       tls 		for (i = 1; i < BSZ1; i++) {
    173   1.1       tls 			move(BSZ1 - j, 2 * i + 1);
    174   1.1       tls 			sp = &board[i + j * BSZ1];
    175   1.1       tls 			if (debug > 1 && sp->s_occ == EMPTY) {
    176  1.11  dholland 				if (sp->s_flags & IFLAGALL)
    177   1.1       tls 					c = '+';
    178  1.11  dholland 				else if (sp->s_flags & CFLAGALL)
    179   1.1       tls 					c = '-';
    180   1.1       tls 				else
    181   1.1       tls 					c = '.';
    182   1.1       tls 			} else
    183   1.1       tls 				c = pcolor[sp->s_occ];
    184   1.1       tls 			addch(c);
    185   1.1       tls 		}
    186   1.1       tls 	}
    187   1.1       tls 	refresh();
    188   1.1       tls }
    189   1.1       tls 
    190   1.1       tls #ifdef DEBUG
    191   1.1       tls /*
    192   1.1       tls  * Dump board display to a file.
    193   1.1       tls  */
    194   1.5     lukem void
    195  1.10  dholland bdump(FILE *fp)
    196   1.1       tls {
    197   1.5     lukem 	int i, j, c;
    198   1.5     lukem 	struct spotstr *sp;
    199   1.1       tls 
    200   1.1       tls 	/* top border */
    201   1.1       tls 	fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
    202   1.1       tls 
    203   1.1       tls 	for (j = BSZ1; --j > 0; ) {
    204   1.1       tls 		/* left edge */
    205   1.1       tls 		fprintf(fp, "%2d ", j);
    206   1.1       tls 		for (i = 1; i < BSZ1; i++) {
    207   1.1       tls 			sp = &board[i + j * BSZ1];
    208   1.1       tls 			if (debug > 1 && sp->s_occ == EMPTY) {
    209  1.11  dholland 				if (sp->s_flags & IFLAGALL)
    210   1.1       tls 					c = '+';
    211  1.11  dholland 				else if (sp->s_flags & CFLAGALL)
    212   1.1       tls 					c = '-';
    213   1.1       tls 				else
    214   1.1       tls 					c = '.';
    215   1.1       tls 			} else
    216   1.1       tls 				c = pcolor[sp->s_occ];
    217   1.1       tls 			putc(c, fp);
    218   1.1       tls 			putc(' ', fp);
    219   1.1       tls 		}
    220   1.1       tls 		/* right edge */
    221   1.1       tls 		fprintf(fp, "%d\n", j);
    222   1.1       tls 	}
    223   1.1       tls 
    224   1.1       tls 	/* bottom border */
    225   1.1       tls 	fprintf(fp, "   A B C D E F G H J K L M N O P Q R S T\n");
    226   1.1       tls }
    227   1.1       tls #endif /* DEBUG */
    228   1.1       tls 
    229   1.1       tls /*
    230   1.1       tls  * Display a transcript entry
    231   1.1       tls  */
    232   1.5     lukem void
    233  1.10  dholland dislog(const char *str)
    234   1.1       tls {
    235   1.1       tls 
    236   1.1       tls 	if (++lastline >= SCRNH - 1) {
    237   1.1       tls 		/* move 'em up */
    238   1.1       tls 		lastline = 1;
    239   1.1       tls 	}
    240  1.14  dholland 	move(lastline, TRANSCRIPT_COL);
    241  1.14  dholland 	addnstr(str, SCRNW - TRANSCRIPT_COL - 1);
    242   1.1       tls 	clrtoeol();
    243  1.14  dholland 	move(lastline + 1, TRANSCRIPT_COL);
    244   1.1       tls 	clrtoeol();
    245   1.1       tls }
    246   1.1       tls 
    247   1.1       tls /*
    248   1.1       tls  * Display a question.
    249   1.1       tls  */
    250   1.5     lukem 
    251   1.5     lukem void
    252  1.10  dholland ask(const char *str)
    253   1.1       tls {
    254   1.1       tls 	int len = strlen(str);
    255   1.1       tls 
    256  1.14  dholland 	move(BSZ4, 0);
    257   1.1       tls 	addstr(str);
    258   1.1       tls 	clrtoeol();
    259  1.14  dholland 	move(BSZ4, len);
    260   1.1       tls 	refresh();
    261   1.1       tls }
    262   1.1       tls 
    263   1.5     lukem int
    264  1.12       roy get_line(char *buf, int size)
    265   1.1       tls {
    266   1.5     lukem 	char *cp, *end;
    267   1.5     lukem 	int c;
    268   1.1       tls 
    269   1.5     lukem 	c = 0;
    270   1.1       tls 	cp = buf;
    271   1.1       tls 	end = buf + size - 1;	/* save room for the '\0' */
    272   1.1       tls 	while (cp < end && (c = getchar()) != EOF && c != '\n' && c != '\r') {
    273   1.1       tls 		*cp++ = c;
    274   1.1       tls 		if (interactive) {
    275   1.1       tls 			switch (c) {
    276   1.1       tls 			case 0x0c: /* ^L */
    277   1.1       tls 				wrefresh(curscr);
    278   1.1       tls 				cp--;
    279   1.1       tls 				continue;
    280   1.1       tls 			case 0x15: /* ^U */
    281   1.1       tls 			case 0x18: /* ^X */
    282   1.1       tls 				while (cp > buf) {
    283   1.1       tls 					cp--;
    284   1.1       tls 					addch('\b');
    285   1.1       tls 				}
    286   1.1       tls 				clrtoeol();
    287   1.1       tls 				break;
    288   1.1       tls 			case '\b':
    289   1.1       tls 			case 0x7f: /* DEL */
    290   1.1       tls 				if (cp == buf + 1) {
    291   1.1       tls 					cp--;
    292   1.1       tls 					continue;
    293   1.1       tls 				}
    294   1.1       tls 				cp -= 2;
    295   1.1       tls 				addch('\b');
    296   1.1       tls 				c = ' ';
    297   1.1       tls 				/* FALLTHROUGH */
    298   1.1       tls 			default:
    299   1.1       tls 				addch(c);
    300   1.1       tls 			}
    301   1.1       tls 			refresh();
    302   1.1       tls 		}
    303   1.1       tls 	}
    304   1.1       tls 	*cp = '\0';
    305   1.1       tls 	return(c != EOF);
    306   1.1       tls }
    307  1.14  dholland 
    308  1.14  dholland /*
    309  1.14  dholland  * Decent (n)curses interface for the game, based on Eric S. Raymond's
    310  1.14  dholland  * modifications to the battleship (bs) user interface.
    311  1.14  dholland  */
    312  1.14  dholland int
    313  1.14  dholland get_coord(void)
    314  1.14  dholland {
    315  1.14  dholland 	static int curx = BSZ / 2;
    316  1.14  dholland 	static int cury = BSZ / 2;
    317  1.14  dholland 	int ny, nx, ch;
    318  1.14  dholland 
    319  1.14  dholland 	BGOTO(cury, curx);
    320  1.14  dholland 	refresh();
    321  1.14  dholland 	nx = curx;
    322  1.14  dholland 	ny = cury;
    323  1.14  dholland 	for (;;) {
    324  1.14  dholland 		mvprintw(BSZ3, (BSZ -6)/2, "(%c %d)",
    325  1.14  dholland 				'A'+ ((curx > 7) ? (curx+1) : curx), cury + 1);
    326  1.14  dholland 		BGOTO(cury, curx);
    327  1.14  dholland 
    328  1.14  dholland 		ch = getch();
    329  1.14  dholland 		switch (ch) {
    330  1.14  dholland 		case 'k':
    331  1.14  dholland 		case '8':
    332  1.14  dholland 		case KEY_UP:
    333  1.14  dholland 			nx = curx;
    334  1.14  dholland 			ny = cury + 1;
    335  1.14  dholland 			break;
    336  1.14  dholland 		case 'j':
    337  1.14  dholland 		case '2':
    338  1.14  dholland 		case KEY_DOWN:
    339  1.14  dholland 			nx = curx;
    340  1.14  dholland 			ny = BSZ + cury - 1;
    341  1.14  dholland 			break;
    342  1.14  dholland 		case 'h':
    343  1.14  dholland 		case '4':
    344  1.14  dholland 		case KEY_LEFT:
    345  1.14  dholland 			nx = BSZ + curx - 1;
    346  1.14  dholland 			ny = cury;
    347  1.14  dholland 			break;
    348  1.14  dholland 		case 'l':
    349  1.14  dholland 		case '6':
    350  1.14  dholland 		case KEY_RIGHT:
    351  1.14  dholland 			nx = curx + 1;
    352  1.14  dholland 			ny = cury;
    353  1.14  dholland 			break;
    354  1.14  dholland 		case 'y':
    355  1.14  dholland 		case '7':
    356  1.14  dholland 		case KEY_A1:
    357  1.14  dholland 			nx = BSZ + curx - 1;
    358  1.14  dholland 			ny = cury + 1;
    359  1.14  dholland 			break;
    360  1.14  dholland 		case 'b':
    361  1.14  dholland 		case '1':
    362  1.14  dholland 		case KEY_C1:
    363  1.14  dholland 			nx = BSZ + curx - 1;
    364  1.14  dholland 			ny = BSZ + cury - 1;
    365  1.14  dholland 			break;
    366  1.14  dholland 		case 'u':
    367  1.14  dholland 		case '9':
    368  1.14  dholland 		case KEY_A3:
    369  1.14  dholland 			nx = curx + 1;
    370  1.14  dholland 			ny = cury + 1;
    371  1.14  dholland 			break;
    372  1.14  dholland 		case 'n':
    373  1.14  dholland 		case '3':
    374  1.14  dholland 		case KEY_C3:
    375  1.14  dholland 			nx = curx + 1;
    376  1.14  dholland 			ny = BSZ + cury - 1;
    377  1.14  dholland 			break;
    378  1.14  dholland 		case 'K':
    379  1.14  dholland 			nx = curx;
    380  1.14  dholland 			ny = cury + 5;
    381  1.14  dholland 			break;
    382  1.14  dholland 		case 'J':
    383  1.14  dholland 			nx = curx;
    384  1.14  dholland 			ny = BSZ + cury - 5;
    385  1.14  dholland 			break;
    386  1.14  dholland 		case 'H':
    387  1.14  dholland 			nx = BSZ + curx - 5;
    388  1.14  dholland 			ny = cury;
    389  1.14  dholland 			break;
    390  1.14  dholland 		case 'L':
    391  1.14  dholland 			nx = curx + 5;
    392  1.14  dholland 			ny = cury;
    393  1.14  dholland 			break;
    394  1.14  dholland 		case 'Y':
    395  1.14  dholland 		        nx = BSZ + curx - 5;
    396  1.14  dholland 			ny = cury + 5;
    397  1.14  dholland 			break;
    398  1.14  dholland 		case 'B':
    399  1.14  dholland 			nx = BSZ + curx - 5;
    400  1.14  dholland 			ny = BSZ + cury - 5;
    401  1.14  dholland 			break;
    402  1.14  dholland 		case 'U':
    403  1.14  dholland 			nx = curx + 5;
    404  1.14  dholland 			ny = cury + 5;
    405  1.14  dholland 			break;
    406  1.14  dholland 		case 'N':
    407  1.14  dholland 			nx = curx + 5;
    408  1.14  dholland 			ny = BSZ + cury - 5;
    409  1.14  dholland 			break;
    410  1.14  dholland 		case '\f':
    411  1.14  dholland 			nx = curx;
    412  1.14  dholland 			ny = cury;
    413  1.14  dholland 			(void)clearok(stdscr, TRUE);
    414  1.14  dholland 			(void)refresh();
    415  1.14  dholland 			break;
    416  1.14  dholland #if 0 /* notyet */
    417  1.14  dholland 		case KEY_MOUSE:
    418  1.14  dholland 		{
    419  1.14  dholland 			MEVENT	myevent;
    420  1.14  dholland 
    421  1.14  dholland 			getmouse(&myevent);
    422  1.14  dholland 			if (myevent.y >= 1 && myevent.y <= BSZ1 &&
    423  1.14  dholland 			    myevent.x >= 3 && myevent.x <= (2 * BSZ + 1)) {
    424  1.14  dholland 				curx = (myevent.x - 3) / 2;
    425  1.14  dholland 				cury = BSZ - myevent.y;
    426  1.14  dholland 				return PT(curx,cury);
    427  1.14  dholland 			} else {
    428  1.14  dholland 				beep();
    429  1.14  dholland 			}
    430  1.14  dholland 		}
    431  1.14  dholland 		break;
    432  1.14  dholland #endif /* 0 */
    433  1.14  dholland 		case 'Q':
    434  1.14  dholland 			return RESIGN;
    435  1.14  dholland 			break;
    436  1.14  dholland 		case 'S':
    437  1.14  dholland 			return SAVE;
    438  1.14  dholland 			break;
    439  1.14  dholland 		case ' ':
    440  1.14  dholland 		case '\r':
    441  1.14  dholland 			(void) mvaddstr(BSZ3, (BSZ -6)/2, "      ");
    442  1.14  dholland 			return PT(curx+1,cury+1);
    443  1.14  dholland 			break;
    444  1.14  dholland 	}
    445  1.14  dholland 
    446  1.14  dholland 	curx = nx % BSZ;
    447  1.14  dholland 	cury = ny % BSZ;
    448  1.14  dholland     }
    449  1.14  dholland }
    450