IA
Liste chainée IA + commencé fonctionnement attaque
This commit is contained in:
parent
a4d2d28a9d
commit
f8853f5775
270
IAEngine.c
Normal file
270
IAEngine.c
Normal file
@ -0,0 +1,270 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** Structure d'un joueur (IA ou utilisateur)
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
|
||||||
|
typedef struct Player
|
||||||
|
{
|
||||||
|
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 main(void)
|
||||||
|
{
|
||||||
|
PLAYER* List=NULL;
|
||||||
|
PLAYER* Element=NULL;
|
||||||
|
int id=0;
|
||||||
|
|
||||||
|
PLAYER* player1=NULL;
|
||||||
|
PLAYER* player2=NULL;
|
||||||
|
|
||||||
|
while(id<2)
|
||||||
|
{
|
||||||
|
id = id + 1;
|
||||||
|
Element = createPlayer(id);
|
||||||
|
List = insertRightPlaceRecursive(List,Element);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
PLAYER* player=NULL;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user