“开发一个三子棋游戏,分为服务器端和客户端。服务器端支持互联网上任意两个客户端玩三子棋游戏。在三子棋游戏中,两个游戏者在3*3网格的有效单元格中轮流作标记(一个人用X,另一个人用O)。如果一个游戏者在网格的水平、垂直或对角线方向上作了三个连续标记,游戏就以这个游戏者得胜而告终。当网络的所有单元格都标满了标记而没有产生优胜者,就出现了平局。
这需要开发一个多用户服务器,服务器创建一个服务器套接字,并接受每两个游戏者一组的连接请求,构成一个会话。每个会话都是一个线程,管理两个游戏者通信并且判断游戏状态。服务器可以建立任意多的会话。”游戏的代码我写出来了,就是套接字不会弄。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;public class Client extends JFrame implements ActionListener
{
/*Socket mysocket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
try
{
in=new DataInputStream(mysovket.getInputStream());
out=new DataOutputStream(mysovket.getOutputStream());
}*/
JFrame frame=new JFrame("CLient");// 框架
JLabel lbl,lb2;//标签
JButton [] btn = new JButton[9];//按钮
JButton btn_start;
JPanel jp1;//面板
int count = 0;
ImageIcon icon1,icon2;//图像
public Client() //设置标签,按钮
{
//mysovket=new Socket();
//thread=new Thread(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,400);
frame.setLayout(new BorderLayout());
lbl = new JLabel("Player 1 first");
frame.add(lbl,BorderLayout.NORTH);
lb2=new JLabel();
frame.add(lb2,BorderLayout.SOUTH);//////////////////////////
jp1 = new JPanel();
jp1.setLayout(new GridLayout(3,3));
for (int i = 0; i<9; i++) //增加按钮并设监听
{
btn[i] = new JButton();
jp1.add(btn[i]);
btn[i].addActionListener(this);
}
frame.add(jp1,BorderLayout.CENTER); this.setResizable(false);
icon1 = new ImageIcon("001.jpg");
icon2 = new ImageIcon("002.jpg"); }
public void actionPerformed(ActionEvent e)
{
count++;
if (count%2 == 1)
{
lbl.setText("Player 2 with token 'O'");
for (int i = 0; i<9; i++)
{
if (e.getSource() == btn[i])
{
btn[i].setIcon(icon1);
btn[i].removeActionListener(this);
this.win();
}
}
}
if (count%2 == 0)
{
lbl.setText("Player 1 with token' X'");
for (int i = 0; i<9; i++)
{
if (e.getSource() == btn[i])
{
btn[i].setIcon(icon2);
btn[i].removeActionListener(this);
this.win();
}
}
}
}
public void win() //判断输赢
{
for (int j = 0; j<9; j=j+3) {
if (btn[j].getIcon()==btn[j+1].getIcon() && btn[j+1].getIcon()==btn[j+2].getIcon())
{
if (btn[j].getIcon() == icon1) this.ply1();
else if (btn[j].getIcon() == icon2) this.ply2();
}
}
for (int j = 0; j<3; j++) {
if (btn[j].getIcon()==btn[j+3].getIcon() && btn[j+3].getIcon()==btn[j+6].getIcon())
{
if (btn[j].getIcon() == icon1) this.ply1();
else if (btn[j].getIcon() == icon2) this.ply2();
}
} if (btn[0].getIcon()==btn[4].getIcon() && btn[4].getIcon()==btn[8].getIcon())
{
if (btn[0].getIcon() == icon1) this.ply1();
else if (btn[0].getIcon() == icon2) this.ply2();
}
if (btn[2].getIcon()==btn[4].getIcon() && btn[4].getIcon()==btn[6].getIcon())
{
if (btn[2].getIcon() == icon1) this.ply1();
else if (btn[2].getIcon() == icon2) this.ply2();
}
}
public void ply1() //player 1 win
{
lbl.setText("Game Over!!!");
lb2.setText("Player 1 win!!!");
for (int i = 0; i<9; i++)
btn[i].removeActionListener(this);
}
public void ply2() //player 2 win
{
lbl.setText("Game is Over!");
lb2.setText("Player 2 win!!!");
for (int i = 0; i<9; i++)
btn[i].removeActionListener(this);
}
public static void main(String[]args)
{
Client client = new Client();
}
}
这需要开发一个多用户服务器,服务器创建一个服务器套接字,并接受每两个游戏者一组的连接请求,构成一个会话。每个会话都是一个线程,管理两个游戏者通信并且判断游戏状态。服务器可以建立任意多的会话。”游戏的代码我写出来了,就是套接字不会弄。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;public class Client extends JFrame implements ActionListener
{
/*Socket mysocket=null;
DataInputStream in=null;
DataOutputStream out=null;
Thread thread;
try
{
in=new DataInputStream(mysovket.getInputStream());
out=new DataOutputStream(mysovket.getOutputStream());
}*/
JFrame frame=new JFrame("CLient");// 框架
JLabel lbl,lb2;//标签
JButton [] btn = new JButton[9];//按钮
JButton btn_start;
JPanel jp1;//面板
int count = 0;
ImageIcon icon1,icon2;//图像
public Client() //设置标签,按钮
{
//mysovket=new Socket();
//thread=new Thread(this);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400,400);
frame.setLayout(new BorderLayout());
lbl = new JLabel("Player 1 first");
frame.add(lbl,BorderLayout.NORTH);
lb2=new JLabel();
frame.add(lb2,BorderLayout.SOUTH);//////////////////////////
jp1 = new JPanel();
jp1.setLayout(new GridLayout(3,3));
for (int i = 0; i<9; i++) //增加按钮并设监听
{
btn[i] = new JButton();
jp1.add(btn[i]);
btn[i].addActionListener(this);
}
frame.add(jp1,BorderLayout.CENTER); this.setResizable(false);
icon1 = new ImageIcon("001.jpg");
icon2 = new ImageIcon("002.jpg"); }
public void actionPerformed(ActionEvent e)
{
count++;
if (count%2 == 1)
{
lbl.setText("Player 2 with token 'O'");
for (int i = 0; i<9; i++)
{
if (e.getSource() == btn[i])
{
btn[i].setIcon(icon1);
btn[i].removeActionListener(this);
this.win();
}
}
}
if (count%2 == 0)
{
lbl.setText("Player 1 with token' X'");
for (int i = 0; i<9; i++)
{
if (e.getSource() == btn[i])
{
btn[i].setIcon(icon2);
btn[i].removeActionListener(this);
this.win();
}
}
}
}
public void win() //判断输赢
{
for (int j = 0; j<9; j=j+3) {
if (btn[j].getIcon()==btn[j+1].getIcon() && btn[j+1].getIcon()==btn[j+2].getIcon())
{
if (btn[j].getIcon() == icon1) this.ply1();
else if (btn[j].getIcon() == icon2) this.ply2();
}
}
for (int j = 0; j<3; j++) {
if (btn[j].getIcon()==btn[j+3].getIcon() && btn[j+3].getIcon()==btn[j+6].getIcon())
{
if (btn[j].getIcon() == icon1) this.ply1();
else if (btn[j].getIcon() == icon2) this.ply2();
}
} if (btn[0].getIcon()==btn[4].getIcon() && btn[4].getIcon()==btn[8].getIcon())
{
if (btn[0].getIcon() == icon1) this.ply1();
else if (btn[0].getIcon() == icon2) this.ply2();
}
if (btn[2].getIcon()==btn[4].getIcon() && btn[4].getIcon()==btn[6].getIcon())
{
if (btn[2].getIcon() == icon1) this.ply1();
else if (btn[2].getIcon() == icon2) this.ply2();
}
}
public void ply1() //player 1 win
{
lbl.setText("Game Over!!!");
lb2.setText("Player 1 win!!!");
for (int i = 0; i<9; i++)
btn[i].removeActionListener(this);
}
public void ply2() //player 2 win
{
lbl.setText("Game is Over!");
lb2.setText("Player 2 win!!!");
for (int i = 0; i<9; i++)
btn[i].removeActionListener(this);
}
public static void main(String[]args)
{
Client client = new Client();
}
}
InetSocketAddress socketAddress = new InetSocketAddress(address,4331);
socket.connect(socketAddress);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
connect.setEnabled(false);
I am not sure,it's helpful for U,take a look.
发送消息的话你需要自定义协议,比如发 " @!@!x=1,y=3@!@!" ,这个'x=1,y=3'表示是对方下的位置...
不管客户端还是服务器端读到这个消息,则在1,3位置上下一个棋
客户端怎样连接服务器,课本上的例子是用一个按钮,我看老师给我们演示的是开一个服务器,俩个客户端,然后就开始游戏了,这要怎么弄》??假如上面的解决了,客户一下棋,然后他的界面直接显示下的位置么?还是给服务器消息,服务器再给他们两客户端消息,然后界面再显示棋子。游戏的实现的代码只在客户端程序里么??服务器应该不用吧,服务器就管接受一个客户端的消息,再返回给两个客户端??