From af4fdad3bd82c926ef742aa419c18cb15595703d Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 4 Dec 2010 19:55:38 +0100 Subject: Added riichi handler. --- server/game.cpp | 6 +++++- server/game.h | 6 ++++++ server/player.cpp | 40 ++++++++++++++++++++++------------------ 3 files changed, 33 insertions(+), 19 deletions(-) (limited to 'server') diff --git a/server/game.cpp b/server/game.cpp index 66a791c..c8950ce 100644 --- a/server/game.cpp +++ b/server/game.cpp @@ -195,7 +195,11 @@ void Game::handle_action_draw(Action action) { round_end(); } break; - case Action::Riichi: + case Action::Riichi: { + players[current_player].declare_riichi(); + round_update_draw(); + } break; + case Action::Kan: case Action::Draw: // Not implemented yet. diff --git a/server/game.h b/server/game.h index c347a6b..f8d9ff4 100644 --- a/server/game.h +++ b/server/game.h @@ -34,6 +34,9 @@ class Game : public boost::enable_shared_from_this { int score; int wind; + //! Indexes of tiles that will give tenpai (used after riichi declaration). + List tenpai_indexes; + //! Prepare for a new round. void round_start(int w); @@ -84,6 +87,9 @@ class Game : public boost::enable_shared_from_this { //! Claim last discard from pond. Tile claim(); + //! Declare riichi. + void declare_riichi(); + //! Make chi from tile. void make_chi(Tile tile, int target); diff --git a/server/player.cpp b/server/player.cpp index 8397c48..7e890fb 100644 --- a/server/player.cpp +++ b/server/player.cpp @@ -7,6 +7,7 @@ void Game::Player::round_start(int w) { hand.clear(); open.clear(); pond.clear(); + tenpai_indexes.clear(); riichi = false; score = 25000; wind = w; @@ -54,8 +55,15 @@ Actions Game::Player::get_actions_draw() { } } else { - // Only tile that can be discarded is the last drawn one. - possible_actions.push_back(Action(Action::Discard, Action::Hand, hand.size() - 1)); + if(tenpai_indexes) { + for(List::iterator it = tenpai_indexes.begin(); it != tenpai_indexes.end(); it++) { + possible_actions.push_back(Action(Action::Discard, Action::Hand, *it)); + } + tenpai_indexes.clear(); + } else { + // Only tile that can be discarded is the last drawn one. + possible_actions.push_back(Action(Action::Discard, Action::Hand, hand.size() - 1)); + } } return possible_actions; @@ -105,26 +113,18 @@ bool Game::Player::can_riichi() { return false; } - Tiles tiles = hand; - - tiles.sort(); - - // Take first tile out of the set. - Tile out = tiles.front(); - tiles.erase(tiles.begin()); - - // Iterate over the rest of the set, exchanging the tile until a tenpai is found or all is tested. - for(Tiles::iterator it = tiles.begin(); it != tiles.end(); it++) { + // Iterate over the hand, testing to remove each tile and see if it gives tenpai. + for(Tiles::iterator it = hand.begin(); it != hand.end(); it++) { + Tiles tiles = hand; + tiles.del(it - hand.begin()); + tiles.sort(); + if(Hand::tenpai(tiles)) { - return true; + tenpai_indexes.push_back(it - hand.begin()); } - - // TODO: Skip unnecessary tests if player got several equal tiles on hand. - - std::swap(*it, out); } - return false; + return bool(tenpai_indexes); } Game::Player::Targets Game::Player::can_chi(Tile tile) { @@ -225,6 +225,10 @@ Tile Game::Player::claim() { return t; } +void Game::Player::declare_riichi() { + riichi = true; +} + void Game::Player::make_chi(Tile tile, int target) { Tiles chi; -- cgit v1.2.3