求代码,写一个石头剪子布的游戏两台机联机玩的,一个作为客户端一个作为服务端,用线程写,要有界面能运行。

解决方案 »

  1.   

    public class Game 
    {
    public static final int ROCK = 0;
    public static final int PAPER = 1;
    public static final int SCISSORS = 2;
    private int use;

    public Game()
    {
    this.use = (int) (Math.random() * 3);
    }

    public Game(int use)
    {
    this.use = use;
    }

    /**
     * 判断输赢
     * @param game 另一个玩家
     * @return 1表示赢,0表示平,-1表示输
     */
    public int isWon(Game game)
    {
    if (use == game.use)
    return 0;
    else if (use == ROCK && game.use == SCISSORS ||
    use == SCISSORS && game.use == PAPER ||
    use == PAPER && game.use == ROCK)
    {
    return 1;
    }
    else
    return -1;
    }
    }import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;import java.io.*;
    import java.net.*;public class Client extends JFrame
    {
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private ButtonGroup group = new ButtonGroup();
    private JButton btnSend = new JButton("猜拳");
    private JPanel panel = new JPanel();
    private Game mine = null;
    private Game other = null;
    private String str;

    public Client() throws IOException
    {
    //初始化各组件
    JRadioButton radioButton = new JRadioButton("石头", true);
    radioButton.setActionCommand("石头");
    group.add(radioButton);
    panel.add(radioButton);
    radioButton = new JRadioButton("布", false);
    radioButton.setActionCommand("布");
    group.add(radioButton);
    panel.add(radioButton);
    radioButton = new JRadioButton("剪刀", false);
    radioButton.setActionCommand("剪刀");
    group.add(radioButton);
    panel.add(radioButton);

    //初始化界面
    setLayout(new FlowLayout());
    add(panel);
    add(btnSend);

    //对按钮动作的响应
    btnSend.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    str = group.getSelection().getActionCommand();
    if (str.equals("石头"))
    mine = new Game(Game.ROCK);
    else if (str.equals("布"))
    mine = new Game(Game.PAPER);
    else if (str.equals("剪刀"))
    mine = new Game(Game.SCISSORS);
    btnSend.setEnabled(false);
    }
    });

    //开启监听远程游戏线程
    new Thread(new Listener()).start();
    }

    class Listener implements Runnable
    {
    public void run()
    {
    while (true)
    {
    try
    {
    try
    {
    //先和服务端建立连接
    socket = new Socket("127.0.0.1", 9999);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
    btnSend.setEnabled(true);
    while (true)
    {
    //当客户端没有出过时,等到出过后将信息发给服务端
    while (btnSend.isEnabled())
    Thread.sleep(100);
    out.println(str);

    //读从服务端传来的数据
    str = in.readLine();
    if (str.equals("石头"))
    other = new Game(Game.ROCK);
    else if (str.equals("布"))
    other = new Game(Game.PAPER);
    else if (str.equals("剪刀"))
    other = new Game(Game.SCISSORS);
    btnSend.setEnabled(true); //判断输赢
    int t = mine.isWon(other);
    switch (t)
    {
    case 1: JOptionPane.showMessageDialog(null, "你赢了"); break;
    case 0: JOptionPane.showMessageDialog(null, "平局"); break;
    case -1: JOptionPane.showMessageDialog(null, "你输了"); break;
    }
    }
    }
    finally
    {
    out.close();
    in.close();
    socket.close();
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    }

    public static void main(String[] args)
    {
    EventQueue.invokeLater(new Runnable()
    {
    public void run()
    {
    try 
    {
    JFrame frame = new Client();
    frame.setTitle("客户端");
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    catch (IOException e) 
    {
    e.printStackTrace();
    }
    }
    });
    }
    }
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.net.*;public class Server extends JFrame
    {
    private ServerSocket server;
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private ButtonGroup group = new ButtonGroup();
    private JButton btnSend = new JButton("猜拳");
    private JPanel panel = new JPanel();
    private Game mine = null;
    private Game other = null;
    private String str;

    public Server() throws IOException
    {
    //初始化各组件
    JRadioButton radioButton = new JRadioButton("石头", true);
    radioButton.setActionCommand("石头");
    group.add(radioButton);
    panel.add(radioButton);
    radioButton = new JRadioButton("布", false);
    radioButton.setActionCommand("布");
    group.add(radioButton);
    panel.add(radioButton);
    radioButton = new JRadioButton("剪刀", false);
    radioButton.setActionCommand("剪刀");
    group.add(radioButton);
    panel.add(radioButton);
    btnSend.setEnabled(false);

    //初始化界面
    setLayout(new FlowLayout());
    add(panel);
    add(btnSend);

    //初始化ServerSocket
    server = new ServerSocket(9999);

    //对按钮动作的响应
    btnSend.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    str = group.getSelection().getActionCommand();
    if (str.equals("石头"))
    mine = new Game(Game.ROCK);
    else if (str.equals("布"))
    mine = new Game(Game.PAPER);
    else if (str.equals("剪刀"))
    mine = new Game(Game.SCISSORS);
    btnSend.setEnabled(false);
    }
    });

    //开启监听远程游戏线程
    new Thread(new Listener()).start();
    }

    class Listener implements Runnable
    {
    public void run()
    {
    while (true)
    {
    try
    {
    try
    {
    //先接受客户端
    socket = server.accept();
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()), true);
    btnSend.setEnabled(true);
    while (true)
    {
    //服务端先读客户端传来的数据
    String str = in.readLine();
    if (str.equals("石头"))
    other = new Game(Game.ROCK);
    else if (str.equals("布"))
    other = new Game(Game.PAPER);
    else if (str.equals("剪刀"))
    other = new Game(Game.SCISSORS); //服务端判断自己是否已出,出过后将所出的内容送到客户端
    while (btnSend.isEnabled())
    Thread.sleep(100);
    out.println(Server.this.str);

    //判断输赢
    int t = mine.isWon(other);
    switch (t)
    {
    case 1: JOptionPane.showMessageDialog(null, "你赢了"); break;
    case 0: JOptionPane.showMessageDialog(null, "平局"); break;
    case -1: JOptionPane.showMessageDialog(null, "你输了"); break;
    }
    btnSend.setEnabled(true);
    }

    finally
    {
    out.close();
    in.close();
    socket.close();
    btnSend.setEnabled(false);
    }
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }
    }
    }

    public static void main(String[] args)
    {
    EventQueue.invokeLater(new Runnable()
    {
    public void run()
    {
    try 
    {
    JFrame frame = new Server();
    frame.setTitle("服务端");
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    catch (IOException e) 
    {
    e.printStackTrace();
    }
    }
    });
    }
    }