Skip to content
Snippets Groups Projects
Commit c8c51305 authored by IKHRICHI Soumaya's avatar IKHRICHI Soumaya
Browse files

partie logique sans SDL

parent 49d0a040
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
CC = gcc
CFLAGS = -W -Wall -ansi -std=c99 -g
EXEC = main
SRC = main.c map.c player.c
OBJ = $(SRC:.c=.o)
all: $(EXEC)
main: $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) -o $@ -c $<
clean:
rm -rf *.o *~
mrproper: clean
rm -rf $(EXEC)
map.c 0 → 100644
#include "map.h"
char** allouer_tab_2D(int n,int m){
char** tab = malloc(n*sizeof(char*));
for(int i=0;i<n;i++){
tab[i] = malloc(m*sizeof(char));
}
for (int x=0;x<n;x++){
for(int y=0;y<m;y++){
tab[x][y]=' ';
}
}
return tab;
}
void afficher_map(char** tab,int n,int m){
for (int i=0;i<n;i++){
for (int j = 0; j < m; j++)
{
printf("%c",tab[i][j]);
}
printf("\n");
}
}
void desallouer_tab_2D(char** tab, int n){
for(int i=0;i<n;i++){
free(tab[i]);
}
free(tab);
}
void taille_map(const char* nomFichier, int* nbLig, int* nbCol){
FILE *fichier;
int nbl=0;
int nbc=0;
char s;
int tmp;
fichier =fopen(nomFichier,"r");
if (fichier != NULL){
while ((s= (char) fgetc(fichier))!=EOF){
if (s =='\n'){
if (nbl==0){
tmp=nbc;
}
else{
if(tmp<nbc){
tmp=nbc;
}
}
nbl++;
nbc=0;
}
else{
nbc++;
}
}
fclose(fichier);
*nbLig =nbl;
*nbCol =tmp;
}
else{
printf("le fichier est null");
}
}
char** lire_map(const char* nomFichier){
int nbl=0;
int nbc=0;
FILE *fichier = NULL;
taille_map(nomFichier,&nbl,&nbc);
char** tab = allouer_tab_2D(nbl,nbc);
fichier =fopen(nomFichier,"r");
if (fichier != NULL){
for (int i=0;i<nbl;i++){
fscanf(fichier,"%s",tab[i]);
}
}
fclose(fichier);
return tab;
}
char** modifier_caractere(char** tab,int n,int m,char ancien,char nouveau){
for (int i=0;i<n;i++){
for (int j = 0; j < m; j++)
{
if (tab[i][j]==ancien)
{
tab[i][j]=nouveau;
}
}
}
return tab;
}
void ecrire_map(const char* nomFichier, char** tab,int n){
FILE* fichier=NULL;
fichier =fopen(nomFichier,"w");
if(fichier!= NULL){
for (int i = 0; i < n; i++){
fprintf(fichier,"%s\n",tab[i]);
}
}
else{
printf("le fichier est null");
}
fclose(fichier);
}
\ No newline at end of file
map.h 0 → 100644
#ifndef MAP_H
#define MAP_H
#include "player.h"
char** allouer_tab_2D(int n,int m);
void afficher_map(char** tab,int n,int m);
void desallouer_tab_2D(char** tab,int n);
void taille_map(const char* nomFichier, int* nbLig, int* nbCol);
char** lire_map(const char* nomFichier);
char** modifier_caractere(char** tab,int n,int m,char ancien,char nouveau);
void ecrire_map(const char* nomFichier, char** tab,int n);
#endif
\ No newline at end of file
player.c 0 → 100644
#include "player.h"
void init_player(player_t *player,char** tab,int nbl,int nbc,int w,int h){
for (int i=0;i<nbl;i++){
for (int j= 0; j < nbc; j++)
{
if(tab[i][j]=='P'){
player->x=i;
player->y=j;
}
}
}
player->score=0;
player->chances=3;
player->height=h;
player->width=w;
}
bool handle_movement_up(char** tab,player_t*player)
{
if (tab[player->y-1][player->x]=='L'){
return true;
}
else{
return false;
}
}
bool handle_movement_down(char** tab,player_t*player)
{
if (tab[player->y+1][player->x]=='L'){
return true;
}
else{
return false;
}
}
bool handle_movement_left(char** tab,player_t*player)
{
if (tab[player->y][player->x-1]=='L'){
return true;
}
else{
return false;
}
}
bool handle_movement_right(char** tab,player_t*player)
{
if (tab[player->y][player->x+1]=='L'){
return true;
}
else{
return false;
}
}
void movement(char** tab,player_t *player,char deplacement){
switch (deplacement)
{
case 'z':
if(handle_movement_up(tab,player)){
player->y--;
}
break;
case 's':
if(handle_movement_down(tab,player)){
player->y++;
}
break;
case 'd':
if(handle_movement_right(tab,player)){
player->x++;
}
break;
case 'q':
if(handle_movement_left(tab,player)){
player->x--;
}
break;
}
}
player.h 0 → 100644
#ifndef PLAYER_H
#define PLAYER_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct player_s
{
int x;
int y;
int width;
int height;
int score;
int chances;
}player_t;
void init_player(player_t *player,char** tab,int nbl,int nbc,int w,int h);
bool handle_movement_up(char** tab,player_t*player);
bool handle_movement_down(char** tab,player_t*player);
bool handle_movement_left(char** tab,player_t*player);
bool handle_movement_right(char** tab,player_t*player);
void movement(char** tab,player_t *player,char deplacement);
#endif
\ No newline at end of file
#####################################################
#P#LLLLLLLLLLL#LLLL########LLLLLLL###LLL####LLLLLLLL#
#LLL######L##LLL##L##############L###L#LLLLLL########
##########L#######L##############L##########LLLLLL###
##LLLLLLLLL#######L##LLLLLLLLLLLLLLLLLL#####L####L###
##L###############L##L######L#####L###L#####L####L###
##LLLL############LLLL######L#####L###L#####L####L###
############################L#####L###L#####L####LLL#
############LLLLLLLLLLLLLLLLL#####L###LLLLLLL########
############L######L######L#######L##################
####LLLLLLLLL######L######L######LLLL#######LLL######
####L##############L######L#####LL##L#######L#L######
####L#LLLLLLLL###############LLLL###L#######L#L######
####L#L######L#####LLLL#############LLLLLLLLL#LLL####
####LLLL#####L#####L##L#####LLL####LL###########L####
#############L#####L##L###########LL############LLLL#
#LLLLLLLLLLLLL#####L##L##LLLLLLLLLL#LLLLLL###########
#L#################L##LLLL######L###L####L###########
#L#################L##L#LL######L###L####LLLL########
#LLLLL###LLLLLLLLLLL##L#########LLLLL#######LLLLLLLL#
#####LLLLL############LLLLLL#######################S#
#####################################################
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment