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

📄 xbi.c

📁 数据挖掘中的bridgit算法的例子。 非常经典
💻 C
📖 第 1 页 / 共 3 页
字号:
  assert(bi && gc);             /* check for game and graph. context */  if (bip.hint != BI_NONE)      /* if there is a hint for a bridge, */    b_draw(bip.hint, -1);       /* undraw this bridge */  bridge = bi_undo(bi);         /* undo a move and get the bridge */  if (bridge == BI_NONE) return;/* if no move to undo, abort */  b_draw(bridge, -1);           /* undraw the corresponding bridge */  bip.next = bi_opp(bip.next);  /* switch to the other player and */  bip.hint = bi_hint(bi, bip.next, drand);      /* get a new hint */}  /* undo() *//*--------------------------------------------------------------------*/static void redo (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Redo Move */  int bridge;                   /* bridge to rebuild */  assert(bi && gc);             /* check for game and graph. context */  if (bip.hint != BI_NONE)      /* if there is a hint for a bridge, */    b_draw(bip.hint, -1);       /* undraw this bridge */  bridge = bi_redo(bi);         /* redo a move and get the bridge */  if (bridge == BI_NONE) return;/* if there is a move to redo, */  b_draw(bridge, 0);            /* draw the corresponding bridge */  bip.next = bi_opp(bip.next);  /* switch to the other player and */  bip.hint = bi_hint(bi, bip.next, drand);      /* get a new hint */}  /* redo() *//*--------------------------------------------------------------------*/static void setup (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Setup... */  char buf[32];                 /* buffer for number */  if (!psh) {                   /* if property sheet not created yet */    psh = psh_create("setup", w_top, 1);    if (!psh) return;           /* create a property sheet */    if ((psh_addline(psh, "lsize")              != 0)    ||  (psh_additem(psh, "size",  PSH_EDIT, 0) != 0)    ||  (psh_addline(psh, "lfirst")             != 0)    ||  (psh_additem(psh, "first", PSH_MENU, 0) != 0)    ||  (psh_menuadd(psh, "first", "blue")      != 0)    ||  (psh_menuadd(psh, "first", "red")       != 0)) {      psh_delete(psh); error(E_WIDGET); }  }  mn_enable(menu, MN_MENU, 0);  /* disable menu bar */  sprintf(buf, "%i", bip.size); /* fill the input gadgets */  psh_setval(psh, "size", buf); /* from the game parameters */  psh_setval(psh, "first", (char *)((bip.first == BI_HORZ) ? 0 : 1));  psh_handle(psh, dcb_setup);   /* handle dialog */}  /* setup() *//*--------------------------------------------------------------------*/static void redraw (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- Actions > Redraw */  int        i, n;              /* loop variable, number of moves */  int        size;              /* size of the board */  int        x,  y;             /* loop variables, coordinates */  int        px, py;            /* pixel coordinates */  Dimension  wd, ht;            /* width and height of window */  XRectangle rc;                /* clipping rectangle */  if (!gc) {                    /* if graphics context not created */    gc = XCreateGC(display, window, 0, NULL);    if (!gc) { error(E_GRAPH); return; }  }                             /* create a graphics context */  if (e && (e->type == Expose)){/* if called by Xt, */    rc.x      = e->xexpose.x;   /* build and set */    rc.y      = e->xexpose.y;   /* clipping rectangle */    rc.width  = e->xexpose.width;    rc.height = e->xexpose.height;    XSetClipRectangles(display, gc, 0, 0, &rc, 1, Unsorted);  }  /* --- fill background --- */  XSetForeground(display, gc, white);  XtVaGetValues(w_board, XtNwidth, &wd, XtNheight, &ht, NULL);  XFillRectangle(display, window, gc, 0, 0, wd, ht);  /* --- draw connection panels --- */  size = bi_size(bi);           /* get the board size and the */  px   = size *PTDIST;          /* length of the connection panel */  XSetForeground(display, gc, dred);   /* panels for vert. player */  XFillRectangle(display, window, gc,                 PTDIST/2 +PTOFFSET,     PTOFFSET-1, px -PTDIST, 3);  XFillRectangle(display, window, gc,                 PTDIST/2 +PTOFFSET, px +PTOFFSET-1, px -PTDIST, 3);  XSetForeground(display, gc, dblue);  /* panels for horz. player */  XFillRectangle(display, window, gc,                     PTOFFSET-1, PTDIST/2 +PTOFFSET, 3, px -PTDIST);  XFillRectangle(display, window, gc,                 px +PTOFFSET-1, PTDIST/2 +PTOFFSET, 3, px -PTDIST);  /* --- draw support points --- */  for (y = size+1; --y >= 0;) { /* traverse the rows */    py = y *PTDIST +PTOFFSET -PTSIZE/2;    for (x = size; --x >= 0;) { /* traverse the columns */      px = x *PTDIST +PTOFFSET +PTDIST/2 -PTSIZE/2;      XSetForeground(display, gc, red);      XFillArc(display, window, gc, px, py, PTSIZE, PTSIZE, 0, 360*64);      XSetForeground(display, gc, black);      XDrawArc(display, window, gc, px, py, PTSIZE, PTSIZE, 0, 360*64);    }                           /* draw the support points */  }                             /* for the red (vertical) player */  for (y = size; --y >= 0; ) {  /* traverse the rows */    py = y *PTDIST +PTOFFSET +PTDIST/2 -PTSIZE/2;    for (x = size+1; --x >= 0;){/* traverse the columns */      px = x *PTDIST +PTOFFSET -PTSIZE/2;      XSetForeground(display, gc, blue);      XFillArc(display, window, gc, px, py, PTSIZE, PTSIZE, 0, 360*64);      XSetForeground(display, gc, black);      XDrawArc(display, window, gc, px, py, PTSIZE, PTSIZE, 0, 360*64);    }                           /* draw the support points */  }                             /* for the blue (horizontal) player */  /* --- draw moves made --- */  for (n = bi_curr(bi), i = 0; i < n; i++)    b_draw(bi_move(bi, i), 0);  /* draw all bridges built */  XFlush(display);              /* flush buffered drawing commands */  XSetClipMask(display, gc, None);      /* and clear clipping mask */}  /* redraw() *//*--------------------------------------------------------------------*/static void about (Widget w, XEvent *e, String *s, Cardinal *c){                               /* --- 'About' dialog box */  mn_enable(menu, MN_MENU, 0);  /* disable menu bar */  if (db_about(w_top, dcb_nop) != 0)    error(E_DIALOG);            /* show `about' dialog box */}  /* about() *//*----------------------------------------------------------------------  Main Function----------------------------------------------------------------------*/int main (int argc, char *argv[]){                               /* --- main program */  Arg       args[8];            /* widget arguments */  int       n;                  /* argument counter */  int       w;                  /* minimal menu width */  Colormap  colmap;             /* color map of display */  XColor    xc[6];              /* buffer for colors */  dseed(time(NULL));            /* init. random number generator */  XtSetLanguageProc(NULL, (XtLanguageProc)NULL, NULL);  /* --- create main window --- */  w_top = XtAppInitialize(&appctx, "Xbi",            NULL, 0, &argc, argv, fallback, NULL, 0);  if (!w_top) error(E_INIT);    /* create toplevel widget */  n = 0;                        /* build argument list */  XtSetArg(args[n], XtNdefaultDistance, 0);    n++;  XtSetArg(args[n], XtNresizable,       True); n++;  w_main = XtCreateManagedWidget("main", formWidgetClass,             w_top, args, n);   /* create main widget */  if (!w_main) error(E_WIDGET); /* (form for window layout) */  menu = mn_create(w_main);     /* create a menu bar and */  if (!menu) error(E_NOMEM);    /* add titles and items */  if ((mn_addtitle(menu, "file",     0, 0,          0) != 0)  ||  (mn_additem (menu, "load",     0, mcb_load,   0) != 0)  ||  (mn_additem (menu, "save",     0, mcb_save,   0) != 0)  ||  (mn_additem (menu, NULL,       0, 0,          0) != 0)  ||  (mn_additem (menu, "quit",     0, mcb_quit,   0) != 0)  ||  (mn_addtitle(menu, "actions",  0, 0,          0) != 0)  ||  (mn_additem (menu, "start",    0, mcb_start,  0) != 0)  ||  (mn_additem (menu, "flip",     0, mcb_flip,   0) != 0)  ||  (mn_additem (menu, "hint",     0, mcb_hint,   0) != 0)  ||  (mn_additem (menu, NULL,       0, 0,          0) != 0)  ||  (mn_additem (menu, "undo",     0, mcb_undo,   0) != 0)  ||  (mn_additem (menu, "redo",     0, mcb_redo,   0) != 0)  ||  (mn_additem (menu, NULL,       0, 0,          0) != 0)  ||  (mn_additem (menu, "setup",    0, mcb_setup,  0) != 0)  ||  (mn_additem (menu, "redraw",   0, mcb_redraw, 0) != 0)  ||  (mn_addtitle(menu, "help",     0, 0,          0) != 0)  ||  (mn_additem (menu, "about",    0, mcb_about,  0) != 0)  ||  (mn_addtitle(menu, NULL,       0, 0,          0) != 0))    error(E_WIDGET);  /* --- create board viewport --- */  n = 0;                        /* build argument list */  XtSetArg(args[n], XtNfromVert,  mn_menubar(menu)); n++;  XtSetArg(args[n], XtNleft,      XtChainLeft);   n++;  XtSetArg(args[n], XtNtop,       XtChainTop);    n++;  XtSetArg(args[n], XtNright,     XtChainRight);  n++;  XtSetArg(args[n], XtNbottom,    XtChainBottom); n++;  XtSetArg(args[n], XtNresizable, True);          n++;  w_view = XtCreateManagedWidget("view",             viewportWidgetClass, w_main, args, n);  if (!w_view) error(E_WIDGET); /* create viewport */  /* --- create board widget --- */  n = 0;                        /* build argument list */  XtSetArg(args[n], XtNborderWidth, 0);             n++;  XtSetArg(args[n], XtNleft,        XtChainLeft);   n++;  XtSetArg(args[n], XtNtop,         XtChainTop);    n++;  XtSetArg(args[n], XtNright,       XtChainRight);  n++;  XtSetArg(args[n], XtNbottom,      XtChainBottom); n++;  XtSetArg(args[n], XtNresizable,   True);          n++;  w_board = XtCreateManagedWidget("board",              coreWidgetClass, w_view, args, n);  if (!w_board) error(E_WIDGET);/* create board */  /* --- set widget extensions --- */  n = 2*PTDIST +2*PTOFFSET +1;  /* get minimal size of board */  w = mn_minwidth(menu);        /* and minimal menu width */  XtVaSetValues(w_top, XtNminWidth,  (w > n) ? w : n,                       XtNminHeight, mn_height(menu) +n+2, NULL);  resize(bip.size);             /* set board and window size */  XtOverrideTranslations(w_board, /* install redraw function */    XtParseTranslationTable("<Expose>: redraw()"));  /* --- install actions, accelerators and translations --- */  XtAppAddActions(appctx, actions, XtNumber(actions));  XtInstallAllAccelerators(w_main, w_main);    /* --- create bridg-it game --- */  bi = bi_create(bip.size);     /* create a bridg-game */  if (!bi) error(E_NOMEM);      /* with a board of default size */  bi_init(bi);                  /* and initialize the game */  bip.hint = bi_hint(bi, bip.next, drand);  /* --- show window --- */  XtRealizeWidget(w_top);       /* show window on display */  display = XtDisplay(w_top);   /* get display of top window */  window  = XtWindow(w_board);  /* and drawable for window */  if (db_alert(w_top, dcb_nop, NULL) != 0)    error(E_WIDGET);            /* create alert dialog box */  /* --- override window manager delete --- */  XtOverrideTranslations(w_top, /* override window manager delete */    XtParseTranslationTable("<Message>WM_PROTOCOLS: quit()"));  wm_delwin = XInternAtom(XtDisplay(w_top), "WM_DELETE_WINDOW", False);  XSetWMProtocols(XtDisplay(w_top), XtWindow(w_top), &wm_delwin, 1);  /* --- allocate colors --- */  screen = DefaultScreen(display);  colmap = DefaultColormap(display, screen);  white  = WhitePixel(display, screen);  black  = BlackPixel(display, screen);  if ((XAllocNamedColor(display, colmap, "red",   xc+1, xc) == 0)  ||  (XAllocNamedColor(display, colmap, "blue",  xc+2, xc) == 0)  ||  (XAllocNamedColor(display, colmap, "green", xc+3, xc) == 0)  ||  (XAllocNamedColor(display, colmap, "red4",  xc+4, xc) == 0)  ||  (XAllocNamedColor(display, colmap, "blue4", xc+5, xc) == 0))    error(E_COLOR);             /* allocate colors and */  red    = xc[1].pixel;         /* get the pixel values */  blue   = xc[2].pixel;         /* of the drawing colors */  green  = xc[3].pixel;  dred   = xc[4].pixel;  dblue  = xc[5].pixel;  /* --- start event loop --- */  XtAppMainLoop(appctx);        /* enter main event loop */  return 0;                     /* return `ok' */}  /* main() */

⌨️ 快捷键说明

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