player order in card game

+1 vote
asked Feb 2, 2011 by anvd

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

    ActionListener one = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (gr.getCounter1() < 5) {
                gr.setCounter1(gr.getCounter1() + 1);
            } else {
                pn5.setText("No more cards");

    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 Answers

+1 vote
answered Feb 2, 2011 by kaod

When the action is performed:

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

Pretty straightforward.

+1 vote
answered Feb 2, 2011 by hovercraft-full-of-e

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");

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

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

    public GamePlayer(String name) { = name;

    public String getName() {
        return name;

    public boolean isMyTurn() {
        return myTurn;

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

    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]);

    public int getPlayerTurnIndex() {
        return playerTurnIndex;

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

    public String[] getPlayerNames() {
        return playerNames;

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

    public int getPlayerCount() {
        return players.length;

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


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) {
            playerButtonMap.put(player, playerButton);


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


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

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

        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);
            if (player.isMyTurn()) {

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter