這個記憶遊戲作爲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));
}
}
如果您希望在兩個客戶端中使用相同的隨機序列,則必須在客戶端代碼之外生成該序列,然後將其傳遞到兩個客戶端。無論是在客戶端的構造函數還是在一些類似setter的方法中。基本上,setArrayListText()代碼必須在客戶端之外執行 – Ivanko
好了setArrayListText()代碼現在在服務器中。但我如何將它發送給兩位球員?網格都是空的。 – insyspower