2017-06-17 177 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