十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
java机器人游戏最经典的是Robocode项目,里面有详细的文档以及源码,可以自己学习修改。(不要说自己参加这个项目,自己不认真学些东西,那可以面壁了)。下面是它的项目主页
我们提供的服务有:做网站、网站制作、微信公众号开发、网站优化、网站认证、渠县ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的渠县网站制作公司
使用System.out是不能无限刷屏的,否则会导致用户无法输入。
输入几个字屏幕就被换行,肯定玩玩。
不过如果是画面形式,当然就无所谓了。
自己纯手打,老半天才弄出来啊
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Demo2 extends JFrame {
private JLabel lb1, lb2, lb3, lb4; // 提示标签
private JTextField ta1, ta2;// 两个文本框
private JButton b1, b2, b3; // 三个按钮
private JPanel p1, p2; // 两个JPanel面板
public Demo2() {
// 初始化所有组件
lb1 = new JLabel("欢迎使用人机猜拳程序");
lb2 = new JLabel("你出拳: ");
lb3 = new JLabel("电脑出拳:");
lb4 = new JLabel("结果");
ta1 = new JTextField();
ta1.setPreferredSize(new Dimension(60, 60)); // 设置大小
ta1.setEditable(false);//设置不可编辑
ta2 = new JTextField();
ta2.setPreferredSize(new Dimension(60, 60));
ta2.setEditable(false);//设置不可编辑
b1 = new JButton("剪刀");
b2 = new JButton("石头");
b3 = new JButton("布");
p1 = new JPanel();
p2 = new JPanel();
// 设置第一个面板内容
Box box = Box.createVerticalBox();
Box box1 = Box.createHorizontalBox();
box1.add(lb2);
box1.add(ta1);
box1.add(lb3);
box1.add(ta2);
box.add(lb1);
box.add(Box.createVerticalStrut(40));
box.add(box1);
box.add(Box.createVerticalStrut(10));
box.add(lb4);
box.add(new JLabel());
p1.add(box);
// 设置第二个面板
p2.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器
p2.setPreferredSize(new Dimension(0, 60));
GridBagConstraints g2 = new GridBagConstraints();
g2.fill = GridBagConstraints.BOTH;
g2.weightx = 1.0;
g2.weighty = 1.0;
g2.gridx = 0;
g2.gridy = 0;
p2.add(b1, g2);
g2.gridx = 1;
p2.add(b2, g2);
g2.gridx = 2;
p2.add(b3, g2);
//为3个按钮添加事件
b1.addActionListener(new buttonAction());
b2.addActionListener(new buttonAction());
b3.addActionListener(new buttonAction());
this.getContentPane().add(p1);
this.getContentPane().add(p2, BorderLayout.SOUTH);
this.setTitle("机器人猜拳游戏");
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//事件类
class buttonAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
ta1.setText("剪刀");
init(ta1.getText());
}else if(e.getSource()==b2){
ta1.setText("石头");
init(ta1.getText());
}else if(e.getSource()==b3){
ta1.setText("布");
init(ta1.getText());
}
}
// 模拟电脑出拳,产生三个随机数。0代表剪刀,1代表石头,2代表布
public String getQuan(){
String str="";
int num=new Random().nextInt(3) ;
if(num==0){
str="剪刀";
}else if(num==1){
str="石头";
}else if(num==2){
str="布";
}
return str;
}
// 判断输赢方法
public String isying(String s1,String s2){
String s="";
if(s1.equals(s2)){
s="平局";
}else if(s1.equals("剪刀")s2.equals("布")){
s="你赢";
}else if(s1.equals("石头")s2.equals("剪刀")){
s="你赢";
}else if(s1.equals("布")s2.equals("石头")){
s="你赢";
}else{
s="电脑赢";
}
return s;
}
public void init(String wo){
String sy=""; // 保存输赢结果
String dncq=getQuan(); //电脑出拳
if(wo.equals(dncq)){
sy="平局";
}else if(wo.equals("剪刀")dncq.equals("布")){
sy="你赢";
}else if(wo.equals("石头")dncq.equals("剪刀")){
sy="你赢";
}else if(wo.equals("布")dncq.equals("石头")){
sy="你赢";
}else{
sy="电脑赢";
}
ta2.setText(dncq);// 电脑出拳
lb4.setText("结果:"+sy);
}
}
public static void main(String[] args) {
new Demo2();
}
}
Server.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class Server {
private ListServerThread serverThreads;
private ServerSocket serverSocket;
public Server() throws IOException {
serverSocket = new ServerSocket(8025);
serverThreads = new ArrayListServerThread();
}
public ListServerThread getServerThreads() {
return serverThreads;
}
public void setServerThreads(ListServerThread serverThreads) {
this.serverThreads = serverThreads;
}
public ServerSocket getServerSocket() {
return serverSocket;
}
public void setServerSocket(ServerSocket serverSocket) {
this.serverSocket = serverSocket;
}
public static void main(String[] args) {
Server server = null;
try {
server = new Server();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
try {
Socket socket;
Thread t;
while (true) {
socket = server.getServerSocket().accept();
ServerThread serverThread = new ServerThread(server, socket);
server.getServerThreads().add(serverThread);
t = new Thread(serverThread);
t.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String message, Socket socket) {
for (ServerThread serverThread : serverThreads) {
try {
serverThread.getDos().writeUTF(message);
} catch (IOException e) {
// e.printStackTrace();
System.out.println("推送消息给" + serverThread.getSocket().getRemoteSocketAddress() + "过程中发生了错误.");
}
}
}
}
class ServerThread implements Runnable {
private Server server;
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
public ServerThread(Server server, Socket socket) throws IOException {
this.server = server;
this.socket = socket;
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public DataInputStream getDis() {
return dis;
}
public void setDis(DataInputStream dis) {
this.dis = dis;
}
public DataOutputStream getDos() {
return dos;
}
public void setDos(DataOutputStream dos) {
this.dos = dos;
}
public void run() {
String message = "";
while (!message.equals("logout")) {
try {
message = socket.getRemoteSocketAddress() + " say: " + dis.readUTF();
// System.out.println(message);
server.sendMessage(message, socket);
} catch (IOException e) {
//e.printStackTrace();
break;
}
}
server.getServerThreads().remove(this);
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Client {
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;
public Client() throws UnknownHostException, IOException {
socket = new Socket("127.0.0.1", 8025);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
new Thread(new ClientThread(this)).start();
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public DataOutputStream getDos() {
return dos;
}
public void setDos(DataOutputStream dos) {
this.dos = dos;
}
public DataInputStream getDis() {
return dis;
}
public void setDis(DataInputStream dis) {
this.dis = dis;
}
public void close() {
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessage(String message) {
try {
dos.writeUTF(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Client client = null;
try {
client = new Client();
System.out.println(client.getSocket().getLocalPort());
} catch (IOException e) {
System.out.println("BUG");
e.printStackTrace();
System.exit(-1);
}
String message = "";
Scanner scanner = new Scanner(System.in);
while (!message.equals("logout")) {
message = scanner.nextLine();
// System.out.println(message);
client.sendMessage(message);
}
scanner.close();
client.close();
}
}
class ClientThread implements Runnable {
private Client client;
public ClientThread(Client client) {
super();
this.client = client;
}
@Override
public void run() {
try {
while (true) {
System.out.println(client.getDis().readUTF());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}