Home > Net >  How can I implement a range of player turns? I.E 3-10 players in Java
How can I implement a range of player turns? I.E 3-10 players in Java

Time:06-07

I am creating a tic tac toe game but in this case, it is more than two players. I am completely unsure how to go about this and had many iterations. It is done via JButtons gridlayout. For normal tictactoe, it works just fine:

buttons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    for(int i=0;i<9;i  ) {
                        if(e.getSource()==buttons[i]) {
                            if(pTurn) {
                                if(buttons[i].getText()=="") {
                                    buttons[i].setForeground(new Color(255,0,0));
                                    buttons[i].setText("X");
                                    pTurn=false;
                                    txt.setText("O turn");
                                    checkCondition();
                                }
                            }
                            else {
                                if(buttons[i].getText()=="") {
                                    buttons[i].setForeground(new Color(0,0,255));
                                    buttons[i].setText("O");
                                    pTurn=true;
                                    txt.setText("X turn");
                                    checkCondition();
                                }
                            }
                        }           
                    }
                }
            });

However, my implementation of 3 players is not working. My logic is skewed but I am unsure where.

           buttons[i].addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    for(int i=0;i<size;i  ) {
                        if(e.getSource()==buttons[i]) {
                            for (int k = 0; k < col_row; k   ) {
                                for (int j = k 1; j < col_row; j  ) {
                                    if (players[k]) {
                                        if(buttons[i].getText()=="") {
                                            buttons[i].setForeground(new Color(0,0,255));
                                            buttons[i].setText(pIcon[k]);
                                            players[k]=false;
                                            txt.setText(pIcon[j]   " turn");
                                            checkCondition();
                                        }
                                    }
                                    else {
                                        if(buttons[i].getText()=="") {
                                            buttons[i].setForeground(new Color(0,0,255));
                                            buttons[i].setText(pIcon[j]);
                                            players[k]=true;
                                            txt.setText(pIcon[j 1]   " turn");
                                            checkCondition();
                                        }
                                    }
                                }
                                    


                            }

                        }           
                    }
                }
            });

In this case, col_row is just the number of players. Players[] is an array of boolean players. My logic is that its checked to see if they have went but it doesn't work really. pIcon[] is an array of player characters (X,O,A,B,C...). I am not really sure how to fix this.

enter image description here

CodePudding user response:

Two player logic (as implemented)

  • mark first active player
  • on click iterate over all fields until clicked field is identified
  • if it is player A's turn do player A turn else do player B turn

n player logic (according to your boolean array idea)

  • mark first active player in boolean array
  • on click iterate over all fields until clicked field is identified
  • iterate over all players until active player k is found then do player k turn and mark next active player j = (k 1) modulo number of players
buttons[i].addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        for(int i=0;i<size;i  ) {
            if(e.getSource()==buttons[i]) {
                // iterate over players to find next active player
                for (int k = 0; k < 3; k   ) { // 3 players
                        if (players[k]) {
                            // next active player found
                            if(buttons[i].getText()=="") {
                                buttons[i].setForeground(new Color(0,0,255));
                                buttons[i].setText(pIcon[k]);
                                players[k]=false;
                                int j = (k   1) % 3; // index for next active player
                                players[j] = true; // mark next active player
                                txt.setText(pIcon[j]   " turn");
                                checkCondition();
                                break; // leave inner loop
                            }
                        }
                    }
                }
            }           
        }
    }
});
  • Related