summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2010-12-04 19:55:38 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2010-12-04 19:55:57 +0100
commitaf4fdad3bd82c926ef742aa419c18cb15595703d (patch)
treedde849a731411a3ebd17dc87bb205d2e3fd9e276 /server
parent97951fe38059cc4f0757ebe83466327c556b4e2d (diff)
Added riichi handler.
Diffstat (limited to 'server')
-rw-r--r--server/game.cpp6
-rw-r--r--server/game.h6
-rw-r--r--server/player.cpp40
3 files changed, 33 insertions, 19 deletions
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<Game> {
int score;
int wind;
+ //! Indexes of tiles that will give tenpai (used after riichi declaration).
+ List<int> tenpai_indexes;
+
//! Prepare for a new round.
void round_start(int w);
@@ -84,6 +87,9 @@ class Game : public boost::enable_shared_from_this<Game> {
//! 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<int>::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;