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

📄 xhamster.c

📁 数据挖掘中de一个算法 hamster的实例
💻 C
📖 第 1 页 / 共 5 页
字号:
/*----------------------------------------------------------------------  File    : xhamster.c  Contents: main program for hamster (X11 version)  Author  : Christian Borgelt  History : 21.10.1997 file created            22.10.1997 field drawing completed            26.10.1997 dialog boxes programmed            27.10.1997 dialog boxes completed, menu accelerators added            28.10.1997 maze widget translations added            30.10.1997 functions to load and save a maze implemented            31.10.1997 minor improvements            01.11.1997 child process and pipe communication added            02.11.1997 child process and pipe communication debugged            04.11.1997 keyboard focus changing added            06.11.1997 event loops in icb_pipe and error removed            09.11.1997 bug in warning messages removed            10.11.1997 bug in function dcb_load (missing resize) removed            21.12.1997 some local variables removed            02.01.1998 minor improvements            04.01.1998 menu popup callback function removed            16.01.1998 constant MAXHEAP defined as HMS_MAXCORN            17.01.1998 counter for crashes against walls added            10.04.1998 minor improvements            16.04.1998 adapted to simplified menu definition            15.05.1998 maze form widget removed            12.06.1998 heap size display added, bug in offsets removed            16.06.1998 menu items Reload Maze.../Save Maze As... added            08.02.1999 dialog boxes adapted to module psheet            08.03.1999 '#include <sys/types.h>' added for Linux            12.01.2000 adapted to module dialog            05.05.2001 function 'resize' redesigned----------------------------------------------------------------------*/#define _POSIX_SOURCE#define select SELECT           /* just to fool HPUX's includes */#define random RANDOM#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <limits.h>#include <time.h>#include <sys/types.h>#include <unistd.h>#include <signal.h>#include <sys/wait.h>#undef select#undef random#include <X11/Intrinsic.h>#include <X11/StringDefs.h>#include <X11/Core.h>#include <X11/Shell.h>#include <X11/Xaw/Form.h>#include <X11/Xaw/Box.h>#include <X11/Xaw/Label.h>#include <X11/Xaw/Viewport.h>#include "menu.h"#include "dialog.h"#include "fselect.h"#include "sprite.h"#include "server.h"/*----------------------------------------------------------------------  Preprocessor Definitions----------------------------------------------------------------------*/#define CCHAR    const char     /* abbreviation */#define ULONG    unsigned long  /* ditto */#define NONAME   "<noname>"     /* name for unsaved maze */#define BDPENALTY   1000        /* penalty for program breakdown *//* --- extensions --- */#define HMS_MINXEXT    1        /* minimal x-extension of maze */#define HMS_MINYEXT    1        /* minimal y-extension of maze */#define MAXHEAP     HMS_MAXCORN /* maximal size of corn heap */#define MINSPEED     0.1        /* minimal hamster speed */#define MAXSPEED     100        /* maximal hamster speed */#define FIELDWD       16        /* width of maze field (pixels) */#define SPRITEWD      13        /* width of hamster sprite (pixels) *//* --- menu definitions --- */#define MENUCNT        3        /* number of menus (titles) */#define ITEMCNT       12        /* number of menu items *//* --- error codes --- */#define OK             0        /* no error */#define E_NONE         0        /* no error */#define E_NOMEM      (-1)       /* not enough memory */#define E_FOPEN      (-2)       /* cannot open file */#define E_FREAD      (-3)       /* read error on file */#define E_FWRITE     (-4)       /* write error on file */#define E_POPEN      (-5)       /* cannot open pipe */#define E_PREAD      (-6)       /* read error on pipe */#define E_PWRITE     (-7)       /* write error on pipe */#define E_PBROKEN    (-8)       /* broken pipe */#define E_FORK       (-9)       /* cannot fork child process */#define E_EXEC      (-10)       /* cannot execute program */#define E_INIT      (-11)       /* initialization failed */#define E_WIDGET    (-12)       /* widget creation failed */#define E_COLOR     (-13)       /* color allocation failed */#define E_GRAPH     (-14)       /* cannot create graphics context */#define E_DIALOG    (-15)       /* cannot create dialog box widget */#define E_UNKNOWN   (-16)       /* unkown error */#define FN_MAX       (PATH_MAX +256)    /* max. length of file names */#define FMT_MAX      256        /* max. len. of error message format *//*----------------------------------------------------------------------  Type Definitions----------------------------------------------------------------------*/typedef struct {                /* --- application specific data */  int     xext, yext;           /* maze extensions */  float   wallprob;             /* wall probability */  int     total;                /* total corn in maze */  int     maxheap;              /* maximal size of corn heap */  float   speed;                /* hamster speed (fields/second) */  int     corncnt;              /* total corn collected */  int     movecnt;              /* number of moves made */  int     crshcnt;              /* number of crashes against walls */  int     score;                /* score achieved */  char    fn_maze[FN_MAX];      /* file name of current maze */  char    fn_hms [FN_MAX];      /* file name of executable */} AppData;                      /* (application specific data) */typedef struct {                /* --- hamster control block --- */  HAMSTER *hamster;             /* hamster to control */  SPRITE  *sprite;              /* sprite associated with hamster */  char    reply[64];            /* reply to be sent to client */} HMSCB;                        /* (hamster control block) */typedef struct {                /* --- error message data --- */  CCHAR   *name, *type;         /* name and type of the error message */  CCHAR   *class;               /* resource class of error message */  CCHAR   *dflt;                /* default message */  int     argc;                 /* number of arguments */} ERRMSG;                       /* (error message) *//*----------------------------------------------------------------------  Function Prototypes----------------------------------------------------------------------*/static void reply    (Widget w, XtPointer client, XtPointer call);static void result   (void);/* --- actions --- */static void load     (Widget w, XEvent *e, String *s, Cardinal *c);static void save     (Widget w, XEvent *e, String *s, Cardinal *c);static void select   (Widget w, XEvent *e, String *s, Cardinal *c);static void quit     (Widget w, XEvent *e, String *s, Cardinal *c);static void redraw   (Widget w, XEvent *e, String *s, Cardinal *c);static void size     (Widget w, XEvent *e, String *s, Cardinal *c);static void random   (Widget w, XEvent *e, String *s, Cardinal *c);static void clear    (Widget w, XEvent *e, String *s, Cardinal *c);static void speed    (Widget w, XEvent *e, String *s, Cardinal *c);static void exec     (Widget w, XEvent *e, String *s, Cardinal *c);static void about    (Widget w, XEvent *e, String *s, Cardinal *c);static void wall     (Widget w, XEvent *e, String *s, Cardinal *c);static void heap     (Widget w, XEvent *e, String *s, Cardinal *c);static void position (Widget w, XEvent *e, String *s, Cardinal *c);/*----------------------------------------------------------------------  Constants----------------------------------------------------------------------*/#include "xhamster.rsc"         /* fallback resources */#include "hamster.xbm"          /* hamster sprite bitmap */static Cardinal one = 1;        /* the number one (action parameter) */static XtActionsRec actions[] = {  { "load",     load     },     /* File    > Load Maze... */  { "save",     save     },     /* File    > Save Maze... */  { "select",   select   },     /* File    > Select Hamster... */  { "quit",     quit     },     /* File    > Quit */  { "redraw",   redraw   },     /* Actions > Redraw */  { "size",     size     },     /* Actions > Set Maze Size... */  { "random",   random   },     /* Actions > Randomize Maze... */  { "clear",    clear    },     /* Actions > Clear Maze */  { "speed",    speed    },     /* Actions > Set Hamster Speed... */  { "exec",     exec     },     /* Actions > Start/Stop Hamster */  { "about",    about    },     /* Help    > About Hamster */  { "wall",     wall     },     /* toggle wall/remove walls */  { "heap",     heap     },     /* change corn heap size */  { "position", position },     /* set start position */  { "db_close", db_close } };   /* close dialog box *//* --- error messages --- */static const ERRMSG errmsgs[] = {   /* error message data */  /* E_NONE      0 */  { "error",   "none",    "Xhamster.Error",                         "no error",                          0 },  /* E_NOMEM    -1 */  { "error",   "nomem",   "XHamster.Error",                         "not enough memory",                 0 },  /* E_FOPEN    -2 */  { "warning", "fopen",   "XHamster.Warning",                         "cannot open file:\n%s",             1 },  /* E_FREAD    -3 */  { "warning", "fread",   "XHamster.Warning",                         "read error on file:\n%s",           1 },  /* E_FWRITE   -4 */  { "warning", "fwrite",  "XHamster.Warning",                         "write error on file:\n%s",          1 },  /* E_POPEN    -5 */  { "warning", "popen",   "XHamster.Warning",                         "cannot open pipe to child process", 0 },  /* E_PREAD    -6 */  { "warning", "pread",   "XHamster.Warning",                         "read error on pipe",                0 },  /* E_PWRITE   -7 */  { "warning", "pwrite",  "XHamster.Warning",                         "write error on pipe",               0 },  /* E_PBROKEN  -8 */  { "warning", "pbroken", "XHamster.Warning",                         "broken pipe",                       0 },  /* E_FORK     -9 */  { "warning", "fork",    "XHamster.Warning",                         "cannot fork child process",         0 },  /* E_EXEC    -10 */  { "warning", "exec",    "XHamster.Warning",                         "cannot execute program:\n%s",       1 },  /* E_INIT    -11 */  { "error",   "init",    "XHamster.Error",                         "initialization failed",             0 },  /* E_WIDGET  -12 */  { "error",   "widget",  "XHamster.Error",                         "widget creation failed",            0 },  /* E_COLOR   -13 */  { "error",   "color",   "XHamster.Error",                         "color allocation failed",           0 },  /* E_GRAPH   -14 */  { "error",   "gc",      "XHamster.Error",                         "cannot create graphics context",    0 },  /* E_DIALOG  -15 */  { "warning", "dialog",  "XHamster.Warning",                         "cannot create dialog box",          0 },  /* E_UNKNOWN -16 */  { "error", "unknown",   "XHamster.Error",                         "unknown error",                     0 } };/*----------------------------------------------------------------------  Global Variables----------------------------------------------------------------------*/static AppData appdata = {      /* application specific data */  20, 15,                       /* initial maze extensions */  0.75, 500, 8,                 /* wall prob., total corn, max. heap */#ifdef DEMO  99,                           /* inital hamster speed */#else  2.5,                          /* inital hamster speed */#endif  0, 0, 0, 0,                   /* score variables */  NONAME,                       /* file name of maze */  "bin/chamster" };             /* file name of executable */static XtAppContext appctx;     /* X11 application context */static Atom      wm_delwin;     /* delete window atom of window mgr. */static Display   *display;      /* the display connected to */static int       screen;        /* screen of the display */static char      fmt[FMT_MAX];  /* format and buffer for messages */static char      buf[PATH_MAX +FMT_MAX];/* --- main widgets and menu --- */static Widget    w_top;         /* top level widget */static Widget    w_main;        /* main window widget */static Widget    w_view;        /* viewport widget for maze */static Widget    w_maze;        /* maze display widget */static Window    window;        /* window of maze widget */static MENU      *menu;         /* pull down menu */static GC        gc;            /* graphics context for maze *//* --- property sheets --- */static PSHEET    *psh_size   = NULL;  /* 'Set Maze Size' dialog box */static PSHEET    *psh_random = NULL;  /* 'Randomize Maze' dialog box */static PSHEET    *psh_speed  = NULL;  /* 'Set H. Speed' dialog box */static PSHEET    *psh_result = NULL;  /* 'Result' dialog box *//* --- color variables --- */static ULONG     black;         /* black  (walls) */static ULONG     white;         /* white  (normal fields) */static ULONG     grey;          /* grey   (start field) */static ULONG     yellow;        /* yellow (corn heaps) */static ULONG     orange;        /* orange (border of corn heaps) */static ULONG     red;           /* red    (the hamster) *//* --- child process and pipe variables --- */

⌨️ 快捷键说明

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