From 29581932f76b8d880109cbcfdda451046456560e Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 11 Dec 2010 05:38:25 +0100 Subject: Moved Player out of Game. --- server/game.h | 100 +-------------------------------------------------- server/player.cpp | 54 ++++++++++++++-------------- server/player.h | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 126 deletions(-) create mode 100644 server/player.h (limited to 'server') diff --git a/server/game.h b/server/game.h index 2a736de..0c47917 100644 --- a/server/game.h +++ b/server/game.h @@ -7,11 +7,7 @@ #include #include "wall.h" -#include "client.h" -#include "../common/action.h" -#include "../common/set.h" -#include "../common/state.h" -#include "../common/cyclicint.h" +#include "player.h" class Game : public boost::enable_shared_from_this { public: @@ -22,100 +18,6 @@ class Game : public boost::enable_shared_from_this { ~Game(); private: - typedef CyclicInt<4> PlayerNum; - - class Player { - public: - ClientBase::p client; - - Tiles hand; - Sets open; - Tiles pond; - bool riichi; - 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); - - //! Get a state snapshot. - PlayerState get_state(); - - //! Get a state snapshot, with concealed tiles filtered. - PlayerState get_state_filtered(); - - //! Get possible actions after a draw. - Actions get_actions_draw(); - - //! Get possible actions on discarded tile. - Actions get_actions_discard(Tile tile, PlayerNum discarder); - - typedef std::vector Targets; - - //! Check if player can declare riichi. - bool can_riichi(); - - //! Check if tile can be called for a chi. - Targets can_chi(Tile tile); - - //! Check if tile can be called for a pon. - int can_pon(Tile tile); - - //! Check if it's possible to make a concealed kan. - Targets can_kan(); - - //! Check if it's possible to extend a pon to a kan. - Targets can_kan_extend(); - - //! Check if tile can be called to kan target. - bool can_kan(Tile tile, int target); - - //! Check if hand is complete. - bool can_tsumo(); - - //! Check if tile can be called to complete hand. - bool can_ron(Tile tile); - - //! Draw tile. - void draw(Tile tile); - - //! Discard tile. - void discard(int target); - - //! Look at last discard in pond. - Tile last_discard(); - - //! Claim last discard from pond. - Tile claim(); - - //! Declare riichi. - void declare_riichi(); - - //! Make chi from tile. - void make_chi(Tile tile, int target); - - //! Make pon from tile. - void make_pon(Tile tile, int target, PlayerNum discarder); - - //! Make open kan from tile. - void make_kan(Tile tile, int target, PlayerNum discarder); - - //! Make concealed kan. - void make_kan(int target); - - //! Make extended kan. - void make_kan_extend(int target); - - //! Declare win on discarded tile. - void declare_ron(Tile tile); - - //! Declare win on self-drawn tile. - void declare_tsumo(); - }; - Player players[4]; Wall wall; diff --git a/server/player.cpp b/server/player.cpp index 67d35c9..59b128e 100644 --- a/server/player.cpp +++ b/server/player.cpp @@ -2,7 +2,7 @@ #include "hand.h" -void Game::Player::round_start(int w) { +void Player::round_start(int w) { // Reset contents. hand.clear(); open.clear(); @@ -16,7 +16,7 @@ void Game::Player::round_start(int w) { client->round_start(); } -PlayerState Game::Player::get_state() { +PlayerState Player::get_state() { Tilegroups h; h.push_back(hand); @@ -28,7 +28,7 @@ PlayerState Game::Player::get_state() { return state; } -PlayerState Game::Player::get_state_filtered() { +PlayerState Player::get_state_filtered() { Tilegroups h; h.push_back(hand); @@ -40,7 +40,7 @@ PlayerState Game::Player::get_state_filtered() { return state; } -Actions Game::Player::get_actions_draw() { +Actions Player::get_actions_draw() { Actions possible_actions; // Check if player can force a draw. @@ -94,9 +94,7 @@ Actions Game::Player::get_actions_draw() { return possible_actions; } -Actions Game::Player::get_actions_discard(Tile tile, PlayerNum discarder) { - std::cout << "Fisk:" << std::endl; - +Actions Player::get_actions_discard(Tile tile, PlayerNum discarder) { Actions possible_actions; if(!riichi) { @@ -136,7 +134,7 @@ Actions Game::Player::get_actions_discard(Tile tile, PlayerNum discarder) { return possible_actions; } -bool Game::Player::can_riichi() { +bool Player::can_riichi() { if(open) { return false; } @@ -155,7 +153,7 @@ bool Game::Player::can_riichi() { return bool(tenpai_indexes); } -Game::Player::Targets Game::Player::can_chi(Tile tile) { +Player::Targets Player::can_chi(Tile tile) { Targets targets; Tile::Set set = tile.get_set(); @@ -199,7 +197,7 @@ Game::Player::Targets Game::Player::can_chi(Tile tile) { return targets; } -int Game::Player::can_pon(Tile tile) { +int Player::can_pon(Tile tile) { Tiles::iterator it = std::find(hand.begin(), hand.end(), tile); if(it + 1 < hand.end() && it[1] == tile) { return it - hand.begin(); @@ -208,7 +206,7 @@ int Game::Player::can_pon(Tile tile) { return -1; } -Game::Player::Targets Game::Player::can_kan() { +Player::Targets Player::can_kan() { Targets targets; for(Tiles::iterator it = hand.begin(); it != hand.end(); it++) { @@ -225,7 +223,7 @@ Game::Player::Targets Game::Player::can_kan() { return targets; } -Game::Player::Targets Game::Player::can_kan_extend() { +Player::Targets Player::can_kan_extend() { Targets targets; for(Sets::iterator it = open.begin(); it != open.end(); it++) { @@ -237,52 +235,54 @@ Game::Player::Targets Game::Player::can_kan_extend() { return targets; } -bool Game::Player::can_kan(Tile tile, int target) { +bool Player::can_kan(Tile tile, int target) { if(std::size_t(target + 2) < hand.size() && hand[target + 2] == tile) { return true; } return false; } -bool Game::Player::can_tsumo() { +bool Player::can_tsumo() { Tiles tiles = hand; tiles.sort(); return Hand::agari(tiles); } -bool Game::Player::can_ron(Tile tile) { +bool Player::can_ron(Tile tile) { + // TODO: Check furiten. + Tiles tiles = hand; tiles.push_back(tile); tiles.sort(); return Hand::agari(tiles); } -void Game::Player::draw(Tile tile) { +void Player::draw(Tile tile) { hand.push_back(tile); } -void Game::Player::discard(int target) { +void Player::discard(int target) { Tile tile = hand[target]; hand.erase(hand.begin() + target); hand.sort(); pond.push_back(tile); } -Tile Game::Player::last_discard() { +Tile Player::last_discard() { return pond.back(); } -Tile Game::Player::claim() { +Tile Player::claim() { Tile& t = pond.back(); t.invisible = true; return t; } -void Game::Player::declare_riichi() { +void Player::declare_riichi() { riichi = true; } -void Game::Player::make_chi(Tile tile, int target) { +void Player::make_chi(Tile tile, int target) { Tiles chi; tile.rotated = true; @@ -325,7 +325,7 @@ void Game::Player::make_chi(Tile tile, int target) { open.push_back(Set(Set::Chi, chi, true)); } -void Game::Player::make_pon(Tile tile, int target, PlayerNum discarder) { +void Player::make_pon(Tile tile, int target, PlayerNum discarder) { Tiles pon; tile.rotated = true; @@ -351,7 +351,7 @@ void Game::Player::make_pon(Tile tile, int target, PlayerNum discarder) { open.push_back(Set(Set::Pon, pon, true)); } -void Game::Player::make_kan(Tile tile, int target, PlayerNum discarder) { +void Player::make_kan(Tile tile, int target, PlayerNum discarder) { Tiles kan; tile.rotated = true; @@ -380,7 +380,7 @@ void Game::Player::make_kan(Tile tile, int target, PlayerNum discarder) { open.push_back(Set(Set::Kan, kan, true)); } -void Game::Player::make_kan(int target) { +void Player::make_kan(int target) { Tiles kan; Tiles::iterator it = hand.begin() + target; @@ -403,7 +403,7 @@ void Game::Player::make_kan(int target) { open.push_back(Set(Set::Kan, kan, false)); } -void Game::Player::make_kan_extend(int target) { +void Player::make_kan_extend(int target) { Set& set = open[target - 1]; Tiles::iterator it = std::find(hand.begin(), hand.end(), set.tiles.front()); @@ -422,10 +422,10 @@ void Game::Player::make_kan_extend(int target) { set.type = Set::Kan; } -void Game::Player::declare_ron(Tile tile) { +void Player::declare_ron(Tile tile) { } -void Game::Player::declare_tsumo() { +void Player::declare_tsumo() { } diff --git a/server/player.h b/server/player.h new file mode 100644 index 0000000..cf98231 --- /dev/null +++ b/server/player.h @@ -0,0 +1,105 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include "client.h" + +#include "../common/set.h" +#include "../common/cyclicint.h" +#include "../common/state.h" +#include "../common/action.h" + +typedef CyclicInt<4> PlayerNum; + +class Player { + public: + ClientBase::p client; + + Tiles hand; + Sets open; + Tiles pond; + bool riichi; + 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); + + //! Get a state snapshot. + PlayerState get_state(); + + //! Get a state snapshot, with concealed tiles filtered. + PlayerState get_state_filtered(); + + //! Get possible actions after a draw. + Actions get_actions_draw(); + + //! Get possible actions on discarded tile. + Actions get_actions_discard(Tile tile, PlayerNum discarder); + + typedef std::vector Targets; + + //! Check if player can declare riichi. + bool can_riichi(); + + //! Check if tile can be called for a chi. + Targets can_chi(Tile tile); + + //! Check if tile can be called for a pon. + int can_pon(Tile tile); + + //! Check if it's possible to make a concealed kan. + Targets can_kan(); + + //! Check if it's possible to extend a pon to a kan. + Targets can_kan_extend(); + + //! Check if tile can be called to kan target. + bool can_kan(Tile tile, int target); + + //! Check if hand is complete. + bool can_tsumo(); + + //! Check if tile can be called to complete hand. + bool can_ron(Tile tile); + + //! Draw tile. + void draw(Tile tile); + + //! Discard tile. + void discard(int target); + + //! Look at last discard in pond. + Tile last_discard(); + + //! Claim last discard from pond. + Tile claim(); + + //! Declare riichi. + void declare_riichi(); + + //! Make chi from tile. + void make_chi(Tile tile, int target); + + //! Make pon from tile. + void make_pon(Tile tile, int target, PlayerNum discarder); + + //! Make open kan from tile. + void make_kan(Tile tile, int target, PlayerNum discarder); + + //! Make concealed kan. + void make_kan(int target); + + //! Make extended kan. + void make_kan_extend(int target); + + //! Declare win on discarded tile. + void declare_ron(Tile tile); + + //! Declare win on self-drawn tile. + void declare_tsumo(); +}; + +#endif -- cgit v1.2.3