Ajout joueur et IA
Modification interface joueur avec action et récupération des touches clavier Comportement de l'IA définit (en cours)
This commit is contained in:
parent
2e9ce5ddab
commit
e02e6df139
315
IAEngine.c
315
IAEngine.c
@ -1,270 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* 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 <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Structure d'un joueur (IA ou utilisateur)
|
int IAEngine(PLAYER * player1, PLAYER * player2)
|
||||||
*
|
|
||||||
* */
|
|
||||||
|
|
||||||
typedef struct Player
|
|
||||||
{
|
{
|
||||||
int Id;
|
//Player 1 est le joueur, player 2 le bot
|
||||||
char Name[35];
|
if((player1 != NULL) & (player2 !=NULL))
|
||||||
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 main(void)
|
|
||||||
{
|
|
||||||
PLAYER* List=NULL;
|
|
||||||
PLAYER* Element=NULL;
|
|
||||||
int id=0;
|
|
||||||
|
|
||||||
PLAYER* player1=NULL;
|
|
||||||
PLAYER* player2=NULL;
|
|
||||||
|
|
||||||
while(id<2)
|
|
||||||
{
|
{
|
||||||
id = id + 1;
|
//On récupère les positions des 2 joueurs . L'IA sait où est le joueur
|
||||||
Element = createPlayer(id);
|
int Player1PositionX = player1->PositionX;
|
||||||
List = insertRightPlaceRecursive(List,Element);
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Fonction qui crée un joueur
|
int FindShortestPath(int Player1PositionX, int Player1PositionY, int Player2PositionX, int Player2PositionY)
|
||||||
*
|
|
||||||
* */
|
|
||||||
PLAYER* createPlayer(int Id)
|
|
||||||
{
|
{
|
||||||
PLAYER* player=NULL;
|
int differenceX = abs(Player1PositionX - Player2PositionX);
|
||||||
|
int differenceY = abs(Player1PositionY - Player2PositionY);
|
||||||
player=(PLAYER*)malloc(sizeof(PLAYER));
|
|
||||||
if(player==NULL)
|
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");
|
if(differenceX > abs(Player1PositionX - (Player2PositionX - 1)))
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
return Element;
|
printf("Chemin plus court sur X\n");
|
||||||
|
printf("Le joueur 2 va vers la gauche\n");
|
||||||
|
action = 4;
|
||||||
}
|
}
|
||||||
|
else if(differenceX > abs(Player1PositionX - (Player2PositionX + 1)))
|
||||||
if(Element->Id < Head->Id)
|
|
||||||
{
|
{
|
||||||
Element->suiv = Head;
|
printf("Chemin plus court sur X\n");
|
||||||
return Element;
|
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
|
else
|
||||||
{
|
{
|
||||||
if(Element->Id == Head->Id)
|
printf("Chemin plus long\n");
|
||||||
{
|
action = 0;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
return action;
|
||||||
|
|
||||||
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");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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_ */
|
23
gameMenu.c
23
gameMenu.c
@ -1,23 +0,0 @@
|
|||||||
#include "SDL2/SDL.h"
|
|
||||||
|
|
||||||
void initDisplayLib() {
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
|
||||||
printf("Erreur chargement librairie SDL ! %s\n",SDL_GetError());
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int createMainWindows() {
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
|
||||||
printf("Erreur chargement librairie SDL ! %s\n",SDL_GetError());
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
#ifndef GAMEMENU_H_
|
|
||||||
#define GAMEMENU_H_
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
70
main.c
70
main.c
@ -1,16 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* main.c
|
||||||
|
*
|
||||||
|
* Created on: 17 juin 2018
|
||||||
|
* Author: isen
|
||||||
|
*/
|
||||||
|
|
||||||
|
//#include "IAEngine.h"
|
||||||
|
#include "playerInterface.h"
|
||||||
|
#include "SDL2/SDL.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "logHelper.h"
|
|
||||||
#include "SDL2/SDL.h"
|
|
||||||
#include "arenaEngine.h"
|
|
||||||
|
|
||||||
#define A_WIDTH 100 //Real value is A_X + 1
|
int main(void)
|
||||||
#define A_LENGHT 100
|
{
|
||||||
|
SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
if (SDL_Init(SDL_INIT_VIDEO) != 0 )
|
||||||
int arena[A_WIDTH][A_LENGHT];
|
{
|
||||||
|
fprintf(stdout,"Échec de l'initialisation de la SDL (%s)\n",SDL_GetError());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int continuer = 1;
|
||||||
|
int action;
|
||||||
|
|
||||||
|
// Création de la fenêtre
|
||||||
|
SDL_Window* pWindow = NULL;
|
||||||
|
pWindow = SDL_CreateWindow("Arena Survival Tournament",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,640,480,SDL_WINDOW_SHOWN);
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Event event; // Cette variable servira plus tard à gérer les événements
|
||||||
|
|
||||||
|
if( pWindow )
|
||||||
|
{
|
||||||
|
SDL_Delay(3000); /* Attendre trois secondes, que l'utilisateur voie la fenêtre */
|
||||||
|
while (continuer)
|
||||||
|
{
|
||||||
|
|
||||||
|
action = PlayerInterface();
|
||||||
|
if (action == -1)
|
||||||
|
{
|
||||||
|
continuer = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
|
||||||
|
}
|
||||||
|
//SDL_DestroyWindow(pWindow);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fprintf(stderr,"Erreur de création de la fenêtre: %s\n",SDL_GetError());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//IAEngine();
|
||||||
|
|
||||||
|
SDL_Quit();
|
||||||
|
//PlayerInterface();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,24 +1,468 @@
|
|||||||
|
/*
|
||||||
|
* playerInterface.c
|
||||||
|
*
|
||||||
|
* Created on: 17 juin 2018
|
||||||
|
* Author: isen
|
||||||
|
*/
|
||||||
|
|
||||||
#include "playerInterface.h"
|
#include "playerInterface.h"
|
||||||
|
|
||||||
|
#include "SDL2/SDL.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "logHelper.h"
|
|
||||||
#include "SDL2/SDL.h"
|
|
||||||
|
|
||||||
void initDisplayLib() {
|
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
#define ArenaMAX 100
|
||||||
printf("Erreur chargement librairie SDL ! %s\n",SDL_GetError());
|
|
||||||
exit(EXIT_FAILURE);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 displayScreen(int scr_id) {
|
void displayList(PLAYER * Head)
|
||||||
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);
|
if(Head == NULL)
|
||||||
SDL_Delay(5000);
|
{
|
||||||
SDL_DestroyWindow(main_test);
|
printf("La liste est vide\n");
|
||||||
|
}
|
||||||
SDL_Quit();
|
while(Head != NULL)
|
||||||
//return EXIT_SUCCESS;
|
{
|
||||||
|
printf("****Player %d****\n",Head->Id);
|
||||||
|
printf("Nom : %s\n",Head->Name);
|
||||||
|
Head = Head->suiv;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void refreshScreen() {}
|
|
||||||
|
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_
|
#ifndef PLAYERINTERFACE_H_
|
||||||
#define 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();
|
int HealthPoints;
|
||||||
void displayScreen(int scr_id);
|
int AttacksPoints;
|
||||||
void refreshScreen();
|
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