import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.Socket;
import javax.swing.border.*;
public class client extends Applet implements ActionListener, Runnable
{
private Cell[][] cells = new Cell[20][20];
private JButton login = new JButton("登录");
private JLabel p_ip = new JLabel("IP");
private JLabel p_name = new JLabel("name");
private JTextField t_ip = new JTextField(10);
private JTextField t_name = new JTextField(8);
private static final int PORT = 1244;
String ip;
protected DataOutputStream out;
protected DataInputStream in;
static char name;
String line;
Thread thread;Socket socket;
private class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
public void init()
{
JPanel p = new JPanel();
JPanel s = new JPanel();
p.setLayout(new GridLayout(20,20));
for(int i = 0;i<20; i ++)
for(int j = 0; j<20 ; j++)
p.add(cells[i][j] = new Cell(i,j));
setLayout(new BorderLayout());
p.setBorder(new LineBorder(Color.red,1));
add("Center",p);
s.setLayout(new FlowLayout());
s.add(p_ip);
s.add(t_ip);
s.add(p_name);
s.add(t_name);
s.add(login);login.addActionListener(this);
add("South",s);
setSize(600,600);
show();
}
public void start()
{
ip=t_ip.getText();
try
{
socket = new Socket(ip,PORT);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e)
{
}
if(thread == null)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public void stop()
{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
if(thread != null&&thread.isAlive())
{
thread.yield();
thread = null;
}
}
public void run() {
try
{
while(true)
{
line = in.readLine();
int x_in=0,y_in=0;
String x_i,y_y;
if(line.startsWith("yes"))
{
x_i=line.substring(2, 3);
y_y=line.substring(line.length()-1,line.length());
if(x_i.endsWith("a"))
{
x_i=x_i.substring(0,x_i.length()-1);
}
if(y_y.startsWith("a"))
{
y_y = y_y.substring(1,y_y.length());
}
x_in = Integer.parseInt(x_i);
y_in = Integer.parseInt(y_y);
//yes x_index a y_index;提取出来
cells[x_in][y_in].settoKen(name);
}
}
}
catch(IOException e)
{
}
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==login)
{
try {
char[] msgChars = (t_name.getText()).toCharArray();
name = msgChars[0];
//需要把字符串变成char型
out.writeUTF("come"+name);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
class Cell extends JPanel
{
private char toKen = ' ';
private int x_index,y_index;
public Cell(int a,int b)
{
setBorder(new LineBorder(Color.gray,1));
addMouseListener(new MouseListener());
x_index = a;
y_index =b;
}
public char gettoKen()
{
return toKen;
}
public void settoKen(char c)
{
toKen = c;
repaint();
}
private class MouseListener extends MouseAdapter
{
public void mouseClicked(MouseEvent e)
{
try {
if(iswon(name))
{
//over
}
else
{
out.writeUTF(name+x_index+"ahea"+y_index);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public boolean iswon(char toKen)
{
//横
for(int i =0;i<20;i++)
for(int j = 0;j<16;j++)
if(cells[i][j].gettoKen() == toKen
&&cells[i][j+1].gettoKen() == toKen
&&cells[i][j+2].gettoKen() == toKen
&&cells[i][j+3].gettoKen() == toKen
&&cells[i][j+4].gettoKen() == toKen)
{
return true;
}
//竖
for(int i =0;i<16;i++)
for(int j = 0;j<20;j++)
if(cells[i][j].gettoKen() == toKen
&&cells[i+1][j].gettoKen() == toKen
&&cells[i+2][j].gettoKen() == toKen
&&cells[i+3][j].gettoKen() == toKen
&&cells[i+4][j].gettoKen() == toKen)
{
return true;
}
//左斜
for(int i =0;i<16;i++)
for(int j = 4;j<20;j++)
if(cells[i][j].gettoKen() == toKen
&&cells[i+1][j-1].gettoKen() == toKen
&&cells[i+2][j-2].gettoKen() == toKen
&&cells[i+3][j-3].gettoKen() == toKen
&&cells[i+4][j-4].gettoKen() == toKen)
{
return true;
}
//右斜
for(int i =0;i<16;i++)
for(int j = 0;j<16;j++)
if(cells[i][j].gettoKen() == toKen
&&cells[i+1][j+1].gettoKen() == toKen
&&cells[i+2][j+2].gettoKen() == toKen
&&cells[i+3][j+3].gettoKen() == toKen
&&cells[i+4][j+4].gettoKen() == toKen)
{
return true;
}
return false;
}
}
}
wuziqi.zip_Java代码五子棋
版权申诉
82 浏览量
2022-09-19
19:15:39
上传
评论
收藏 20KB ZIP 举报
御道御小黑
- 粉丝: 61
- 资源: 1万+