www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/22/17:30:20

From: cx926 AT FreeNet DOT Carleton DOT CA (Stephen Lang)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: [Q]: Why is my djgpp program in C rebooting my machine?
Date: 22 Nov 1997 21:08:09 GMT
Organization: The National Capital FreeNet
Lines: 175
Message-ID: <657hjp$52l@freenet-news.carleton.ca>
References: <3474D145 DOT 5BE9A39C AT pdnt DOT com>
Reply-To: cx926 AT FreeNet DOT Carleton DOT CA (Stephen Lang)
NNTP-Posting-Host: freenet2.carleton.ca
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Consalus (consalus AT pdnt DOT com) writes:
> Okay, here is my problem.
> I am making a very simple program in djgpp. I am making a happy little
> pixel that avoids obstacles on the screen and
> eventually makes it to its destination point with as little moves as
> possible. I started off by making the dot moves to the destination.


Here is a half corrected version.
It probably still doesn't work for all starting and finishing positions.
I put in getch()'s to see what was happening, so hold the space bar ...



#include <stdio.h>
#include <dos.h>
#include <dpmi.h>
#include <sys/farptr.h>
#include <go32.h>
#include <stdlib.h>
#define sgn(x) ((x<0)?-1:((x>0)?1:0))
#define VGA_MODE 0
#define TEXT_MODE 1


struct coord {
    int             x;
    int             y;
};

struct coord    ailoc;

void            set_vgamode(int whatmode)
{
    if (whatmode == 0) {
        union REGS      regs;
        regs.h.ah = 0x00;
        regs.h.al = 0x13;
        int86(0x10, &regs, &regs);
    }
    if (whatmode == 1) {
        union REGS      regs;
        regs.h.ah = 0x00;
        regs.h.al = 0x03;
        int86(0x10, &regs, &regs);
    }
}

void            putpixel(int x, int y, unsigned char color)
{
    _farpokeb(_dos_ds, 0xa0000 + (y << 8) + (y << 6) + x, color);
}


int             get_pixel(int x, int y)
{
    int             thing;
    thing = _farpeekb(_dos_ds, 0xa0000 + (y << 8) + (y << 6) + x);
    return thing;
}



void            moveguy(int howmanyx, int howmanyy)
{
    int             bleah,
                    bleah2;
    putpixel(ailoc.x + howmanyx, ailoc.y + howmanyy, 15);
    bleah = ailoc.x + howmanyx;
    bleah2 = ailoc.y + howmanyy;
    putpixel(ailoc.x, ailoc.y, 0);
    ailoc.x = bleah;
    ailoc.y = bleah2;


}


void line(int x1, int y1, int x2, int y2, char color)
{
        short int dx, dy, sdx, sdy, x, y, px, py;

        dx = x2 - x1;
        dy = y2 - y1;

        sdx = (dx < 0) ? -1 : 1;
        sdy = (dy < 0) ? -1 : 1;

        dx = sdx * dx + 1;
        dy = sdy * dy + 1;

        x = y = 0;

        px = x1;
        py = y1;

        if (dx >= dy) {
                for (x = 0; x < dx; x++) {
                        _farpokeb(_dos_ds, 0xa0000 + (py << 8) + (py <<
6) + px, color);
                        y += dy;
                        if (y >= dx) {
                                y -= dx;
                                py += sdy;
                        }
                        px += sdx;
                }
        } else {
                for (y = 0; y < dy; y++) {
                        _farpokeb(_dos_ds, 0xa0000 + (py << 8) + (py <<
6) + px, color);
                        x += dx;
                        if (x >= dy) {
                                x -= dy;
                                px += sdx;
                        }
                        py += sdy;
                }
        }
}


void            main(void)
{
    struct coord    destloc;
    struct coord    distance;
    int             counter;
    set_vgamode(VGA_MODE);
    line(1, 1, 319, 1, 15);
    line(1, 1, 1, 199, 15);
    line(319, 199, 319, 1, 15);
    line(319, 199, 1, 199, 15);
    destloc.x = 300;
    destloc.y = 100;
    ailoc.x = 10;
    ailoc.y = 10;
    counter = 0;
    putpixel(destloc.x, destloc.y, 1);
    while (1) {
        distance.x = destloc.x - ailoc.x;
        distance.y = destloc.y - ailoc.y;
if (distance.x==0 && distance.y==0) {
            set_vgamode(TEXT_MODE);
            printf("\n\n   Yeah! It took %d turns to get to the destination.", counter);
exit(0);
}
        if (distance.x < distance.y) {
            moveguy(0, 1);
	    getch();
            continue;
        } else if (distance.x == distance.y) {
            moveguy(1,1);
	    getch();
	    continue;
        } else {
            moveguy(1, 0);
	    getch();
            continue;
        }


        delay(3);

    }
    getch();
    set_vgamode(TEXT_MODE);

}






- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019