Bonjour à tous,
je suis en train de coder un jeu du style mastermind en C++ qui s’exécute sur la console.
La version du programme où il faut ré-exécuter le programme après chaque saisie de l'utilisateur jusqu'à la bonne combinaison marche. Cependant, j'ai essayé de faire en sorte que le joueur puisse entrer plusieurs combinaisons sans devoir exécuter le programme après chaque saisie ce qui a échoué.
Je suis un véritable novice en C++ et je n'ai encore accès qu'à peu d'outils, les recherches sur internet se sont donc trouvées infructueuses.
A cause de la gestion des erreurs le programme affiche:
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 /* We are here trying to create a game like a mastermind, the aim is to find the combination compose of four numbers beetween 0 and 9. The player enters 4 numbers: If the number appears in the secret combination but his position is wrong: it's a white pawn. If the number appears in the secret combination and his position is right: it's a red pawn. So we should get this: Enter a random entire (a seed): 15 Enter a combination of numbers separated by a space. 1 6 5 4 a Red pawns: 1 White pawns : 2 Enter a combination of numbers separated by a space. ...*/ #include "std_lib_facilities.h" int main() try{ int seed = 0; cout << "Enter a random entire(a seed):\n"; cin >> seed; srand(seed); // Seed for the secret combination Vector<int> code(4); code[0]=randint(10); code[1]=randint(10); // The combination that we have to find code[2]=randint(10); code[3]=randint(10); Vector<int> answ(4); // Combination written by the user int red = 0; // red pawns int white = 0; //white pawns int a = 0; for( int w=0; w<15; ++w){ cout << "Enter a combination of numbers separated by a space.\n"; while (cin >> a) { answ.push_back(a); // The user's combination if (a>9 or a<0){ error("Number has to be beetween 0 and 9"); } } if (answ.size()!=4){ error("Combination composed of 4 numbers"); } for(int m=0; m<4; ++m){ // Represents numbers written by the user for(int i=0; i<4; ++i){ // Represents numbers of the secret combination if(answ[m] == code[i] and m == i){ ++white; } else if (answ[m]==code[i]){ ++ white; } } } if (red == 4){ cout << "You won!\n"; //End of the game if four red pawns break; } else{ cout << "Red pawns: " << red << " White pawns: " << white << '\n'; for( int c=0; c<5; ++c){ answ.pop_back(); } red = 0; // new combination from user, so at the beginning: white = 0; //0 red pawns and 0 white pawns } } return 0; } catch(exception& e){ cout << "Exception: "<< e.what() << '\n'; return 1; } catch(...){ cout << "Oops unknown exception.\n"; return 2; }
Enter a random entire:
15
Enter a combination of numbers separated by a space.
1 2 3 4 a
Red: 0 White: 1
Enter a combination of numbers separated by a space.
Exception: Combination composed of 4 numbers.
Merci d'avance .
PS:j'espère ne pas m'être trompé de forum.
Partager