Question

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
Was it helpful?

Solution

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

}

OTHER TIPS

When the action is performed:

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

Pretty straightforward.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top