Skip to content

Commit

Permalink
feat: Change the way shoots are distributed
Browse files Browse the repository at this point in the history
Shoots (5) are distributed when a player round starts.
  • Loading branch information
arcadien committed Oct 15, 2023
1 parent 731bab8 commit 6f085db
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 41 deletions.
7 changes: 6 additions & 1 deletion lib/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,19 @@ void Game::recordSucceededShoot() {
* Note: loop on player 4
*/
void Game::nextRound() {

currentPlayer->endRound();

currentRound++;
uint8_t playerId = currentPlayer->id;
auto nextPlayerId = 0;
if (playerId < (PLAYER_COUNT - 1)) {
nextPlayerId = playerId + 1;
}
currentPlayer = &players[nextPlayerId];
currentPlayer->nextRound();

currentPlayer->startRound();

}

bool Game::isFinished() {
Expand Down
29 changes: 16 additions & 13 deletions lib/Gui/BTEGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ bool BTEGui::isTargetHit(IGui::TARGET target) {
return ((targetState & (1 << target)) == (1 << target));
}

void BTEGui::resetTargets() { targetState = 0; }
void BTEGui::resetTargets() {
targetState = 0;
for (char letter : TARGET_APP_LETTERS) {
sprintf(stringBuffer, "*%cR0G0B0*", letter);
_output(stringBuffer);
}
}

void BTEGui::displayPlayerInfo(const Player &player) {

Expand All @@ -71,20 +77,20 @@ static void sendApplication() {

// targets
Serial.println(F("add_led(2,1,2,A,0,0,0)"));
Serial.println(F("add_led(6,1,2,B,0,0,0)"));
Serial.println(F("add_led(10,1,2,C,0,0,0)"));
Serial.println(F("add_led(14,1,2,D,0,0,0)"));
Serial.println(F("add_led(18,1,2,E,0,0,0)"));
Serial.println(F("add_led(6,1,2,Z,0,0,0)"));
Serial.println(F("add_led(10,1,2,E,0,0,0)"));
Serial.println(F("add_led(14,1,2,R,0,0,0)"));
Serial.println(F("add_led(18,1,2,T,0,0,0)"));

// Shoot row
Serial.println(F("add_text_box(0,5,2,L,Shoot,245,240,245,)"));
Serial.println(F("add_text(0,5,large,L,Shoot,245,240,245,)"));
Serial.println(F("add_text_box(4,5,1,L,0,245,240,245,Q)"));
Serial.println(F("add_text_box(8,5,1,L,0,245,240,245,S)"));
Serial.println(F("add_text_box(12,5,1,L,0,245,240,245,D)"));
Serial.println(F("add_text_box(16,5,1,L,0,245,240,245,F)"));

// Hit row
Serial.println(F("add_text_box(0,6,2,L,Hit,245,240,245,)"));
Serial.println(F("add_text(0,6,large,L,Hit,245,240,245,)"));
Serial.println(F("add_text_box(4,6,1,L,0,245,240,245,W)"));
Serial.println(F("add_text_box(8,6,1,L,0,245,240,245,X)"));
Serial.println(F("add_text_box(12,6,1,L,0,245,240,245,C)"));
Expand All @@ -96,7 +102,7 @@ static void sendApplication() {
Serial.println(F("add_button(12,4,14,R,r)"));
Serial.println(F("add_button(16,4,17,B,b)"));

Serial.println(F("add_text_box(18,4,3,L,Next,245,240,245,)"));
Serial.println(F("add_text(19,4,large,L,Next,245,240,245,)"));
Serial.println(F("add_button(19,5,25,N,|)"));

Serial.println(F("add_button(0,8,30,R,|)"));
Expand All @@ -113,13 +119,10 @@ static void sendApplication() {}

void BTEGui::restart() {
sendApplication();
for (char letter : TARGET_APP_LETTERS) {
sprintf(stringBuffer, "*%cR0G0B0*", letter);
_output(stringBuffer);
}
resetTargets();
}

const char BTEGui::TARGET_APP_LETTERS[5] = {'A', 'B', 'C', 'D', 'E'};
const char BTEGui::TARGET_APP_LETTERS[5] = {'A', 'Z', 'E', 'R', 'T'};
const char BTEGui::PLAYER_DATA_APP_LETTERS[8] = {
'Q', 'W', // player 1
'S', 'X', // player 2
Expand Down
8 changes: 6 additions & 2 deletions lib/Player/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@ void Player::reset() {
}
currentRound = 0;
}
void Player::nextRound() {

void Player::startRound() {
totalShoots += 5;
}

void Player::endRound() {
currentRound = 0;
if (currentRound < (ROUND_COUNT - 1)) {
currentRound++;
}
totalShoots += 5;
}

void Player::recordSucceededShoot() {
Expand Down
9 changes: 4 additions & 5 deletions lib/Player/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
*/
class Player {


public:

static const uint8_t ROUND_COUNT = 5;
static const uint8_t MAX_HIT_IN_A_ROUND = 5;

Expand All @@ -35,10 +33,11 @@ class Player {
uint8_t totalShoots;

Player(uint8_t id);
uint8_t getTotalHitCount() const;

uint8_t getTotalHitCount() const;

void reset();
void nextRound();
void startRound();
void endRound();
void recordSucceededShoot();
};
7 changes: 6 additions & 1 deletion src/target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void loop() {
uint16_t value = analogRead(A0);
if (value > threshold) {
serialPrintInfo(value);
gui.hitTarget(IGui::TARGET::One);
game.recordSucceededShoot();
ledOn();
delay(500);
Expand All @@ -97,6 +98,7 @@ void cmd_unrecognized(SerialCommands *sender, const char *cmd) {
sender->GetSerial()->print(cmd);
sender->GetSerial()->println("]");
}

void cmd_setThreshold(SerialCommands *sender) {
char *threshold_value = sender->Next();
uint16_t value = atoi(threshold_value);
Expand Down Expand Up @@ -126,7 +128,10 @@ void cmd_resetTargets(SerialCommands *sender) {
delay(2000);
ledOff();
}
void cmd_nextRound(SerialCommands *sender) { game.nextRound(); }
void cmd_nextRound(SerialCommands *sender) {
gui.resetTargets();
game.nextRound();
}

void ledOff() { digitalWrite(LED_PIN, HIGH); }
void ledOn() { digitalWrite(LED_PIN, LOW); }
29 changes: 24 additions & 5 deletions test/noarch/test_BTEGui/BTEGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,32 @@ void expect_target_to_manage_5_targets() {

cut.hitTarget(IGui::TARGET::Two);

expectedLine = "*BR255G255B255*";
expectedLine = "*ZR255G255B255*";
std::getline(fakeSerial, actual);
TEST_ASSERT_EQUAL_STRING_MESSAGE(expectedLine.c_str(), actual.c_str(),
"Hit target shall be white");

cut.hitTarget(IGui::TARGET::Three);

expectedLine = "*ER255G255B255*";
std::getline(fakeSerial, actual);
TEST_ASSERT_EQUAL_STRING_MESSAGE(expectedLine.c_str(), actual.c_str(),
"Hit target shall be white");

cut.hitTarget(IGui::TARGET::Four);

expectedLine = "*RR255G255B255*";
std::getline(fakeSerial, actual);
TEST_ASSERT_EQUAL_STRING_MESSAGE(expectedLine.c_str(), actual.c_str(),
"Hit target shall be white");

cut.hitTarget(IGui::TARGET::Five);

expectedLine = "*TR255G255B255*";
std::getline(fakeSerial, actual);
TEST_ASSERT_EQUAL_STRING_MESSAGE(expectedLine.c_str(), actual.c_str(),
"Hit target shall be white");

TEST_ASSERT_TRUE(cut.isTargetHit(IGui::TARGET::One));
TEST_ASSERT_TRUE(cut.isTargetHit(IGui::TARGET::Two));
TEST_ASSERT_TRUE(cut.isTargetHit(IGui::TARGET::Three));
Expand All @@ -72,8 +89,8 @@ void expect_gui_to_display_player_info() {

// clang-format off
TestData testCases[4] = {
{Player(0), givenPlayerId: 0, givenRound: 0, givenHit: 1, "*Q0*", "*W1*"},
{Player(1), givenPlayerId: 1, givenRound: 1, givenHit: 3, "*S5*", "*X3*"},
{Player(0), givenPlayerId: 0, givenRound: 1, givenHit: 1, "*Q5*", "*W1*"},
{Player(1), givenPlayerId: 1, givenRound: 2, givenHit: 3, "*S10*", "*X3*"},
{Player(2), givenPlayerId: 2, givenRound: 3, givenHit: 4, "*D15*", "*C4*"},
{Player(3), givenPlayerId: 3, givenRound: 2, givenHit: 5, "*F10*", "*V5*"}
};
Expand All @@ -86,8 +103,11 @@ void expect_gui_to_display_player_info() {
TEST_ASSERT_EQUAL_UINT8(currentPlayer->id, testCase.givenPlayerId);

for (uint8_t round = 0; round < testCase.givenRound; round++) {
currentPlayer->nextRound();
currentPlayer->startRound();
currentPlayer->endRound();
}

// note: for simplicity sake, all hits are recorded in the last round
for (uint8_t hit = 0; hit < testCase.givenHit; hit++) {
currentPlayer->recordSucceededShoot();
}
Expand All @@ -96,7 +116,6 @@ void expect_gui_to_display_player_info() {
std::string actual;
std::getline(fakeSerial, actual);


buffer << "( Player ";
buffer << unsigned(testCase.givenPlayer.id) << " total shoots)"
<< std::endl;
Expand Down
39 changes: 25 additions & 14 deletions test/noarch/test_Player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,50 @@ void setUp() {}

void expect_player_to_have_5_shots_at_beginning() {
Player p(0);
TEST_ASSERT_EQUAL_UINT8_MESSAGE(0, p.totalShoots,
"Player begins with 0 shoots");
p.nextRound();
p.startRound();
TEST_ASSERT_EQUAL_UINT8_MESSAGE(5, p.totalShoots,
"Shoots are incremented when round ends");
"Player first round begins with 5 shoots");
p.endRound();

TEST_ASSERT_EQUAL_UINT8_MESSAGE(5, p.totalShoots,
"Shoots are only incremented when round starts");
}

void expect_met_count_to_spread_well_on_five_rounds() {
Player p(0);

// round 0
p.startRound();
p.recordSucceededShoot();
p.nextRound();
p.endRound();

// round 1
p.startRound();
p.recordSucceededShoot();
p.nextRound();
p.endRound();

// round 2
p.startRound();
p.recordSucceededShoot();
p.nextRound();
p.endRound();

// round 3
p.startRound();
p.recordSucceededShoot();
p.nextRound();
p.endRound();

// round 4
p.startRound();
p.recordSucceededShoot();
p.endRound();

TEST_ASSERT_EQUAL_UINT8_MESSAGE(
5, p.getTotalHitCount(), "Met shots shall be collected from all rounds");
}

void expect_a_player_to_stop_counting_hit_after_5_in_a_round() {
Player p(0);
p.startRound();
p.recordSucceededShoot();
p.recordSucceededShoot();
p.recordSucceededShoot();
Expand All @@ -69,14 +79,15 @@ void expect_a_player_to_stop_counting_hit_after_5_in_a_round() {
p.recordSucceededShoot();
p.recordSucceededShoot();

TEST_ASSERT_EQUAL_UINT8_MESSAGE(
5, p.getTotalHitCount(), "Hit shots shall not exceed 5 in a round");
p.nextRound();
TEST_ASSERT_EQUAL_UINT8_MESSAGE(5, p.getTotalHitCount(),
"Hit shots shall not exceed 5 in a round");

p.endRound();
p.recordSucceededShoot();

TEST_ASSERT_EQUAL_UINT8_MESSAGE(
6, p.getTotalHitCount(), "After switching to a new round, hit count resumes");
TEST_ASSERT_EQUAL_UINT8_MESSAGE(
6, p.getTotalHitCount(),
"After switching to a new round, hit count resumes");
}

int main(int, char **) {
Expand Down

0 comments on commit 6f085db

Please sign in to comment.