Bonjour,

J'ai comme projet de développer un puissance 4 avec sauvegarde + chargement.

Je rencontre quelque problème, j'aimerai de l'aide:
- La boucle se casse après le premier coup.
- Lorsque je souhaite charger la partie, cela ne fonctionne pas.

Voici mon programme:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include<stdio.h>
#include<iostream>
#include<time.h>
#include<math.h>
#include <stdlib.h>
#include <windows.h>
#include<conio.h>
using namespace std;
 
//INITIALISATION
void initialisation(char tab[6][7]){
int i,j;
for (i = 0; i < 6; i++)
for (j = 0; j < 7; j++)
tab[i][j] =' ';}
 
//AFFICHAGE
void affichage(char tab[6][7]){
int b;
int br[7]={1,2,3,4,5,6,7};
 
 
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 7; ++j)
cout <<"| "<< tab[i][j] <<" |";
cout << endl;}
 
 
for(b=0;b<7;b++){
cout<<"  "<<br[b]<<"  ";
}
cout<<endl;
cout<<endl;}
 
 
//COMPTEUR PION(S) DANS COLONNE
int counter (char tab [6][7],int j){
int x,i;
x=0;
for(i=0;i<6;i++)
{
if(tab[i][j-1]=='X'||tab[i][j-1]=='O'){
x=x+1;}}
return x;}
 
 
int JOUEUR(int n){
    int N;
if(n%2==0){
    N=2;}
else{N=1;}
 
return N;
}
 
 
 
 
 
// VERIFICATION
int verif(char tab[6][7],char a, int x, int j, int N){
 
    int fini;
    fini=0;
 
 
 
 
if(a==tab[5-x][j]&&a==tab[5-x][j+1]&&a==tab[5-x][j+2]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;
 
}
 
if(a==tab[5-x][j-2]&&a==tab[5-x][j]&&a==tab[5-x][j+1]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
if(a==tab[5-x][j-3]&&a==tab[5-x][j-2]&&a==tab[5-x][j]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
if(a==tab[5-x][j-4]&&a==tab[5-x][j-3]&&a==tab[5-x][j-2]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
 
//VERTICAL
if(a==tab[6-x][j-1]&&a==tab[7-x][j-1]&&a==tab[8-x][j-1]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
//DIAGONAL
if(a==tab[4-x][j]&&a==tab[3-x][j+1]&&a==tab[2-x][j+2]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
 
if(a==tab[6-x][j-2]&&a==tab[4-x][j]&&a==tab[3-x][j+1]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
 
if(a==tab[7-x][j-3]&&a==tab[6-x][j-2]&&a==tab[4-x][j]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
if(a==tab[6-x][j-2]&&a==tab[7-x][j-3]&&a==tab[8-x][j-4]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
 
if(a==tab[2-x][j-4]&&a==tab[3-x][j-3]&&a==tab[4-x][j-2]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
if(a==tab[3-x][j-3]&&a==tab[4-x][j-2]&&a==tab[6-x][j]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
if(a==tab[4-x][j-2]&&a==tab[6-x][j]&&a==tab[7-x][j+1]){
cout<<"      Player "<<N<< "is the winner"<<endl;
 return fini;}
if(a==tab[6-x][j]&&a==tab[7-x][j+1]&&a==tab[8-x][j+2]){
cout<<"      Player "<<N<< " is the winner"<<endl;
 return fini;}
 
 
}
 
 
//PLACEMENT
int placement (char tab [6][7],int x,int j,int n, int N){
char a;
int fini;
//joueur 1
if(n%2==0){
if(x==0){tab [5][j-1]='O';
a=tab[5][j-1];}
else{tab[5-x][j-1]='O';
a=tab[5-x][j-1];}}
//joueur 2
else{if(x==0){tab [5][j-1]='X';
a=tab[5][j-1];}
else{tab[5-x][j-1]='X';
a=tab[5-x][j-1];}}
fini=verif(tab,a,x,j,N);
return fini;
}
 
 
//Sauvegarde
void sauvegarde (char tab[6][7]){
int i=0;
int j=0;
FILE* sauv;
sauv=fopen("sauvegarde","w");
if(sauv==NULL){
    cout<<"Erreur"<<endl;}
else{
for(i=0;i<6;i++){
       for(j=0;j<7;j++){
            fprintf(sauv,"%c",tab[i][j]);}}}
fclose(sauv);
}
 
//Chargement
void chargement (char tab [6][7]){
int i=0;
int j=0;
FILE* charg;
charg=fopen("sauvegarde","r");
if (charg=NULL){
cout<<"Erreur"<<endl;}
else{
for(i=0;i<6;i++){
       for(j=0;j<7;j++){
            fscanf(charg,"%c",&tab[i][j]);
            }}}
fclose(charg);
}
 
 
 
//MAIN
 
 
int main (){
 
HANDLE console;
console=GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, 242);
 
 
char tab[6][7];
int i,j,x,n,N,fini;
n=0;
 
    int choix;
    printf("PUISSANCE 4\n");
    printf("1 - New game\n");
    printf("2 - Continue the game\n");
    printf("Saisissez votre choix\n");
    scanf("%d",&choix);
 
    switch(choix){
 
    case 1 :
 
        initialisation(tab);
        affichage(tab);
 
        do{
        n=n+1;
 
N=JOUEUR(n);
cout<<"Joueur "<< N <<" choisissez une colonne:"<<endl;
cin>>j;
cout<<endl;
 
x=counter(tab,j);
fini=placement(tab,x,j,n,N);
affichage(tab);
sauvegarde(tab);
 
}while(fini!=0);
break;
 
    case 2 :
 
        chargement(tab);
        affichage(tab);
 
        do{
        n=n+1;
 
N=JOUEUR(n);
cout<<"Joueur "<< N <<" choisissez une colonne:"<<endl;
cin>>j;
cout<<endl;
 
x=counter(tab,j);
fini=placement(tab,x,j,n,N);
affichage(tab);
sauvegarde(tab);
 
}while(fini!=0);
break;
    }}
Merci pour votre aide.

Cordialement.