#include "graphics.h" #include "math.h" #include "stdio.h" #include "conoe.h" #include "bios.h" #include "dos.h" #include "io.h"
Char key_state [128], key_pressed [128]; void interrupt far (* Oldint9handler) ();
enum modes {AUTO, HIGH}; enum bool {FALSE = 0, TRUE = 1}; # define MODE enum modes # define BOOL enum bool # define K_ENTER 0x1c # define K_DOWN 0x50 # define K_UP 0x48 # define K_SPACE 0x39 # define K_ESC 0x01 #define k_c 0x2e # define k_a 0x1e # define k_x 0x2d # define up 0 # define down 1 # define pi 3.1415 # Define mode_key 0 # define mode_cmd 1 # define max_cmd_num 15
/ * Instruction define * / # define exit 0 # define chm 1 # define pu 2 # define pd 3 # define chc 4 # define chht 5 # define adth 6 # define fw 7 # define bw 8 # define CLR 9 # define chP 10
enum hlpmsg {ALL_CMD, IN_ERROR, PU_OK, PD_OK, CHC_OK, CHP_OK, CHM_OK, FW_OK, BW_OK, CHHT_OK, ADTH_OK, CLR_OK, M_KEY, IN_COLOR, IN_POS, IN_HEAD, IN_STEPS}; # define HLPMSG enum hlpmsgtypedef int penState; typedef struct {float v, h;} apoint; typef struct {float heading; penstate pen; int color; apoint pos;
NewTurtle Turtle; char gmode = mode_cmd; char cmdwords [max_cmd_num] [4] = {{'e', 'x', 'i', 't'}, {'c', 'h', 'm', ' '}, {' P ',' u ',' ',' '}, {' p ',' d ',' ',' '}, {' c ',' h ',' c ',' ' }, {'C', 'h', 'h', 't'}, {'a', 'd', 't', 'h'}, {'f', 'w', ', ''}, {'B', 'W', '', ''}, {'c', 'L', 'R', ''}, {'c', 'h', 'P', ''},}; Char cmdtmp [4]; unsigned char number {Int Color; INT X1, X2, Y1, Y2;} Node;
/ * ------------------------------------- * // * Other functions * / void OpenSeccmdWnd () {setColor (2); Rectangle (510, 250, 595, 275); While (Bioskey (1) == 0) {setColor (2); line (513, 270, 518, 270); setColor (0 ); Line (513, 270, 518, 270); WINDOW (66, 17, 66, 17);} void closeccmdWnd ()} void closeseccmdWnd () {setFillStyle (Solid_Fill, 0); BAR (511, 250, 595, 275);} INT CVT4CTOINT (INT I) {IF (i == 0) Return NumTMP [0] NumTMP [1] * 10 NumTMP [2] * 100 NumTMP [3] * 1000-53328; Else Return NumTMP1 [0] NumTMP1 [1] * 10 NumTMP1 [2] * 100 NumTMP1 [3] * 1000-53328;} int GETAN4BINTERGER () {INT I, J; Union Regs Regs; unsigned char t; regs.h.ah = 0x07; IN_AGAIN: FOR (i = 0; I <4; i ) NumTMP [i] = 48; for (i = 0; i <4; i ) {in: while (bioskey (1) == 0) {;}; IF (Bioskey (1) == 0x1c0d) {BIOSKEY (0); goto out;} int86 (0x21, & regs, & regs); t = regs.h.al; if (t> 57 || t <48) goto in Else {for (j = 3; j> 0; J -) NumTMP [J] = NumTMP [J-1]; NUMTMP [0] = T; Printf ("% C", T);}}} while (bioskey (1) == 0) {;} IF (1) ! = 0x1c0d) {bioskey (0); CloseSecCmdWnd (); OpenSecCmdWnd (); goto IN_AGAIN;} OUT: return Cvt4CToInt (0);} void StoreTwo4BInterger () {int i, j; union rEGS regs; unsigned char t; regs .h.ah = 0x07; IN_AGAIN1: FOR (i = 0; i <4; i ) {numTmp [i] = 48; NumTMP1 [i] = 48;} for (i = 0; i <4; i ) { IN1: While (Bioskey (1) == 0) {;};
IF (Bioskey (1) == 0x1c0d) {Bioskey (0); goto over;} int86 (0x21, & regs, & regs); t = regs.h.al; if (t == 44) {Printf ("% C) ", t); goto output1;} IF (T> 57 || T <48) goto in1; else {for (j = 3; j> 0; j - NumTMP [J] = NUMTMP [J-1] NumTMP [0] = T; Printf ("% c", t);}}} while (bioskey (1) == 0) {;} IF (bioskey (1)! = 0x1c0d) {INT86 (0x21, & regs, & regs); T = regs.h.al; IF (t == 44) {Printf ("% c", t); goto out1;} closeseccmdWnd (); openseccmdWnd (); goto in_again1;} Out1: for (i = 0; i <4; i ) NUMTMP1 [I] = 48; for (i = 0; i <4; i ) {in2: while (Bioskey (1) == 0) {;}; if (Bioskey (1 ) == 0x1c0d) {bioskey (0); goto over;} int86 (0x21, & regs, & regs); t = regs.h.al; if (t> 57 || t <48) goto in2; else {for J = 3; J> 0; J - NumTMP1 [J] = NUMTMP1 [J-1]; NUMTMP1 [0] = T; Printf ("% C", T);}} while (Bioskey (1) == 0) {;} IF (Bioskey (1)! = 0x1c0d) {bioskey (0); CloseSecCmdWnd (); OpenSecCmdWnd (); goto IN_AGAIN1;} OVER:;} void far interrupt NewInt9 (void) {unsigned char ScanCode, temp; ScanCode = inportb (0x60); temp = inportb ( 0x61); OUTPORTB (0x61, Temp | 0x80); OUTPORTB (0x61, Temp & 0x7f); if (Scancode & 0x80) {Scancode & = 0x7f;
Key_State [scancode] = 0;} else {key_state [scancode] = 1; key_pressed [scancode] = 1;} outportb (0x20, 0x20);}; void installKeyboard (void) {INT i; for (i = 0; i <128; i ) Key_State [i] = key_pressed [i] = 0; Oldint9Handler = getVect (9); setVect (9, newint9);} void shutdownkeyboard (void) {setter (9, Old1nd9handler);} int GetKey ScanCode) {int res; res = key_state [ScanCode] | key_pressed [ScanCode]; key_pressed [ScanCode] = 0; return res;} void InitMyGraph (MODE m) {int graphdriver, graphmode; if (m == AUTO) graphdriver = Detect; Else if (m == high) {graphdriver = vga; graphmode = vgahi;} else {printf ("Wrong Prameter of /" init_mygraph (); / ""); return;} / * registerbgidriver (egavga_driver); * / Initgraph (& graphdriver, & graphmode, ");}; int getKey1 () {union regs rg; rg.h.ah = 0; int86 (0x16, & rg, & rg); return rg.h.ah;}; void InputNote () {While (bioskey (1) == 0) {setColor (9); line (467, 270, 472, 270); setColor (0); Line (467, 270, 472, 270);
} void getAcmd () {INT I, J; Window (60, 17, 60, 17); inputnote (); for (i = 0; i <4; i ) {again: while (Bioskey (1) == 0 ) {}; == 0x0f09) {bioskey (0); goto again;}} (bioskey (1) == 0x0E08) {BIOSKEY (0); goto again;}} (bioskey (1) == 0x1c0d) {for (; i <4; i ) cmdtmp [i] = '; Break;}; cmdtmp [i] = getCH (); Printf ("% c", cmdtmp [i]);}} INT decidentCMD () {INT I, J, R = Max_cmd_num; for (i = 0; I }}} F (r! = Max_cmd_num) Break;} return r;} / * Display the Turtle to Every Prameter of its OWN * / VOID DISTURTLE () {FLOAT X, Y, H, V, X1, Y1; Circle (Turtle .Pos.h-8, Turtle.Pos.v, 4); Circle (Turtle.Pos.h 8, Turtle.Pos.V, 4); Circle (Turtle.Pos.h, Turtle.Pos.v 8 , 4); Circle (Turtle.Pos.h, Turtle.Pos.V-8, 4); Circle (Turtle.Pos.h, Turtle.Pos.V, 15); H = 18 * COS (Turtle.Heading * PI / 180); v = 18 * sin (Turtle.Heading * pi / 180); x = Turtle.pos.h h; y = Turtle.pos.vv; Circle (x, y, 3); x = Turtle .Pos.hh; y = Turtle.pos.v v; x1 = Turtle.pos.h-15 * cos (Turtle.Heading * Pi / 180 pi / 45); Y1 = Turtle.pos.v 15 * SIN (Turtle.Heading * Pi / 180 Pi / 45); line (x, y, x1, y1); x1 = Turtle.pos.h-15 * cos (Turtle.Heading * Pi / 180-Pi / 45) Y1 = Turtle.Pos.V 15 * sin (Turtle.Heading * Pi / 180-Pi / 45); line (x, y, x1, y1);} void dishelpmenu () {setColor (2); OutTextxy 465, 55, "Help:"); Outtextxy (465, 300, "Turtle State:"); Outtextxy (465, 240, "Struction Input:"); Rectangle (465, 250, 510, 275);} void diststate () {setFillStyle (Solid_Fill, 8); BAR (465, 310, 595, 445); setColor (1); Outtextxy (480, 315, "Pen:"); OutTextxy (480, 328, "GMODE:"); Outtextxy (480, 341, "color:"); Outtextxy (480, 353, "Heading:"); Outtextxy (480, 365, "position"); Outtextxy (490, 377, " x: "); Outtextxy (490, 389," Y: "); OutTextxy (480, 401," x Range: "); Outtextxy (480, 422," y "); setColor (7); Outtextxy 520, 413, "0 ~ 360"); Outtextxy (520, 435, "0 ~ 350");} void renovatetState () {char * t; setfillstyle (Solid_Fill, 0); BAR (520, 315, 599, 322); setColor (7); if (Turtle.Pen == Up) Outtextxy (520, 315, "UP"); Else Outtextxy (520, 315, "down"); bar ( 536, 328, 599, 335; if (gmode == mode_key) Outtextxy (536, 328, "key"); else Outtextxy (536, 328, "command"); bar (550, 353, 599, 360); Sprintf (t, "% .1f", turtle.Heading; Outtextxy (550, 353, t); bar (515, 377, 599, 384); sprintf (t, "% .2f", turtle.pos.h -65); OutTextxy (515, 377, t); BAR (515, 389, 599, 396); Sprintf (t, "% .2f", turtle.pos.v-75); Outtextxy (515, 389, t ); SetFillStyle (Solid_Fill, Turtle.color); BAR (536, 341, 590, 348); if (Turtle.Color == 8) {setColor (7); Rectangle (536, 341, 590, 348);}} INT getacolor () {int C; OpenSecmdWnd (); c = getan4binterger (); return c;} float getanangle () {float h; openseccmdwnd (); h = getan4binterger (); return h;} float getx (float s) {Float x; x = Turtle.pos.h s * cos (Turtle.Heading * pi / 180); return x;} float gety Float s) {float y; y = Turtle.pos.vs * sin (Turtle.Heading * pi / 180); returnvatedarea () {file * fp; node n; if ((fp = fopen) Temp.txt "," RB ")) == NULL) {;} else {if (FileLength)! = 0) {fseek (fp, 0, 0); while (! feof (fp)) {Fread (& n, sizeof (node), 1, fp); setColor (n.color); line (n.x1, n.y1, n.x2, n.y2);} } Fclose (fp);} setColor (2); Disturtle (); LINE (65, 75, 70, 75); LINE (65, 75, 65, 80); Line (425, 75, 420, 75); line (425, 75, 425, 80); Line (425, 425, 420, 425); Line (425, 425, 425, 420); line (65, 425, 70, 425); line (65, 425, 65 , 420);} void cleardrawing () {file * fp; node n; if ((FP = FOPEN ("Temp.txt", "RB")) == null) {;} else {ix (FileLength FP))! = 0) {fseek (fp, 0, 0); while (! feof (fp)) {fread (& n, sizeof (node), 1, fp); setColor (0); line (n.x1 , n.y1, n.x2, n.y2);}} fclose (fp);} IF ((fp = fopen ("temp = fopen (" temp.txt "," w ")) == null) {;} else {fclose (fp);}} void Helpmsg (HLPMSG HM) {setFillStyle (Solid_Fill, 8); BAR (465, 65, 595, 235); switch (hm) {case all_cmd: {setcolor (1); Outtextxy (470, 70 , "Cmds listed:"); setColor (7); OutTextxy (490, 85, "PU, PD, CHC,"); Outtextxy (490, 100, "FW, BW, CHP,"); OutTextxy (490, 115, "Chht, adth,"); OutTextxy (490, 130, "CLR, CHM,"); OUTTEXTXY (490, 145, "exit"); setColor (1); Outtextxy (470, 175, "Make Sure:"); SetColor (7); Outtextxy (480, 190, "Capital"; setColor (1); Outtextxy (545, 190, "input!"); Break;} casein_error: {setcolor (4); Outtextxy (480, 140, " Illegal cmd !!! "); Break;} case pu_ok: {setColor (14); Outtextxy (480, 140," Pen Uo OK! "); Break;} case pd_ok: {setColor (14); OutTextxy (480, 140, "Pen Down OK!"); Break;} case fw_ok: {setColor (14); Outtextxy (480, 140, "Forward OK!"); Break;} case bw_ok: {setColor (14); Outtextxy (480 , 140, "Backward OK!"); Break;} case chc_ok: {setColor (14); Outtextxy (470, 130, "change color"); OutTextxy (520, 160, "ok!"); SetfillStyle (Solid_Fill, 8); BAR (535, 160, 590, 170); Outtextxy (535, 160, "!"); Break;} case chp_ok: {setcolor (14); Outtextxy (470, 140, "change"); SetFillStyle (Solid_Fill, 8); BAR (470, 140, 590, 159); Outtextxy (470, 140, "Change Postion); OutTextxy (520, 160," OK! "); Break;} Case CLR_OK: {setColor (14); OutTextxy (480, 140, "Clear OK!"); Break;} case chht_ok: {setColor (14); Outtextxy (470, 140, "Change Head to-"); OutTextxy (520, 160, " OK! "); Break;} case adth_ok: {setcolor (14); Outtextxy (480, 140," add to head "); Outtextxy (520, 160, "ok!"); Break;} case m_key: {setColor (1); Outtextxy (470, 70, "key in use:"); setColor (7); Outtextxy (470, 80, " Up arrow: "); setColor (1); Outtextxy (520, 90," forward "); setColor (7); Outtextxy (470, 100," Down Arrow: "); setColor (1); Outtextxy (520, 110 , "Backword"); setColor (7); Outtextxy (470, 120, "A:"); setColor (1); Outtextxy (490, 120, "change color"); setColor (7); OutTextxy (470, 130 , "Enter:"); setColor (1); Outtextxy (469, 140, "change pen stat"); setColor (7); OutTextxy (470, 150, "space:"); setColor (1); Outtextxy (480 , 160, "Change Heading"); SetColor (7); Outtextxy (470, 170, "C:"); SetColor (1); Outtextxy (490, 170, "Clear Draw"); SetColor (7); Outtextxy (470, 180, "x:"); setColor (1); Outtextxy (490, 180, "exit"); SetColor (7); Outtextxy (470, 190, "ESC:"); SetColor (1); Outtextxy (490, 200, "Return TO"); Outtextxy (490, 210, "Command Mode"); Break;} Case IN_COLOR: {setColor (1); OutTextxy (470, 100, "graph"); OutTextxy (490, 115, "driver & mod:"); setColor (7); Outtextxy (490, 130, "VGA & Vgahi"); Outtextxy (470, 150, "Input a number"); Outtextxy (470, 165, "Between 1 ~ 15 "); setColor (12); Outtextxy (470, 185," Thanks "); setColor (13); Outtextxy (540, 185," :) "); setColor (12); Outtextxy (470, 200," For cooperation! "); Break;} case in_head: {setColor (7); Outtextxy (480, 140," Using Degrees! "); Break;} case in_pos: {setColor (7); Outtextxy (470, 70," WITH / ", /"); Outtextxy (470, 85, "betWeen X and Y"); setColor (1); Outtextxy (470, 105, "eXample:"); setColor (7); Outtextxy (490, 120 , "24 166"); Outtextxy (470, 140, "Best within") OutTextxy (470, 155, "THE X & Y"); OutTextxy (520, 170, "Below!"); SetColor (12); Outtextxy (470, 185, "thanks"); setColor (13); Outtextxy (540 , 185, ":)"); setColor (12); Outtextxy (470, 200, "for battery!"); Break;} case in_steps: {setColor (7); Outtextxy (470, 110, "best within") Outtextxy (470, 125, "the x & y range"); Outtextxy (520, 140, "below!"); setColor (12); OutTextxy (470, 155, "thanks"); setColor (13); Outtextxy (540, 155, ":)"); setColor (12); Outtextxy (470, 170, "for cooperation!"); break;}}} / * ------------------------------------------------------------------------------------------------------------------------------------ --------- * / Void StartTurtlegraphics () {setColor (2); Rectangle (10, 10, 630, 470); Line (40, 40, 600, 40); Outtextxy (70, 30, "MyName: 9cbs"); Outtextxy (220, 30 "Experiment Num: 008"); Outtextxy (420, 30, "Question Num: 008"); Rectangle (40, 50, 450, 450); Rectangle (460, 50, 600, 450); Rectangle (15, 15 , 625, 465);} void startTurtle () {Turtle.pos.h = 245; Turtle.Pos.V = 250; Turtle.Color = Green; Turtle.Heading = 0; Turtle.Pen = Up; SetColor (2) Distutle ();} void penup () {Turtle.pen = Up;} void pendown () {Turtle.Pen = down;} int title} () {Return Turtle.Heading;} apoint * turtlepos () {Return & Turtle. POS;} void Movetto (apoint newpos) {file * fp; node n; if (newpos.h> 425) newpos.h = 65; if (newpos.h <65) newpos.h = 425; if (NewPos.V) > 425) NewPos.V = 75; if (NewPos.V <75) NewPos.V = 425; if (Turtle.Pen == Down) {setColor (Turtle.color); n.x1 = Turtle.pos.h; n.y1 = Turtle.pos.v; n.x2 = newpos.h; n.y2 = newpos.v; if ((fp = fopen ("Temp.txt", "ab")) == null) {;} else {fwrite (& n, sizeof (node), 1, fp); fclose (fp);}} setColor (0); Disturtle (); turtle.pos = newpos; SetColor (2); Disturtle ();} void move (int step) {file * fp; node n; apoint p; bool sc = false; pH = getX (steps); pv = getY (steps); if (pH> 425) {if (Turtle.Pen == Down) sc = true; pH = 65; Turtle.Pen = Up;} IF (pH <65) {if (Turtle.Pen == Down) sc = true; pH = 425 Turtle.Pen = Up;} f (PV> 425) {if (Turtle.Pen == Down) sc = true; PV = 75; Turtle.Pen = Up;} IF (PV <75) {if (Turtle.Pen == Down) SC = true; PV = 425 Turtle.Pen = Up;} setColor (0); Disturtle (); if (Turtle.Pen == Up) Movetto (P); Else {n.color = Turtle.color; n.x1 = (int) Turtle. POS.H; N.Y1 = (int) Turtle.Pos.V; Movetto (P); n.x2 = (int) Turtle.pos.h; n.y2 = (int) Turtle.pos.V; IF (fp = fopen ("temp.txt", "ab")) == null) {;} else {fwrite (& n, sizeof (node), 1, fp); fclose (fp);}} setColor (2) Disturtle (); if (sc == true) Turtle.Pen = Down;} void Turn (int de devrees) {Turtle.Heading = DegRees; if (Turtle.Heading> 360) While (Turtle.Heading> 360) {Turtle .Heading- = 360;}; if (Turtle.Heading <0) while (Turtle.Heading <0) {Turtle.Heading = 360;};} void turntto (float angle) {Turtle.Heading = Angle;} void setTurtlecolor INT color {Turtle.color = color;} Main () {INT I, J, Size; FLOAT T; file * fp; node n; apoint p; bool quit = false; hlpmsg hm = all_cmd; initMygraph (high); startTurtlegraphics (); dishelpmenu (); startTurtle () DistState (); renovatetState (); renovatedarea (); while (quit == false) {hm = all_cmd; helpmsg (hm); getAcmd (); if (getKey1 () == k_enter) {i = decidentcmd (); Switch (i) {copy exit: {cleardrawing (); quit = true; break;} case max_cmd_num: {helpmsg (in_ERROR); SLEEP (1); Break;} Case PU: {Penup (); hm = pu_ok; Break Case Pd: {PENDOWN (); hm = pd_ok; break;} case chm: {if (gmode == mode_key) gmode = mode_cmd; else gmode = mode_key; for (i = 0; i <4; i ) cmdtmp [i] = 0 Window (60, 17, 60, 17); Printf (""); helpmsg (m_key); installKeyboard (); while (1) {renovatedarea (); renovatetstate (); Delay (1000); if (getKey (k_esc)) Break; if (getKey (k_up)) {move (10);}}}} {move (-10);} if (getKey (k_space) {T = 1.0; setColor (0); Disturtle (); TURN (T); setColor (2); Disturtle ();} if (getKey (k_a)) {Turtle.Color ; if (Turtle.Color> 15) Turtle .color = 1;} f (getKey (k_enter)) {if (Turtle.pen == Up) Turtle.p EN = Down; Else Turtle.pen = Up;}} (getKey (k_c)) {cleardrawing ();}; if (getKey (k_x)) {cleardrawing (); goto end;}} shutdownKeyboard (); gmode = mode_cmd Break;} Case chc: {helpmsg (in_color); SetTurtlecolor (Getacolor ()); CloseSeccmdWnd (); HM = CHC_OK; Break;} Case Chht: {Helpmsg (In_Head); T = getanangle (); setColor (0); Disturtle (); TURNTTO (T); setColor (2 ); Discute (); closeseccmdwnd (); hm = chht_ok; break;} case adth: {helpmsg (in_head); t = getanangle (); setColor (0); Disturtle (); Turn (T); setColor (2) Disturtle (); CloseSeccmdWnd (); hm = adth_ok; break;} case chp: {Helpm SG (in_pos); OpenSeccmdWnd (); StoretWO4Binterger (); pH = CVT4CTOINT (0); PV = CVT4CTOINT (1); P.H = 65; P.V = 75; Movetto (P); CloseSeccmdWnd (); hm = CHP_OK; BREAK;} Case FW: {helpmsg (in_steps); OpenSecmdWnd (); t = getan4binterger (); move (int) t); CloseSeccmdWnd (); hm = fw_ok;