update readme
[rofl0r-oopoker.git] / player.cpp
blob6ce1957ee84dc15e33c40135eaa4f8f4d73180da
1 /*
2 OOPoker
4 Copyright (c) 2010 Lode Vandevenne
5 All rights reserved.
7 This file is part of OOPoker.
9 OOPoker is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
14 OOPoker is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with OOPoker. If not, see <http://www.gnu.org/licenses/>.
23 #include "player.h"
24 #include "info.h"
25 #include "ai.h"
26 #include "ai_human.h"
27 #include "random.h"
29 #include <set>
31 Player::Player(AI* ai, const std::string& name)
32 : ai(ai)
33 , stack(0)
34 , wager(0)
35 , buyInTotal(0)
36 , folded(false)
37 , showdown(false)
38 , name(name)
42 void Player::setCards(Card card1, Card card2)
44 holeCard1 = card1;
45 holeCard2 = card2;
48 std::string Player::getName() const
50 return name;
53 std::string Player::getAIName() const
55 return ai->getAIName();
58 Action Player::doTurn(const Info& info)
60 return ai->doTurn(info);
63 bool Player::isAllIn() const
65 return stack <= 0 && wager > 0;
68 bool Player::isOut() const
70 return stack <= 0 && wager <= 0;
73 bool Player::isFolded() const
75 return folded;
78 bool Player::canDecide() const
80 return stack > 0 && !folded;
83 bool Player::isHuman() const
85 return dynamic_cast<AIHuman*>(ai) != 0;
88 void Player::onEvent(const Event& event)
90 ai->onEvent(event);
95 static const int SC = 17;
96 static std::string sc[SC] = { "b", "d", "f", "g", "h", "k", "j", "l", "m", "n", "p", "r", "s", "t", "v", "w", "ch" };
97 static const int MC = 20;
98 static std::string mc[MC] = { "b", "d", "f", "g", "h", "k", "l", "m", "n", "p", "r", "s", "t", "v", "w"
99 , "ch", "rl", "nh", "tt", "wr" };
100 static const int EC = 12;
101 static std::string ec[EC] = { "f", "k", "l", "m", "n", "p", "r", "s", "t", "v", "nny", "try" };
102 static const int V = 7;
103 static std::string v[V] = { "a", "e", "i", "o", "u", "oe", "ee" };
105 std::string getRandomSyllable(bool begin, bool end)
107 std::string a, b, c;
109 if(begin && end)
111 a = sc[getRandom(0, SC-1)];
112 b = v[getRandom(0, V-1)];
114 else if(begin)
116 a = sc[getRandom(0, SC-1)];
117 b = v[getRandom(0, V-1)];
119 else if(end)
121 a = mc[getRandom(0, MC-1)];
122 b = v[getRandom(0, V-1)];
123 if(getRandom() < 0.5) c = ec[getRandom(0, EC-1)];
125 else
127 a = mc[getRandom(0, MC-1)];
128 b = v[getRandom(0, V-1)];
131 return a + b + c;
134 std::string getRandomName(int numsyl, bool capitalize)
136 std::string result;
137 for(int i = 0; i < numsyl; i++)
139 result += getRandomSyllable(i == 0, i == numsyl - 1);
142 if(capitalize) result[0] -= 'a' - 'A';
144 return result;
147 static std::string getRandomNameImpl()
149 int r = getRandom(0, 9);
150 int numsyl = 1;
151 if(r < 1) numsyl = 1;
152 else if(r < 7) numsyl = 2;
153 else numsyl = 3;
155 std::string result = getRandomName(numsyl, true);
157 /*if(getRandom() < 0.33)
159 result += " ";
160 result += 'A' + getRandom(0, 25);
164 return result;
167 std::set<std::string> usedNames; //avoid duplicate names, it's quite important that players all have a different name, or AI's can't recognise the difference!
169 std::string getRandomName()
171 for(;;)
173 std::string name = getRandomNameImpl();
174 if(usedNames.count(name) == 0)
176 usedNames.insert(name);
177 return name;