This is a contiunation of https://www.daniweb.com/software-development/java/threads/484036/java-network-game-keeping-track-of-a-player-turn#

Right now my server will read for incoming connections from the clients.
if it reaches 3, it will break off from the while loop and any more clients connected
after that will be refused. I set the first player by doing this.

game.currentThread = clientThreadList.get(0);

In my Game class I have a method nextTurn(ClientThread clientThread) which will set the player turn from player 0 to player 1.

ClientThread currentThread;

public synchronized void nextTurn(ClientThread clientThread) {
    currentThread = clientThreadList.get(1);
    System.out.println(currentThread);
}

I am not sure whether what I have done is correct, if I on the right track,
I now some more help on how to change the player's turn dynamically. instead of hard coding
currentThread = clientThreadList.get(1) in the nextTurn(ClientThread clientThread) because this
will only change the player turns from 0 to 1.

Partial of my Codes

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("serial")
public class whoseTurnTest  {

    private Socket threadSocket;
    private int id = 0;
    private List<Game.ClientThread> clientThreadList = new ArrayList<Game.ClientThread>()
    private Socket socket;

    try {
            ServerSocket sSocket = new ServerSocket(4444);
            while(true) {
                socket = sSocket.accept();
                id++;
                Game game = new Game();
                Game.ClientThread ct =  game.new ClientThread(socket,id);
                clientThreadList.add(ct);
                ct.start();
                if(id == 3) {
                   game.currentThread = clientThreadList.get(0);
                   break;
                }
            }
    } catch(IOException exception) {
            System.out.println("Error: " + exception);
    }


    class Game {
        ClientThread currentThread;

        public synchronized void nextTurn(ClientThread clientThread) {
            currentThread = clientThreadList.get(1);
            System.out.println(currentThread);
        }

        class ClientThread extends Thread {
            int clientId = -1;
            ObjectOutputStream output;  
            ObjectInputStream input;    
            ClientThread clientThread;

            public ClientThread(Socket socket,int id) {
                threadSocket = socket;
                clientId = id;
                /*...
                  ...
                  ...*/
        }   
    }
}

I don't know why you have started a new thread to continue this topic, but anyway, see my replies in part 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.