-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmon_board.c
197 lines (161 loc) · 6.41 KB
/
mon_board.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdio.h>
#include <stdlib.h>
#include "mon_board.h"
board new_game()
{
board jeu;
jeu = malloc(sizeof(board_s));
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
jeu->tableauSize[i][j]=NONE;
jeu->tableauPlayer[i][j]=NO_PLAYER;
for (int k = 0; k < 3; k++)
{
jeu->tableauComplet[i][j][k] = NO_PLAYER;
}
}
}
for (int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
jeu->compteurGobelet[i][j]=NB_INITIAL_PIECES;
}
}
return jeu;
}
void destroy_game(board game)
{
free(game);
}
player get_place_holder(board game, int line, int column)
{
player place_holder = game->tableauPlayer[line][column];
return place_holder;
}
size get_piece_size(board game, int line, int column)
{
size piece_size = game->tableauSize[line][column];
return piece_size;
}
player get_winner(board game)
{
player winner=NO_PLAYER;
player winner2=NO_PLAYER;
for(int i=0; i < 2; i++)
{
//Vérifier les lignes, les colonnes et les diagonales
if(game->tableauPlayer[0][0]==game->tableauPlayer[0][1] && game->tableauPlayer[0][0]==game->tableauPlayer[0][2] && game->tableauPlayer[0][0]!=NO_PLAYER)
{
winner = game->tableauPlayer[0][0];
}
if(game->tableauPlayer[1][0]==game->tableauPlayer[1][1] && game->tableauPlayer[1][0]==game->tableauPlayer[1][2] && game->tableauPlayer[1][0]!=NO_PLAYER)
{
if(winner!=NO_PLAYER) winner2 = game->tableauPlayer[1][0];
else winner = game->tableauPlayer[1][0];
}
if(game->tableauPlayer[2][0]==game->tableauPlayer[2][1] && game->tableauPlayer[2][0]==game->tableauPlayer[2][2] && game->tableauPlayer[2][0]!=NO_PLAYER)
{
if (winner != NO_PLAYER) winner2 = game->tableauPlayer[2][0];
else winner = game->tableauPlayer[2][0];
}
if(game->tableauPlayer[0][0]==game->tableauPlayer[1][0] && game->tableauPlayer[0][0]==game->tableauPlayer[2][0] && game->tableauPlayer[0][0]!=NO_PLAYER)
{
if(winner != NO_PLAYER) winner2 = game->tableauPlayer[0][0];
else winner = game->tableauPlayer[0][0];
}
if(game->tableauPlayer[0][1]==game->tableauPlayer[1][1] && game->tableauPlayer[0][1]==game->tableauPlayer[2][1] && game->tableauPlayer[0][1]!=NO_PLAYER)
{
if(winner != NO_PLAYER) winner2 = game->tableauPlayer[0][1];
else winner = game->tableauPlayer[0][1];
}
if(game->tableauPlayer[0][2]==game->tableauPlayer[1][2] && game->tableauPlayer[0][2]==game->tableauPlayer[2][2] && game->tableauPlayer[0][2]!=NO_PLAYER)
{
if(winner != NO_PLAYER) winner2 = game->tableauPlayer[0][2];
else winner = game->tableauPlayer[0][2];
}
if(game->tableauPlayer[0][0]==game->tableauPlayer[1][1] && game->tableauPlayer[0][0]==game->tableauPlayer[2][2] && game->tableauPlayer[0][0]!=NO_PLAYER)
{
if(winner != NO_PLAYER) winner2 = game->tableauPlayer[0][0];
else winner = game->tableauPlayer[0][0];
}
if(game->tableauPlayer[0][2]==game->tableauPlayer[1][1] && game->tableauPlayer[0][2]==game->tableauPlayer[2][0] && game->tableauPlayer[0][2]!=NO_PLAYER)
{
if(winner != NO_PLAYER) winner2 = game->tableauPlayer[0][2];
else winner = game->tableauPlayer[0][2];
}
}
if(winner != NO_PLAYER && winner2 != NO_PLAYER && winner != winner2) winner = 3;
return winner;
}
player next_player(player current_player)
{
player returned_player=0;
if(current_player==1) returned_player=2;
else if(current_player==2) returned_player=1;
return returned_player;
}
int get_nb_piece_in_house(board game, player checked_player, size piece_size)
{
int nb_piece=game->compteurGobelet[checked_player-1][piece_size-1];
return nb_piece;
}
int place_piece(board game, player current_player, size piece_size, int line, int column)
{
int value=-1;
if(line > 2 || line < 0 || column > 2 || column < 0) value = 3;
else
{
if (game->compteurGobelet[current_player-1][piece_size-1] > 0)
{
if (piece_size > game->tableauSize[line][column])
{
game->tableauComplet[line][column][piece_size-1]=current_player;
game->tableauSize[line][column] = piece_size;
game->tableauPlayer[line][column] = current_player;
game->compteurGobelet[current_player-1][piece_size-1]--;
value = 0;
}
else value = 2;
}
else value = 1;
}
return value;
}
int move_piece(board game, int source_line, int source_column, int target_line, int target_column)
{
int value=-1; //On initialise la valeur de sortie
size piece_size; //On initialise la taille de la pièce déplacée
//On vérifie que les coordonnées de la case sont valides
if(source_line > 2 || source_line < 0 || source_column > 2 || source_column < 0 || target_line > 2 || target_line < 0 || target_column > 2 || target_column < 0) value = 3; //Non : on retourne 3
else //Si oui :
{
if (game->tableauSize[source_line][source_column] != NONE) //On vérifie qu'il y a bien un gobelet à déplacer sur la case
{
piece_size = game->tableauSize[source_line][source_column]; //Si oui, on récupère sa taille
if (game->tableauSize[source_line][source_column] > game->tableauSize[target_line][target_column]) //On vérifie que le gobelet sur la case de départ est plus grand que le gobelet sur la case d'arrivé
{
game->tableauComplet[target_line][target_column][piece_size-1]=game->tableauComplet[source_line][source_column][piece_size-1]; //On donne à la case cible la valeur de la case source
game->tableauSize[target_line][target_column] = piece_size; //On enregistre la taille du nouveau gobelet sur la case de surface
game->tableauPlayer[target_line][target_column] = game->tableauComplet[source_line][source_column][piece_size-1]; //On enregistre le nouveau détenteur de la case de surface
game->tableauSize[source_line][source_column] = NONE; //On efface la taille du gobelet de la case source
game->tableauPlayer[source_line][source_column] = NO_PLAYER; //On met à jour le détenteur de la case cible
game->tableauComplet[source_line][source_column][piece_size-1] = NO_PLAYER; //On efface la valeur de la case source dans le tableau principal
for (int i = 0; i < 2; ++i)
{
if (game->tableauComplet[source_line][source_column][i]!=0)
{
game->tableauSize[source_line][source_column]=i+1;
game->tableauPlayer[source_line][source_column]=game->tableauComplet[source_line][source_column][i];
}
}
value = 0; //Si tout se passe bien on renvoie 0
}
else value = 2; //Si le gobelet déplacé ne peut pas recouvrir le gobelet de la case choisie on retourne 2
}
else value = 1; //S'il n'y a pas de gobelet sur la case on renvoie 1
}
return value;
}