delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/10/19/14:04:08

From: "Bart van den Burg" <bart AT bart99 DOT tmfweb DOT nl>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: getkey()
Date: Fri, 19 Oct 2001 19:51:40 +0200
Organization: XO Communications B.V.
Lines: 611
Message-ID: <9qpp2o$b1t$1@cyan.nl.gxn.net>
References: <9qpj2s$96v$1 AT cyan DOT nl DOT gxn DOT net> <Xns913F803093AC5ASINANUNUR AT 132 DOT 236 DOT 56 DOT 8>
NNTP-Posting-Host: asd-tel-ap01-d04-161.dial.freesurf.nl
X-Trace: cyan.nl.gxn.net 1003513752 11325 62.100.3.161 (19 Oct 2001 17:49:12 GMT)
X-Complaints-To: abuse AT freesurf DOT nl
NNTP-Posting-Date: 19 Oct 2001 17:49:12 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

the program gives me no compilation errors at all (compliled with -Wall)
I could be wrong here but:
you have "(int)variable" which is the same as "int(variable), so
"(char)variable" is similar to "char(variable)"?

thx
Bart

here are the files
mine.cpp and letters.h are both written by myself completely and drawing.h
was made by me too, but I didn't write it. I copied it from a .cpp file
somewhere on the net and made a .h file of it.

///////////////////////// mine.cpp //////////////////////////
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include "drawing.h"

int minesA[9][9];
int numbersA[9][9];
int checkedA[9][9];
int mines = 10;
unsigned char color = 0;
unsigned char backgroundColor = 0;

void init();
void quit();
void printNumber(int x, int y);
void playGame();
int makeField(int width, int height, int mines);

#include "letters.h"

int main() {
 init();
 srand(time(NULL));

 makeField(9, 9, mines);
 while (mines > 0)
  playGame();
 getkey();
 quit();
}

void playGame() {
 char x = -1;
 char y = -1;
 bool markMine = false;
 while (!(x >= 1 && x <= 9)) {
  x = getkey() - 48;
  if (x == 35) {
   markMine = !markMine;
   color = int(markMine);
   regel("MARK MINE ", 60, 54, color);
  }
 }
 char buffer[0];
 sprintf(buffer, "%d", char(x));
 regel(buffer, 80, 40, 1);
 while (!(y >= 1 && y <= 9))
  y = getkey() - 48;
  if (y == 35) {
   markMine = !markMine;
   color = int(markMine);
   regel("MARK MINE ", 60, 54, color);
  }
 sprintf(buffer, "%d", char(y));
 regel(buffer, 80, 47, 1);
 int temp = 0;
 do {
  temp = getkey();
 } while (temp != 13);
 printNumber(x - 1, y - 1);
 mines = 0;
}

int makeField(int width, int height, int mines) {
 for (int temp = 0; temp < mines; temp++) {
  int x = rand() % 9;
  int y = rand() % 9;
  if (minesA[x][y] != 2)
   minesA[x][y] = 2;
 }

 for (int temp = 1; temp <= 10; temp++) {
  if (temp != 10) {
   char buffer[2];
   sprintf(buffer, "%d", temp);
   color = 1;
   regel(buffer, 6 * temp, 1, color);
   regel(buffer, 1, 6 * temp + 1, color);
  }
 }
 for (int temp = 1; temp <= 10; temp++) {
  color = 4;
  drawLine(4, 6 * temp, 58, 6 * temp, color);
  drawLine(6 * temp - 2, 6, 6 * temp - 2, 60, color);
 }

 for (int x = 0; x < 9; x++) {
  for (int y = 0; y < 9; y++) {
   for (int z1 = -1; z1 <= 1; z1++) {
    for (int z2 = -1; z2 <= 1; z2++) {
     if (z1 || z2) {
      if (minesA[x+z1][y+z2]) {
       if (minesA[x+z1][y+z2] == 2 && x+z1 > -1 && y+z2 > -1 && x+z1 < 9 &&
y+z2 < 9)
        numbersA[x][y]++;
      }
     }
    }
   }
   if (minesA[x][y] == 2)
    numbersA[x][y] = 9;
  }
 }
 color = 2;
 regel("GIVE X AND Y", 60, 6, color);
 regel("MARK MINE -> DEL ", 60, 12, color);
 regel("CONFIRM -> EXE", 60, 18, color);
 regel("LEFT:", 62, 30, 4);
 char buffer[3];
 sprintf(buffer, "%d", mines);
 regel(buffer, 82, 30, 1);
 regel("X:", 70, 40, 4);
 regel("Y:", 70, 47, 4);
 return 1;
}

void init() {
 if ( VGAMODE != setVGAMode(VGAMODE) ) {
  setVGAMode(VGA_TEXT);
  fprintf(stderr, "Could not set graphics mode: %d\n", VGAMODE);
  exit(1);
 }
}

void quit() {
 setVGAMode(VGA_TEXT);
}

void printNumber(int x, int y) {
 switch (numbersA[x][y]) {
  case 9:
   color = 15;
   break;
  case 1:
   color = 1;
   break;
  case 2:
   color = 2;
   break;
  case 3:
   color = 4;
   break;
  case 4:
   color = 5;
   break;
  case 5:
   color = 6;
   break;
  case 6:
   color = 3;
   break;
  case 7:
   color = 7;
   break;
  case 8:
   color = 8;
   break;
  case 0:
   color = 53;
   break;
 }
 char buffer[2];
 sprintf(buffer, "%d", numbersA[x][y]);
 regel(buffer, x, y, color);
}
/////////////////////////////////////////////////////////////

//////////////////////// letter.h ///////////////////////////
void regel(char *letters, int left, int top, unsigned char color) {
 int length = strlen(letters);
 int i = 0;
 while (i < length * 4) {
  switch(*letters++) {
   case '0':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 1, top, left + i + 1, top, color);
    drawLine(left + i + 2, top + 1, left + i + 2, top + 3, color);
    drawLine(left + i + 1, top + 4, left + i + 1, top + 4, color);
    drawLine(left + i, top + 3, left + i, top + 1, color);
    break;
   case '1':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top + 1, left + i + 1, top, color);
    drawLine(left + i + 1, top + 1, left + i + 1, top + 4, color);
    drawLine(left + i, top + 4, left + i + 2, top + 4, color);
    break;
   case '2':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i + 2, top, color);
    drawLine(left + i + 2, top, left + i + 2, top + 2, color);
    drawLine(left + i + 2, top + 2, left + i, top + 2, color);
    drawLine(left + i, top + 2, left + i, top + 4, color);
    drawLine(left + i, top + 4, left + i + 2, top + 4, color);
    break;
   case '3':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i + 2, top, color);
    drawLine(left + i + 2, top, left + i + 2, top + 4, color);
    drawLine(left + i, top + 2, left + i + 2, top + 2, color);
    drawLine(left + i, top + 4, left + i + 2, top + 4, color);
    break;
   case '4':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 2, color);
    drawLine(left + i, top + 2, left + i + 2, top + 2, color);
    drawLine(left + i + 2, top, left + i + 2, top + 4, color);
    break;
   case '5':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 2, top, left + i, top, color);
    drawLine(left + i, top, left + i, top + 2, color);
    drawLine(left + i, top + 2, left + i + 2, top + 2, color);
    drawLine(left + i + 2, top + 2, left + i + 2, top + 4, color);
    drawLine(left + i + 2, top + 4, left + i, top + 4, color);
    break;
   case '6':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 2, top, left + i, top, color);
    drawLine(left + i, top, left + i, top + 4, color);
    drawLine(left + i, top + 2, left + i + 2, top + 2, color);
    drawLine(left + i + 2, top + 2, left + i + 2, top + 4, color);
    drawLine(left + i + 2, top + 4, left + i, top + 4, color);
    break;
   case '7':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top + 1, left + i, top, color);
    drawLine(left + i, top, left + i + 2, top, color);
    drawLine(left + i + 2, top, left + i + 2, top + 4, color);
    break;
   case '8':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 4, color);
    drawLine(left + i + 2, top, left + i + 2, top + 4, color);
    drawLine(left + i, top, left + i + 2, top, color);
    drawLine(left + i, top + 2, left + i + 2, top + 2, color);
    drawLine(left + i, top + 4, left + i + 2, top + 4, color);
    break;
   case '9':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 2, color);
    drawLine(left + i + 2, top, left + i + 2, top + 4, color);
    drawLine(left + i, top, left + i + 2, top, color);
    drawLine(left + i, top + 2, left + i + 2, top + 2, color);
    drawLine(left + i, top + 4, left + i + 2, top + 4, color);
    break;
   case 'A':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 1, top, left + i + 1, top, color);
    drawLine(left + i, top + 1, left + i, top + 4, color);
    drawLine(left + i + 2, top + 1, left + i + 2, top + 4, color);
    drawLine(left + i, top + 2, left + i + 2, top + 2, color);
    break;
   case 'C':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 1, top, left + i + 2, top, color);
    drawLine(left + i, top + 1, left + i, top + 3, color);
    drawLine(left + i + 1, top + 4, left + i + 2, top + 4, color);
    break;
   case 'D':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 1, top, left + i, top, color);
    drawLine(left + i + 2, top + 1, left + i + 2, top + 3, color);
    drawLine(left + i + 1, top + 4, left + i, top + 4, color);
    drawLine(left + i, top, left + i, top + 4, color);
    break;
   case 'E':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    for (int j = 0; j <= 4; j += 2) {
     drawLine(left + i, top + j, left + i + 2, top + j, color);
    }
    drawLine(left + i, top, left + i, top + 4, color);
    break;
   case 'F':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    for (int j = 0; j <= 2; j += 2) {
     drawLine(left + i, top + j, left + i + 2, top + j, color);
    }
    drawLine(left + i, top, left + i, top + 4, color);
    break;
   case 'G':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 1, top, left + i + 2, top, color);
    drawLine(left + i, top + 1, left + i, top + 3, color);
    drawLine(left + i + 1, top + 4, left + i + 2, top + 4, color);
    drawLine(left + i + 2, top + 2, left + i + 2, top + 3, color);
    break;
   case 'I':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i + 2, top, color);
    drawLine(left + i + 1, top + 1, left + i + 1, top + 4, color);
    drawLine(left + i, top + 4, left + i + 2, top + 4, color);
    break;
   case 'K':
    drawFilledBox(left + i, top, left + i + 4, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 4, color);
    drawLine(left + i + 1, top + 2, left + i + 3, top, color);
    drawLine(left + i + 1, top + 2, left + i + 3, top + 4, color);
    i++;
    break;
   case 'L':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 4, color);
    drawLine(left + i, top + 4, left + i + 2, top + 4, color);
    break;
   case 'M':
    drawFilledBox(left + i, top, left + i + 5, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 4, color);
    drawLine(left + i + 4, top, left + i + 4, top + 4, color);
    drawLine(left + i, top, left + i + 2, top + 2, color);
    drawLine(left + i + 4, top, left + i + 2, top + 2, color);
    i += 2;
    break;
   case 'N':
    drawFilledBox(left + i, top, left + i + 4, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 4, color);
    drawLine(left + i + 3, top, left + i + 3, top + 4, color);
    drawLine(left + i, top, left + i + 3, top + 3, color);
    i++;
    break;
   case 'O':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 1, top, left + i + 1, top, color);
    drawLine(left + i + 1, top + 4, left + i + 1, top + 4, color);
    drawLine(left + i, top + 1, left + i, top + 3, color);
    drawLine(left + i + 2, top + 1, left + i + 2, top + 3, color);
    break;
   case 'R':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 4, color);
    drawLine(left + i, top, left + i + 1, top, color);
    drawLine(left + i + 2, top + 1, left + i + 2, top + 1, color);
    drawLine(left + i + 1, top + 2, left + i + 1, top + 2, color);
    drawLine(left + i + 2, top + 3, left + i + 2, top + 4, color);
    break;
   case 'T':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i + 2, top, color);
    drawLine(left + i + 1, top, left + i + 1, top + 4, color);
    break;
   case 'V':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 3, color);
    drawLine(left + i + 2, top, left + i + 2, top + 3, color);
    drawLine(left + i + 1, top + 4, left + i + 1, top + 4, color);
    break;
   case 'X':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 1, color);
    drawLine(left + i + 2, top, left + i + 2, top + 1, color);
    drawLine(left + i, top + 3, left + i, top + 4, color);
    drawLine(left + i + 2, top + 3, left + i + 2, top + 4, color);
    drawLine(left + i + 1, top + 2, left + i + 1, top + 2, color);
    break;
   case 'Y':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i, top + 1, color);
    drawLine(left + i + 2, top, left + i + 2, top + 1, color);
    drawLine(left + i + 1, top + 2, left + i + 1, top + 4, color);
    break;
   case '-':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top + 2, left + i + 2, top + 2, color);
    break;
   case '>':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i, top, left + i + 2, top + 2, color);
    drawLine(left + i, top + 4, left + i + 2, top + 2, color);
    break;
   case ':':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    drawLine(left + i + 1, top + 1, left + i + 1, top + 1, color);
    drawLine(left + i + 1, top + 3, left + i + 1, top + 3, color);
    break;
   case ' ':
    drawFilledBox(left + i, top, left + i + 3, top + 4, backgroundColor);
    break;
  }
  i += 4;
 }
}
/////////////////////////////////////////////////////////////

//////////////////////// drawing.h //////////////////////////
#include <dos.h>
#include <dpmi.h>
#include <go32.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/farptr.h>
#include <math.h>

typedef enum VGA_MODE {
    VGA_INVALID = -1,
    VGA_TEXT = 0x03,
    VGA_MODE13 = 0x13,
 VGA_MODE12 = 0x12,
    VGA_MODE11 = 0x11,
 VGA_MODE10 = 0x10,
    VGA_MODE9 = 0x9,
 VGA_MODE8 = 0x8,
    VGA_MODE7 = 0x7,
 VGA_MODE6 = 0x6,
    VGA_MODE5 = 0x5,
 VGA_MODE4 = 0x4,
 VGA_MODE2 = 0x2,
    VGA_MODE1 = 0x1,
 VGA_MODE0 = 0x0,
 VGAMODE = VGA_MODE13
} VGA_MODE;

static int sign(int n) {
    int ret = 0;
    if (n != 0) ret = ( (n > 0) ? 1 : -1 );
    return ret;
}

// set VGA Mode
VGA_MODE setVGAMode(VGA_MODE mode) {
    __dpmi_regs r;
    memset(&r, 0, sizeof(r));
    r.x.ax = mode;
    return ( __dpmi_int(0x10, &r) ? VGA_INVALID : mode );
}

void pp13(int x, int y, unsigned char /*color*/ c) {
    _farpokeb(_dos_ds, 0xa0000 + 320*y + x, c);
    return;
}

void nspp13(int x, int y, int c) {
    _farnspokeb(0xa0000 + 320*y + x, c);
    return;
}

unsigned char gp13(int x, int y) {
    return _farpeekb(_dos_ds, 0xa0000 + 320*y + x);
}

unsigned char nsgp13(int x, int y) {
    return _farnspeekb(0xa0000 + 320*y + x);
}

////////////////////////////////shapes////////////////////////////////////
// ======================== vertical line ============================= //
// x  = starting x coordinate      //
// y  = starting y coordinate      //
// dy = vertical distance in pixels     //
// c  = color        //

void drawVerticalLine(int x, int y, int dy, unsigned char /* color */ c) {
    int i;
    unsigned short old_sel = _fargetsel();

    _farsetsel(_dos_ds);
    for(i = 0; i <= abs(dy); i++)
        nspp13(x, y + sign(dy)*i, c);

    _farsetsel(old_sel);
    return;
}

// ======================== horizontal line =========================== //
// horizontal line              //
// x  = starting x coordinate           //
// y  = starting y coordinate           //
// dx = horizontal distance in pixels         //
// c  = color               //

void drawHorizontalLine(int x, int y, int dx, unsigned char /* color */ c) {
    int i;
    unsigned short old_sel = _fargetsel();

    _farsetsel(_dos_ds);
    for( i = 0; i <= abs(dx); i++)
        nspp13(x + sign(dx)*i, y, c);

    _farsetsel(old_sel);
    return;
}

// ============================= lines ================================ //
// general purpose line             //
// x1, y1 = starting point            //
// x2, y2 = end point             //
// c    = color              //

void drawLine(int x1, int y1, int x2, int y2, unsigned char /* color */ c) {
    double m;
    int i;
    unsigned short old_sel;
    int dx = x2 - x1;
    int dy = y2 - y1;

    /* handle vertical line */
    if( dx == 0 ) {
        drawVerticalLine(x1, y1, dy, c);
        return;
    }
    /* handle horizontal line */
    if ( dy == 0 ) {
        drawHorizontalLine(x1, y1, dx, c);
        return;
    }

    /* bad bad line drawing algorithm */
    m = ((double) dy)/dx;

    old_sel = _fargetsel();
    _farsetsel(_dos_ds);

    if( fabs(m) <= 1) {
        if( dx < 0 ) {
            x1 = x2; y1 = y2; dx = -dx;
        }

        for( i = 0; i < dx; i++)
            nspp13(x1 + i, (int)(y1 + m*i), c);
        nspp13(x2, y2, c);
    } else {
        m = ((double) dx/dy);
        if( dy < 0 ) {
            x1 = x2; y1 = y2; dy = -dy;
        }

        for( i = 0; i < dy; i++)
            nspp13((int)(x1 + m*i), y1 + i, c);
        nspp13(x2, y2, c);
    }

    _farsetsel(old_sel);
    return;
}

// =========================== circle ================================= //
// x, y = starting point            //
// size = diameter              //
// c  = color              //

void drawCircle(int x, int y, int size, unsigned char c) {
 unsigned short old_sel = _fargetsel();

 _farsetsel(_dos_ds);

 for (int i = 1; i < 359; i++) {
  nspp13(int(cos(i) * size + x), int(sin(i) * size + y), c);
 }

 _farsetsel(old_sel);
 return;
}

// ============================= box ================================== //
// x1, y1 = starting point            //
// x2, y2 = end point             //
// c    = color              //

void drawBox(int x1, int y1, int x2, int y2, unsigned char c) {
    drawVerticalLine(x1, y1, y2 - y1, c);
    drawVerticalLine(x2, y2, y1 - y2, c);
    drawHorizontalLine(x1, y1, x2 - x1, c);
    drawHorizontalLine(x2, y2, x1 - x2, c);

    return;
}

// ======================== filled box ================================ //
// x1, y1 = starting point      //
// x2, y2 = end point       //
// c    = color       //

void drawFilledBox(int x1, int y1, int x2, int y2, unsigned char c) {
    int i;
    int dx = x2 - x1;
    int dy = y2 - y1;

    if( abs(dx) < abs(dy) ) {
        if( dx < 0 ) {
            x1 = x2; y1 = y2; dx = -dx; dy = -dy;
        }
        for( i = 0; i <= dx; i++)
            drawVerticalLine(x1+i, y1, dy, c);
    } else {
        if( dy < 0 ) {
            x1 = x2; y1 = y2; dx = -dx; dy = -dy;
        }
        for( i = 0; i <= dy; i++)
            drawHorizontalLine(x1, y1+i, dx, c);
    }

    return;
}
/////////////////////////////////////////////////////////////


- Raw text -


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