纸牌游戏中的玩家订单

时间:2011-02-02 02:19:35

标签: java swing gridbaglayout playing-cards

我的球员顺序有问题。我需要第一个玩家是圆形的。所以我有四个按钮,有可能阻止不正确的玩家移动,比如“不是你的回合,玩家二是第一个,因为它是前一轮的赢家”

我怎么能控制它?最终验证方法

我有四张卡牌,每张牌手拿五张牌

对于每个按钮,设置我做的对应卡

//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

2 个答案:

答案 0 :(得分:1)

执行操作时:

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

非常简单。

答案 1 :(得分:1)

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

编辑:除了回答

不确定你是否仍然在身边,但这里有一个快速的例子,说明我的意思。 GamePlayer类(显然)代表游戏的玩家并且有一个字段,myTurn只有在该玩家的回合时才是真的,并且该字段具有关联的void setMyTurn(boolean)方法和boolean isMyTurn()方法。 / p>

GameModel类有一个名为player的GamePlayer数组,一个getPlayer(int)方法,允许其他类获取数组中的每个Player,一个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();
            }
        }
    }

}