TCP客户端 - 服务器线程问题Blackjack游戏 [英] TCP client-server thread issue Blackjack game

查看:73
本文介绍了TCP客户端 - 服务器线程问题Blackjack游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在开发一个TCP客户端 - 服务器二十一点游戏,我在理解线程如何在这里工作时遇到了一些麻烦:



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();
    }

}





基本上我需要创建第一个玩家并问他/她有多少其他玩家将参与该游戏。理想情况下,我会接受他的回复并在for循环中创建更多客户端。这不起作用,似乎线程被绕过(因为它是一个异步任务),它直接进入for循环,然后实际用户甚至可以输入一个数字。我不知道如何阻止这一点,我对线程的了解非常有限。我需要它等待获得用户数量才能继续。



非常感谢任何和所有帮助!谢谢



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

推荐答案

这篇关于TCP客户端 - 服务器线程问题Blackjack游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆