Optimised arena memory occupation
This commit is contained in:
parent
3ea1f60e95
commit
a2635b3833
313
IAEngine.c
313
IAEngine.c
@ -1,270 +1,99 @@
|
||||
/*
|
||||
* IAEngine.c
|
||||
*
|
||||
* Created on: 17 juin 2018
|
||||
* Author: isen
|
||||
*
|
||||
* Fonction qui va définir le comportement joueur(s) "ordinateur"
|
||||
*/
|
||||
|
||||
#include "playerInterface.h"
|
||||
#include "SDL2/SDL.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
|
||||
|
||||
/** Structure d'un joueur (IA ou utilisateur)
|
||||
*
|
||||
* */
|
||||
|
||||
typedef struct Player
|
||||
int IAEngine(PLAYER * player1, PLAYER * player2)
|
||||
{
|
||||
int Id;
|
||||
char Name[35];
|
||||
char Race[20];
|
||||
|
||||
int HealthPoints;
|
||||
int AttacksPoints;
|
||||
int DefensePoints;
|
||||
|
||||
//char Weapons[Max_Weapons];
|
||||
|
||||
//int Coins;
|
||||
|
||||
struct Player * suiv;
|
||||
|
||||
}PLAYER;
|
||||
|
||||
//Prototypes
|
||||
PLAYER* createPlayer(int Id);
|
||||
PLAYER* insertRightPlaceRecursive(PLAYER* Head, PLAYER* Element);
|
||||
void displayList(PLAYER * Head);
|
||||
PLAYER * freeElement(PLAYER *Head, int Id);
|
||||
PLAYER * freeList(PLAYER *Head);
|
||||
//PLAYER * AttackPlayer(PLAYER *Head, int idPlayer1, int idPlayer2);
|
||||
PLAYER * SearchPlayer(PLAYER *Head, int idPlayer);
|
||||
void AttackPlayer( PLAYER *player1, PLAYER *player2);
|
||||
|
||||
|
||||
|
||||
int init(void)
|
||||
{
|
||||
PLAYER* List=NULL;
|
||||
PLAYER* Element=NULL;
|
||||
int id=0;
|
||||
|
||||
PLAYER* player1=NULL;
|
||||
PLAYER* player2=NULL;
|
||||
|
||||
while(id<2)
|
||||
//Player 1 est le joueur, player 2 le bot
|
||||
if((player1 != NULL) & (player2 !=NULL))
|
||||
{
|
||||
id = id + 1;
|
||||
Element = createPlayer(id);
|
||||
List = insertRightPlaceRecursive(List,Element);
|
||||
//On récupère les positions des 2 joueurs . L'IA sait où est le joueur
|
||||
int Player1PositionX = player1->PositionX;
|
||||
int Player1PositionY = player1->PositionY;
|
||||
|
||||
int Player2PositionX = player2->PositionX;
|
||||
int Player2PositionY = player2->PositionY;
|
||||
int action;
|
||||
|
||||
action = FindShortestPath(Player1PositionX,Player1PositionY,Player2PositionX,Player2PositionY);
|
||||
|
||||
printf("Tour de l'IA\n");
|
||||
if(action == 5)
|
||||
{
|
||||
AttackPlayer(player1,player2);
|
||||
}
|
||||
else if ((action >= 1) && (action <= 4))
|
||||
{
|
||||
ActionPlayer(player2,action);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
displayList(List);
|
||||
|
||||
|
||||
player2 = SearchPlayer(List, 1);
|
||||
player1 = SearchPlayer(List, 2);
|
||||
|
||||
printf("Nom player 1 : %s\n",player1->Name);
|
||||
printf("Nom player 2 : %s\n",player2->Name);
|
||||
|
||||
AttackPlayer(player1,player2);
|
||||
|
||||
//freeElement(List, 2); // Fonctionne
|
||||
//freeList(List); // Fonctionne
|
||||
|
||||
//displayList(List);
|
||||
|
||||
/** Pour la simulation d'un combat, on rentre la lettre A au clavier
|
||||
* Le joueur 1 va attaqué le joueur 2 et inversement
|
||||
* On affiche le score des joueurs
|
||||
*
|
||||
* */
|
||||
//List = AttackPlayer(List, 1, 2);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**Fonction qui crée un joueur
|
||||
*
|
||||
* */
|
||||
PLAYER* createPlayer(int Id)
|
||||
int FindShortestPath(int Player1PositionX, int Player1PositionY, int Player2PositionX, int Player2PositionY)
|
||||
{
|
||||
PLAYER* player=NULL;
|
||||
|
||||
player=(PLAYER*)malloc(sizeof(PLAYER));
|
||||
if(player==NULL)
|
||||
int differenceX = abs(Player1PositionX - Player2PositionX);
|
||||
int differenceY = abs(Player1PositionY - Player2PositionY);
|
||||
|
||||
int action;
|
||||
|
||||
|
||||
//Revoir cette partie il faut que les 2 joueurs soit sur la même ligne ou colone avec une différence de ligne ou colonne de 1
|
||||
if((abs(Player1PositionX - (Player2PositionX - 1)) == 1) ||
|
||||
(abs(Player1PositionX - (Player2PositionX + 1)) == 1) ||
|
||||
(abs(Player1PositionX - (Player2PositionY - 1)) == 1) ||
|
||||
(abs(Player1PositionX - (Player2PositionY + 1)) == 1))
|
||||
{
|
||||
printf("ERREUR Allocation joueur ");
|
||||
printf("Les 2 joueur sont à coté\n");
|
||||
printf("Le joueur 2 va combattre\n");
|
||||
action = 5;
|
||||
}
|
||||
|
||||
printf("Entrer le nom du joueur\n");
|
||||
scanf("%s",player->Name);
|
||||
printf("Nb PV?\n");
|
||||
scanf("%d",&player->HealthPoints);
|
||||
player->Id = Id;
|
||||
player->AttacksPoints = 10;
|
||||
|
||||
|
||||
|
||||
player->suiv=NULL;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
/**Fonction qui ajoute un joueur à la liste
|
||||
*
|
||||
* */
|
||||
|
||||
PLAYER* insertRightPlaceRecursive(PLAYER* Head, PLAYER* Element)
|
||||
{
|
||||
if(Head == NULL)
|
||||
if(differenceX > abs(Player1PositionX - (Player2PositionX - 1)))
|
||||
{
|
||||
return Element;
|
||||
printf("Chemin plus court sur X\n");
|
||||
printf("Le joueur 2 va vers la gauche\n");
|
||||
action = 4;
|
||||
}
|
||||
|
||||
if(Element->Id < Head->Id)
|
||||
else if(differenceX > abs(Player1PositionX - (Player2PositionX + 1)))
|
||||
{
|
||||
Element->suiv = Head;
|
||||
return Element;
|
||||
printf("Chemin plus court sur X\n");
|
||||
printf("Le joueur 2 va vers la droite\n");
|
||||
action = 2;
|
||||
}
|
||||
else if(differenceY > abs(Player1PositionY - (Player2PositionY + 1)))
|
||||
{
|
||||
printf("Chemin plus court sur Y\n");
|
||||
printf("Le joueur 2 va vers le bas\n");
|
||||
action = 3;
|
||||
}
|
||||
else if(differenceY > abs(Player1PositionY - (Player2PositionY - 1)))
|
||||
{
|
||||
printf("Chemin plus court sur Y\n");
|
||||
printf("Le joueur 2 va vers le haut\n");
|
||||
action = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(Element->Id == Head->Id)
|
||||
{
|
||||
printf("Le joueur à l'id %d exite déja dans la liste\n",Element->Id);
|
||||
free(Element);
|
||||
return Head;
|
||||
}
|
||||
else
|
||||
{
|
||||
Head->suiv = insertRightPlaceRecursive(Head->suiv,Element);
|
||||
return Head;
|
||||
}
|
||||
printf("Chemin plus long\n");
|
||||
action = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void displayList(PLAYER * Head)
|
||||
{
|
||||
if(Head == NULL)
|
||||
{
|
||||
printf("La liste est vide\n");
|
||||
}
|
||||
while(Head != NULL)
|
||||
{
|
||||
printf("****Player %d****\n",Head->Id);
|
||||
printf("Nom : %s\n",Head->Name);
|
||||
Head = Head->suiv;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PLAYER * freeElement(PLAYER *Head, int Id)
|
||||
{
|
||||
PLAYER * Element;
|
||||
|
||||
if(Head == NULL)
|
||||
{
|
||||
printf("La liste est vide\n");
|
||||
}
|
||||
|
||||
if(Id == Head->Id)
|
||||
{
|
||||
Element = Head;
|
||||
Head = Head->suiv;
|
||||
printf("Le joueur %d a été supprimé de la liste\n",Element->Id);
|
||||
free(Element);
|
||||
return Head;
|
||||
}
|
||||
else
|
||||
{
|
||||
Head->suiv = freeElement(Head->suiv,Id);
|
||||
return Head;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PLAYER * freeList(PLAYER *Head)
|
||||
{
|
||||
if(Head != NULL)
|
||||
{
|
||||
freeList(Head->suiv);
|
||||
|
||||
}
|
||||
free(Head);
|
||||
Head=NULL;
|
||||
|
||||
return Head;
|
||||
}
|
||||
|
||||
|
||||
PLAYER * SearchPlayer(PLAYER *Head, int idPlayer)
|
||||
{
|
||||
printf("Fonction Recherche\n");
|
||||
|
||||
PLAYER * player;
|
||||
|
||||
|
||||
if(Head == NULL)
|
||||
{
|
||||
printf("La liste est vide\n");
|
||||
//return player;
|
||||
}
|
||||
//else
|
||||
//{
|
||||
//printf("A\n");
|
||||
|
||||
if(Head->Id == idPlayer)
|
||||
{
|
||||
printf("Trouvé\n");
|
||||
player = Head;
|
||||
printf(" Joueur %s\n",player->Name);
|
||||
return player;
|
||||
//printf("J1\n");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
|
||||
|
||||
Head->suiv = SearchPlayer(Head->suiv,idPlayer);
|
||||
|
||||
return Head;
|
||||
}
|
||||
//}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void AttackPlayer( PLAYER *player1, PLAYER *player2)
|
||||
{
|
||||
printf("Fonction Attaque\n");
|
||||
|
||||
|
||||
if((player1 != NULL) && (player2 != NULL))
|
||||
{
|
||||
printf("MMMM\n");
|
||||
while((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
|
||||
{
|
||||
printf("\n\n** Tour **\n");
|
||||
|
||||
printf("Attaque Joueur %d sur joueur %d \n",player1->Id,player2->Id);
|
||||
player1->HealthPoints = player1->HealthPoints - player2->AttacksPoints;
|
||||
|
||||
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
|
||||
printf("Joueur %d : %s PV = %d\n ",player2->Id,player2->Name,player2->HealthPoints);
|
||||
|
||||
printf("\nAttaque Joueur %d sur joueur %d\n ",player2->Id,player1->Id);
|
||||
player2->HealthPoints = player2->HealthPoints - player1->AttacksPoints;
|
||||
|
||||
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
|
||||
printf("Joueur %d : %s PV = %d \n",player2->Id,player2->Name,player2->HealthPoints);
|
||||
}
|
||||
printf("Partie finie");
|
||||
|
||||
}
|
||||
|
||||
|
||||
return action;
|
||||
|
||||
}
|
||||
|
||||
|
14
IAEngine.h
Normal file
14
IAEngine.h
Normal file
@ -0,0 +1,14 @@
|
||||
/*
|
||||
* IAEngine.h
|
||||
*
|
||||
* Created on: 17 juin 2018
|
||||
* Author: isen
|
||||
*/
|
||||
|
||||
#ifndef IAENGINE_H_
|
||||
#define IAENGINE_H_
|
||||
|
||||
int FindShortestPath(int Player1PositionX, int Player1PositionY, int Player2PositionX, int Player2PositionY);
|
||||
int IAEngine(PLAYER * player1, PLAYER * player2);
|
||||
|
||||
#endif /* IAENGINE_H_ */
|
@ -4,22 +4,24 @@
|
||||
#include "fileHandler.h"
|
||||
#include "logHelper.h"
|
||||
|
||||
|
||||
/*
|
||||
* Arena generate functions
|
||||
*/
|
||||
ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile) {
|
||||
ARENA_H_TILE* tile_h = NULL;
|
||||
|
||||
if (prevHTile == NULL) {
|
||||
tile_h = calloc(1,sizeof(ARENA_H_TILE)); //Using calloc because of resetting all memory allocated to 0
|
||||
tile_h->type_id = 0;
|
||||
tile_h->isGround = 1;
|
||||
tile_h->canBeMined = 0;
|
||||
tile_h->playerOnTile = NULL;
|
||||
tile_h->nextRow = NULL;
|
||||
tile_h->nextColumn = NULL;
|
||||
} else if (prevHTile != NULL) {
|
||||
tile_h = calloc(1,sizeof(ARENA_H_TILE));
|
||||
prevHTile->nextRow = tile_h;
|
||||
tile_h->type_id = 0;
|
||||
tile_h->isGround = 1;
|
||||
tile_h->canBeMined = 0;
|
||||
tile_h->playerOnTile = NULL;
|
||||
tile_h->nextRow = NULL;
|
||||
tile_h->nextColumn = NULL;
|
||||
}
|
||||
@ -34,8 +36,7 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
|
||||
tile_w = calloc(1,sizeof(ARENA_W_TILE));
|
||||
prevHTile->nextColumn = tile_w;
|
||||
tile_w->type_id = 0;
|
||||
tile_w->isGround = 1;
|
||||
tile_w->canBeMined = 0;
|
||||
tile_w->playerOnTile = NULL;
|
||||
tile_w->nextColumn = NULL;
|
||||
|
||||
return tile_w;
|
||||
@ -43,8 +44,7 @@ ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile) {
|
||||
tile_w = calloc(1,sizeof(ARENA_W_TILE));
|
||||
prevWTile->nextColumn = tile_w;
|
||||
tile_w->type_id = 0;
|
||||
tile_w->isGround = 1;
|
||||
tile_w->canBeMined = 0;
|
||||
tile_w->playerOnTile = NULL;
|
||||
tile_w->nextColumn = NULL;
|
||||
}
|
||||
|
||||
@ -79,6 +79,11 @@ ARENA_H_TILE* genNewArena(int size_h, int size_w) {
|
||||
return arenaOrigin;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Arena delete functions
|
||||
*/
|
||||
void deleteWTile(ARENA_W_TILE* WTile) {
|
||||
if (WTile->nextColumn != NULL) {
|
||||
deleteWTile(WTile->nextColumn);
|
||||
@ -99,3 +104,47 @@ void deleteHTile(ARENA_H_TILE* HTile) {
|
||||
void deleteArena(ARENA_H_TILE* arena) {
|
||||
deleteHTile(arena);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
* Arena status functions
|
||||
*/
|
||||
int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y) {
|
||||
int type_id = -1;
|
||||
int i;
|
||||
|
||||
if (coord_y == 0) {
|
||||
ARENA_H_TILE* tile_h = NULL;
|
||||
|
||||
tile_h = arena;
|
||||
if (coord_x != 0) {
|
||||
for (i=0;i<coord_x;i++) {
|
||||
tile_h = arena->nextRow;
|
||||
}
|
||||
}
|
||||
|
||||
type_id = tile_h->type_id;
|
||||
} else {
|
||||
ARENA_W_TILE* tile_w = NULL;
|
||||
ARENA_H_TILE* tile_h = NULL;
|
||||
|
||||
tile_h = arena;
|
||||
if (coord_x != 0) {
|
||||
for (i=0;i<coord_x;i++) {
|
||||
tile_h = tile_h->nextRow;
|
||||
}
|
||||
}
|
||||
|
||||
tile_w = tile_h->nextColumn;
|
||||
if (coord_y != 0) {
|
||||
for (i=0;i<coord_y;i++) {
|
||||
tile_w = tile_w->nextColumn;
|
||||
}
|
||||
}
|
||||
|
||||
type_id = tile_w->type_id;
|
||||
}
|
||||
|
||||
return type_id;
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
#include "playerInterface.h"
|
||||
|
||||
#ifndef ARENAENGINE_H_
|
||||
#define ARENAENGINE_H_
|
||||
|
||||
@ -13,26 +15,32 @@ typedef struct items{
|
||||
}ITEMS;
|
||||
*/
|
||||
|
||||
typedef struct arena_h_tile{ //Rows chained list
|
||||
typedef struct tileType{
|
||||
int type_id;
|
||||
//texture?
|
||||
int isGround;
|
||||
int canBeMined;
|
||||
}TILE;
|
||||
|
||||
typedef struct arena_h_tile{ //Rows chained list
|
||||
int type_id;
|
||||
PLAYER* playerOnTile;
|
||||
struct arena_h_tile *nextRow;
|
||||
struct arena_w_tile *nextColumn;
|
||||
}ARENA_H_TILE;
|
||||
|
||||
typedef struct arena_w_tile{ //Columns chained list
|
||||
int type_id;
|
||||
//texture?
|
||||
int isGround;
|
||||
int canBeMined;
|
||||
PLAYER* playerOnTile;
|
||||
struct arena_w_tile *nextColumn;
|
||||
}ARENA_W_TILE;
|
||||
|
||||
|
||||
|
||||
//Generation functions
|
||||
ARENA_H_TILE* genNewArena(int size_h, int size_w);
|
||||
void deleteArena(ARENA_H_TILE* arena);
|
||||
|
||||
//Status functions
|
||||
int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y);
|
||||
|
||||
#endif
|
||||
|
4
main.c
4
main.c
@ -37,6 +37,10 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
addLogInfo("Successfully created arena.");
|
||||
|
||||
printf("#1 : %d", getTileTypeID(arena, 20, 12));
|
||||
printf("#2 : %d", getTileTypeID(arena, 5, 0));
|
||||
printf("#3 : %d", getTileTypeID(arena, 0, 75));
|
||||
|
||||
deleteArena(arena);
|
||||
addLogInfo("Cleared arena.");
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "SDL2/SDL.h"
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
int createGameMenuWindows() {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||
|
@ -1,17 +1,468 @@
|
||||
/*
|
||||
* playerInterface.c
|
||||
*
|
||||
* Created on: 17 juin 2018
|
||||
* Author: isen
|
||||
*/
|
||||
|
||||
#include "playerInterface.h"
|
||||
|
||||
#include "SDL2/SDL.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "logHelper.h"
|
||||
#include "SDL2/SDL.h"
|
||||
|
||||
void displayScreen(int scr_id) {
|
||||
SDL_Window *main_test;
|
||||
main_test = SDL_CreateWindow("My test windows", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 320, 140, SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
|
||||
SDL_Delay(5000);
|
||||
SDL_DestroyWindow(main_test);
|
||||
|
||||
SDL_Quit();
|
||||
//return EXIT_SUCCESS;
|
||||
#define ArenaMAX 100
|
||||
|
||||
int PlayerInterface(void)
|
||||
{
|
||||
|
||||
PLAYER* List=NULL;
|
||||
PLAYER* Element=NULL;
|
||||
int id=0;
|
||||
int numberAlive;
|
||||
|
||||
PLAYER* player1=NULL;
|
||||
PLAYER* player2=NULL;
|
||||
|
||||
while(id<2)
|
||||
{
|
||||
id = id + 1;
|
||||
Element = createPlayer(id);
|
||||
List = insertRightPlaceRecursive(List,Element);
|
||||
}
|
||||
|
||||
displayList(List);
|
||||
|
||||
player1 = SearchPlayer(List, 1);
|
||||
player2 = SearchPlayer(List, 2);
|
||||
|
||||
printf("Nom player 1 : %s\n",player1->Name);
|
||||
printf("Nom player 2 : %s\n",player2->Name);
|
||||
|
||||
numberAlive = NumberPlayerAlive(List);
|
||||
printf("Nombre de joueur en vie : %i",numberAlive);
|
||||
|
||||
|
||||
int action;
|
||||
|
||||
|
||||
do
|
||||
{
|
||||
action=getEvent();
|
||||
|
||||
if(action == 1)
|
||||
{
|
||||
ActionPlayer(player1,1); //Déplacement vers le haut
|
||||
}
|
||||
else if(action == 2)
|
||||
{
|
||||
ActionPlayer(player1,2); //Déplacement vers la droite
|
||||
}
|
||||
else if(action == 3)
|
||||
{
|
||||
ActionPlayer(player1,3); //Déplacement vers le bas
|
||||
}
|
||||
else if(action == 4)
|
||||
{
|
||||
ActionPlayer(player1,4); //Déplacement vers la gauche
|
||||
}
|
||||
else if(action == 5)
|
||||
{
|
||||
//Regarder le perso en face, le plus près
|
||||
AttackPlayer(player1,player2); //Voir quel player on choisit d'attaquer
|
||||
}
|
||||
|
||||
|
||||
//Tour de l'IA
|
||||
IAEngine(player1,player2);
|
||||
|
||||
}
|
||||
|
||||
while(action != -1);
|
||||
|
||||
return -1;
|
||||
|
||||
//return 0;
|
||||
}
|
||||
|
||||
void refreshScreen() {}
|
||||
|
||||
int getEvent()
|
||||
{
|
||||
int action;
|
||||
SDL_Event event;
|
||||
SDL_WaitEvent(&event); /* Récupération de l'événement dans event */
|
||||
|
||||
switch(event.type) /* Test du type d'événement */
|
||||
{
|
||||
/* Si c'est un événement de type "Quitter" */
|
||||
case SDL_QUIT : printf("QUITTER\n");
|
||||
action = -1;
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
switch(event.key.keysym.sym) //La valeur de touche
|
||||
{
|
||||
case SDLK_UP: printf("FLECHE DU HAUT\n");
|
||||
action = 1;
|
||||
break;
|
||||
case SDLK_DOWN: printf("FLECHE DU BAS\n");
|
||||
action = 3;
|
||||
break;
|
||||
case SDLK_RIGHT: printf("FLECHE DE DROITE\n");
|
||||
action = 2;
|
||||
break;
|
||||
case SDLK_LEFT: printf("FLECHE DE GAUCHE\n");
|
||||
action = 4;
|
||||
break;
|
||||
case SDLK_SPACE:printf("BARRE D'ESPACE\n");
|
||||
action = 5;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return action;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
|
||||
ActionPlayer(player2,1); //Déplacement vers le haut
|
||||
ActionPlayer(player2,2); //Déplacement vers la droite
|
||||
ActionPlayer(player2,3); //Déplacement vers le bas
|
||||
ActionPlayer(player2,4); //Déplacement vers la gauche
|
||||
|
||||
*/
|
||||
|
||||
|
||||
//freeElement(List, 2); // Fonctionne
|
||||
//freeList(List); // Fonctionne
|
||||
|
||||
//displayList(List);
|
||||
|
||||
/** Pour la simulation d'un combat, on rentre la lettre A au clavier
|
||||
* Le joueur 1 va attaqué le joueur 2 et inversement
|
||||
* On affiche le score des joueurs
|
||||
*
|
||||
* */
|
||||
//List = AttackPlayer(List, 1, 2);
|
||||
|
||||
// SDL_Quit();
|
||||
|
||||
|
||||
/**Fonction qui crée un joueur
|
||||
*
|
||||
* */
|
||||
PLAYER* createPlayer(int Id)
|
||||
{
|
||||
PLAYER* player=NULL;
|
||||
//Utilisé la fonction de récupération de la taille de l'arène
|
||||
|
||||
player=(PLAYER*)malloc(sizeof(PLAYER));
|
||||
if(player==NULL)
|
||||
{
|
||||
printf("ERREUR Allocation joueur ");
|
||||
}
|
||||
|
||||
printf("Entrer le nom du joueur\n");
|
||||
scanf("%s",player->Name);
|
||||
printf("Nb PV?\n");
|
||||
scanf("%d",&player->HealthPoints);
|
||||
player->Id = Id;
|
||||
player->AttacksPoints = 10;
|
||||
|
||||
if(Id == 1)
|
||||
{
|
||||
player->PositionX=0;
|
||||
player->PositionY=0;
|
||||
}
|
||||
else if(Id ==2)
|
||||
{
|
||||
player->PositionX=ArenaMAX;
|
||||
player->PositionY=0;
|
||||
}
|
||||
else if(Id ==3)
|
||||
{
|
||||
player->PositionX=0;
|
||||
player->PositionY=ArenaMAX;
|
||||
}
|
||||
else
|
||||
{
|
||||
player->PositionX=ArenaMAX;
|
||||
player->PositionY=ArenaMAX;
|
||||
}
|
||||
|
||||
player->suiv=NULL;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
/**Fonction qui ajoute un joueur à la liste
|
||||
*
|
||||
* */
|
||||
|
||||
PLAYER* insertRightPlaceRecursive(PLAYER* Head, PLAYER* Element)
|
||||
{
|
||||
if(Head == NULL)
|
||||
{
|
||||
return Element;
|
||||
}
|
||||
|
||||
if(Element->Id < Head->Id)
|
||||
{
|
||||
Element->suiv = Head;
|
||||
return Element;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(Element->Id == Head->Id)
|
||||
{
|
||||
printf("Le joueur à l'id %d exite déja dans la liste\n",Element->Id);
|
||||
free(Element);
|
||||
return Head;
|
||||
}
|
||||
else
|
||||
{
|
||||
Head->suiv = insertRightPlaceRecursive(Head->suiv,Element);
|
||||
return Head;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void displayList(PLAYER * Head)
|
||||
{
|
||||
if(Head == NULL)
|
||||
{
|
||||
printf("La liste est vide\n");
|
||||
}
|
||||
while(Head != NULL)
|
||||
{
|
||||
printf("****Player %d****\n",Head->Id);
|
||||
printf("Nom : %s\n",Head->Name);
|
||||
Head = Head->suiv;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PLAYER * freeElement(PLAYER *Head, int Id)
|
||||
{
|
||||
PLAYER * Element;
|
||||
|
||||
if(Head == NULL)
|
||||
{
|
||||
printf("La liste est vide\n");
|
||||
}
|
||||
|
||||
if(Id == Head->Id)
|
||||
{
|
||||
Element = Head;
|
||||
Head = Head->suiv;
|
||||
printf("Le joueur %d a été supprimé de la liste\n",Element->Id);
|
||||
free(Element);
|
||||
return Head;
|
||||
}
|
||||
else
|
||||
{
|
||||
Head->suiv = freeElement(Head->suiv,Id);
|
||||
return Head;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PLAYER * freeList(PLAYER *Head)
|
||||
{
|
||||
if(Head != NULL)
|
||||
{
|
||||
freeList(Head->suiv);
|
||||
|
||||
}
|
||||
free(Head);
|
||||
Head=NULL;
|
||||
|
||||
return Head;
|
||||
}
|
||||
|
||||
int NumberPlayerAlive(PLAYER *Head)
|
||||
{
|
||||
int numberAlive=0;
|
||||
if(Head == NULL)
|
||||
{
|
||||
printf("La liste est vide\n");
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
do
|
||||
{
|
||||
if(Head->HealthPoints > 0)
|
||||
{
|
||||
numberAlive = numberAlive + 1;
|
||||
}
|
||||
Head=Head->suiv;
|
||||
}
|
||||
while(Head != NULL);
|
||||
return numberAlive;
|
||||
}
|
||||
}
|
||||
|
||||
PLAYER * SearchPlayer(PLAYER *Head, int idPlayer)
|
||||
{
|
||||
printf("Fonction Recherche\n");
|
||||
|
||||
if(Head == NULL)
|
||||
{
|
||||
printf("La liste est vide\n");
|
||||
}
|
||||
|
||||
if(Head->Id == idPlayer)
|
||||
{
|
||||
printf("Trouvé\n");
|
||||
|
||||
printf(" Joueur %s\n",Head->Name);
|
||||
return Head;
|
||||
}
|
||||
else
|
||||
{
|
||||
Head->suiv = SearchPlayer(Head->suiv,idPlayer);
|
||||
|
||||
return Head->suiv;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void AttackPlayer( PLAYER *player1, PLAYER *player2)
|
||||
{
|
||||
printf("Fonction Attaque\n");
|
||||
|
||||
|
||||
if((player1 != NULL) && (player2 != NULL))
|
||||
{
|
||||
|
||||
while((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
|
||||
{
|
||||
printf("\n\n** Tour **\n");
|
||||
|
||||
printf("Attaque Joueur %d sur joueur %d \n",player1->Id,player2->Id);
|
||||
|
||||
player2->HealthPoints = player2->HealthPoints - player1->AttacksPoints;
|
||||
|
||||
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
|
||||
printf("Joueur %d : %s PV = %d\n ",player2->Id,player2->Name,player2->HealthPoints);
|
||||
|
||||
if((player1->HealthPoints > 0) && (player2->HealthPoints > 0))
|
||||
{
|
||||
printf("\nAttaque Joueur %d sur joueur %d\n ",player2->Id,player1->Id);
|
||||
player1->HealthPoints = player1->HealthPoints - player2->AttacksPoints;
|
||||
|
||||
printf("Joueur %d : %s PV = %d\n ",player1->Id,player1->Name,player1->HealthPoints);
|
||||
printf("Joueur %d : %s PV = %d \n",player2->Id,player2->Name,player2->HealthPoints);
|
||||
}
|
||||
}
|
||||
|
||||
if(player1->HealthPoints > player2->HealthPoints)
|
||||
{
|
||||
printf("Combat finie \n Le joueur 1 a gagné le combat\n");
|
||||
}
|
||||
else if(player1->HealthPoints < player2->HealthPoints)
|
||||
{
|
||||
printf("Combat finie \n Le joueur 2 a gagné le combat\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Combat finie \n Les 2 joueurs sont morts");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ActionPlayer(PLAYER * player, int action)
|
||||
{
|
||||
/* action = 1 --> Déplacement d'1 carreau vers le haut
|
||||
* action = 2 --> Déplacement d'1 carreau vers la droite
|
||||
* action = 3 --> Déplacement d'1 carreau vers le bas
|
||||
* action = 4 --> Déplacement d'1 carreau vers la gauche
|
||||
*
|
||||
* */
|
||||
|
||||
if(player != NULL)
|
||||
{
|
||||
// récupère la position du personnage. La position de l'instance sur l'arène est récupérer par une fonction
|
||||
int positionX = player->PositionX;
|
||||
int positionY = player->PositionY;
|
||||
|
||||
|
||||
|
||||
printf("\n**** Player %s ****",player->Name);
|
||||
|
||||
if(action == 1)
|
||||
{
|
||||
printf("On veut aller vers le haut\n");
|
||||
|
||||
if(positionY-1 >= 0)
|
||||
{
|
||||
player->PositionY = positionY-1;
|
||||
printf("Déplacement de 1 vers le haut\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Impossible d'aller vers le haut\n");
|
||||
}
|
||||
}
|
||||
else if(action == 2)
|
||||
{
|
||||
printf("On veut aller vers la droite\n");
|
||||
|
||||
if(positionX + 1 <= ArenaMAX)
|
||||
{
|
||||
player->PositionX = positionX + 1;
|
||||
printf("Déplacement de 1 vers la droite\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Impossible d'aller vers la droite\n");
|
||||
}
|
||||
}
|
||||
else if(action == 3)
|
||||
{
|
||||
printf("On veut aller vers le bas\n");
|
||||
|
||||
if(positionY + 1 <= ArenaMAX)
|
||||
{
|
||||
player->PositionY = positionY + 1;
|
||||
printf("Déplacement de 1 vers le bas\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Impossible d'aller vers le bas\n");
|
||||
}
|
||||
}
|
||||
else if(action == 4)
|
||||
{
|
||||
printf("On veut aller vers la gauche\n");
|
||||
|
||||
if(positionX - 1 >= 0)
|
||||
{
|
||||
player->PositionX = positionX - 1;
|
||||
printf("Déplacement de 1 vers le bas\n La nouvelle position est de X: %i et Y: %i \n",player->PositionX,player->PositionY);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Impossible d'aller vers la gauche\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Autre action\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Echec, le joueur est NULL\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,10 +1,49 @@
|
||||
/*
|
||||
* playerInterface.h
|
||||
*
|
||||
* Created on: 17 juin 2018
|
||||
* Author: isen
|
||||
*/
|
||||
|
||||
#ifndef PLAYERINTERFACE_H_
|
||||
#define PLAYERINTERFACE_H_
|
||||
|
||||
enum scr_id{ARENA,MAIN_MENU,GAME_OVER};
|
||||
/** Structure d'un joueur (IA ou utilisateur)
|
||||
*
|
||||
* */
|
||||
typedef struct Player
|
||||
{
|
||||
int Id;
|
||||
char Name[35];
|
||||
char Race[20];
|
||||
|
||||
void initDisplayLib();
|
||||
void displayScreen(int scr_id);
|
||||
void refreshScreen();
|
||||
int HealthPoints;
|
||||
int AttacksPoints;
|
||||
int DefensePoints;
|
||||
|
||||
#endif
|
||||
int PositionX;
|
||||
int PositionY;
|
||||
//char Weapons[Max_Weapons];
|
||||
|
||||
//int Coins;
|
||||
|
||||
struct Player * suiv;
|
||||
|
||||
}PLAYER;
|
||||
|
||||
//Prototypes
|
||||
|
||||
PLAYER* createPlayer(int Id);
|
||||
PLAYER* insertRightPlaceRecursive(PLAYER* Head, PLAYER* Element);
|
||||
void displayList(PLAYER * Head);
|
||||
PLAYER * freeElement(PLAYER *Head, int Id);
|
||||
PLAYER * freeList(PLAYER *Head);
|
||||
|
||||
PLAYER * SearchPlayer(PLAYER *Head, int idPlayer);
|
||||
void AttackPlayer( PLAYER *player1, PLAYER *player2);
|
||||
void ActionPlayer(PLAYER * player, int action);
|
||||
|
||||
int NumberPlayerAlive(PLAYER *Head);
|
||||
int getEvent(void);
|
||||
|
||||
#endif /* PLAYERINTERFACE_H_ */
|
||||
|
Loading…
x
Reference in New Issue
Block a user