{"id":10,"date":"2004-07-27T00:32:48","date_gmt":"2004-07-27T04:32:48","guid":{"rendered":"http:\/\/www.hongliangjie.com\/blog\/?p=10"},"modified":"2008-05-31T23:30:43","modified_gmt":"2008-06-01T03:30:43","slug":"helping-yuting-1","status":"publish","type":"post","link":"https:\/\/www.hongliangjie.com\/blog\/2004\/07\/27\/helping-yuting-1\/","title":{"rendered":"\u8d2a\u5403\u86c7\u8fdb\u5ea6\uff081\uff09"},"content":{"rendered":"<p>\u73b0\u5728\u5b9e\u73b0\u4e86\u8ba1\u65f6\u5668\u673a\u5236\u548c\u4e8b\u4ef6\u673a\u5236\u3002\u4e0b\u4e00\u6b65\u662f\u600e\u4e48\u53bb\u5b9e\u73b0\u8d2a\u5403\u86c7\u7684\u8d70\u52a8\u3002<\/p>\n<p>\u73b0\u5728\u5206\u4e3a\u4e24\u4e2a\u6587\u4ef6\uff1asnake.h\u548csnake.c<\/p>\n<p>\u5982\u4e0b\uff1asnake.h<\/p>\n<pre lang=\"c\" line=\"1\">\r\n#define NEWEVENT (EventLink)malloc(sizeof(event))\r\n#define MAXSNAKELEN 30 \/* Max length of the snake *\/\r\n#define DEFAULTSNAKECOLOR WHITE \/* The default snake color *\/\r\n#define DEFAULTSPEED 100000 \/* The start speed of the snake *\/\r\n#define DEFAULTSTARTX 100 \/* The start x of the snake *\/\r\n#define DEFAULTSTARTY 100 \/* The start y of the snake *\/\r\n#define MAXEVENT 1 \/* The startup event number *\/\r\n\r\n\/* Keyboard definition *\/\r\n\r\n#define ENTER 13\r\n\r\n\/* The prototype of bool type *\/\r\n\r\ntypedef enum {\r\n    true,\r\n    false\r\n} bool;\r\n\r\n\/* The prototype of directions *\/\r\n\r\ntypedef enum {\r\n    up,\r\n    right,\r\n    down,\r\n    left\r\n} direction;\r\n\r\n\/* The prototype of a point of the snake *\/\r\n\r\ntypedef struct {\r\n    int posx;\r\n    int posy;\r\n    direction dir; \/* The current direction of this point *\/\r\n} point;\r\n\r\n\/* The prototype of the snake *\/\r\n\r\ntypedef struct {\r\n    point dots[MAXSNAKELEN];\r\n} snake;\r\n\r\n\r\n\r\n#ifndef __SNAKE\r\n\r\n\r\n\r\n\/* prevent multiple includes *\/\r\n\r\n#define __SNAKE\r\n\r\n\/* The prototype of the event *\/\r\n\r\ntypedef struct {\r\n    long interval; \/* The interval of the event occurs *\/\r\n    long timer;\r\n    void (*invoke)(); \/* The function pointer to the invoke action *\/\r\n\r\n} event;\r\n\r\n\/* The prototype of the eventlist *\/\r\n\r\nstruct eventlist {\r\n    event *eventnow;\r\n    struct eventlist *next;\r\n\r\n};\r\n\r\ntypedef struct eventlist Node;\r\ntypedef Node *Link;\r\ntypedef event *EventLink;\r\n\r\n\r\nint screenx = 0;\r\nint screeny = 0;\r\n\r\n\r\n\r\nvoid Init(snake *s);\r\nvoid EventHandler(Link);\r\nvoid EventInsert(Link, EventLink);\r\nLink EventCreater(Link);\r\nvoid EventFree(Link);\r\n\r\n\r\n\r\n\/* Registry Events *\/\r\n\r\nvoid Controler(); \/* The main keyboard controler *\/\r\nvoid DrawPoint();\r\nvoid DrawLine();\r\n\r\nvoid DrawSnake(snake *s);\r\nvoid EventInsert(Link eventhead, EventLink newevent) {\r\n\r\n    Link pointer;\r\n    Link newlist;\r\n    pointer = eventhead;\r\n\r\n    while (1) {\r\n\r\n        if (pointer->next == NULL) {\r\n            newlist = (Link) malloc(sizeof (Node));\r\n            newlist->eventnow = newevent;\r\n            newlist->next = NULL;\r\n            pointer->next = newlist;\r\n            break;\r\n\r\n        }\r\n\r\n        pointer = pointer->next;\r\n    }\r\n}\r\n\r\nLink EventCreater(Link eventhead) {\r\n\r\n    Link newevent;\r\n    EventLink tempevent;\r\n    int i;\r\n    eventhead = (Link) malloc(sizeof (Node));\r\n    \/* Create event list *\/\r\n    if (eventhead == NULL) {\r\n        printf(\"Memory allocate Failure!!\\n\");\r\n\r\n    } else {\r\n        tempevent = (EventLink) malloc(sizeof (event));\r\n        tempevent->interval = 1;\r\n        tempevent->timer = 0;\r\n        tempevent->invoke = Controler;\r\n        eventhead->eventnow = tempevent;\r\n        eventhead->next = NULL;\r\n    }\r\n    return eventhead;\r\n\r\n}\r\n\r\nvoid EventFree(Link eventhead) {\r\n\r\n    Link pointer;\r\n    EventLink tempevent;\r\n    while (eventhead != NULL) {\r\n        \r\n        pointer = eventhead;\r\n        eventhead = eventhead->next;\r\n        tempevent = eventhead->eventnow;\r\n        free(pointer);\r\n        free(tempevent);\r\n    }\r\n}\r\n#endif \r\n<\/pre>\n<p>Snake.c<\/p>\n<pre lang=\"c\" line=\"1\">\r\n#include <stdio.h>\r\n#include <stdlib.h>\r\n#include <time.h>\r\n#include <conio.h>\r\n#include <graphics.h>\r\n#include <dos.h>\r\n#include <snake.h>\r\n\/* To return the head pointer of the eventlist *\/\r\nint main(void) {\r\n\r\n    snake me;\r\n    Link head;\r\n    int gdriver = DETECT, gmode;\r\n    EventLink tempevent;\r\n\r\n    \/* Init graphic mode *\/\r\n    initgraph(&gdriver, &gmode, \"\");\r\n    Init(&me);\r\n    head = EventCreater(head);\r\n    tempevent = NEWEVENT;\r\n    tempevent->interval = 10000;\r\n    tempevent->timer = 0;\r\n    tempevent->invoke = DrawPoint;\r\n    EventInsert(head, tempevent);\r\n    EventHandler(head);\r\n    EventFree(head);\r\n    closegraph();\r\n    clrscr();\r\n    return 0;\r\n}\r\n\r\nvoid DrawLine() {\r\n\r\n    int x;\r\n    int y;\r\n    x = random(500);\r\n    y = random(500);\r\n    circle(x, y, 20);\r\n}\r\n\r\nvoid DrawPoint() {\r\n\r\n    int x1, x2;\r\n    int y1, y2;\r\n    x1 = random(200);\r\n    x2 = random(100);\r\n    y1 = random(200);\r\n    y2 = random(100);\r\n    line(x1, y1, x2, y2);\r\n}\r\n\r\nvoid Controler() {\r\n\r\n}\r\n\r\nvoid Init(snake *s) {\r\n\r\n    int i, j;\r\n    int sx = DEFAULTSTARTX, sy = DEFAULTSTARTY;\r\n    randomize();\r\n    for (i = 0; i < MAXSNAKELEN; i++) {\r\n        s->dots[i].posx = sx;\r\n        s->dots[i].posy = sy;\r\n        s->dots[i].dir = up;\r\n        sy++;\r\n    }\r\n\r\n    screenx = getmaxx();\r\n    screeny = getmaxy();\r\n}\r\n\r\n\/* Draw snake *\/\r\n\r\nvoid DrawSnake(snake *s) {\r\n\r\n    int i;\r\n    for (i = 0; i < MAXSNAKELEN; i++) {\r\n        putpixel(s->dots[i].posx, s->dots[i].posy, DEFAULTSNAKECOLOR);\r\n    }\r\n}\r\n\r\nvoid EventHandler(Link eventhead) {\r\n\r\n    int i;\r\n    Link pointer;\r\n    while (1) {\r\n        delay(1);\r\n        pointer = eventhead;\r\n        while (pointer != NULL) {\r\n            pointer->eventnow->timer++;\r\n            if ((pointer->eventnow->timer) >= (pointer->eventnow->interval)) {\r\n                \/* Invoke current event *\/\r\n                pointer->eventnow->invoke();\r\n                pointer->eventnow->timer = 0;\r\n            }\r\n\r\n            pointer = pointer->next;\r\n        }\r\n\r\n        if (kbhit()) {\r\n            if (getch() == ENTER)\r\n                break;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u73b0\u5728\u5b9e\u73b0\u4e86\u8ba1\u65f6\u5668\u673a\u5236\u548c\u4e8b\u4ef6\u673a\u5236\u3002\u4e0b\u4e00\u6b65\u662f\u600e\u4e48\u53bb\u5b9e\u73b0\u8d2a\u5403\u86c7\u7684\u8d70\u52a8\u3002 \u73b0\u5728\u5206\u4e3a\u4e24\u4e2a\u6587\u4ef6\uff1asnake.h\u548csnake [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[8],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-thinking-on-the-keyboard","tag-8"],"_links":{"self":[{"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":0,"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongliangjie.com\/blog\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}