(如何操作)使用ENUM进行扑克定位

发布于 2024-11-29 20:17:22 字数 1009 浏览 2 评论 0原文

假设我创建了以下类:

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 技术交流群。

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

发布评论

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

评论(6

一人独醉 2024-12-06 20:17:22

可以根据其代码的值来选择枚举实例。您可以使用静态 Map 来执行此操作。唯一的问题是它必须位于静态内部类中:

public enum Position {

    ...

    Position(int code) {
        this.code = code;
        MapHolder.BY_CODE.put(code, this);
    }

    private static class MapHolder {
        private static final Map<Integer, Position> BY_CODE = new HashMap<Integer, Position>();
    }

    public static Position findByCode(int code) {
        return MapHolder.BY_CODE.get(code);
    }

我还建议将选择下一个位置的逻辑委托给枚举本身。只需将此方法添加到 Position 枚举中即可:

public Position nextPosition() {
     return findByCode((this.code + 1) % Position.values().length);
}

然后,您的客户端代码可以简单地执行:

for (Player player : players) {
    player.setPosition(player.getPosition().nextPosition());
}

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:

public enum Position {

    ...

    Position(int code) {
        this.code = code;
        MapHolder.BY_CODE.put(code, this);
    }

    private static class MapHolder {
        private static final Map<Integer, Position> BY_CODE = new HashMap<Integer, Position>();
    }

    public static Position findByCode(int code) {
        return MapHolder.BY_CODE.get(code);
    }

I'd also recommend delegating the logic of picking the next position to the enum itself. Just add this method to the Position enum:

public Position nextPosition() {
     return findByCode((this.code + 1) % Position.values().length);
}

Then, your client code can simply go:

for (Player player : players) {
    player.setPosition(player.getPosition().nextPosition());
}
对岸观火 2024-12-06 20:17:22

看这里是一个在线扑克游戏示例

    #include <iostream>
#include <stdlib.h>
#include <cctype>
#include <memory.h>
#include "logic.h"

using namespace std;
// & means location
// * value of location

struct Cards {

enum Value {Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace};
Value V;
int Suite;
enum Suite {Hearts, Diamonds, Clubs, Spades};
};

struct Hand {
Cards [5];

};

struct Deck {
Cards [52];

};

char * personsName [20]; //this means location with *
int numNames = 0;
char * Name;
char * search;
bool Continue;

int main ()
{
do {
for(int x = 0; x < 20; x++){
cout << "Enter a name, type END to stop: ";
Name = readString ();

if(strcmp (Name, "END") != 0){
personsName [x] = Name;
numNames++;
}
else{
Continue = (strcmp (Name, "END") != 0);
break;
}

}
cout << "Pre-sorted Names" << endl;
printNames(personsName,numNames);

bubbleSort(personsName,numNames);

cout << "Post-sorted Names" << endl;
printNames(personsName,numNames);

cout << "What would you like to find?" << endl;
search = readString();
cout << "The name is at index " << binarySearch(personsName,search,numNames) << endl;

} while (Continue);
delete [] * personsName;
return 0;
}

Look here is an onlinepoker game sample

    #include <iostream>
#include <stdlib.h>
#include <cctype>
#include <memory.h>
#include "logic.h"

using namespace std;
// & means location
// * value of location

struct Cards {

enum Value {Two = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace};
Value V;
int Suite;
enum Suite {Hearts, Diamonds, Clubs, Spades};
};

struct Hand {
Cards [5];

};

struct Deck {
Cards [52];

};

char * personsName [20]; //this means location with *
int numNames = 0;
char * Name;
char * search;
bool Continue;

int main ()
{
do {
for(int x = 0; x < 20; x++){
cout << "Enter a name, type END to stop: ";
Name = readString ();

if(strcmp (Name, "END") != 0){
personsName [x] = Name;
numNames++;
}
else{
Continue = (strcmp (Name, "END") != 0);
break;
}

}
cout << "Pre-sorted Names" << endl;
printNames(personsName,numNames);

bubbleSort(personsName,numNames);

cout << "Post-sorted Names" << endl;
printNames(personsName,numNames);

cout << "What would you like to find?" << endl;
search = readString();
cout << "The name is at index " << binarySearch(personsName,search,numNames) << endl;

} while (Continue);
delete [] * personsName;
return 0;
}
乖不如嘢 2024-12-06 20:17:22

我不确定我是否正确理解您的问题,但您可以为您的枚举声明一个 getCode() 方法:

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

  int code;

  //Constructor
  Position(int code) {
    this.code = code;
  }

  public int getCode() {
    return code;
  }
}

I am not sure if I understand your question correctly, but you can declare a getCode() method for your 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

  int code;

  //Constructor
  Position(int code) {
    this.code = code;
  }

  public int getCode() {
    return code;
  }
}
她如夕阳 2024-12-06 20:17:22

code 是包访问范围。

如果您从包内调用,那么您可能需要执行以下操作:

Position.BB.code

如果您从包外部调用,则可能需要在枚举类内提供访问器。

public int getCode(){
    return this.code;
}

code is package access scope.

If you are calling from within the package, then you may do a

Position.BB.code

If you are calling from outside the package, then you may need to provide an accessor inside the enum class.

public int getCode(){
    return this.code;
}
梦晓ヶ微光ヅ倾城 2024-12-06 20:17:22

你可以只使用ordinal()作为位置(从0开始,就像下标一样)。此外,如果枚举按相反顺序(因为交易传递到左侧)可能会更容易,如下面的代码。这在短游戏中不起作用(您需要对玩家数量进行取模)。

enum Position {
    Dealer, CO, HJ, UTG3, UTG2, UTG, BB, SS;
    Position next() {
        return values()[(ordinal() + 1) % values().length];
    }
}
class Player {
    Player(Position position) {
        this.position = position;
    }
    void next() {
        position = position.next();
    }
    Position position;
}
public class Main {
    public static void main(String[] args) {
        Player[] players = new Player[Position.values().length];
        for (Position position : Position.values())
            players[position.ordinal()] = new Player(position);
        for (Player player : players)
            System.out.println(player.position);
        for (int i = 0; i < Position.values().length; i++)
            for (Player player : players)
                player.next();
        for (Player player : players)
            System.out.println(player.position);
    }
}

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).

enum Position {
    Dealer, CO, HJ, UTG3, UTG2, UTG, BB, SS;
    Position next() {
        return values()[(ordinal() + 1) % values().length];
    }
}
class Player {
    Player(Position position) {
        this.position = position;
    }
    void next() {
        position = position.next();
    }
    Position position;
}
public class Main {
    public static void main(String[] args) {
        Player[] players = new Player[Position.values().length];
        for (Position position : Position.values())
            players[position.ordinal()] = new Player(position);
        for (Player player : players)
            System.out.println(player.position);
        for (int i = 0; i < Position.values().length; i++)
            for (Player player : players)
                player.next();
        for (Player player : players)
            System.out.println(player.position);
    }
}
九命猫 2024-12-06 20:17:22

我不太确定这是否是您的要求。看看下面给出的代码是否有帮助:

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

    private static Position[] currentPosition = Position.values();

    public static Position[] getCurrentPosition() {
        return currentPosition;
    }

    private static List<Position> positionWrap = Arrays.asList(currentPosition);

    public static void shiftPostion() {
        Collections.rotate(positionWrap, 1);
        updatePositions();
    }

    private static void updatePositions() {
        for (int i = 0; i < currentPosition.length; i++){
            currentPosition[i].code = i + 1;
        }
    }

    public static void main(String[] args) {
        Position[] pos = getCurrentPosition();
        System.out.println(Arrays.toString(pos));
        Position.shiftPostion();
        System.out.println(Arrays.toString(pos));
        Position.shiftPostion();
        System.out.println(Arrays.toString(pos));

    }

    @Override
    public String toString() {

        return this.name() + "(" + code + ")";
    }
}

I'm not quite sure if this is your requirement.See if the code given below helps :

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

    private static Position[] currentPosition = Position.values();

    public static Position[] getCurrentPosition() {
        return currentPosition;
    }

    private static List<Position> positionWrap = Arrays.asList(currentPosition);

    public static void shiftPostion() {
        Collections.rotate(positionWrap, 1);
        updatePositions();
    }

    private static void updatePositions() {
        for (int i = 0; i < currentPosition.length; i++){
            currentPosition[i].code = i + 1;
        }
    }

    public static void main(String[] args) {
        Position[] pos = getCurrentPosition();
        System.out.println(Arrays.toString(pos));
        Position.shiftPostion();
        System.out.println(Arrays.toString(pos));
        Position.shiftPostion();
        System.out.println(Arrays.toString(pos));

    }

    @Override
    public String toString() {

        return this.name() + "(" + code + ")";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文