0

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;
                /*...
                  ...
                  ...*/
        }   
    }
}

Edited by rowen_1

2
Contributors
1
Reply
10
Views
3 Years
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.