1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:07:44 +00:00

LibCards+Games+GamesSettings: Return ErrorOr from CardStack::push()

Very few of these calls can propagate their errors yet, but one step at
a time. :^)
This commit is contained in:
Sam Atkins 2023-01-20 12:45:02 +00:00 committed by Linus Groh
parent 83687f85df
commit 8b3a94ffbc
6 changed files with 32 additions and 30 deletions

View file

@ -39,10 +39,10 @@ public:
TRY(preview->add_stack(point, Cards::CardStack::Type::Normal)); TRY(preview->add_stack(point, Cards::CardStack::Type::Normal));
for (size_t i = 0; i < Cards::Card::card_count; ++i) for (size_t i = 0; i < Cards::Card::card_count; ++i)
preview->stack_at_location(0).push(TRY(Cards::Card::try_create(Cards::Suit::Diamonds, static_cast<Cards::Rank>(i)))); TRY(preview->stack_at_location(0).push(TRY(Cards::Card::try_create(Cards::Suit::Diamonds, static_cast<Cards::Rank>(i)))));
preview->stack_at_location(1).push(TRY(Cards::Card::try_create(Cards::Suit::Spades, Cards::Rank::Ace))); TRY(preview->stack_at_location(1).push(TRY(Cards::Card::try_create(Cards::Suit::Spades, Cards::Rank::Ace))));
preview->stack_at_location(2).push(TRY(Cards::Card::try_create(Cards::Suit::Hearts, Cards::Rank::Queen))); TRY(preview->stack_at_location(2).push(TRY(Cards::Card::try_create(Cards::Suit::Hearts, Cards::Rank::Queen))));
preview->stack_at_location(3).push(TRY(Cards::Card::try_create(Cards::Suit::Clubs, Cards::Rank::Jack))); TRY(preview->stack_at_location(3).push(TRY(Cards::Card::try_create(Cards::Suit::Clubs, Cards::Rank::Jack))));
preview->stack_at_location(0).peek().set_upside_down(true); preview->stack_at_location(0).peek().set_upside_down(true);
preview->stack_at_location(2).set_highlighted(true); preview->stack_at_location(2).set_highlighted(true);

View file

@ -70,9 +70,9 @@ void Game::timer_event(Core::TimerEvent&)
if (current_pile.count() < m_new_game_animation_pile) { if (current_pile.count() < m_new_game_animation_pile) {
auto card = m_new_deck.take_last(); auto card = m_new_deck.take_last();
card->set_upside_down(true); card->set_upside_down(true);
current_pile.push(card); current_pile.push(card).release_value_but_fixme_should_propagate_errors();
} else { } else {
current_pile.push(m_new_deck.take_last()); current_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
++m_new_game_animation_pile; ++m_new_game_animation_pile;
} }
@ -81,7 +81,7 @@ void Game::timer_event(Core::TimerEvent&)
if (m_new_game_animation_pile == piles.size()) { if (m_new_game_animation_pile == piles.size()) {
auto& stock_pile = stack_at_location(Stock); auto& stock_pile = stack_at_location(Stock);
while (!m_new_deck.is_empty()) while (!m_new_deck.is_empty())
stock_pile.push(m_new_deck.take_last()); stock_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
update(stock_pile.bounding_box()); update(stock_pile.bounding_box());
@ -404,13 +404,13 @@ void Game::draw_cards()
NonnullRefPtrVector<Card> moved_cards; NonnullRefPtrVector<Card> moved_cards;
while (!play.is_empty()) { while (!play.is_empty()) {
auto card = play.pop(); auto card = play.pop();
stock.push(card); stock.push(card).release_value_but_fixme_should_propagate_errors();
moved_cards.prepend(card); moved_cards.prepend(card);
} }
while (!waste.is_empty()) { while (!waste.is_empty()) {
auto card = waste.pop(); auto card = waste.pop();
stock.push(card); stock.push(card).release_value_but_fixme_should_propagate_errors();
moved_cards.prepend(card); moved_cards.prepend(card);
} }
@ -424,7 +424,7 @@ void Game::draw_cards()
update(stock.bounding_box()); update(stock.bounding_box());
} else { } else {
auto play_bounding_box = play.bounding_box(); auto play_bounding_box = play.bounding_box();
play.take_all(waste); play.take_all(waste).release_value_but_fixme_should_propagate_errors();
size_t cards_to_draw = 0; size_t cards_to_draw = 0;
switch (m_mode) { switch (m_mode) {
@ -445,7 +445,7 @@ void Game::draw_cards()
for (size_t i = 0; (i < cards_to_draw) && !stock.is_empty(); ++i) { for (size_t i = 0; (i < cards_to_draw) && !stock.is_empty(); ++i) {
auto card = stock.pop(); auto card = stock.pop();
cards_drawn.prepend(card); cards_drawn.prepend(card);
play.push(move(card)); play.push(move(card)).release_value_but_fixme_should_propagate_errors();
} }
remember_move_for_undo(stock, play, cards_drawn); remember_move_for_undo(stock, play, cards_drawn);
@ -466,7 +466,7 @@ void Game::pop_waste_to_play_stack()
if (play.is_empty() && !waste.is_empty()) { if (play.is_empty() && !waste.is_empty()) {
auto card = waste.pop(); auto card = waste.pop();
moving_cards().append(card); moving_cards().append(card);
play.push(move(card)); play.push(move(card)).release_value_but_fixme_should_propagate_errors();
} }
} }
@ -490,7 +490,7 @@ bool Game::attempt_to_move_card_to_foundations(CardStack& from)
auto card = from.pop(); auto card = from.pop();
mark_intersecting_stacks_dirty(card); mark_intersecting_stacks_dirty(card);
foundation.push(card); foundation.push(card).release_value_but_fixme_should_propagate_errors();
NonnullRefPtrVector<Card> moved_card; NonnullRefPtrVector<Card> moved_card;
moved_card.append(card); moved_card.append(card);
@ -612,12 +612,12 @@ void Game::perform_undo()
if (m_last_move.from->type() == CardStack::Type::Play && m_mode == Mode::SingleCardDraw) { if (m_last_move.from->type() == CardStack::Type::Play && m_mode == Mode::SingleCardDraw) {
auto& waste = stack_at_location(Waste); auto& waste = stack_at_location(Waste);
if (!m_last_move.from->is_empty()) if (!m_last_move.from->is_empty())
waste.push(m_last_move.from->pop()); waste.push(m_last_move.from->pop()).release_value_but_fixme_should_propagate_errors();
} }
for (auto& to_intersect : m_last_move.cards) { for (auto& to_intersect : m_last_move.cards) {
mark_intersecting_stacks_dirty(to_intersect); mark_intersecting_stacks_dirty(to_intersect);
m_last_move.from->push(to_intersect); m_last_move.from->push(to_intersect).release_value_but_fixme_should_propagate_errors();
(void)m_last_move.to->pop(); (void)m_last_move.to->pop();
} }
@ -632,7 +632,7 @@ void Game::perform_undo()
} }
} }
for (auto& card : cards_popped) { for (auto& card : cards_popped) {
play.push(card); play.push(card).release_value_but_fixme_should_propagate_errors();
} }
} }

View file

@ -95,7 +95,7 @@ void Game::perform_undo()
for (size_t i = 0; i < m_last_move.card_count; i++) for (size_t i = 0; i < m_last_move.card_count; i++)
cards.append(m_last_move.to->pop()); cards.append(m_last_move.to->pop());
for (ssize_t i = m_last_move.card_count - 1; i >= 0; i--) for (ssize_t i = m_last_move.card_count - 1; i >= 0; i--)
m_last_move.from->push(cards[i]); m_last_move.from->push(cards[i]).release_value_but_fixme_should_propagate_errors();
update_score(-1); update_score(-1);
@ -162,7 +162,7 @@ void Game::detect_full_stacks()
auto original_current_rect = current_pile.bounding_box(); auto original_current_rect = current_pile.bounding_box();
for (size_t j = 0; j < Card::card_count; j++) { for (size_t j = 0; j < Card::card_count; j++) {
completed_stack.push(current_pile.pop()); completed_stack.push(current_pile.pop()).release_value_but_fixme_should_propagate_errors();
} }
update(original_current_rect); update(original_current_rect);
@ -384,9 +384,9 @@ void Game::timer_event(Core::TimerEvent&)
if (current_pile.count() < (cards_to_draw - 1)) { if (current_pile.count() < (cards_to_draw - 1)) {
auto card = m_new_deck.take_last(); auto card = m_new_deck.take_last();
card->set_upside_down(true); card->set_upside_down(true);
current_pile.push(card); current_pile.push(card).release_value_but_fixme_should_propagate_errors();
} else { } else {
current_pile.push(m_new_deck.take_last()); current_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
++m_new_game_animation_pile; ++m_new_game_animation_pile;
} }
@ -397,7 +397,7 @@ void Game::timer_event(Core::TimerEvent&)
auto& stock_pile = stack_at_location(Stock); auto& stock_pile = stack_at_location(Stock);
while (!m_new_deck.is_empty()) while (!m_new_deck.is_empty())
stock_pile.push(m_new_deck.take_last()); stock_pile.push(m_new_deck.take_last()).release_value_but_fixme_should_propagate_errors();
update(stock_pile.bounding_box()); update(stock_pile.bounding_box());
@ -414,7 +414,7 @@ void Game::timer_event(Core::TimerEvent&)
auto& current_pile = stack_at_location(piles.at(m_draw_animation_pile)); auto& current_pile = stack_at_location(piles.at(m_draw_animation_pile));
auto card = stock_pile.pop(); auto card = stock_pile.pop();
card->set_upside_down(false); card->set_upside_down(false);
current_pile.push(card); current_pile.push(card).release_value_but_fixme_should_propagate_errors();
update(current_pile.bounding_box()); update(current_pile.bounding_box());
++m_draw_animation_pile; ++m_draw_animation_pile;

View file

@ -88,7 +88,7 @@ void CardGame::drop_cards_on_stack(Cards::CardStack& stack, CardStack::MovementR
VERIFY(stack.is_allowed_to_push(m_moving_cards.at(0), m_moving_cards.size(), movement_rule)); VERIFY(stack.is_allowed_to_push(m_moving_cards.at(0), m_moving_cards.size(), movement_rule));
for (auto& to_intersect : moving_cards()) { for (auto& to_intersect : moving_cards()) {
mark_intersecting_stacks_dirty(to_intersect); mark_intersecting_stacks_dirty(to_intersect);
stack.push(to_intersect); stack.push(to_intersect).release_value_but_fixme_should_propagate_errors();
(void)moving_cards_source_stack()->pop(); (void)moving_cards_source_stack()->pop();
} }

View file

@ -290,7 +290,7 @@ bool CardStack::make_top_card_visible()
return false; return false;
} }
void CardStack::push(NonnullRefPtr<Card> card) ErrorOr<void> CardStack::push(NonnullRefPtr<Card> card)
{ {
auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last(); auto top_most_position = m_stack_positions.is_empty() ? m_position : m_stack_positions.last();
@ -306,9 +306,10 @@ void CardStack::push(NonnullRefPtr<Card> card)
card->set_position(top_most_position); card->set_position(top_most_position);
m_stack.append(card); TRY(m_stack.try_append(card));
m_stack_positions.append(top_most_position); TRY(m_stack_positions.try_append(top_most_position));
calculate_bounding_box(); calculate_bounding_box();
return {};
} }
NonnullRefPtr<Card> CardStack::pop() NonnullRefPtr<Card> CardStack::pop()
@ -323,15 +324,16 @@ NonnullRefPtr<Card> CardStack::pop()
return card; return card;
} }
void CardStack::take_all(CardStack& stack) ErrorOr<void> CardStack::take_all(CardStack& stack)
{ {
while (!m_stack.is_empty()) { while (!m_stack.is_empty()) {
auto card = m_stack.take_first(); auto card = m_stack.take_first();
m_stack_positions.take_first(); m_stack_positions.take_first();
stack.push(move(card)); TRY(stack.push(move(card)));
} }
calculate_bounding_box(); calculate_bounding_box();
return {};
} }
void CardStack::calculate_bounding_box() void CardStack::calculate_bounding_box()

View file

@ -43,9 +43,9 @@ public:
bool make_top_card_visible(); // Returns true if the card was flipped. bool make_top_card_visible(); // Returns true if the card was flipped.
void push(NonnullRefPtr<Card> card); ErrorOr<void> push(NonnullRefPtr<Card>);
NonnullRefPtr<Card> pop(); NonnullRefPtr<Card> pop();
void take_all(CardStack&); ErrorOr<void> take_all(CardStack&);
void rebound_cards(); void rebound_cards();
bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const; bool is_allowed_to_push(Card const&, size_t stack_size = 1, MovementRule movement_rule = MovementRule::Alternating) const;