-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay.cpp
More file actions
37 lines (34 loc) · 1.15 KB
/
play.cpp
File metadata and controls
37 lines (34 loc) · 1.15 KB
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
/**
* Implementation of bull-pgia game
*
* @author Erel Segal-Halevi
* @since 2019-04
*/
#include "play.hpp"
#include "calculate.hpp"
#include <string>
using std::string;
namespace bullpgia {
uint play(Chooser& chooser, Guesser& guesser, uint length, uint maxTurns) {
const uint TECHNICAL_VICTORY_TO_GUESSER = 0;
const uint TECHNICAL_VICTORY_TO_CHOOSER = maxTurns+1;
string choice = chooser.choose(length);
// std::cout << "choice: " << choice << std::endl; // for debug
if (choice.length()!=length) // Illegal choice
return TECHNICAL_VICTORY_TO_GUESSER;
guesser.startNewGame(length); // tell the guesser that a new game starts now
uint indexOfTurn;
for (indexOfTurn=0; indexOfTurn<maxTurns; ++indexOfTurn) {
string guess = guesser.guess();
if (guess.length()!=length) // Illegal guess
return TECHNICAL_VICTORY_TO_CHOOSER;
if (guess==choice) {
return indexOfTurn + 1;
} else {
auto reply = calculateBullAndPgia(choice, guess);
guesser.learn(reply); // tell the guesser how many bull and pgia were in its latest guess
}
}
return TECHNICAL_VICTORY_TO_CHOOSER; // Guesser could not guess in time
}
}