纸牌游戏中的玩家顺序

发布于 2024-10-15 13:25:14 字数 1031 浏览 6 评论 0原文

我对玩家的顺序有疑问。我需要第一个玩家成为该轮的获胜者。所以我有四个按钮,有可能阻止错误玩家的移动,比如“不是你的回合,玩家二是第一个,因为是上一轮的获胜者”

我如何控制?最终一种方法来验证

我有四个卡片标签,

每个按钮的每个玩家手上有五张卡片,设置我做的对应卡片

//PLAYER1
    //card1 
    ActionListener one = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (gr.getCounter1() < 5) {
                gr.setCounter1(gr.getCounter1() + 1);
                test1.setIcon(play1a);
                pn1.setText(Integer.toString(play2a));
                pn5.setText(play3a);
                pn50.setText(play4a);
            } else {
                pn5.setText("No more cards");
            }
        }
    };

    arraybtn[1].addActionListener(one);
    arraybtn[1].setPreferredSize(new Dimension(120, 20));

    play1a = gr.GameRules1().getImage(); //image of card
    play2a = gr.GameRules2(); // value
    play3a = gr.GameRules3(); // king of hearts for exemple
    play4a = gr.GameRules4(); // hearts

    arraybtn[1].setText(gr.GameRules3()); // button name

i have a problem with the order of players. I need that the first player be the winer of round. So i have, four buttons, there is any possibility to block the move of incorrect player, something like "is not your turn, player two is the first because is the winer of previous round"

how i can control that? eventually a method to verify

i have four labels for cards, and five cards in hand for each player

for each button, set the correspondent card i do

//PLAYER1
    //card1 
    ActionListener one = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (gr.getCounter1() < 5) {
                gr.setCounter1(gr.getCounter1() + 1);
                test1.setIcon(play1a);
                pn1.setText(Integer.toString(play2a));
                pn5.setText(play3a);
                pn50.setText(play4a);
            } else {
                pn5.setText("No more cards");
            }
        }
    };

    arraybtn[1].addActionListener(one);
    arraybtn[1].setPreferredSize(new Dimension(120, 20));

    play1a = gr.GameRules1().getImage(); //image of card
    play2a = gr.GameRules2(); // value
    play3a = gr.GameRules3(); // king of hearts for exemple
    play4a = gr.GameRules4(); // hearts

    arraybtn[1].setText(gr.GameRules3()); // button name

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

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

发布评论

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

评论(2

九局 2024-10-22 13:25:14

你们的课程有多少个课程?希望它有一个驱动 GUI 的非 GUI 模型,并且应该是这个完全相同的模型来决定游戏的控制。我不会像本线程中的一位海报所建议的那样抛出错误,而是使用游戏控制器模型只允许正确的玩家玩。 GUI 将通过禁用所有不应按下的按钮并仅启用应按下的按钮来反映这一点。如何在代码中实现这一点取决于您当前程序的代码和整体结构。

编辑:答案的补充

不确定你是否还在,但这里有一个快速拼凑起来的例子来说明我的意思。 GamePlayer 类(显然)代表游戏的玩家,并且有一个字段 myTurn,仅当轮到该玩家时才为 true,并且该字段具有关联的 void setMyTurn(boolean) 方法和 boolean isMyTurn() 方法。

GameModel 类有一个名为players 的 GamePlayer 数组、一个允许其他类获取数组中每个 Player 的 getPlayer(int) 方法、一个表示当前轮到的玩家数组中索引的playerTurnIndex 以及一个 advancePlay( ) 方法,该方法增加此索引(按玩家数组的长度进行修改,以便它环绕),然后循环遍历玩家数组,根据playerTurnIndex 值为每个玩家设置 myTurn 字段。

GameGui 类有一个名为 gameModel 的 GameModel 字段,并保存 JButton 的集合(实际上是一个 HashMap),在每个按钮的 ActionPerformed 方法中,调用模型的 advancePlay() 方法,并调用一个名为 enablePlayerTurns() 的私有方法来启用或禁用玩家 JButtons 取决于模型中相应玩家的值:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

public class GamePlayMain {
    private static void createAndShowUI() {
        String[] players = {"John", "Bill", "Frank", "Henry"};
        GameModel model = new GameModel(players);
        GameGui gui = new GameGui(model);

        JFrame frame = new JFrame("GamePlayMain");
        frame.getContentPane().add(gui);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

class GamePlayer {
    private String name;
    private boolean myTurn = false;

    public GamePlayer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean isMyTurn() {
        return myTurn;
    }

    public void setMyTurn(boolean myTurn) {
        this.myTurn = myTurn;
    }

    @Override
    public String toString() {
        return name;
    }
}

class GameModel {
    private String[] playerNames;
    private GamePlayer[] players;
    private int playerTurnIndex = 0;

    public GameModel(String[] playerNames) {
        this.playerNames = playerNames;
        players = new GamePlayer[playerNames.length];
        for (int i = 0; i < playerNames.length; i++) {
            players[i] = new GamePlayer(playerNames[i]);
        }
        players[0].setMyTurn(true);
    }

    public int getPlayerTurnIndex() {
        return playerTurnIndex;
    }

    public GamePlayer getCurrentPlayer() {
        return players[playerTurnIndex];
    }

    public String[] getPlayerNames() {
        return playerNames;
    }

    public void advancePlay() {
        playerTurnIndex++;
        playerTurnIndex %= players.length;
        for (GamePlayer player : players) {
            if (player != getCurrentPlayer()) {
                player.setMyTurn(false);
            } else {
                player.setMyTurn(true);
            }
        }
    }

    public int getPlayerCount() {
        return players.length;
    }

    public GamePlayer getPlayer(int i) {
        return players[i];
    }

}

@SuppressWarnings("serial")
class GameGui extends JPanel {
    private GameModel gameModel;
    private JTextArea textArea = new JTextArea(14, 30);
    private Map<GamePlayer, JButton> playerButtonMap = new HashMap<GamePlayer, JButton>();

    public GameGui(GameModel gameModel) {
        this.gameModel = gameModel;

        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
        for (int i = 0; i < gameModel.getPlayerCount(); i++) {
            GamePlayer player = gameModel.getPlayer(i);
            JButton playerButton = new JButton(player.getName());
            playerButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    playerButtonActionPerformed(e);
                }
            });
            buttonPanel.add(playerButton);
            playerButtonMap.put(player, playerButton);
        }

        textArea.setEditable(false);
        textArea.setFocusable(false);

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new BorderLayout(5, 5));
        add(buttonPanel, BorderLayout.NORTH);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane, BorderLayout.CENTER);

        enablePlayerTurns();
    }

    private void playerButtonActionPerformed(ActionEvent e) {
        GamePlayer currentPlayer = gameModel.getCurrentPlayer();

        // TODO: have player "play"
        textArea.append("Player " + currentPlayer + " has just played\n");
        gameModel.advancePlay();
        enablePlayerTurns();

        currentPlayer = gameModel.getCurrentPlayer();
        textArea.append("It is now player " + currentPlayer + "'s turn\n");
    }

    private void enablePlayerTurns() {
        for (int i = 0; i < gameModel.getPlayerCount(); i++) {
            GamePlayer player = gameModel.getPlayer(i);
            JButton playerButton = playerButtonMap.get(player);
            playerButton.setEnabled(player.isMyTurn());
            if (player.isMyTurn()) {
                playerButton.requestFocusInWindow();
            }
        }
    }

}

How many classes does your program have? Hopefully it has a non-GUI model that drives the GUI, and it should be this very same model that dictates the control of game play. Rather than throw an error as one poster suggested in this thread, I would use the game controller model only allow the correct player to play. The GUI would reflect this by disabling all buttons that shouldn't be pushed and only enabling the ones that should be pushed. How this is implemented in code depends on your current program's code and overall structure.

edit: addition to answer

Not sure if you're still around, but here's a quickly slapped-together example of what I meant. The GamePlayer class represents (obviously) a player of the game and has a field, myTurn that is true only if it is that player's turn, and this field has an associated void setMyTurn(boolean) method and a boolean isMyTurn() method.

The GameModel class has an array of GamePlayer called players, a getPlayer(int) method that allows other classes to get each Player in the array, a playerTurnIndex that represents the index in the array of the player whose turn it currently is and a advancePlay() method that increments this index (mod'd by the length of the players array so it wraps around) and then loops through the players array setting the myTurn fields for each player depending on the playerTurnIndex value.

The GameGui class had a GameModel field called gameModel and holds a collection of JButtons (actually a HashMap), and in each button's ActionPerformed method, the model's advancePlay() method is called and a private method called enablePlayerTurns() is called that enables or disables the player JButtons depending on the value of the corresponding player in the model:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.*;

public class GamePlayMain {
    private static void createAndShowUI() {
        String[] players = {"John", "Bill", "Frank", "Henry"};
        GameModel model = new GameModel(players);
        GameGui gui = new GameGui(model);

        JFrame frame = new JFrame("GamePlayMain");
        frame.getContentPane().add(gui);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

class GamePlayer {
    private String name;
    private boolean myTurn = false;

    public GamePlayer(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public boolean isMyTurn() {
        return myTurn;
    }

    public void setMyTurn(boolean myTurn) {
        this.myTurn = myTurn;
    }

    @Override
    public String toString() {
        return name;
    }
}

class GameModel {
    private String[] playerNames;
    private GamePlayer[] players;
    private int playerTurnIndex = 0;

    public GameModel(String[] playerNames) {
        this.playerNames = playerNames;
        players = new GamePlayer[playerNames.length];
        for (int i = 0; i < playerNames.length; i++) {
            players[i] = new GamePlayer(playerNames[i]);
        }
        players[0].setMyTurn(true);
    }

    public int getPlayerTurnIndex() {
        return playerTurnIndex;
    }

    public GamePlayer getCurrentPlayer() {
        return players[playerTurnIndex];
    }

    public String[] getPlayerNames() {
        return playerNames;
    }

    public void advancePlay() {
        playerTurnIndex++;
        playerTurnIndex %= players.length;
        for (GamePlayer player : players) {
            if (player != getCurrentPlayer()) {
                player.setMyTurn(false);
            } else {
                player.setMyTurn(true);
            }
        }
    }

    public int getPlayerCount() {
        return players.length;
    }

    public GamePlayer getPlayer(int i) {
        return players[i];
    }

}

@SuppressWarnings("serial")
class GameGui extends JPanel {
    private GameModel gameModel;
    private JTextArea textArea = new JTextArea(14, 30);
    private Map<GamePlayer, JButton> playerButtonMap = new HashMap<GamePlayer, JButton>();

    public GameGui(GameModel gameModel) {
        this.gameModel = gameModel;

        JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 5, 0));
        for (int i = 0; i < gameModel.getPlayerCount(); i++) {
            GamePlayer player = gameModel.getPlayer(i);
            JButton playerButton = new JButton(player.getName());
            playerButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    playerButtonActionPerformed(e);
                }
            });
            buttonPanel.add(playerButton);
            playerButtonMap.put(player, playerButton);
        }

        textArea.setEditable(false);
        textArea.setFocusable(false);

        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        setLayout(new BorderLayout(5, 5));
        add(buttonPanel, BorderLayout.NORTH);
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(scrollPane, BorderLayout.CENTER);

        enablePlayerTurns();
    }

    private void playerButtonActionPerformed(ActionEvent e) {
        GamePlayer currentPlayer = gameModel.getCurrentPlayer();

        // TODO: have player "play"
        textArea.append("Player " + currentPlayer + " has just played\n");
        gameModel.advancePlay();
        enablePlayerTurns();

        currentPlayer = gameModel.getCurrentPlayer();
        textArea.append("It is now player " + currentPlayer + "'s turn\n");
    }

    private void enablePlayerTurns() {
        for (int i = 0; i < gameModel.getPlayerCount(); i++) {
            GamePlayer player = gameModel.getPlayer(i);
            JButton playerButton = playerButtonMap.get(player);
            playerButton.setEnabled(player.isMyTurn());
            if (player.isMyTurn()) {
                playerButton.requestFocusInWindow();
            }
        }
    }

}
白首有我共你 2024-10-22 13:25:14

执行操作时:

if (whatever.getTurn() != thisPlayer) 
  error
else
  run normally

非常简单。

When the action is performed:

if (whatever.getTurn() != thisPlayer) 
  error
else
  run normally

Pretty straightforward.

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