-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadme
250 lines (199 loc) · 5.62 KB
/
readme
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
240
241
242
243
244
245
246
247
248
249
250
Sure! I'll draft the code for `main.cpp`, `Game.h`, `Game.cpp`, `Menu.h`, and `Menu.cpp`, keeping it cohesive with the solver integration. Here's the structure:
---
### `main.cpp`
```cpp
#include <SFML/Graphics.hpp>
#include "Game.h"
#include "Menu.h"
#include "Solver.h"
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Mastermind");
Menu menu;
Game game(4, 6);
Solver solver(4, 6);
bool isPlaying = false;
bool solverMode = false;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (!isPlaying) {
int menuChoice = menu.handleInput(event);
if (menuChoice == 1) {
isPlaying = true;
solverMode = false;
game.reset();
} else if (menuChoice == 2) {
isPlaying = true;
solverMode = true;
solver.reset();
} else if (menuChoice == 3) {
window.close();
}
} else {
if (solverMode) {
auto guess = solver.makeGuess();
int correctPosition, correctColor;
if (game.handleGuess(guess, correctPosition, correctColor)) {
std::cout << "Solver cracked the code!\n";
isPlaying = false;
} else {
solver.receiveFeedback(correctPosition, correctColor);
}
} else {
game.handlePlayerInput(event);
}
}
}
window.clear(sf::Color::Black);
if (!isPlaying) {
menu.draw(window);
} else {
game.draw(window);
}
window.display();
}
return 0;
}
```
---
### `Game.h`
```cpp
#ifndef GAME_H
#define GAME_H
#include <SFML/Graphics.hpp>
#include <vector>
class Game {
public:
Game(int codeLength, int numColors);
void reset();
bool handleGuess(const std::vector<int>& guess, int& correctPosition, int& correctColor);
void handlePlayerInput(const sf::Event& event);
void draw(sf::RenderWindow& window);
private:
int codeLength;
int numColors;
std::vector<int> secretCode;
std::vector<std::vector<int>> guesses;
void generateSecretCode();
};
#endif // GAME_H
```
---
### `Game.cpp`
```cpp
#include "Game.h"
#include <random>
#include <algorithm>
#include <iostream>
Game::Game(int codeLength, int numColors)
: codeLength(codeLength), numColors(numColors) {
reset();
}
void Game::reset() {
generateSecretCode();
guesses.clear();
}
void Game::generateSecretCode() {
secretCode.resize(codeLength);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, numColors - 1);
for (int& color : secretCode) {
color = dist(gen);
}
}
bool Game::handleGuess(const std::vector<int>& guess, int& correctPosition, int& correctColor) {
correctPosition = 0;
correctColor = 0;
std::vector<int> codeCount(numColors, 0), guessCount(numColors, 0);
for (int i = 0; i < codeLength; ++i) {
if (guess[i] == secretCode[i]) {
++correctPosition;
} else {
++codeCount[secretCode[i]];
++guessCount[guess[i]];
}
}
for (int i = 0; i < numColors; ++i) {
correctColor += std::min(codeCount[i], guessCount[i]);
}
guesses.push_back(guess);
return correctPosition == codeLength;
}
void Game::handlePlayerInput(const sf::Event& event) {
// Placeholder for player input handling
std::cout << "Handle player input here.\n";
}
void Game::draw(sf::RenderWindow& window) {
// Placeholder for rendering the game
std::cout << "Render the game here.\n";
}
```
---
### `Menu.h`
```cpp
#ifndef MENU_H
#define MENU_H
#include <SFML/Graphics.hpp>
class Menu {
public:
Menu();
int handleInput(const sf::Event& event);
void draw(sf::RenderWindow& window);
private:
sf::Font font;
sf::Text title;
sf::Text playOption;
sf::Text solverOption;
sf::Text quitOption;
};
#endif // MENU_H
```
---
### `Menu.cpp`
```cpp
#include "Menu.h"
Menu::Menu() {
font.loadFromFile("arial.ttf");
title.setFont(font);
title.setString("Mastermind");
title.setCharacterSize(50);
title.setPosition(300, 50);
playOption.setFont(font);
playOption.setString("1. Play Game");
playOption.setCharacterSize(30);
playOption.setPosition(300, 150);
solverOption.setFont(font);
solverOption.setString("2. Solver Mode");
solverOption.setCharacterSize(30);
solverOption.setPosition(300, 200);
quitOption.setFont(font);
quitOption.setString("3. Quit");
quitOption.setCharacterSize(30);
quitOption.setPosition(300, 250);
}
int Menu::handleInput(const sf::Event& event) {
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Num1) {
return 1;
} else if (event.key.code == sf::Keyboard::Num2) {
return 2;
} else if (event.key.code == sf::Keyboard::Num3) {
return 3;
}
}
return 0;
}
void Menu::draw(sf::RenderWindow& window) {
window.draw(title);
window.draw(playOption);
window.draw(solverOption);
window.draw(quitOption);
}
```
---
These files bring together your `Mastermind` game with a menu system, gameplay mechanics, and solver integration. Let me know if you need enhancements or tweaks!