c++ - 需要对 C++ 中 Table 类的两个成员函数的反馈

标签 c++ poker

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;
}

想要一些关于德州扑克模拟表类的这两个成员函数的反馈。任何信息语法、效率甚至常见做法都将不胜感激。

最佳答案

您在 addPlayer() 中的第一个 while 循环正在取消引用尚未检查有效性的迭代器。如果传入的 position 值大于容器中元素的数量,则可能会发生崩溃。这可能由调用者控制,但最好在引用点控制它。

关于c++ - 需要对 C++ 中 Table 类的两个成员函数的反馈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2954442/

相关文章:

c++ - 如何减少逻辑表达式?

c++ - 使用 ofstream 保存文件

c++ - 如何指定 vc11 lambda 调用约定

java - 为什么我的 isFullHouse() 方法也接受简单的三类?

php - 检查扑克直

c++ - CMake 测试 : was a library compiled/linked against libc++ or libstd++?

c++ - 错误 C2065 : 'frame' : undeclared identifier

java - 为 N 名玩家生成一手牌(每手五张牌)

javascript - Array.reduce 将多维数组转换为对象数组

C# 扑克库