Programme qui marche 100%

Dependencies:   mbed BSP_DISCO_F746NG

prog_util/fonctions.cpp

Committer:
blbl
Date:
2020-11-18
Revision:
4:58c50d72b423
Parent:
3:54a96adea9cd

File content as of revision 4:58c50d72b423:

#include "header.h"

TS_StateTypeDef TS_State;

void init_cellules(GRILLE G){
	int demarrer=0;
	int x;
	int y;
	int l;
	int h;
	while(demarrer==0){
		Affichagepxl2_debut(G);
		BSP_TS_GetState(&TS_State);
		if(TS_State.touchDetected==1){
        	BSP_TS_GetState(&TS_State);
			if (TS_State.touchX[0]>Gauche_bouton_demarrer && TS_State.touchY[0]<Bas_bouton_demarrer){
				demarrer=1;
			}
			else{
				BSP_TS_GetState(&TS_State);
				x = TS_State.touchX[0],
				BSP_TS_GetState(&TS_State);
				y = TS_State.touchY[0];
				l = (x-1)/2 ;
				h = (y-1)/2 ;
				G[l][h]=1;
				Affichagepxl2_debut(G);
			}
		}
	}
}





void Changementetat(GRILLE G, GRILLE Gsomme) //réactualise les états des cases dans G en fonction des cases adjacentes dans Gsomme
{
	int l;
	int h;
	for (l=0; l<LONGUEUR+1; l++)
	{
		for (h=0; h<HAUTEUR+1; h++)
		{	
			if (G[l][h]==1)
			{
				if (Gsomme[l][h]<=CVA_VIVANT && Gsomme[l][h]>=CVA_MORT) //condition pour la survie d'une case
				{
					G[l][h]=1;
				}
				else //meurt d'isolement ou d'étouffement
				{
					G[l][h]=0;
				}
			}
			else if (G[l][h]==0)
			{
				if (Gsomme[l][h]==CVA_VIVANT) //passage a l'état vivant si la case est morte
				{
					G[l][h]=1;
				}
				else 
				{
					G[l][h]=0;
				}
			}
		}
	}
}

void Affichagepxl2(GRILLE G) //surtout ne pas confondre les dimensions de l'écran avec les dimension du tableau des cases si on utilise des pixels 2x2 ou plus
{
	int l; //longueur sur G
	int h; //hauteur sur H
	for (l=0; l<LONGUEUR+1; l++)
	{
		for (h=0; h<HAUTEUR+1; h++)
		{	
			int x=(2*l)+1;  //position sur l'écran x,y
			int y=(2*h)+1;
			if (G[l][h]==1){
				BSP_LCD_DrawPixel(x,y,couleurvivante);
				BSP_LCD_DrawPixel(x+1,y,couleurvivante);
				BSP_LCD_DrawPixel(x,y+1,couleurvivante);
				BSP_LCD_DrawPixel(x+1,y+1,couleurvivante);
			}
			else if (G[l][h]==0){
				BSP_LCD_DrawPixel(x,y,couleurmorte);
				BSP_LCD_DrawPixel(x+1,y,couleurmorte);
				BSP_LCD_DrawPixel(x,y+1,couleurmorte);
				BSP_LCD_DrawPixel(x+1,y+1,couleurmorte);
			}
		}
	}
	BSP_LCD_SetTextColor(couleur_stop);
	BSP_LCD_FillRect(Gauche_bouton_demarrer,0,Droit_bouton_continuer,Bas_bouton_demarrer);
	BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
	BSP_LCD_DisplayStringAt(0,0, (uint8_t *)"STOP", RIGHT_MODE);
}

void Affichagepxl2_debut(GRILLE G) 
{
	int l; //longueur sur G
	int h; //hauteur sur H
	for (l=0; l<LONGUEUR+1; l++)
	{
		for (h=0; h<HAUTEUR+1; h++)
		{	
			int x=(2*l)+1;  //position sur l'écran x,y
			int y=(2*h)+1;
			if (G[l][h]==1){
				BSP_LCD_DrawPixel(x,y,couleurvivante);
				BSP_LCD_DrawPixel(x+1,y,couleurvivante);
				BSP_LCD_DrawPixel(x,y+1,couleurvivante);
				BSP_LCD_DrawPixel(x+1,y+1,couleurvivante);
			}
			else if (G[l][h]==0){
				BSP_LCD_DrawPixel(x,y,couleurmorte);
				BSP_LCD_DrawPixel(x+1,y,couleurmorte);
				BSP_LCD_DrawPixel(x,y+1,couleurmorte);
				BSP_LCD_DrawPixel(x+1,y+1,couleurmorte);
			}
		}
	}
	BSP_LCD_SetTextColor(couleur_demarrer);
	BSP_LCD_FillRect(Gauche_bouton_demarrer,0,Droit_bouton_continuer,Bas_bouton_demarrer);
	BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
	BSP_LCD_DisplayStringAt(0,0, (uint8_t *)"START", RIGHT_MODE);
}






void Tabsomme(GRILLE G, GRILLE Gsomme) //réalisation du tableau des sommes a partir du tableau des états
{	
	int l;
	int h;
	for (l=0; l<LONGUEUR+1; l++)
	{
		for (h=0; h<HAUTEUR+1; h++)
		{	
			if (l<LONGUEUR+1 && l>0 && h<HAUTEUR+1 && h>0)
			{
				Gsomme[l][h]=SommeADJcorps(G,l,h);
			}
			else if (((l==0)&&(h==HAUTEUR||h==0))||((l==LONGUEUR)&&(h==HAUTEUR||h==0)))
			{
				Gsomme[l][h]=SommeADJcoins(G,l,h);
			}
			else if ((l==0 && (h!=0 && h!=HAUTEUR))||(l==LONGUEUR && (h!=0 && h!=HAUTEUR)))
			{
				Gsomme[l][h]=SommeADJcoteslat(G,l,h);
			}
			else if ((h==0 && (l!=0 && l!=LONGUEUR))||(h==HAUTEUR && (l!=0 && l!=LONGUEUR)))
			{
				Gsomme[l][h]=SommeADJcoteslon(G,l,h);
			}
				
			
		}
	}
}
			
			
int SommeADJcorps(GRILLE G, int i, int h) // sous-fonction de Tabsomme qui gère les cases au centre du tableau (sans les cotés et les coins)
{	
	int s=0;
	s+=(G[i+1][h] + G[i-1][h]);
	s+=(G[i-1][h-1] + G[i][h-1] + G[i+1][h-1]);
	s+=(G[i-1][h+1] + G[i][h+1] + G[i+1][h+1]);
	return s;
}

int SommeADJcoteslat(GRILLE G,int i, int h) // sous-fonction de Tabsomme qui gère les cotés latéraux du tableau
{	
	if (i==0)
		{
		int s=0;
		s+=(G[i][h+1] + G[i][h-1]);
		s+=(G[i+1][h+1] + G[i+1][h-1] + G[i+1][h]);
		s+=(G[LONGUEUR][h+1] + G[LONGUEUR][h-1] + G[LONGUEUR][h]);
		return s; 
	}
	if (i==LONGUEUR)
		{
		int s=0;
		s+=(G[i][h+1] + G[i][h-1]);
		s+=(G[i-1][h+1] + G[i-1][h-1] + G[i-1][h]);
		s+=(G[0][h+1] + G[0][h-1] + G[0][h]);
		return s;
	}
	return 0;
}

int SommeADJcoteslon(GRILLE G,int i, int h)// sous-fonction de Tabsomme qui gère les cotés en longueur du tableau
{	
	if (h==0)
		{
		int s=0;
		s+=(G[i+1][h] + G[i-1][h]);
		s+=(G[i-1][h+1] + G[i][h+1] + G[i+1][h+1]);
		s+=(G[i-1][HAUTEUR] + G[i][HAUTEUR] + G[i+1][HAUTEUR]);
		return s; 
	}
	if (h==HAUTEUR)
		{
		int s=0;
		s+=(G[i+1][h] + G[i-1][h]);
		s+=(G[i-1][h+1] + G[i][h+1] + G[i+1][h+1]);
		s+=(G[i-1][0] + G[i][HAUTEUR] + G[i+1][HAUTEUR]);
		return s;
	}
	return 0;
}
	

int SommeADJcoins(GRILLE G, int i, int h) // sous-fonction de Tabsomme qui gère les coins du tableaux
{
	int s=0;
	if (i==0)
		{
		if (h==0) //coin sup gauche
			{
			s+=(G[LONGUEUR][HAUTEUR]);
			s+=(G[1][0] + G[1][1] + G[0][1]);
			s+=(G[LONGUEUR][0] + G[LONGUEUR][1]);
			s+=(G[0][HAUTEUR] + G[1][HAUTEUR]);
			return s;
		}
		else if (h==HAUTEUR) //coin inf gauche
			{
			s+=(G[LONGUEUR][0]);
			s+=(G[0][HAUTEUR - 1] + G[1][HAUTEUR - 1] + G[1][HAUTEUR]);
			s+=(G[LONGUEUR][HAUTEUR -1] + G[LONGUEUR][HAUTEUR]);
			s+=(G[0][0] + G[1][0]);
			return s;
		}
	}
	else if (i==LONGUEUR)
		{
		if (h==0) //coin sup droit
			{
			s+=(G[0][HAUTEUR]);
			s+=(G[LONGUEUR - 1][0] + G[LONGUEUR - 1][0] + G[LONGUEUR - 1][1]);
			s+=(G[LONGUEUR][HAUTEUR] + G[LONGUEUR-1][HAUTEUR]);
			s+=(G[0][0] + G[0][1]);
			return s;
		}
		else if (h==HAUTEUR) //coin inf droit
			{
			s+=(G[0][0]);
			s+=(G[LONGUEUR - 1][HAUTEUR] + G[LONGUEUR - 1][HAUTEUR - 1] + G[LONGUEUR][HAUTEUR - 1]);
			s+=(G[LONGUEUR][0] + G[LONGUEUR - 1][0]);
			s+=(G[0][HAUTEUR] + G[0][HAUTEUR - 1]);
			return s;
		}
	}
	return 0;
}



void partie(GRILLE G,GRILLE Gsomme){
	BSP_LCD_Clear(LCD_COLOR_BLACK);
    BSP_LCD_SetBackColor(LCD_COLOR_BLACK);
    Affichagepxl2(G);
    HAL_Delay(TEMPS_TOUR);
	int stop=0;
	while(stop==0){
		HAL_Delay(TEMPS_TOUR);
		Tabsomme(G,Gsomme);
		Changementetat(G,Gsomme);
		Affichagepxl2(G);
		BSP_TS_GetState(&TS_State);
		if(TS_State.touchDetected != 0){
			BSP_TS_GetState(&TS_State);
			if (TS_State.touchX[0]>Gauche_bouton_demarrer && TS_State.touchY[0]<Bas_bouton_demarrer){
				stop=1;
			}
		}
	}
}




int fin_de_partie(GRILLE G,GRILLE Gsomme){
	int rejouer=0;
	int continuer=0;
	BSP_LCD_SetTextColor(couleur_stop);
	BSP_LCD_FillRect(Gauche_bouton_demarrer,0,Droit_bouton_continuer,Bas_bouton_demarrer);
	BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
	BSP_LCD_DisplayStringAt(0,0, (uint8_t *)"NEW TRY", RIGHT_MODE);
	BSP_LCD_SetTextColor(couleur_demarrer);
	BSP_LCD_FillRect(0,0,Droit_bouton_continuer,Bas_bouton_demarrer);
	BSP_LCD_SetTextColor(LCD_COLOR_WHITE);
	BSP_LCD_DisplayStringAt(0,0, (uint8_t *)"CONTINUE", LEFT_MODE);
	HAL_Delay(1000);
	while(1){
		BSP_TS_GetState(&TS_State);
		if(TS_State.touchDetected==1){
			BSP_TS_GetState(&TS_State);
			if (TS_State.touchX[0]<Droit_bouton_continuer && TS_State.touchY[0]<Bas_bouton_demarrer){
				continuer=1;
			}
			else if (TS_State.touchX[0]>Gauche_bouton_demarrer && TS_State.touchY[0]<Bas_bouton_demarrer){
				rejouer=1;
			}

		}
		if (continuer==1){
			partie(G,Gsomme);
			fin_de_partie(G,Gsomme);
		}
		if (rejouer ==1){
			break;
		}
	}
	return rejouer;
}