(如何操作)使用ENUM进行扑克定位
假设我创建了以下类:
public enum Position {
Dealer(1), //1
SB(2), //2
BB(3), //3
UTG(4), //4
UTG1(5), //5
UTG2(6), //6
UTG3(7), //7
HJ(8), //8
CO(9); //9
//Constructor
int code;
Position(int code) {
this.code = code;
}
}
如何使用括号中的数字来操作 ENUM? 例如,在我的扑克桌课上,我会培养新玩家。每个玩家传递参数Position。 所以一开始,
player[1].getPosition() = Dealer
player[2].getPosition() = SB
player[3].getPosition() = BB
etc etc etc
手牌结束后,所有的位置都需要移一位。
所以玩家[1]需要有CO(9)位置。
玩家[2]需要拥有庄家(1)位置。
玩家[3]需要拥有SB(2)位置。
等等
我知道我可以创建一个 for 循环,其中的变量循环遍历数字 1 到 9,但是如何根据 PositionENUM 内的整数访问位置?
编辑: 我已经有了 getter 和 setter。
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
然而,吸气剂和吸气剂并没有为我提供每轮正确改变球员位置的信息。
在每轮下注之后,我需要更改每个玩家的位置,因此我需要弄清楚如何在每轮下注之后移动每个玩家的 ENUM 位置。
Let's say I created the following class:
public enum Position {
Dealer(1), //1
SB(2), //2
BB(3), //3
UTG(4), //4
UTG1(5), //5
UTG2(6), //6
UTG3(7), //7
HJ(8), //8
CO(9); //9
//Constructor
int code;
Position(int code) {
this.code = code;
}
}
How do I manipulate ENUM by using the numbers in parenthesis?
For example, in my Poker Table class, I initiate new players. Each player passes the parameter Position.
So initially,
player[1].getPosition() = Dealer
player[2].getPosition() = SB
player[3].getPosition() = BB
etc etc etc
After the hand is over, all the positions need to be shifted over by one.
So player[1] needs to have the CO(9) position.
player[2] needs to have the Dealer(1) position.
player[3] needs to have the SB(2) position.
etc etc
I understand that I can just make a for loop with a variable cycling through the numbers 1 through 9, but how do I access the position based on the integer inside the PositionENUM?
EDIT:
I already have the getters and setters.
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
However the getters and the setters do not provide me with the correctly change the Positions of the players each round.
After every betting round, I need to change the Position of each player, so I need to figure out how to shift the ENUM Position of each player after each betting round.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
可以根据其代码的值来选择枚举实例。您可以使用静态
Map
来执行此操作。唯一的问题是它必须位于静态内部类中:我还建议将选择下一个位置的逻辑委托给枚举本身。只需将此方法添加到
Position
枚举中即可:然后,您的客户端代码可以简单地执行:
It's possible to choose an enum instance based on the value of its code. You can use a static
Map<Integer, Position>
to do this. The only gotcha is that it has to be housed in a static inner class:I'd also recommend delegating the logic of picking the next position to the enum itself. Just add this method to the
Position
enum:Then, your client code can simply go:
看这里是一个在线扑克游戏示例
Look here is an onlinepoker game sample
我不确定我是否正确理解您的问题,但您可以为您的枚举声明一个
getCode()
方法:I am not sure if I understand your question correctly, but you can declare a
getCode()
method for your enum:code
是包访问范围。如果您从包内调用,那么您可能需要执行以下操作:
如果您从包外部调用,则可能需要在枚举类内提供访问器。
code
is package access scope.If you are calling from within the package, then you may do a
If you are calling from outside the package, then you may need to provide an accessor inside the enum class.
你可以只使用ordinal()作为位置(从0开始,就像下标一样)。此外,如果枚举按相反顺序(因为交易传递到左侧)可能会更容易,如下面的代码。这在短游戏中不起作用(您需要对玩家数量进行取模)。
you could just use the ordinal() for the position (starts at 0 like subscripts). also it might be easier if the enums were in reverse order (since the deal passes to the left) like the code below. this will not work in a short game (you would need to do a modulo the number of players).
我不太确定这是否是您的要求。看看下面给出的代码是否有帮助:
I'm not quite sure if this is your requirement.See if the code given below helps :