求代码,写一个石头剪子布的游戏两台机联机玩的,一个作为客户端一个作为服务端,用线程写,要有界面能运行。
解决方案 »
- 生成XML关于空格的问题,急
- Servlet API包含了两个软件包,十二个接口和九个类,为什么找不到其他的
- 如何设计出这种效果的JTable?
- 急!一个关于JTable的构造问题!
- 如何生成可执行的JAVA程序
- -------------关于JAVA流文件的操作使用的一些问题--------------
- 有人用过swt吗?帮小弟个忙可以吗?
- oracle驱动程序问题
- 服务器是SUN机器,Solarise8操作系统,要下载JDK1.4,可是SUN网站上提供了六中用于Solarise的JDK包,用那种呀?
- 谁有jbuilder5的注册机
- Java一个整形的数组,写一个方法,实现 最大,最小,次大,次小 ……输出
- 想编写一个java程序,实现多线程批量扫描网络文件的大小、存在与否
{
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();
}
}
});
}
}