需要有关 C++ 中 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;
}
希望获得有关德州扑克模拟的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
AddPlayer()
上分配它们。EmptySeatPlayer
类。null
播放器直接存储在tableList
中。AddPlayer
需要一个position
参数,如果它只是分配下一个可用座位(直到找到一个)。也许完全删除该参数并让Table
来计算它。位置
,您应该对玩家和桌子进行操作。人们可能会认为它是类的一个细节,不应该由函数公开。std::find
,而不是使用while (*it != player)
并在每次迭代中检查结束。const Player&
以避免不必要的复制。AddPlayer()
.EmptySeatPlayer
class.null
players to be stored directly in thetableList
.AddPlayer
needs aposition
parameter, if it just allocates the next available seat (until it finds one). Maybe remove the parameter entirely and let theTable
figure it out.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.while (*it != player)
and checking for end in each iteration, usestd::find
.const Player&
to avoid unnecessary copies.删除可以在 for 循环中完成。
我发现这更干净一些,而且我个人非常喜欢评论。
the remove could be done in a for loop..
I find this a bit cleaner, and I am personally a big fan of comments.
1) 如果您不打算修改方法中的参数,则通过 const 引用传递:
这提供了后者隐藏的好处,但也引入了 const 正确性的概念。
2)尝试并学习如何使用标准算法:
在这种情况下,您的循环可以替换为使用 std::find()
并且
1) If you are not going to modify a parameter in a method then pass by const reference:
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()
And