Sokoban  1.1.3
Le fameux jeu Sokoban, poussez les boîtes !
player.c
Aller à la documentation de ce fichier.
1 /*
2 Game movement management functions for Sokoban
3 Copyright (C) 2022, 2023 Efe ERKEN
4 
5 This file is part of Sokoban
6 
7 Sokoban is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Sokoban is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Sokoban. If not, see <https://www.gnu.org/licenses/>.
19 
20 SPDX-License-Identifier: GPL-3.0-or-later
21 */
22 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include "player.h"
37 #include "grid.h"
38 
65 void move_player(grid* G, enum Direction D) {
66  int target_row = G->player.y, target_column = G->player.x;
67  switch (D) {
68  case LEFT:
69  target_column--;
70  break;
71  case BOTTOM:
72  target_row++;
73  break;
74  case TOP:
75  target_row--;
76  break;
77  case RIGHT:
78  target_column++;
79  }
80  enum CaseType target = G->game_grid[target_row][target_column];
81  enum CaseType player = G->game_grid[G->player.y][G->player.x];
82  if (target == WALL) {
83  return;
84  } else if (target == BOX) {
85  int box_target_row = target_row + (target_row - G->player.y);
86  int box_target_column = target_column + (target_column - G->player.x);
87  enum CaseType box_target = G->game_grid[box_target_row][box_target_column];
88  if (box_target == WALL || box_target == BOX || box_target == BOX_GOAL) {
89  return;
90  } else if (box_target == GOAL) {
91  G->game_grid[box_target_row][box_target_column] = BOX_GOAL;
92  G->game_grid[target_row][target_column] = PLAYER;
93  if (player == PLAYER) {
94  G->game_grid[G->player.y][G->player.x] = NONE;
95  } else if (player == PLAYER_GOAL) {
96  G->game_grid[G->player.y][G->player.x] = GOAL;
97  }
99  } else if (box_target == NONE) {
100  G->game_grid[box_target_row][box_target_column] = BOX;
101  G->game_grid[target_row][target_column] = PLAYER;
102  if (player == PLAYER) {
103  G->game_grid[G->player.y][G->player.x] = NONE;
104  } else if (player == PLAYER_GOAL) {
105  G->game_grid[G->player.y][G->player.x] = GOAL;
106  }
107  } else {
109  }
110  } else if (target == BOX_GOAL) {
111  int box_target_row = target_row + (target_row - G->player.y);
112  int box_target_column = target_column + (target_column - G->player.x);
113  enum CaseType box_target = G->game_grid[box_target_row][box_target_column];
114  if (box_target == WALL || box_target == BOX || box_target == BOX_GOAL) {
115  return;
116  } else if (box_target == GOAL) {
117  G->game_grid[box_target_row][box_target_column] = BOX_GOAL;
118  G->game_grid[target_row][target_column] = PLAYER_GOAL;
119  if (player == PLAYER) {
120  G->game_grid[G->player.y][G->player.x] = NONE;
121  } else if (player == PLAYER_GOAL) {
122  G->game_grid[G->player.y][G->player.x] = GOAL;
123  }
124  } else if (box_target == NONE) {
125  G->game_grid[box_target_row][box_target_column] = BOX;
126  G->game_grid[target_row][target_column] = PLAYER_GOAL;
127  if (player == PLAYER) {
128  G->game_grid[G->player.y][G->player.x] = NONE;
129  } else if (player == PLAYER_GOAL) {
130  G->game_grid[G->player.y][G->player.x] = GOAL;
131  }
133  } else {
135  }
136  } else if (target == GOAL) {
137  G->game_grid[target_row][target_column] = PLAYER_GOAL;
138  if (player == PLAYER) {
139  G->game_grid[G->player.y][G->player.x] = NONE;
140  } else if (player == PLAYER_GOAL) {
141  G->game_grid[G->player.y][G->player.x] = GOAL;
142  }
143  } else if (target == NONE) {
144  G->game_grid[target_row][target_column] = PLAYER;
145  if (player == PLAYER) {
146  G->game_grid[G->player.y][G->player.x] = NONE;
147  } else if (player == PLAYER_GOAL) {
148  G->game_grid[G->player.y][G->player.x] = GOAL;
149  }
150  } else {
152  }
153  G->player.x = target_column;
154  G->player.y = target_row;
155 }
156 
170  exit_routine(G);
171  fprintf(stderr, "Error, this level map has features unmanaged by the game\n");
172  exit(1);
173 }
void exit_routine(grid *G)
Fonction qui s'occupe des routines de fermeture du programme.
Definition: grid.c:598
Fichier header contenant les structures de données pour traiter les niveaux du jeu sokoban.
CaseType
Structure indiquant quel caractère correspond à quel élément du niveau.
Definition: grid.h:48
@ PLAYER_GOAL
Superposition d'un joueur et d'un objectif.
Definition: grid.h:55
@ BOX
Une boîte.
Definition: grid.h:50
@ GOAL
Les objectifs.
Definition: grid.h:52
@ BOX_GOAL
Superposition d'une boîte et d'un objectif.
Definition: grid.h:54
@ NONE
Le vide.
Definition: grid.h:53
@ PLAYER
Le joueur.
Definition: grid.h:51
@ WALL
Un mur.
Definition: grid.h:49
void move_player(grid *G, enum Direction D)
Fonction qui bouge le joueur dans la direction voulue dans le niveau.
Definition: player.c:65
void move_player_error(grid *G)
Fonction qui affiche un message d'erreur et quitte le jeu.
Definition: player.c:169
Fichier header contenant les structures pour traiter le joueur et son mouvement.
Direction
Cette structure contient les directions possibles dans le jeu.
Definition: player.h:60
@ TOP
La direction haute.
Definition: player.h:63
@ BOTTOM
La direction basse.
Definition: player.h:62
@ LEFT
La direction gauche.
Definition: player.h:61
@ RIGHT
La direction droite.
Definition: player.h:64
Cette structure contient les informations concernant le niveau du jeu et son contenu.
Definition: grid.h:84
enum CaseType ** game_grid
Tableau contenant les entités présents dans le jeu.
Definition: grid.h:85
player player
Structure pour stocker la position du joueur.
Definition: grid.h:90
int box_over_goal_number
Nombre d'objectifs réussi de game_grid.
Definition: grid.h:89
Cette structure contient la position du joueur dans le niveau.
Definition: player.h:46
int y
Coordonnée y du joueur.
Definition: player.h:48
int x
Coordonnée x du joueur.
Definition: player.h:47