Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #47 from tresinformal/issue_23
Browse files Browse the repository at this point in the history
Fix #23

Looks good.
  • Loading branch information
martprenger authored Nov 29, 2023
2 parents f2d9178 + ebd170b commit a7f899b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 11 deletions.
Binary file added .DS_Store
Binary file not shown.
31 changes: 31 additions & 0 deletions game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ ball game::get_ball() const
return b;
}

bool has_winner(const game& g) {

Check warning on line 21 in game.cpp

View workflow job for this annotation

GitHub Actions / lint

/game.cpp:21:6 [modernize-use-trailing-return-type]

use a trailing return type for this function
return
g.get_players()[0].get_score() >= 20
|| g.get_players()[1].get_score() >= 20
;
}
int get_player_score(const game& g, const int player_index) {

Check warning on line 27 in game.cpp

View workflow job for this annotation

GitHub Actions / lint

/game.cpp:27:5 [modernize-use-trailing-return-type]

use a trailing return type for this function
return g.get_players().at(player_index).get_score();
}


void set_player_score(
game& g,
const int player_index,
const int score
) {
g.get_players().at(player_index).set_score(score);
}

void test_game()
{
// Here write tests for the game logic
Expand Down Expand Up @@ -65,4 +84,16 @@ void test_game()
assert(is_more_or_less_equal(ball_center_y, 0.0));
}
#endif // FIX_ISSUE_25
// #23: The game finishes when a player reaches 20 points
{
game g;
const int player_index{0};
const int score{20};
// Equivalent to g.get_player(player_index).set_score(score);
set_player_score(g, player_index, score);
get_player_score(g, player_index);
assert(get_player_score(g, player_index) == score); // Check ourselves
assert(has_winner(g));

}
}
13 changes: 13 additions & 0 deletions game.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ class game {
int get_n_players();
ball get_ball() const;

/// Read-only version of get_players
const auto& get_players() const noexcept { return players; }

/// Modifyable-players version of get_players
auto& get_players() noexcept { return players; }

private:
std::vector<player> players;
};


bool has_winner(const game& g);

int get_player_score(const game& g, const int player_index);

Check warning on line 29 in game.h

View workflow job for this annotation

GitHub Actions / lint

/game.h:29:22 [readability-avoid-const-params-in-decls]

parameter 1 is const-qualified in the function declaration; const-qualification of parameters only has an effect in function definitions

void set_player_score(game& g, const int player_index, const int score);

void test_game();

#endif // GAME_H
33 changes: 23 additions & 10 deletions player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,31 @@ player::player(): m_score{0} {

}

int player::get_score() const {
int player::get_score() const noexcept {
return m_score;
}

void test_player() {
// Here write tests for players
// 17 - The player has a score counting system
const player player_g;
const int expected_player_score = 0;
const int player_score = player_g.get_score();
//check player score
assert(expected_player_score == player_score);

void player::set_score(const int new_score) {
assert(new_score >= 0);
m_score = new_score;
}

void test_player()
{
{
// Here write tests for players
// 17 - The player has a score counting system
const player player_g;
const int expected_player_score = 0;
const int player_score = player_g.get_score();
//check player score
assert(expected_player_score == player_score);
}
// can set and get the score
{
player p;
const int some_score{3}; // just a value, can be any between zero and 19 (inclusive)
p.set_score(some_score);
assert(p.get_score() == some_score);
}
}
3 changes: 2 additions & 1 deletion player.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
class player {

Check failure on line 5 in player.h

View workflow job for this annotation

GitHub Actions / lint

/player.h:5:1 [clang-diagnostic-error]

unknown type name 'class'

Check warning on line 5 in player.h

View workflow job for this annotation

GitHub Actions / lint

/player.h:5:7 [cppcoreguidelines-avoid-non-const-global-variables]

variable 'player' is non-const and globally accessible, consider making it const

Check failure on line 5 in player.h

View workflow job for this annotation

GitHub Actions / lint

/player.h:5:13 [clang-diagnostic-error]

expected ';' after top level declarator
public:
player();
int get_score() const;
int get_score() const noexcept;
void set_score(const int new_score);
private:
int m_score;
};
Expand Down

0 comments on commit a7f899b

Please sign in to comment.