gpt4 book ai didi

java - Java中的Turn机制

转载 作者:行者123 更新时间:2023-12-01 12:49:09 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现以下场景的转动机制:AppFrame 获取系统中的用户列表 > 它提示消息对话框“轮到用户了! > 用户输入他的答案,单击 SubmitButton > 系统将他的答案添加到 hashMap > 如果没有更多用户留下,它会显示结果框架,否则它将开始第二个用户的回合带有消息对话框等。

我尝试做类似的事情

public void turnMechanism() {
for (final User user : gf.getUsers()) {
JOptionPane.showMessageDialog(null, user.getUserName()+'s turn);
gameStopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
gameStopButtonActionPerformed(evt, user);

}
});
}
new ResultsFrame(gf);
dispose();

}
private void userStop(User u){
AnswerSet userSet = new AnswerSet(gameNameField.getText().trim()
.toLowerCase(), gameAnimalField.getText().trim().toLowerCase(),
gamePlantField.getText().trim().toLowerCase(), gameCountryField
.getText().trim().toLowerCase(), gameCityField
.getText().trim().toLowerCase(), gameOccupationField
.getText().trim().toLowerCase());
answers.put(u, userSet);
}

private void gameStopButtonActionPerformed(ActionEvent evt, User u) {
userStop(u);

但是它不能正常工作。你能给我一个提示如何做到这一点吗?没有静态字段。

最佳答案

这是我在板上的代码,可以为您工作。用户存储答案的结构类:

public class Player {
private final User user;
private AnswerSet answer;

public Player(User p_user) {
user = p_user;
}

public User getUser() {
return user;
}

public void setAnswer(AnswerSet answer) {
this.answer = answer;
}

public AnswerSet getAnswer() {
return answer;
}
}

这里是包含回合行为主要逻辑的类:

public class TurnBasedGame {
public enum State {
READY,
RUNNING,
ENDED
}

private final List<Player> players = new ArrayList<>();
private final int rounds;
private int currentRound;
private int currentTurn;
private State currentState = State.READY;


public TurnBasedGame() {
this(1);
}

public TurnBasedGame(int rounds) {
this.rounds = rounds;
}

public void addPlayer(Player player) {
players.add(player);
}

public List<Player> getPlayers() {
return players;
}

public void start() {
currentTurn = 0;
currentRound = 0;
currentState = State.RUNNING;
}

public void endPlayerTurn() {
if (currentState != State.RUNNING) {
throw new IllegalStateException("Game is not started");
}

currentTurn++;

if (currentTurn >= players.size()) {
currentTurn = 0;
currentRound = 0;
}
if (currentRound >= rounds) {
currentState = State.ENDED;
}
}

public State getCurrentState() {
return currentState;
}

public Player getCurrentPlayer() {
return players.get(currentTurn);
}
}

以下是将其与代码结合起来的方法:

public void turnMechanism() {
final TurnBasedGame game = new TurnBasedGame();

for (final User user : gf.getUsers()) {
game.addPlayer(new Player(user));
}

gameStopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
final Player currentPlayer = game.getCurrentPlayer();
currentPlayer.setAnswer(createAnswerSet());
game.endPlayerTurn();

switch (game.getCurrentState()) {
case RUNNING:
//next player
showCurrentPlayerInfo(game);
break;
case ENDED:
//show your resutls here
showResults(game);
break;
}
}
});
game.start();
showCurrentPlayerInfo(game);

//not sure what this does
new ResultsFrame(gf);
dispose();
}

private void showCurrentPlayerInfo(TurnBasedGame p_game) {
JOptionPane.showMessageDialog(null, p_game.getCurrentPlayer().getUser().getUserName() + 's turn);
}

private void showResults(TurnBasedGame p_game) {
for (Player player : p_game.getPlayers()) {
final AnswerSet answer = player.getAnswer();
//do whatever you want with the answer
}
}

关于java - Java中的Turn机制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24385479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com