Demineur

Contenu du snippet

Un Démineur que j'ai réalisé sous borland C++ 3.0,dont l'interface graphique est assez original

Source / Exemple :


//Demineur

//By Neurokida 19/06/01.

#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include<time.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <mem.h>               // memset, memcpy
#include <stdio.h>             // fread, printf
#include <conio.h>             // getch, kbhit
#include <string.h>

int  LARGEURs =  16;         // largeur de la police
int  HAUTEURs =  16;          // hauteur de la police

char *ecran  = (char *) (0xA0000000L);

char *virtuel = new unsigned char[64000L];

char *police  = (char *) (0xF000FA6EL);

// Message qui deroule sur l'ecran
unsigned char message[] = "Game Over !!!";

// Dessine un caractere
void dessinetexte(int xpos, int ypos, int c, unsigned char coul)
{
  char far *car = &police[(c & 255) * 8]; // Obtient l'adresse du caractere
  unsigned char bit;                      // Utiliser pour masquer l'octet

  for (int y=0; y<HAUTEURs; y+=HAUTEURs/8)
  {
    if (y + ypos < 200)
    {
      bit = 0x80;
      for (int x=0; x<LARGEURs; x+=LARGEURs/8)
      {
	if ((x + xpos < 320) && (x + xpos > 0))
	  if (*car & bit) virtuel[(y+ypos)*320+x+xpos] = coul;
	bit >>= 1;                // Si le bit est egal a 1, affiche le pixel
      }
    }
    coul++; // Incremente la couleur
    car++;  // Prochain octet du bitmap
  }
}

void textePerdu(int x,int y)
{

   for (int i=0; message[i]; i++) dessinetexte(x+(i*LARGEURs),y,message[i],22);

}

unsigned char messageGagne[] = "You Win !!!";
void texteGagne(int x,int y)
{

   for (int i=0; messageGagne[i]; i++) dessinetexte(x+(i*LARGEURs),y,messageGagne[i],22);

}

unsigned char Replays[] = "Rejouer?";
void Replay(int x,int y)
{

 for (int i=0; Replays[i]; i++) dessinetexte(x+(i*LARGEURs),y,Replays[i],22);

}

unsigned char Goouts[] = "Exit?";
void Goout(int x,int y)
{

 for (int i=0; Goouts[i]; i++) dessinetexte(x+(i*LARGEURs),y,Goouts[i],22);

}

unsigned char NeurokidaProducs[] = "-\\ A Neurokida production /-";
void NeurokidaProduc(int x,int y)
{

 for (int i=0; NeurokidaProducs[i]; i++) dessinetexte(x+(i*LARGEURs),y,NeurokidaProducs[i],59);

}

unsigned char DemineurTextes[] = "<Demineur>";
void DemineurTexte(int x,int y)
{

 for (int i=0; DemineurTextes[i]; i++) dessinetexte(x+(i*LARGEURs),y,DemineurTextes[i],55);

}

unsigned char Instructions[] = "Instructions :";
void Instruction(int x,int y)
{

 for (int i=0; Instructions[i]; i++) dessinetexte(x+(i*LARGEURs),y,Instructions[i],55);

}

unsigned char InstructionsSourieGs[] = "Sourie Gauche =";
void InstructionsSourieG(int x,int y)
{

 for (int i=0; InstructionsSourieGs[i]; i++) dessinetexte(x+(i*LARGEURs),y,InstructionsSourieGs[i],55);

}

unsigned char InstructionsSourieGs2[] = "- Deminer.";
void InstructionsSourieG2(int x,int y)
{

 for (int i=0; InstructionsSourieGs2[i]; i++) dessinetexte(x+(i*LARGEURs),y,InstructionsSourieGs2[i],60);

}

unsigned char InstructionsSourieDs[] = "Sourie Droite =";
void InstructionsSourieD(int x,int y)
{

 for (int i=0; InstructionsSourieDs[i]; i++) dessinetexte(x+(i*LARGEURs),y,InstructionsSourieDs[i],55);

}

unsigned char InstructionsSourieDs2[] = "- Drapeaux.";
void InstructionsSourieD2(int x,int y)
{

 for (int i=0; InstructionsSourieDs2[i]; i++) dessinetexte(x+(i*LARGEURs),y,InstructionsSourieDs2[i],60);

}

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////

void setmode(unsigned int mode)
{
asm {
MOV AX, [mode]
INT 0x10
}}

//CLS l'ecran.
void clsvirt()
{memset(virtuel,0,64000L);}

//RAM TO memoire video
void cpyvirt()
{memcpy(ecran,virtuel,64000L);}

void pixel(int x, int y, unsigned char col)
{
if ((x<0)||(x>319)||(y<0)||(y>199))
return;
virtuel[(y*320)+x] = col;
}

void line(int x1, int y1, int x2, int y2, unsigned char coul)
{
   int x_chan, y_chan;                // Pour le changement dans x et y
   int offset = (y1<<8)+(y1<<6) + x1; // Calcule l'offset dans la RAM
   int ydiff = y2-y1;                 // Calcule la difference entre y2 et y1
   int deviation =  0;                // Initialise la deviation a 0;

   if (ydiff < 0)                     // Si la ligne va dans un direction -
   {
     ydiff = -ydiff;
     y_chan = -320;
   }
   else y_chan = 320;

   int xdiff = x2-x1;                 // Calcule la difference entre x2 et x1

   if (xdiff < 0)                     // Si la ligne va dans un direction +
   {
	 xdiff = -xdiff;
	 x_chan = -1;
   }

   else x_chan = 1;

   if (xdiff > ydiff)  {
     int longueur = xdiff+1;
     for(int i = 0; i < longueur; i++)
     {
       virtuel[offset] = coul;
       offset+=x_chan;
       deviation+=ydiff;
     if (deviation>xdiff){
deviation-=xdiff;
offset+=y_chan;}}}
else
{
int longueur = ydiff+1;
for(int i = 0; i < longueur; i++)
{
virtuel[offset] = coul;
offset+=y_chan;
deviation+=xdiff;
if (deviation>0)
{
deviation-=ydiff;
offset+=x_chan;}}}}

/*________________________________________________________//
			  SOURIE                         */

//Delimiter la ou la sourie a le droit d'aller
void SourieDelimitation()
{_AX=7;
_CX=24;
_DX=620;
geninterrupt(0x33);
_AX=8;
_CX=20;
_DX=194;
geninterrupt(0x33);}

struct // Structure pour la souris
{
			  int X;
			  int Y;
			  int bouton1;
			  int bouton2;
			  int bouton3;
			  int bouton0;
			  } mouse;

			  void mouse_xy()
			  {
			  int x,y,boutons=0;
			  asm{
			  MOV AX, 0x03
			  INT 0x33
			  MOV [x], CX
			  MOV [y], DX
			  MOV [boutons], BX
			  }
			  mouse.Y = y;
			  mouse.X = x >> 1;
			  if (boutons == 0) { mouse.bouton1 = 0; mouse.bouton2 = 0; }
			  if (boutons == 1) { mouse.bouton1 = 1; mouse.bouton2 = 0; }
			  if (boutons == 2) { mouse.bouton1 = 0; mouse.bouton2 = 1; }
			  if (boutons == 3) { mouse.bouton1 = 1; mouse.bouton2 = 1; }

}
/*________________________________________________________//*/

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

unsigned char  LARGEUR =8;
unsigned char  HAUTEUR =8;

unsigned char EXIT=0;
unsigned char Rejouer=1;

const unsigned char NombreDeMines=10;

unsigned int PointX=0;
unsigned char PointY=0;

unsigned int AngleInit=0;

unsigned char CouleurCercle=10;

void dessinecar(int xpos, int ypos, int c, unsigned char coul)
{
  char far *car = &police[(c & 255) * 8]; // Obtient l'adresse du caractere
  unsigned char bit;                      // Utiliser pour masquer l'octet

  for (int y=0; y<HAUTEUR; y+=HAUTEUR/8)
  {
    if (y + ypos < 200)
    {
      bit = 0x80;
      for (int x=0; x<LARGEUR; x+=LARGEUR/8)
      {
	if ((x + xpos < 320) && (x + xpos > 0))
	  if (*car & bit) virtuel[(y+ypos)*320+x+xpos] = coul;
	bit >>= 1;                // Si le bit est egal a 1, affiche le pixel
      }
    }
    coul++; // Incremente la couleur
    car++;  // Prochain octet du bitmap
  }
}

//Structure Qui contient X,Y....
struct Mines
{
	int X;
	unsigned char Y;
	unsigned char Mine;
	unsigned char Visible;
	unsigned char DrapeauxOn;
	unsigned char MinesAlentours;
	unsigned char PointX;
	unsigned char PointY;
};

/////////////////////////
struct Mines Mine[10][10];//
//\\\\\\\\\\\\\\\\\\\\\//

//Met le cercle ds l'ecran virtuel
void Cercle(int X,int Y,int Rayon,unsigned char Coul)
{

int PointX,PointY;

PointX= X + Rayon*cos((3.14159265359/180)*AngleInit);
PointY= Y + Rayon*sin((3.14159265359/180)*AngleInit);
pixel(PointX,PointY,Coul);

AngleInit+=1;
if(AngleInit==360) AngleInit=0;

if((AngleInit>180) && (AngleInit<270)) CouleurCercle=15;
else CouleurCercle=10;
}

void Carre(int X,int Y)//Rempli la case pour la cacher
{

float Coul=80;
for(int i=0;i<15;i++)
{
Coul+=0.2;
for (int e=0;e<15;e++)
{
pixel((Mine[X][Y].X-7)+e,(Mine[X][Y].Y-7)+i,Coul);
if(Mine[X][Y].DrapeauxOn==1)//DRAPEAUX
{
line(Mine[X][Y].X,Mine[X][Y].Y-1,Mine[X][Y].X-5,Mine[X][Y].Y-4,40);
line(Mine[X][Y].X-6,Mine[X][Y].Y-4,Mine[X][Y].X,Mine[X][Y].Y-4,40);
line(Mine[X][Y].X,Mine[X][Y].Y-4,Mine[X][Y].X,Mine[X][Y].Y+4,15);
}
}
}
}

void InitTable()
{

for (unsigned char i=0;i<10;i++)//Initialise le tableau de structures
	{
	for (unsigned char e=0;e<10;e++)
	{
	Mine[e][i].X=0;
	Mine[e][i].Y=0;
	Mine[e][i].Visible=0;
	Mine[e][i].Mine=0;
	Mine[e][i].DrapeauxOn=0;
	Mine[e][i].MinesAlentours=0;
	Mine[e][i].PointX=0;
	Mine[e][i].PointY=0;
	}
	}

int CoordonnesX=10;
unsigned char CoordonnesY=35;
for (i=0;i<10;i++,CoordonnesY+=16)
{
	for (unsigned char e=0;e<10;e++)
	{
		Mine[e][i].X=(CoordonnesX+=16);
		Mine[e][i].Y=CoordonnesY;
	}
CoordonnesX=10;
}

for(i=0;i<NombreDeMines;i++)//Pose une mine al‚atoirement
	{
	unsigned char RandMineDeposeX=random(10);//Nombre de cases
	unsigned char RandMineDeposeY=random(10);
	Mine[RandMineDeposeX][RandMineDeposeY].Mine=1;
	}

for (unsigned char e=0;e<10;e++)
{
	for (int i=0;i<10;i++)
	{
	unsigned char MineAlentours=0;
	if((Mine[e][i].Mine)!=1)

	{//dessus,dessous,gauche,droite
	if((Mine[e+1][i].Mine==1) && (e!=9)) MineAlentours++;
	if((Mine[e][i-1].Mine==1) && (i!=0)) MineAlentours++;
	if((Mine[e][i+1].Mine==1) && (i!=9)) MineAlentours++;
	if((Mine[e-1][i].Mine==1) && (e!=0)) MineAlentours++;

	//4 diagonales
	if((Mine[e-1][i-1].Mine==1)&&(i!=0)&&(e!=0)) MineAlentours++;
	if((Mine[e+1][i+1].Mine==1)&&(i!=9)&&(e!=9)) MineAlentours++;
	if((Mine[e-1][i+1].Mine==1)&&(i!=9)&&(e!=0)) MineAlentours++;
	if((Mine[e+1][i-1].Mine==1)&&(i!=0)&&(e!=9)) MineAlentours++;

	Mine[e][i].MinesAlentours=MineAlentours;//Assigne a la structure de la mine,combien de mine sont au alentours
	}
}}}

void ConvertionToChar()
{
for(int i=0;i<10;i++)
{
for(int e=0;e<10;e++)
{
	if (Mine[i][e].MinesAlentours==0) Mine[i][e].MinesAlentours='0';
	if (Mine[i][e].MinesAlentours==1) Mine[i][e].MinesAlentours='1';
	if (Mine[i][e].MinesAlentours==2) Mine[i][e].MinesAlentours='2';
	if (Mine[i][e].MinesAlentours==3) Mine[i][e].MinesAlentours='3';
	if (Mine[i][e].MinesAlentours==4) Mine[i][e].MinesAlentours='4';
	if (Mine[i][e].MinesAlentours==5) Mine[i][e].MinesAlentours='5';
	if (Mine[i][e].MinesAlentours==6) Mine[i][e].MinesAlentours='6';
	if (Mine[i][e].MinesAlentours==7) Mine[i][e].MinesAlentours='7';
	if (Mine[i][e].MinesAlentours==8) Mine[i][e].MinesAlentours='8';
}}}

void GrilleDemineur()
{
unsigned char PointX=0,PointY=0;

for (int i=0;i<10;i++)
{
for (int e=0;e<10;e++)
{
PointX=(Mine[e][i].X)-8;//Permet de savoir l'extremit‚ en haut a gauche du point centrale
PointY=(Mine[e][i].Y)-8;//a partir de ce point ont peut tracer le carrer et pour la sourie
Mine[e][i].PointX=PointX;
Mine[e][i].PointY=PointY;
}
}
}

void ObjetsVisuel()
{
LARGEURs =  16;         // largeur de la police
HAUTEURs =  16;
DemineurTexte(20,8);

LARGEURs =  8;         // largeur de la police
HAUTEURs =  8;
Instruction(220,8);

InstructionsSourieG(205,30);
InstructionsSourieG2(205,40);

InstructionsSourieD(205,60);
InstructionsSourieD2(205,70);
for(int i=0;i<10;i++)
{
for (int e=0;e<10;e++)
{
line(Mine[e][i].PointX,Mine[e][i].PointY,Mine[e][i].PointX+16,Mine[e][i].PointY,24);
line(Mine[e][i].PointX,Mine[e][i].PointY,Mine[e][i].PointX,Mine[e][i].PointY+16,26);
if(Mine[i][e].Visible==0) Carre(i,e);
}
}
line(18,187,178,187,78);
line(178,187,178,27,79);

line(200,0,200,196,26);
line(202,0,202,198,28);

line(200,196,0,196,26);
line(202,198,0,198,28);

line(202,85,319,85,28);
line(202,87,319,87,28);
}

void AffichageChiffres()
{
for(int i=0;i<10;i++)
{
for (int e=0;e<10;e++)
//if(Mine[i][e].Mine==0) dessinecar(Mine[i][e].X-2,Mine[i][e].Y-3,Mine[i][e].MinesAlentours,24);
if((Mine[i][e].Visible==1)&&(Mine[i][e].MinesAlentours!='0'))
{
dessinecar(Mine[i][e].X-2,Mine[i][e].Y-3,Mine[i][e].MinesAlentours,24);
}
}
}

void GameOver()
{
delay(2000);
unsigned char ExitWhile=0;
clsvirt();
while(ExitWhile==0)
{
line(50,80,120,80,10);
line(50,100,120,100,10);
line(50,80,50,100,12);
line(120,80,120,100,12);

line(270,100,270,80,10);
line(200,100,270,100,10);
line(200,80,270,80,12);
line(200,100,200,80,12);

mouse_xy();
pixel(mouse.X,mouse.Y-1,10);
pixel(mouse.X+1,mouse.Y+1,10);
pixel(mouse.X-1,mouse.Y+1,10);
LARGEURs =  16;
HAUTEURs =  16;
textePerdu(63,45);
LARGEURs =  8;         // largeur de la police
HAUTEURs =  8;
Goout(215,87);
Replay(55,87);
NeurokidaProduc(50,130);
if (mouse.bouton1)
{

if ((mouse.X>50) && (mouse.X<120) && (mouse.Y>80) && (mouse.Y<100))
{
ExitWhile=1;
Rejouer=1;
printf("\a");
delay(2000);
}
if ((mouse.X>200) && (mouse.X<270) && (mouse.Y>80) && (mouse.Y<100))
{
ExitWhile=1;
Rejouer=0;
printf("\a");
}
}
cpyvirt();
clsvirt();
}
EXIT=1;
}

//recherche si toute les case ont et‚ deterr‚
void Gagne()
{

unsigned char MinesDeminees=0;
for(int i=0;i<10;i++)
{
for (int e=0;e<10;e++)
{
if ((Mine[i][e].Visible==0) && (Mine[i][e].Mine==0)) MinesDeminees++;
}
}

if (MinesDeminees==0)
{
delay(4000);
unsigned char ExitWhile=0;
clsvirt();
while(ExitWhile==0)
{
line(50,80,120,80,10);
line(50,100,120,100,10);
line(50,80,50,100,12);
line(120,80,120,100,12);

line(270,100,270,80,10);
line(200,100,270,100,10);
line(200,80,270,80,12);
line(200,100,200,80,12);

mouse_xy();
pixel(mouse.X,mouse.Y-1,10);
pixel(mouse.X+1,mouse.Y+1,10);
pixel(mouse.X-1,mouse.Y+1,10);
LARGEURs =  16;
HAUTEURs =  16;
texteGagne(66,45);
LARGEURs =  8;         // largeur de la police
HAUTEURs =  8;
Goout(215,87);
Replay(55,87);
NeurokidaProduc(50,130);
if (mouse.bouton1)
{

if ((mouse.X>50) && (mouse.X<120) && (mouse.Y>80) && (mouse.Y<100))
{
ExitWhile=1;
Rejouer=1;
printf("\a");
delay(2000);
}
if ((mouse.X>200) && (mouse.X<270) && (mouse.Y>80) && (mouse.Y<100))
{
ExitWhile=1;
Rejouer=0;
printf("\a");
}
}
cpyvirt();
clsvirt();
}
EXIT=1;
}
}

void ActionClickDroit()
{
for(int i=0;i<10;i++)
{
for(int e=0;e<10;e++)
{
	if ((mouse.X>Mine[i][e].PointX) && (mouse.X<Mine[i][e].PointX+16) && (mouse.Y>Mine[i][e].PointY) && (mouse.Y<Mine[i][e].PointY+16))
{
	if(Mine[i][e].DrapeauxOn==1)
{
	delay(350);
	Mine[i][e].DrapeauxOn=0;
}
else
{
	delay(350);
	Mine[i][e].DrapeauxOn=1;
}
}}}
}

void ActionClickGauche()
{
for(int i=0;i<10;i++)
{

for(int e=0;e<10;e++)
{
if ((mouse.X>Mine[i][e].PointX) && (mouse.X<Mine[i][e].PointX+16) && (mouse.Y>Mine[i][e].PointY) && (mouse.Y<Mine[i][e].PointY+16))
{
if(Mine[i][e].DrapeauxOn==0 && Mine[i][e].Visible==0)
{
printf("\a");
Mine[i][e].Visible=1;
}

if(Mine[i][e].Mine==1)
{
for (i=0;i<10;i++)
{
for(e=0;e<10;e++)
{
if(Mine[i][e].Mine==1)
{
delay(500);
pixel(Mine[i][e].X,Mine[i][e].Y-3,50);
line(Mine[i][e].X-5,Mine[i][e].Y,Mine[i][e].X+5,Mine[i][e].Y,55);
line(Mine[i][e].X-5,Mine[i][e].Y-1,Mine[i][e].X+5,Mine[i][e].Y-1,50);
line(Mine[i][e].X-5,Mine[i][e].Y+1,Mine[i][e].X+5,Mine[i][e].Y+1,20);
line(Mine[i][e].X-4,Mine[i][e].Y-2,Mine[i][e].X+4,Mine[i][e].Y-2,10);
cpyvirt();
}
}}
delay(3000);
GameOver();
return;
}

}}}
}

void Operateur()
{
while(EXIT==0)
{
ObjetsVisuel();
AffichageChiffres();
mouse_xy();//Rafraichie la sourie

line(mouse.X,mouse.Y-9,mouse.X,0,10);
line(mouse.X-9,mouse.Y,0,mouse.Y,10);
line(mouse.X-5,mouse.Y,mouse.X+4,mouse.Y,15);
line(mouse.X,mouse.Y-5,mouse.X,mouse.Y+4,15);
pixel(mouse.X,mouse.Y,60);
Cercle(mouse.X,mouse.Y,15,CouleurCercle);

if (mouse.bouton1) ActionClickGauche();
if (mouse.bouton2) ActionClickDroit();
Gagne();
while(!(inp(0x3DA)&8));
while(!(inp(0x3DA)&8));
cpyvirt();
clsvirt();

}
}

void main()
{
clsvirt();
setmode(0x13);

SourieDelimitation();
randomize();
InitTable();
GrilleDemineur();
ConvertionToChar();

while(Rejouer==1)
{

clsvirt();
EXIT=0;
randomize();
InitTable();
GrilleDemineur();
ConvertionToChar();

Operateur();
}

setmode(0x03);
delete []virtuel;
}

Conclusion :


Il y a du code ASM pour eviter d'inclure des headear trop volumineux

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.

Du même auteur (Neurokida)