📄 boggle.c
字号:
switch (state) { case INSTRUCTIONS: if (ie->ie_code == 'y' || ie->ie_code == 'Y' || ie->ie_code == '?') { printinstr(); state = CONFIRM_INSTR; } else { state = PLAYING; startboggle(1); } break; case CONFIRM_INSTR: if (isupper(ie->ie_code)) { state = PLAYING; startboggle(1); } break; case PLAYING: if (feedback_w > 0) { /* remove feedback from isduplicate() */ pw_write(bogwin, feedback_x, feedback_y, feedback_w, fontheight, PIX_NOT(PIX_DST), NULL, 0, 0); feedback_w = 0; } if (isalpha(ie->ie_code)) addchar(ie->ie_code); else if (ie->ie_code == '\n' || ie->ie_code == '\r' || ie->ie_code == '\t' || ie->ie_code == ' ') newline(); else if (ie->ie_code == '\177' || ie->ie_code == '\010') deletechar(); else if (ie->ie_code == '\025') deleteline(); else flash(1); break; case GAMEOVER: if (isupper(ie->ie_code)) { state = PLAYING; startboggle(1); } break; } } else if (ie->ie_code == MS_RIGHT) { if (win_inputposevent(ie)) domenu(ie); } else if (ie->ie_code == MS_LEFT || ie->ie_code == MS_MIDDLE || ie->ie_code == LOC_MOVEWHILEBUTDOWN) { if (laststr[0] != '\0') { word = mouseonword(ie->ie_locx, ie->ie_locy); if (word == NULL) unshowword(); else if (strcmp(laststr, word)) unshowword(); } if (ie->ie_locx > DISPLAY_RIGHT || ie->ie_locy > DISPLAY_BOTTOM) { if (win_inputposevent(ie)) { word = mouseonword(ie->ie_locx, ie->ie_locy); if (word != NULL && strcmp(word, laststr)) { if (laststr[0] != '\0') unshowword(); showcubes(word); } } else { unshowword(); } } } else if (ie->ie_code == LOC_WINEXIT) { unshowword(); }}domenu(ie)struct inputevent *ie;{ struct menuitem *m_item; m_item = menu_display(&bog_menu, ie, bogsw->ts_windowfd); if (m_item != NULL) { switch (m_item->mi_data) { case BOG_RESTARTTIMER: if (state != PLAYING) break; starttimer(); drawtimer(); break; case BOG_EXIT: if (wmgr_confirm(bogsw->ts_windowfd, "Press the left mouse button to confirm Quit. To cancel, press the right mouse button now.")) tool_done(tool); break; case BOG_RESTARTGAME: state = PLAYING; startboggle(1); break; case BOG_GIVEUP: if (state != PLAYING) break; state = GAMEOVER; stoptimer = 1; timeisup(0); } }}drawbog(){ pw_write(bogwin, 0, 0, bogwidth, bogheight, PIX_SRC, 0, 0, 0); drawboard(); drawtimer(); drawtypein();}staticdrawboard(){ char *b = board; int row, column, x, y; for (column = 0; column < 4; column++) { for (row = 0; row < 4; row++) { x = row * (TILE_WIDTH + BOARD_SPACING) + BOARD_BASELEFT; y = column * (TILE_WIDTH + BOARD_SPACING) + BOARD_BASETOP; pw_write(bogwin, x, y, TILE_WIDTH, TILE_WIDTH, PIX_SRC, &tile_pr, 0, 0); if (*b == 'q') { pw_write(bogwin, x + FIRST_LETTER_X_OFFSET, y + LETTER_Y_OFFSET, LETTER_WIDTH, LETTER_HEIGHT, PIX_SRC, letters['q'-'a'], 0, 0); pw_write(bogwin, x + SECOND_LETTER_X_OFFSET, y + LETTER_Y_OFFSET, LETTER_WIDTH, LETTER_HEIGHT, PIX_SRC, &small_u_pr, 0, 0); } else { pw_write(bogwin, x + CENTER_LETTER_X_OFFSET, y + LETTER_Y_OFFSET, LETTER_WIDTH, LETTER_HEIGHT, PIX_SRC, letters[*b - 'a'], 0, 0); } b++; } }}/* * addchar: handle a typed character; make sure this part of word is in * the grid */addchar(c)char c;{ cword[cwordlen] = c; cword[cwordlen + 1] = '\0'; if (!checkword(cword, cwordlen + 1)) { /* be sure word is in grid */ flash(1); return; } drawcursor(); /* erase cursor */ if ((!scrolled && cwordlen >= MAXWORDLEN) || /* scroll it */ (scrolled && cwordlen - scrolled + 1 >= MAXWORDLEN)) { if (!scrolled) scrolled++; scrolled += MAXWORDLEN - SCROLL_OVERLAP; pw_write(bogwin, leftmargin, typein_y, fontwidth * MAXWORDLEN, fontheight, PIX_SRC, NULL, 0, 0); pw_write(bogwin, leftmargin, typein_y, fontwidth, fontheight, PIX_SRC, tri_left_pr, 0, 0); pw_text(bogwin, leftmargin + fontwidth, typein_y + fontoffset, PIX_SRC, bogfont, &cword[scrolled]); typein_x = leftmargin + (cwordlen - scrolled) * fontwidth + fontwidth; } pw_char(bogwin, typein_x, typein_y + fontoffset, PIX_SRC, bogfont, c); typein_x += fontwidth; drawcursor(); /* replace cursor */ cwordlen++;}/* * newline: handle a typed newline; make sure word is in dictionary * and hasn't already been entered; insert into list; move * cursor on display */newline(){ if (cwordlen == 0) return; if (cwordlen < 3) { flash(1); return; } cword[cwordlen] = '\0'; if (isduplicate(cword)) { /* already entered this word */ flash(1); return; } if (!verify(cword)) { /* isn't in the dictionary */ flash(1); return; } addhumanword(cword, leftmargin, typein_y); cwordlen = 0; drawcursor(); /* erase cursor */ if (scrolled) { /* unscroll word if it is scrolled */ char buf[MAXWORDLEN]; strncpy(buf, cword, MAXWORDLEN - 1); buf[MAXWORDLEN - 1] = '\0'; pw_text(bogwin, leftmargin, typein_y + fontoffset, PIX_SRC, bogfont, buf); pw_write(bogwin, leftmargin + (MAXWORDLEN - 1) * fontwidth, typein_y, fontwidth, fontheight, PIX_SRC, tri_right_pr, 0, 0); scrolled = 0; } (void) nextscreenpos(&leftmargin, &typein_x, &typein_y); /* move cursor to new line */ removefeedback(typein_x, typein_y); /* remove feedback address from word which was here (if wrapped) */ pw_write(bogwin, typein_x, typein_y, MAXWORDLEN * fontwidth, /* erase any previous word */ fontheight, PIX_SRC, NULL, 0, 0); drawcursor(); /* replace cursor */}/* * deletechar: handle a backspace from user */deletechar(){ if (cwordlen > 0) { drawcursor(); /* erase cursor */ if (scrolled && cwordlen == scrolled + 2) { /* unscroll */ if (cwordlen <= MAXWORDLEN) { /* word will still be scrolled */ scrolled = 0; pw_write(bogwin, leftmargin, typein_y, fontwidth * MAXWORDLEN, fontheight, PIX_SRC, NULL, 0, 0); cword[--cwordlen] = '\0'; pw_text(bogwin, leftmargin, typein_y + fontoffset, PIX_SRC, bogfont, cword); typein_x = leftmargin + cwordlen * fontwidth; } else { /* word no longer scrolled */ scrolled -= MAXWORDLEN - SCROLL_OVERLAP; pw_write(bogwin, leftmargin, typein_y, fontwidth * MAXWORDLEN, fontheight, PIX_SRC, NULL, 0, 0); pw_write(bogwin, leftmargin, typein_y, fontwidth, fontheight, PIX_SRC, tri_left_pr, 0, 0); cword[--cwordlen] = '\0'; pw_text(bogwin, leftmargin + fontwidth, typein_y + fontoffset, PIX_SRC, bogfont, &cword[scrolled]); typein_x = leftmargin + (cwordlen - scrolled) * fontwidth + fontwidth; } } else { /* handle a non-scrolled word */ typein_x -= fontwidth; pw_write(bogwin, typein_x, typein_y, fontwidth, fontheight, PIX_SRC, NULL, 0, 0); cwordlen--; } drawcursor(); /* redraw cursor */ }}/* * deleteline: handle a line kill from user */deleteline(){ if (cwordlen > 0) { drawcursor(); /* erase cursor */ typein_x = leftmargin; pw_write(bogwin, typein_x, typein_y, fontwidth * cwordlen, fontheight, PIX_SRC, NULL, 0, 0); drawcursor(); /* redraw cursor */ cwordlen = 0; scrolled = 0; }}genboard(){ int i, j; if (setupboard) return; for (i = 0; i < 16; i++) board[i] = 0; for (i = 0; i < 16; i++) { j = random() % 16; while (board[j] != 0) j = (j + 1) % 16; board[j] = cubeletters[i][random() % 6]; }}/* * nextscreenpos: determine where on the screen the next word goes */nextscreenpos(lm, x, y)int *lm, *x, *y; /* current left margin, x, and y; gets changed to new value */{ int full = 0; if (*x < 0 || *y < 0) { /* display is blank; move to first position */ *y = DISPLAY_BOTTOM; if (*y % fontheight != 0) *y += fontheight - (*y % fontheight); if (*y + fontheight > bogheight) { *y = 0; *lm = *x = DISPLAY_RIGHT + BOARD_SPACING; } else { *lm = *x = 0; } return(0); } *x = *lm; *y += fontheight; if (*y + fontheight > bogheight) { *lm = *x += fontwidth * (MAXWORDLEN + 1); if (*lm + 10 * fontwidth > bogwidth) { *y = DISPLAY_BOTTOM; /* wrap to beginning of screen */ if (*y % fontheight != 0) *y += fontheight - (*y % fontheight); if (*y + fontheight > bogheight) { *lm = *x = DISPLAY_RIGHT + BOARD_SPACING; *y = 0; } else { *lm = *x = 0; } full = 1; } else if (*x > DISPLAY_RIGHT + BOARD_SPACING) { *y = 0; } else { *y = DISPLAY_BOTTOM; if (*y % fontheight != 0) *y += fontheight - (*y % fontheight); } } return(full);}askinstr(){ pw_write(bogwin, 0, 0, bogwidth, bogheight, PIX_SRC, 0, 0, 0); pw_text(bogwin, 0, fontoffset, PIX_SRC, bogfont, "Would you like instructions? "); pw_write(bogwin, 29 * fontwidth, 0, fontwidth, fontheight, PIX_NOT(PIX_DST), NULL, 0, 0);}#define INSTRLEN 15char *instr[INSTRLEN] = { " The object of Boggle (TM Parker Bros.) is to find as many words as", "possible in a 4 by 4 grid of letters within a certain time limit. Words", "may be formed from any sequence of 3 or more adjacent letters in the grid.", "The letters may join horizontally, vertically, or diagonally. Normally,", "no letter in the grid may be used more than once in a word. If boggletool", "is invoked with a numeric argument, it is taken to be the time limit (in", "minutes). A single + on the command line removes the restriction that", "letters may be used only once, and two + signs allow a letter to be con-", "sidered adjacent to itself as well as its neighbors.", " Boggletool displays a random grid of letters and an hourglass to mark", "the remaining time. Enter words separated by newlines, using the DEL key", "or the line-kill character (^U) to correct mistakes. When your time is", "up, the computer will display a list of words it found from the online", "dictionary.", " Type any capital letter to begin."};printinstr(){ int y; pw_write(bogwin, 0, 0, bogwidth, bogheight, PIX_SRC, 0, 0, 0); for (y = 0; y < INSTRLEN; y++) pw_text(bogwin, 0, y * fontheight + fontoffset, PIX_SRC, bogfont, instr[y]);}timeisup(c)int c;{ if (c) flash(3); state = GAMEOVER; comparelists(); feedback_w = 0; displaylists();}flash(times)int times;{ while (times--) { pw_write(bogwin, 0, 0, bogwidth, bogheight, PIX_NOT(PIX_DST), 0, 0, 0); select(0, 0, 0, 0, &flashdelay); pw_write(bogwin, 0, 0, bogwidth, bogheight, PIX_NOT(PIX_DST), 0, 0, 0); if (times) select(0, 0, 0, 0, &flashdelay); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -