Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I'm working on a TCP client-server blackjack game and I'm having a bit of trouble understanding how threads work here:

import java.net.*;
import java.util.*;
import java.io.*;

    public class Game{
    public final int MAX_NUM_PLAYERS = 2;
    public ArrayList<PlayerThread> currentPlayers;
    public int numberOfPlayers;
    public boolean firstPlayerSet;
    public Player dealer;
    public Deck gameDeck;
    public int playerCount;

    public Game(){
        //numberOfPlayers = 0;
        playerCount = 0;
        firstPlayerSet = false;
        currentPlayers = new ArrayList<PlayerThread>();
    }

    public boolean startGame(){
        try {
            ServerSocket serverSocket = new ServerSocket(6789);

            /*do{
                Socket socket = serverSocket.accept();
                PlayerThread playerThread  = new PlayerThread(socket);      
                currentPlayers.add(playerThread);
                new Thread(playerThread).start();
            }while(playerCount <= numberOfPlayers);*/


            Socket firstSoc = serverSocket.accept();
            PlayerThread playerThread = new PlayerThread(firstSoc);
            currentPlayers.add(playerThread);
            new Thread(playerThread).start();

            for(int i = 1; i < numberOfPlayers; i++){
                //if(playerCount > numberOfPlayers){
                //  break;
                //}


                System.out.println(numberOfPlayers);
                //System.out.println(playerCount);
                System.out.println("player added to game"); 
            }


            System.out.println("dasdhasdk");
            gameDeck = new Deck();
            gameDeck.shuffle();

            //deal to dealer
            Card d1 = gameDeck.dealCard();
            Card d2 = gameDeck.dealCard();
            dealer.addToHand(d1);
            dealer.addToHand(d2);
            System.out.println("after deal to dealer");

        } catch (Exception e) {
            System.out.println("Error: " + e);
        }

        return true;
    }

    private class PlayerThread implements Runnable{
        private Player player;
        private Socket playerSocket;
        private BufferedReader input;
        private PrintWriter output;
        private String playerInputData;
        private String playerOutputData;

        public PlayerThread(Socket p_playerSocket) {
            playerSocket = p_playerSocket;
            input = null;
            output = null;
            playerOutputData = "";
            playerInputData = "";
        }

        public void run(){
            try{
                input = new BufferedReader(new InputStreamReader(playerSocket.getInputStream()));
                output = new PrintWriter(playerSocket.getOutputStream(), true);
                playerCount++;
                if(firstPlayerSet == false){    
                    String output = "How many players? (1-4)";
                    send(output);

                    playerInputData = input.readLine();
                    int intPlayerInputData = Integer.parseInt(playerInputData);
                    if(intPlayerInputData >= 1 && intPlayerInputData <= 4){
                        numberOfPlayers = intPlayerInputData;
                        firstPlayerSet = true;
                    }
                }

            }catch(Exception e){
                System.out.println("Error: " + e);
            }
        }

        public void send(String p_string){
            output.println(p_string);
        }

        public Player getPlayer(){
            return player;
        }

        public void setPlayer(Player p){
            player = p;
        }

        public Socket getPlayerSocket(){
            return playerSocket;
        }

        public void setPlayerSocket(Socket s){
            playerSocket = s;
        }

    }

    public static void main(String[] args){
        Game newGame = new Game();
        newGame.startGame();
    }

}


Basically I need to create the first player and ask him/her how many other players will be participating in that game. Ideally, I would take his response and create more clients in a for loop right after. This doesn't work and it seems as though the thread is being bypassed (as it is an asynch task) and it goes straight into the for loop before an actual user could even enter a number. I have no idea how to stop this though, my knowledge with threads is very limited. I need it to wait to get the number of users before continuing.

Any and all help is greatly appreciated! Thanks
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900