需要有关 C++ 中 Table 类的两个成员函数的反馈;

发布于 2024-09-04 00:52:34 字数 1532 浏览 2 评论 0原文

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

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

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

Would like some feedback on these two member functions of a Table class for Texas Hold Em Poker simulation. Any information syntax, efficiency or even common practices would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一笑百媚生 2024-09-11 00:52:34

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

Your first while loop in addPlayer() is dereferencing an iterator that hasn't been checked for validity. If a value of position is passed in that is greater than the number of elements in the container you will likely have a crash. This might be controlled by the caller, but it is much better practice to control it at the point of reference.

把昨日还给我 2024-09-11 00:52:34
  • 正确缩进代码,可以使以后更容易阅读和理解。如果您缺乏经验,请考虑采用样式指南(例如 Google 的 C++ 样式指南< /a>)。
  • 检查玩家迭代器是否取消引用“(空座位)”是有问题的,您可能需要考虑一些替代方案:
    • 保留一个单独的空椅子列表,并在 AddPlayer() 上分配它们。
    • 让多态性发挥作用并使用 EmptySeatPlayer 类。
    • 允许 null 播放器直接存储在 tableList 中。
  • 我不清楚为什么 AddPlayer 需要一个 position 参数,如果它只是分配下一个可用座位(直到找到一个)。也许完全删除该参数并让 Table 来计算它。
  • 您最终可能希望将游戏文本与业务逻辑分离。
  • 您可能不应该直接使用位置,您应该对玩家和桌子进行操作。人们可能会认为它是类的一个细节,不应该由函数公开。
  • 使用 std::find,而不是使用 while (*it != player) 并在每次迭代中检查结束。
  • 此外,您还进行了大量的“按值传递”,最好传递 const Player& 以避免不必要的复制。
  • Indent your code properly, it makes it far easier to read and understand later. If you're inexperienced, consider adopting a style guide (e.g. Google's C++ style guide).
  • Checking that the player iterator dereferences to "(empty seat)" is questionable, you may want to consider a few alternatives:
    • Keep a separate list of empty chairs and allocate them on AddPlayer().
    • Let polymorphism fly and use a EmptySeatPlayer class.
    • Allow null players to be stored directly in the tableList.
  • I'm unclear why AddPlayer needs a position parameter, if it just allocates the next available seat (until it finds one). Maybe remove the parameter entirely and let the Table figure it out.
  • You'll eventually probably want to decouple your game-play text from the business logic.
  • You probably shouldn't be using position directly, you should be operating on the players and the table. One might consider it a detail of the class that shouldn't be exposed by functions.
  • Rather than while (*it != player) and checking for end in each iteration, use std::find.
  • Also you're doing a lot of 'pass-by-value', it's good practice to pass const Player& to avoid unnecessary copies.
向日葵 2024-09-11 00:52:34

删除可以在 for 循环中完成。

for(deque<Player>::iterator it = playerList.begin(); it!= playerList.end(); it++){
    //if we've found what we're looking for
    if(*it == player){
     //then remove the player and return his/her position.
     *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;

我发现这更干净一些,而且我个人非常喜欢评论。

the remove could be done in a for loop..

for(deque<Player>::iterator it = playerList.begin(); it!= playerList.end(); it++){
    //if we've found what we're looking for
    if(*it == player){
     //then remove the player and return his/her position.
     *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;

I find this a bit cleaner, and I am personally a big fan of comments.

毁虫ゝ 2024-09-11 00:52:34

1) 如果您不打算修改方法中的参数,则通过 const 引用传递:

int Table::addPlayer(Player const& player, int position)

这提供了后者隐藏的好处,但也引入了 const 正确性的概念。

2)尝试并学习如何使用标准算法:

在这种情况下,您的循环可以替换为使用 std::find()

int Table::addPlayer(Player const& player, int position)
{       
    deque<Player>::iterator itStart = playerList.begin()+position;

    deque<Player>::iterator it      = std::find(itStart, playerList.end(), "(empty seat)");
    if (it == playerList.end())
    {
        it  = std::find(playerList.begin(), itStart, "(empty seat)");
        if (it == itStart)
        {
            cout << "Table full" << endl;
            return -1;
        }
    }
    ...

并且

int Table::removePlayer(Player const& player)
{
    deque<Player>::iterator it = std::find(playerList.begin(), playerList.end(), "(empty seat)");
    if (it == playerList.end())
    {
        cout << "Player " << player << " not found" << endl;
        return -1;
    }
    .....

1) If you are not going to modify a parameter in a method then pass by const reference:

int Table::addPlayer(Player const& player, int position)

This provides hidden benfits latter but also introduces the concept of const correctness.

2) Try and learn how to use the standard algorithms:

In this case your loops can be replaced with the use of std::find()

int Table::addPlayer(Player const& player, int position)
{       
    deque<Player>::iterator itStart = playerList.begin()+position;

    deque<Player>::iterator it      = std::find(itStart, playerList.end(), "(empty seat)");
    if (it == playerList.end())
    {
        it  = std::find(playerList.begin(), itStart, "(empty seat)");
        if (it == itStart)
        {
            cout << "Table full" << endl;
            return -1;
        }
    }
    ...

And

int Table::removePlayer(Player const& player)
{
    deque<Player>::iterator it = std::find(playerList.begin(), playerList.end(), "(empty seat)");
    if (it == playerList.end())
    {
        cout << "Player " << player << " not found" << endl;
        return -1;
    }
    .....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文