2017-06-17 121 views
0

这个记忆游戏作为2个玩家/客户,其中一个是'X',另一个是'O'。我的游戏使用4×4网格,在成对(1到8)上填充随机数,当第一个玩家已经开始使用随机数填充网格时,但是当我开始第二个玩家时,网格填充了不同的数字,我想随机两个球员网格中的数字相等。记忆游戏两个玩家Java客户端服务器

是否必须更改函数setArrayListText()上的代码,或者添加/修改某些代码实例?

Cliente.java(客户端)

public class Cliente extends JFrame implements ActionListener,Runnable 
{ 
    ...  
    private JPanel boardPanel, panel2; 
    private JButton[] gameBtn = new JButton[16]; 
    private JPanel gamePnl = new JPanel(); 
    ... 
    public void start2() 
    { 
     try 
     { 
      connection = new Socket(InetAddress.getByName("localhost"), 5000); 
      output = new ObjectOutputStream(connection.getOutputStream()); 
      input = new ObjectInputStream(connection.getInputStream()); 

      inputThread = new Thread(this); 
      inputThread.start(); 

      MsgIdt msg = new MsgIdt(); 
      msg.envia(output); 
      System.out.println(msg); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
    ... 
    public void createGUI() 
    { 
     for (int i = 0; i < gameBtn.length; i++) 
     { 
      gameBtn[i] = new JButton(ButtonIcon); 
      gameBtn[i].addActionListener(this); 
     } 
    ... 

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

    // You will need to fit this properly in your client class 
    public List<Integer> listReciever() throws IOException 
    { 
     List<Integer> lista = new ArrayList<>(); 
     int size = input.readInt(); 
     for(int i = 0; i < size; i++) 
     { 
      lista.add(input.readInt()); 
     } 
     return lista; 
    } 

Servidor.java(服务器)

public class Servidor extends JFrame 
    { 
     private ArrayList<Integer> gameList = new ArrayList<Integer>(); 

     public Servidor() 
     { 
      super("Tic-Tac-Toe Server"); 
      Container cont = getContentPane(); 
      board = new byte[9]; 
      players = new Player[2]; 
      currentPlayer = 0; 
      try 
      { 
       server = new ServerSocket(5000, 2); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       System.exit(1); 
      } 
      output = new TextArea(10, 30); 
      output.setEditable(false); 
      JScrollPane sp = new JScrollPane(output); 
      cont.add("Center", sp); 
      display("Tic-Tac-Toe Server running."); 
      addWindowListener(new ProcessaJanela()); 
      setVisible(true); 
      pack(); 
     } 

     public void execute() 
     { 
      for (int i = 0 ; i < players.length; i++) 
      { 
       try 
       { 
        players[i] = new Player(server.accept(), this, i); 
        players[i].start(); 
        ++numberOfPlayers; 
       } 
       catch (IOException e) 
       { 
        e.printStackTrace(); 
        System.exit(1); 
       } 
      } 
      setTurn(); 
     } 

     public int getNumberOfPlayers() 
     { 
      return numberOfPlayers; 
     } 

     public int getCurrentPlayer() 
     { 
      return currentPlayer; 
     } 

     public int getWinner() 
     { 
      return winner; 
     } 

     public boolean getGameOver() 
     { 
      return gameOver; 
     } 

     public void setArrayListText() // generates random numbers 
     { 
      java.util.List<Integer> lista = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8); 
      Collections.shuffle(lista); 
      gameList.addAll(lista); 
     }  
     public void gridclients() throws IOException 
     { 
      players[0].sendList(gameList); 
      players[1].sendList(gameList); 
     } 

     public void setTurn() 
     { 
      try 
      {   
       players[currentPlayer].setTurn("OK"); 
       players[(currentPlayer == 0 ? 1 : 0)].setTurn("NOK"); 

      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
     } 
     ... 
    } 
    ... 

Player.java

public class Player extends Thread 
{ 
    private JButton[] gameBtn = new JButton[16]; 
    Servidor control; 
    Socket connection; 
    ObjectInputStream input; //por no protocolo as msgs serem da classe Object 
    ObjectOutputStream output; 
    int number; 
    char mark; 

    public Player(Socket s, Servidor t, int num) 
    { 
     connection = s; 
     control = t; 
     number = num; 
     mark = (num == 0 ? 'X' : 'O'); 
     try 
     { 
      input = new ObjectInputStream(connection.getInputStream()); 
      output = new ObjectOutputStream(connection.getOutputStream()); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
      System.exit(1); 
     } 
    } 
    public void run() 
    { 
     try 
     { 
      while(!control.getGameOver()) 
      { 
       processaMsg(Protocolo.recebe(input)); 
      } 
      connection.close(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
      System.exit(1); 
     } 
    } 

    public void sendList(List<Integer> gameList) throws IOException 
    { 
     output.writeInt(gameList.size()); 
     for(int i = 0; i < gameList.size(); i++) 
     { 
      output.writeInt(gameList.get(i)); 
     } 
    } 
+0

如果您希望在两个客户端中使用相同的随机序列,则必须在客户端代码之外生成该序列,然后将其传递到两个客户端。无论是在客户端的构造函数还是在一些类似setter的方法中。基本上,setArrayListText()代码必须在客户端之外执行 – Ivanko

+0

好了setArrayListText()代码现在在服务器中。但我如何将它发送给两位球员?网格都是空的。 – insyspower

回答

0

你的问题是,你在客户端洗牌,当你有两个客户运行时,他们彼此分开运行,洗牌会给你不同的结果,所以你应该服务器上的转移洗牌逻辑,服务器将生成网格,然后发送给两个玩家。

我不喜欢使用ObjectOutputStream,特别是在发送更复杂的对象时,该方法容易出现大的错误和失败,它总是更好地使用原始数据,并使用DataOutputStream/DataInputStream发送它。

假设服务器端的客户端的处理程序是球员类,因为您要发送的Socket有

服务器发送列表的方法应该是里面Player类:

public void sendList(List<Integer> gameList) throws IOException { 
    output.writeInt(gameList.size()); 
    for(int i = 0; i < gameList.size(); i++){ 
     output.writeInt(gameList.get(i)); 
    } 
} 

然后你会打电话给sendList两个Servidor class这样的球员:

player[0].sendList(gameList); 
player[1].sendList(gameList); 

客户端收到:

// You will need to fit this properly in your client class 
public List<Integer> listReciever(){ 
List<Integer> list = new ArrayList<>(); 
int size = input.readInt(); 
for(int i = 0; i < size; i++){ 
     list.add(input.readInt()); 
} 
+0

确定setArrayListText()代码现在在服务器中。但我如何将它发送给两位球员?网格都是空的。 – insyspower

+0

您能否提供您用于客户端和服务器之间通信的代码,没有代码很难给出正确的答案,因为有多种方式。 – FilipRistic

+0

还有一点建议,如果你是客户端服务器的新手,那么最好从简单的事情开始,比如从客户端向服务器发送2个数字,然后将总和从服务器发送回客户端。一旦你掌握了,你可以慢慢添加新的东西。 – FilipRistic