Skip to content

Commit

Permalink
check_only needs unit tests
Browse files Browse the repository at this point in the history
check_only hasn't broken existing unit tests
need to implement check_option next
  • Loading branch information
r3w0p committed Nov 4, 2024
1 parent 3733938 commit 4ca6682
Show file tree
Hide file tree
Showing 11 changed files with 267 additions and 137 deletions.
12 changes: 6 additions & 6 deletions include/caravan/model/caravan.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ class Caravan {
explicit Caravan(CaravanName cvname) :
name(cvname), track({}), i_track(0) {};

void clear();

uint16_t get_bid();

Slot get_slot(uint8_t pos);
Expand All @@ -47,13 +45,15 @@ class Caravan {

Suit get_suit();

void put_numeral_card(Card card);
bool clear(bool check_only = false);

bool put_numeral_card(Card card, bool check_only = false);

Card put_face_card(Card card, uint8_t pos);
bool put_face_card(Card card, uint8_t pos, Card *target = nullptr, bool check_only = false);

void remove_rank(Rank rank, uint8_t pos_exclude);
bool remove_rank(Rank rank, uint8_t pos_exclude, bool check_only = false);

void remove_suit(Suit suit, uint8_t pos_exclude);
bool remove_suit(Suit suit, uint8_t pos_exclude, bool check_only = false);
};

#endif //CARAVAN_MODEL_CARAVAN_H
6 changes: 3 additions & 3 deletions include/caravan/model/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class Game {

bool has_sold(CaravanName cvname);

bool option_clear(Player *pptr, GameCommand *command, bool check);
bool option_clear(Player *pptr, GameCommand *command, bool check_only);

bool option_discard(Player *pptr, GameCommand *command, bool check);
bool option_discard(Player *pptr, GameCommand *command, bool check_only);

bool option_play(Player *pptr, GameCommand *command, bool check);
bool option_play(Player *pptr, GameCommand *command, bool check_only);

public:
explicit Game(GameConfig *gc);
Expand Down
2 changes: 1 addition & 1 deletion include/caravan/model/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Player {

void maybe_add_card_to_hand();

Card discard_from_hand_at(uint8_t pos);
bool discard_from_hand_at(uint8_t pos, Card *discarded = nullptr, bool check_only = false);
};

#endif //CARAVAN_MODEL_PLAYER_H
6 changes: 3 additions & 3 deletions include/caravan/model/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class Table {

Caravan *get_caravan(CaravanName cvname);

void clear_caravan(CaravanName cvname);
bool clear_caravan(CaravanName cvname, bool check_only = false);

void play_face_card(CaravanName cvname, Card card, uint8_t pos);
bool play_face_card(CaravanName cvname, Card card, uint8_t pos, bool check_only = false);

void play_numeral_card(CaravanName cvname, Card card);
bool play_numeral_card(CaravanName cvname, Card card, bool check_only = false);
};

#endif //CARAVAN_MODEL_TABLE_H
171 changes: 120 additions & 51 deletions src/caravan/model/caravan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@
*
* @throws CaravanGameException Caravan track is empty.
*/
void Caravan::clear() {
bool Caravan::clear(bool check_only) {
if (i_track == 0) {
throw CaravanGameException("Cannot clear empty caravan.");
if(check_only) {
return false;

} else {
throw CaravanGameException("Cannot clear empty caravan.");
}
}

i_track = 0;
if(!check_only) {
i_track = 0;
}

return true;
}

/**
Expand Down Expand Up @@ -163,28 +172,39 @@ Suit Caravan::get_suit() {
* @throws CaravanGameException Numeral card has same rank as most recent card in caravan.
* @throws CaravanGameException Numeral card does not follow direction of caravan.
*/
void Caravan::put_numeral_card(Card card) {
bool Caravan::put_numeral_card(Card card, bool check_only) {
Direction dir;
Suit suit;
bool ascends;
bool not_same_suit;
bool not_same_dir;

if (!is_numeral_card(card)) {
throw CaravanGameException(
"The card must be a numeral card.");
if(check_only) {
return false;
} else {
throw CaravanGameException("The card must be a numeral card.");
}
}

if (i_track == TRACK_NUMERIC_MAX) {
throw CaravanGameException(
"The caravan is at its maximum numeral card capacity.");
if (check_only) {
return false;
} else {
throw CaravanGameException(
"The caravan is at its maximum numeral card capacity.");
}
}

if (i_track > 0) {
if (card.rank == track[i_track - 1].card.rank) {
throw CaravanGameException(
"A numeral card must not have same rank as "
"the most recent card in the caravan.");
if (check_only) {
return false;
} else {
throw CaravanGameException(
"A numeral card must not have same rank as "
"the most recent card in the caravan.");
}
}

if (i_track > 1) {
Expand All @@ -197,15 +217,23 @@ void Caravan::put_numeral_card(Card card) {
(dir == DESCENDING and ascends);

if (not_same_suit and not_same_dir) {
throw CaravanGameException(
"The numeral card must follow the caravan's "
"direction or match the caravan's suit.");
if (check_only) {
return false;
} else {
throw CaravanGameException(
"The numeral card must follow the caravan's "
"direction or match the caravan's suit.");
}
}
}
}

track[i_track] = {card, {}, 0};
i_track += 1;
if(!check_only) {
track[i_track] = {card, {}, 0};
i_track += 1;
}

return true;
}

/**
Expand All @@ -218,42 +246,67 @@ void Caravan::put_numeral_card(Card card) {
* @throws CaravanGameException Chosen card is not a face card.
* @throws CaravanGameException Numeral card is at maximum face card capacity.
*/
Card Caravan::put_face_card(Card card, uint8_t pos) {
bool Caravan::put_face_card(Card card, uint8_t pos, Card *target, bool check_only) {
uint8_t i;
Card c_on;

if (pos < TRACK_NUMERIC_MIN) {
throw CaravanGameException(
"A caravan position has not been entered.");
if(check_only) {
return false;
} else {
throw CaravanGameException(
"A caravan position has not been entered.");
}
}


if (pos > i_track) {
throw CaravanGameException(
"There is not a numeral card at caravan position " +
std::to_string(pos) + ".");
if(check_only) {
return false;
} else {
throw CaravanGameException(
"There is not a numeral card at caravan position " +
std::to_string(pos) + ".");
}
}

if (!is_face_card(card)) {
throw CaravanGameException("The chosen card must be a face card.");
if(check_only) {
return false;
} else {
throw CaravanGameException(
"The chosen card must be a face card.");
}
}

i = pos - 1;
c_on = track[i].card;

if (card.rank == JACK) {
remove_numeral_card(i);
if(!check_only) {
remove_numeral_card(i);
}

} else {
if (track[i].i_faces == TRACK_FACE_MAX) {
throw CaravanGameException("The caravan is at its maximum face card capacity.");
if(check_only) {
return false;
} else {
throw CaravanGameException(
"The caravan is at its maximum face card capacity.");
}
}

track[i].faces[track[i].i_faces] = card;
track[i].i_faces += 1;
if(!check_only) {
track[i].faces[track[i].i_faces] = card;
track[i].i_faces += 1;
}
}

if(!check_only and target != nullptr) {
*target = c_on;
}

return c_on;
return true;
}

/**
Expand All @@ -265,29 +318,37 @@ Card Caravan::put_face_card(Card card, uint8_t pos) {
*
* @throws CaravanFatalException Exclude position is out of range.
*/
void Caravan::remove_rank(Rank rank, uint8_t pos_exclude) {
bool Caravan::remove_rank(Rank rank, uint8_t pos_exclude, bool check_only) {
uint8_t i_track_original;

if (i_track == 0) {
return;
return true;
}

if (pos_exclude > i_track) {
throw CaravanFatalException(
"The exclude position is out of range.");
if(check_only) {
return false;
} else {
throw CaravanFatalException(
"The exclude position is out of range.");
}
}

i_track_original = i_track;
if(!check_only) {
i_track_original = i_track;

for (int t = i_track_original - 1; t >= 0; --t) {
if (pos_exclude > 0 and t == (pos_exclude - 1)) {
continue;
}
for (int t = i_track_original - 1; t >= 0; --t) {
if (pos_exclude > 0 and t == (pos_exclude - 1)) {
continue;
}

if (track[t].card.rank == rank) {
remove_numeral_card(t);
if (track[t].card.rank == rank) {
remove_numeral_card(t);
}
}
}

return true;
}

/**
Expand All @@ -299,29 +360,37 @@ void Caravan::remove_rank(Rank rank, uint8_t pos_exclude) {
*
* @throws CaravanFatalException Exclude position is out of range.
*/
void Caravan::remove_suit(Suit suit, uint8_t pos_exclude) {
bool Caravan::remove_suit(Suit suit, uint8_t pos_exclude, bool check_only) {
uint8_t i_track_original;

if (i_track == 0) {
return;
return true;
}

if (pos_exclude > i_track) {
throw CaravanFatalException(
"The exclude position is out of range.");
if(check_only) {
return false;
} else {
throw CaravanFatalException(
"The exclude position is out of range.");
}
}

i_track_original = i_track;
if(!check_only) {
i_track_original = i_track;

for (int t = i_track_original - 1; t >= 0; --t) {
if (pos_exclude > 0 and t == (pos_exclude - 1)) {
continue;
}
for (int t = i_track_original - 1; t >= 0; --t) {
if (pos_exclude > 0 and t == (pos_exclude - 1)) {
continue;
}

if (track[t].card.suit == suit) {
remove_numeral_card(t);
if (track[t].card.suit == suit) {
remove_numeral_card(t);
}
}
}

return true;
}

/*
Expand Down
Loading

0 comments on commit 4ca6682

Please sign in to comment.