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

📄 xbi.c

📁 数据挖掘中的bridgit算法的例子。 非常经典
💻 C
📖 第 1 页 / 共 3 页
字号:
  assert(bridge != BI_NONE);    /* check the function arguments */  if (hint) color = (hint > 0)                     ? green : white;  else      color = (bi_player(bridge) == BI_HORZ) ? blue  : red;  XSetForeground(display, gc, color);       /* set the color */  x1 = bi_col(bridge) *PTDIST/2 +PTOFFSET;  /* get the coordinates */  y1 = bi_row(bridge) *PTDIST/2 +PTOFFSET;  /* of the crossing point */  if (bi_dir(bridge) == BI_HORZ)/* draw a horizontal bridge */    XFillRectangle(display, window, gc, x1-PTDIST/2+2,y1-1, PTDIST-4,3);  else                          /* draw a vertical   bridge */    XFillRectangle(display, window, gc, x1-1,y1-PTDIST/2+2, 3,PTDIST-4);  if (hint >= 0) return;        /* if not to undraw a bridge, abort */  x2 = x1 -= PTSIZE/2;          /* get upper left corner */  y2 = y1 -= PTSIZE/2;          /* of crossing point */  if (bi_dir(bridge) == BI_HORZ) {   /* if the bridge is horizontal, */    x1 -= PTDIST/2; x2 += PTDIST/2;  /* adapt the horizontal coords. */    a   = 315*64; }                  /* get right quarter circle */  else {                             /* if the bridge is vertical, */    y1 -= PTDIST/2; y2 += PTDIST/2;  /* adapt the vertical coords. */    a   = 225*64;                    /* get top quarter circle */  }  XSetForeground(display, gc,   /* set the player color */                 (bi_player(bridge) == BI_HORZ) ? blue : red);  XFillArc(display, window, gc, x1, y1, PTSIZE,PTSIZE, a,        90*64);  XFillArc(display, window, gc, x2, y2, PTSIZE,PTSIZE, a+180*64, 90*64);  XSetForeground(display, gc, black);  XDrawArc(display, window, gc, x1, y1, PTSIZE,PTSIZE, a,        90*64);  XDrawArc(display, window, gc, x2, y2, PTSIZE,PTSIZE, a+180*64, 90*64);}  /* b_draw() */               /* redraw the support points *//*----------------------------------------------------------------------  Dialog Box Callback Functions----------------------------------------------------------------------*/static void dcb_load (Widget widget, XtPointer ok, XtPointer fname){                               /* --- 'Load Game...' callback */  FILE    *file;                /* output file */  BRIDGIT *new;                 /* loaded bridg-it game */  int     bridge;               /* a bridge built in the game */  mn_enable(menu, MN_MENU, 1);  /* enable menu bar */  file = fopen(fname, "r");     /* open the input file */  if (!file) { error(E_FOPEN, fname); return; }  new = bi_load(file);          /* load the bridg-it game */  fclose(file);                 /* and close the input file */  if (!new)  { error(E_FREAD, fname); return; }  if (bi) bi_delete(bi);        /* delete the old bridg-it game */  bi = new;                     /* and set the new one */  do {                          /* find player to move next */    bridge = bi_redo(bi);       /* either by going forward */    if (bridge != BI_NONE) {    /* and getting the player */      bi_undo(bi); bip.next = bi_player(bridge);         break; }    bridge = bi_undo(bi);       /* or by going backwards */    if (bridge != BI_NONE) {    /* and getting the opponent */      bi_redo(bi); bip.next = bi_opp(bi_player(bridge)); break; }    bridge = bip.first;         /* if there is no move, */  } while (0);                  /* simply get the first player */  bip.hint = bi_hint(bi, bip.next, drand);  bip.size = bi_size(bi);       /* get the size of the board */  resize(bip.size);             /* and resize the board */  redraw(NULL,NULL,NULL,NULL);  /* redraw the window contents */}  /* dcb_load() *//*--------------------------------------------------------------------*/static void dcb_save (Widget widget, XtPointer ok, XtPointer fname){                               /* --- 'Save File...' callback */  FILE *file;                   /* output file */  int  r;                       /* result of save function */  mn_enable(menu, MN_MENU, 1);  /* enable menu bar */  file = fopen(fname, "w");     /* open the output file */  if (!file) { error(E_FOPEN, fname); return; }  r = bi_save(bi, file);        /* save the bridg-it game */  if ((fclose(file) != 0) || (r != 0)) {    error(E_FWRITE, fname); return; }}  /* dcb_save() *//*--------------------------------------------------------------------*/static void dcb_setup (Widget widget, XtPointer ok, XtPointer call){                               /* --- 'Setup...' callback */  mn_enable(menu, MN_MENU, 1);  /* enable menu bar */  if (!ok) return;              /* if not ok pressed, abort */  bip.size = atoi(psh_getval(psh, "size"));  if (bip.size < MINSIZE) bip.size = MINSIZE;  if (bip.size > MAXSIZE) bip.size = MAXSIZE;  bip.first = ((int)psh_getval(psh, "first") == 0) ? BI_HORZ : BI_VERT;  start(NULL,NULL,NULL,NULL);   /* get the values from the gadgets */}  /* dcb_setup() */            /* and restart the game *//*----------------------------------------------------------------------  Menu Callback Functions----------------------------------------------------------------------*/static void mcb_load   (Widget widget, XtPointer client, XtPointer call){ load(NULL, NULL, NULL, NULL); }static void mcb_save   (Widget widget, XtPointer client, XtPointer call){ save(NULL, NULL, NULL, NULL); }static void mcb_quit   (Widget widget, XtPointer client, XtPointer call){ quit(NULL, NULL, NULL, NULL); }static void mcb_start  (Widget widget, XtPointer client, XtPointer call){ start(NULL, NULL, NULL, NULL); }static void mcb_flip   (Widget widget, XtPointer client, XtPointer call){ flip(NULL, NULL, NULL, NULL); }static void mcb_undo   (Widget widget, XtPointer client, XtPointer call){ undo(NULL, NULL, NULL, NULL); }static void mcb_redo   (Widget widget, XtPointer client, XtPointer call){ redo(NULL, NULL, NULL, NULL); }static void mcb_hint   (Widget widget, XtPointer client, XtPointer call){ hint(NULL, NULL, NULL, NULL); }static void mcb_setup  (Widget widget, XtPointer client, XtPointer call){ setup(NULL, NULL, NULL, NULL); }static void mcb_redraw (Widget widget, XtPointer client, XtPointer call){ redraw(NULL, NULL, NULL, NULL); }static void mcb_about  (Widget widget, XtPointer client, XtPointer call){ about(NULL, NULL, NULL, NULL); }/*----------------------------------------------------------------------  Action Functions----------------------------------------------------------------------*/static void mouse (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- mouse button pressed */  int x, y, px, py;             /* (pixel) coordinates */  int size;                     /* size of the board */  int bridge;                   /* bridge to draw */  assert(bi && gc);             /* check for game and graph. context */  if (bip.hint == BI_NONE)      /* if the game has already ended, */    return;                     /* abort the function */  b_draw(bip.hint, -1);         /* undraw the hint bridge (if any) */  size = bi_size(bi);           /* get the size of the bridg-it board */  px = e->xbutton.x -PTOFFSET;  /* get the coordinates at which */  py = e->xbutton.y -PTOFFSET;  /* the mouse button was pressed */  x  = px / (PTDIST/2); px %= PTDIST/2;  y  = py / (PTDIST/2); py %= PTDIST/2;  if ((x+y) & 1) { if (px > py) x++; else y++; }  else           { if (px+py > PTDIST/2) { x++; y++; } }  if ((x <= 0) || (x >= 2*size) /* compute the coordinates of */  ||  (y <= 0) || (y >= 2*size) /* the closest crossing point */  ||  (bi_state(bi, x, y) != BI_NONE))    return;                     /* check for a valid crossing point */  bridge = bi_bridge(bip.next, x, y);  bi_build(bi, bridge);         /* construct a bridge, */  b_draw(bridge, 0);            /* build and draw it */  XFlush(display);              /* flush buffered drawing commands */  bip.next = bi_opp(bip.next);  /* switch to the other player */  bip.hint = bi_hint(bi, bip.next, drand);  if (bip.hint == BI_NONE) {    /* if there is no new hint */    db_alert(w_top, dcb_nop, (bip.next == BI_HORZ) ? REDWIN : BLUEWIN);    return;                     /* notify that the game has ended */  }                             /* and abort the function */  bi_build(bi, bip.hint);       /* build the computed bridge */  b_draw(bip.hint, 0);          /* and draw it */  bip.next = bi_opp(bip.next);  /* switch to the other player */  bip.hint = bi_hint(bi, bip.next, drand);  if (bip.hint == BI_NONE)      /* if there is no new hint */    db_alert(w_top, dcb_nop, (bip.next == BI_HORZ) ? REDWIN : BLUEWIN);}  /* mouse() */                /* notify that the game has ended *//*--------------------------------------------------------------------*/static void load (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- File > Load... */  const char *t;                /* buffer for dialog box title */  mn_enable(menu, MN_MENU, 0);  /* disable menu bar */  XtVaGetValues(XtNameToWidget(mn_menubar(menu), "*load"),    XtNlabel, &t, NULL);        /* get name of menu entry */  if (fselect(w_top, t, NULL, NULL, dcb_load) != 0) {    error(E_DIALOG); mn_enable(menu, MN_MENU, 1); }}  /* load() *//*--------------------------------------------------------------------*/static void save (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- File > Save... */  const char *t;                /* buffer for dialog box title */  mn_enable(menu, MN_MENU, 0);  /* disable menu bar */  XtVaGetValues(XtNameToWidget(mn_menubar(menu), "*save"),    XtNlabel, &t, NULL);        /* get name of menu entry */  if (fselect(w_top, t, NULL, NULL, dcb_save) != 0) {    error(E_DIALOG); mn_enable(menu, MN_MENU, 1); }}  /* save() *//*--------------------------------------------------------------------*/static void quit (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- File > Quit */  if (w_top) XtDestroyApplicationContext(appctx);  exit(0);                      /* terminate program */}  /* quit() *//*--------------------------------------------------------------------*/static void start (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > New Game */  BRIDGIT *new;                 /* new bridg-it game */  assert(bi);                   /* check for an existing game */  if (bip.size != bi_size(bi)){ /* if the size has been changed, */    new = bi_create(bip.size);  /* create a new bridg-it game */    if (!new) { error(E_NOMEM); return; }    if (bi) bi_delete(bi);      /* delete the old bridg-it game */    bi = new;                   /* and set the new one */    resize(bip.size);           /* resize the window to accomodate */  }                             /* the new board size */  bi_init(bi);                  /* initialize the game and */  bip.next = bip.first;         /* the player to move first */  bip.hint = bi_hint(bi, bip.next, drand);  redraw(NULL,NULL,NULL,NULL);  /* get a hint for the first move */}  /* start() */                /* and redraw the window contents *//*--------------------------------------------------------------------*/static void flip (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Flip Sides */  assert(bi && gc);             /* check for game and graph. context */  if (bip.hint == BI_NONE)      /* if the game has already ended, */    return;                     /* abort the function */  bi_build(bi, bip.hint);       /* build the hint bridge */  b_draw(bip.hint, 0);          /* and draw it */  bip.next = bi_opp(bip.next);  /* switch to other player and */  bip.hint = bi_hint(bi, bip.next, drand);  /* get a new hint */  if (bip.hint == BI_NONE)      /* if there is no new hint */    db_alert(w_top, dcb_nop, (bip.next == BI_HORZ) ? REDWIN : BLUEWIN);}  /* flip() */                 /* notify that the game has ended *//*--------------------------------------------------------------------*/static void hint (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Give a Hint */  assert(bi && gc);             /* check for game and graph. context */  if (bip.hint != BI_NONE)      /* if there is a hint bridge, */    b_draw(bip.hint, 1);        /* draw the hint bridge */}  /* hint() *//*--------------------------------------------------------------------*/static void undo (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Undo Move */  int bridge;                   /* bridge to delete */

⌨️ 快捷键说明

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