Java Algorithm for Determining Players Turns (Tic-Tac-Toe to Memory Game)

Thread Starter

insyspower

Joined May 29, 2017
34
Java:
Cliente.java (Client)
        public class Cliente extends JFrame implements ActionListener,Runnable
        {
            private Player players[] = null;
            private int numberOfPlayers;
            private int currentPlayer;
            boolean done = false;
            boolean myTurn = false;
            char myMark;
     
        public void createJPanels()
        {
             gamePnl.setLayout(new GridLayout(4, 4));
             TrataRato tr = new TrataRato();
             for (int i = 0; i < gameBtn.length; i++)
                {
                    gamePnl.add(gameBtn[I]);       
                    gameBtn[I].addMouseListener(tr);
      
        public void processaMsgVez(MsgVez msg)  throws Exception
        {
             if (msg.getEstado().equals("OK")) myTurn = true;
             else myTurn = false;
        }
      
        class TrataRato extends MouseAdapter
        {
             public void mouseReleased(MouseEvent evt)
             {
                 if (!myTurn)
                 {
                      display2.setText("Wait for your turn.\n");           
                      return;
                 }
      
    Player.java
  
    public class Player extends Thread
    {
        public Player(Socket s, Servidor t, int num )
        {
            connection = s;
            control = t;
            number = num;
            mark = ( num == 0 ? 'X' : 'O' );
      
        public void processaMsgPos(MsgPos msg) throws Exception
        {
            control.display( msg.toString() );
            int location = msg.getPos();
      
            if( control.validMove( location, number ) )
            {
                 MsgPos ans = new MsgPos(mark, location, "OK");
                 ans.envia(output);
                 System.out.println("processaMsgPos: "+ans);
                 if (control.getWinner() != ' ') control.setRes();
            }
            else
            {
                 MsgPos ans = new MsgPos(mark, location, "NOK");
                 System.out.println("processaMsgPos: "+ans.toString());
                 msg.envia(output);
                 System.out.println("processaMsgPos: "+ans);
            }
        } 
      
    Servidor.java (Server)
        private int numberOfPlayers;
        private int currentPlayer;
      
        public class Servidor extends JFrame
        {
            public Servidor ()
            {
                super( "Tic-Tac-Toe Server" );
               Container cont = getContentPane();
                board = new byte[9];
                players = new Player[2];
                currentPlayer = 0;
      
            public void setTurn()
            {
                try
                {
                    players[currentPlayer].setTurn("OK");
                    players[(currentPlayer == 0 ? 1 : 0 )].setTurn("NOK");
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
      
            public boolean validMove( int loc, int player )
            {
                if( player != currentPlayer ) return false;
      
                if ( !isOccupied( loc ) )
                {
                    board[loc] = (byte)(currentPlayer == 0 ? 'X': 'O' );
                    currentPlayer = ++currentPlayer % 2;
                    players[ currentPlayer ].otherPlayerMoved( loc );
Just copy paste some instances of the code (Tic-Tac-Toe Game).
My problem is this game as 2 players, each player plays 1 turn each time, then after being the other player turn. But my Game is Memory Game (Matching), each player plays 2 turns, if right, continues to play, is not right passes turn to another player, and vice versa until one player wins.

What i have to change in this code to each player plays 2 turns?, the turns are defined with mouse pressed.

Java:
    public void setTurn()

This is my code of Memory Game.   

        public void actionPerformed(ActionEvent e)
        {
            if (myTurn)
            {
                for (int i = 0; i < gameBtn.length; i++)
                {          
                    if (gameBtn[I] == e.getSource())
                    {
                        gameBtn[I].setText("" + gameList.get(i));
                        gameBtn[I].setEnabled(false);
                        counter++;

                        if (counter == 3)
                        {              
                            if (sameValues())
                            {
                               gameBtn[btnID[0]].setEnabled(false);
                               gameBtn[btnID[1]].setEnabled(false);
                               gameBtn[btnID[0]].setVisible(true);
                               gameBtn[btnID[1]].setVisible(true);
                               Hit = Hit +1;
                               Pontos = Pontos + 25;

                               try
                               {
                                    players[currentPlayer].setTurn("OK");
                                    //players[(currentPlayer == 0 ? 1 : 0 )].setTurn("NOK");
                               }
                               catch (Exception y)
                               {
                                    y.printStackTrace();
                               }
                               //currentPlayer = 0;                         
                               myTurn = true;
                           }

                           else
                           {
                               gameBtn[btnID[0]].setEnabled(true);
                               gameBtn[btnID[0]].setText("");
                               gameBtn[btnID[1]].setEnabled(true);
                               gameBtn[btnID[1]].setText("");
                               Miss = Miss +1;
                               Pontos = Pontos - 5;  
                          
                               try
                               {
                                    //players[currentPlayer].setTurn("OK");
                                    players[(currentPlayer == 0 ? 1 : 0 )].setTurn("NOK");
                               }
                               catch (Exception y)
                               {
                                    y.printStackTrace();
                               }
                               myTurn = false;
                               currentPlayer = 1;
                           }
                           counter = 1;
                       }
                  
                       if (counter == 1) // on grid identifies 1º click of mouse
                       {
                           btnID[0] = i;
                           btnValue[0] = gameList.get(i);
                       }
                       if (counter == 2) // on grid identifies 2º click of mouse
                       {
                           btnID[1] = i;
                           btnValue[1] = gameList.get(i);
                       }
                   }
               }
Moderators note : please use code tags for pieces of code
 
Last edited by a moderator:

WBahn

Joined Mar 31, 2012
25,928
How would you do it if you were sitting off to the side and were responsible for telling the players whose turn it was?

You have to base your decision only on what you have written down in front of you and what the outcome of the most recent move was.

What pieces of information would you need to know in order to make your decision?
 
Top