int Table::addPlayer(Player const& player, int position)
{
if (position > 0 || position < 11) {
deque<Player>::iterator it = playerList.begin()+position;
deque<Player>::iterator itStart = playerList.begin()+postion;
while(*it != "(empty seat)") {
it++;
if (it == playerList.end()) {
it = playerList.begin();
}
if (it == itStart) {
cout << "Table full" << endl;
return -1;
}
}
//TODO overload Player assignment, << operator
*it = player;
cout << "Player " << player << " sits at position " << it - playerList.begin() << endl;
return it - playerList.begin();
} else {
cout << "Position not a valid position, must be 1-10" << endl;
return -1;
}
}
int Table::removePlayer(Player const& player)
{
for (deque<Player>::iterator it = playerList.begin();it != playerList.end(); it++) {
//TODO Do I need to overload == in Player?
if(*it == player) {
*it = "(empty seat)";
int pos = it - playerList.begin();
cout << "Player " << player << " stands up from position " << pos << endl;
return pos;
}
}
cout << "Player " << player << " not found" << endl;
return -1;
}
希望获得有关Texas Hold Em Poker模拟的Table类的这两个成员函数的一些反馈。任何信息语法,效率或什至是常见的做法都将不胜感激。
最佳答案
addPlayer()中的第一个while循环正在取消引用尚未检查其有效性的迭代器。如果传入的position值大于容器中的元素数量,则可能会发生崩溃。这可以由调用方控制,但是在引用点进行控制是更好的做法。
关于c++ - 需要有关C++中Table类的两个成员函数的反馈,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2954442/