100分求高手给修改一个程序 谢谢先~

duanlong 2004-11-30 10:52:22
我是java初学者
我写了一个网络对战的五子棋程序 但是就是网络数据交换时候出现了问题
不知道怎么修改了 求高手花时间给看看 最好是能给出修改过的代码 我把自己的代码给出来
见结果马上结帖 谢谢了 :)



import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.net.*;

/*<applet code=wzq height=600 width=600>
</applet>
*/

public class wzq extends JApplet implements ActionListener
{
JButton b1;
JButton b2;
JButton b3;

Panel p1;

qipan q=new qipan();

public wzq()
{

}

public void init()
{

getContentPane().setLayout(new BorderLayout());
b1=new JButton("Start");
b2=new JButton("Exit");
b3=new JButton("Last");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

p1=new Panel();

p1.add(b1);
p1.add(b2);
p1.add(b3);

getContentPane().add("North",p1);
getContentPane().add("Center",q);

}

public void paint(Graphics g)
{

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{

}

if(e.getSource()==b2)
{

}

if(e.getSource()==b3)
{

}
}
}

class qipan extends JApplet implements MouseListener //canvas
{
int maxHeight;
int maxWidth;

int x1=0;
int y1=0; // 自己下的棋子的坐标

String x2=new String();
String y2=new String(); //对方下的棋子的坐标

boolean mark[][]=new boolean [15][15];

int x[]=new int[15];
int y[]=new int[15];

int position_x=0;
int position_y=0;

boolean win=false;

BufferedReader in;
PrintWriter out;

/* ServerSocket server;
Socket s=null;*/

public qipan()
{
addMouseListener(this);

for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
mark[i][j]=false;
}
/* try
{
System.out.println("@@@@@@@@@@@@@@@2");
server=new ServerSocket(9999);
System.out.println("start:"+server);
s=server.accept();
System.out.println(s);
}catch(Exception e){System.out.println(e);}*/

}

public void init()
{

}

public void paint(Graphics g)
{
maxHeight=getHeight();
maxWidth=getWidth();
System.out.println(""+maxHeight);
System.out.println(""+maxWidth);

setBackground(Color.gray);

x[0]=50;
y[0]=50;

for(int i=1;i<15;i++)
{
x[i]=x[i-1]+35;
y[i]=y[i-1]+35;
}

for(int i=0;i<15;i++)
{
g.drawLine(x[0],y[i],x[14],y[i]);
g.drawLine(x[i],y[0],x[i],y[14]);
}
}

public void update(Graphics g)
{

g.setColor(Color.black); //客户端和服务器端设置不同的颜色
draw(position_x,position_y,g);
try{
send(x1,y1);
receive();
g.setColor(Color.white);
draw(x[Integer.parseInt(x2)],y[Integer.parseInt(y2)],g);
}catch(Exception e){System.out.println(e);}
}

public void draw(int x,int y,Graphics g)
{
g.fillArc(position_x-8,position_y-8,16,16,0,360);

isWin();
}

public void send(int x,int y)throws IOException //客户端发送消息
{

try
{
ServerSocket server=new ServerSocket(9999);
System.out.println("start:"+server);

Socket s=server.accept();
System.out.println("connecting :"+s);

out=new PrintWriter(s.getOutputStream());

String str=null;

out.println(Integer.toString(x));
out.println(Integer.toString(y));

System.out.println(""+x+y);

out.close();
server.close();
}
catch(Exception e){System.out.println("2"+e);}
finally
{

}
}

public void receive()throws IOException
{

try
{
ServerSocket server=new ServerSocket(9999);
System.out.println("start:"+server);

Socket s=server.accept();
System.out.println("connecting :"+s);

in=new BufferedReader(
new InputStreamReader(s.getInputStream()));

x2 = in.readLine();
y2 = in.readLine();

System.out.println(""+x2+y2);

in.close();
server.close();
}
catch(Exception e){System.out.println("2"+e);}
finally
{

}
}

public void isWin()
{
int num=0; //竖

for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(mark[i][j]==true;
num=num+1;
}
if(num>=5)
{
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); //赢了之后的操作
}
num=0;
}

int num1=0; //横

for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(mark[j][i]==true)
num1++;
}
if(num1>=5)
{
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); //赢了之后的操作
}
num1=0;
}

int num2=0; //左上右下 下

int m;
for(int j=0;j<11;j++)
{
m=j;
for(int i=0;i<15-j;i++,j++)
{
if(mark[i][j]==true)
System.out.println("*******************************");
num2++;
}
if(num2>=5)
{
//System.out.println("*******************************"); //赢了之后的操作
}
num2=0;
j=m;
}
}

public void mouseClicked(MouseEvent e)
{
for(int i=0;i<15;i++)
{
if(x[i]-10<e.getX()&&e.getX()<x[i]+10)
{
position_x=x[i];
x1=i;
break;
}
}


for(int i=0;i<15;i++)
{
if(y[i]-10<e.getY()&&e.getY()<y[i]+10)
{
position_y=y[i];
y1=i;
break;
}
}

mark[x1][y1]=true;

repaint();
}

public void mousePressed(MouseEvent e)
{

}

public void mouseReleased(MouseEvent e)
{

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}
}









大家复制下来看吧 我写的格式在这里不好用
...全文
145 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
Avampire 2004-12-02
  • 打赏
  • 举报
回复
package testclass;

import java.net.*;
import java.io.*;

/**
* @version 1.0
*/

public class TestServer extends Thread{
public TestServer() {
}

public void run(){
try {
ServerSocket serverSocket = new ServerSocket(5555);
while (true){
Socket client = serverSocket.accept();
if (client != null){
OutputStream out = client.getOutputStream();
InputStream in = client.getInputStream();

byte[] buffer = new byte[10000];
if (in.read(buffer) > 0) {
System.out.println("read:" + new String(buffer).trim());
}
System.out.println("got it");
String report = "got you";
out.write(report.getBytes());
// Thread.sleep(1000);

out.flush();
client.close();
System.out.println("report ok");
client.close();
Thread.sleep(50);
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
TestServer testServer = new TestServer();
testServer.start();
}

}
package testclass;

import java.net.*;
import java.io.*;
/**
* @version 1.0
*/

public class TestClient extends Thread {
public TestClient() {
}

public void run(){
try {
Socket client = new Socket("localhost", 5555);
InputStream in = client.getInputStream();
OutputStream out = client.getOutputStream();
out.write( ("hello").getBytes());
out.flush();

byte[] inputBytes = new byte[20000];
int readPos = 0;
int readNum = 0;
while((readNum=in.read(inputBytes,readPos,20000-readPos))>0){
readPos+=readNum;
}


System.out.println(new String(inputBytes).trim());
}
catch (Exception ex) {
ex.printStackTrace();
}
}

public static void main(String[] args) {
TestClient testClient = new TestClient();
testClient.start();
}

}
给你个简单的tcp通信的例子吧,希望对你有帮助
jerrykey 2004-12-01
  • 打赏
  • 举报
回复
我非常郁闷!初学者都能做五子棋程序了……
duanlong 2004-12-01
  • 打赏
  • 举报
回复
大家给我看看吧
我实在找不出来毛病了

中间实现的时候 判断输赢没有作完
还有就是 zzzhc 说的
我也感觉每次接受 发送时候新建一个socket 不怎么好
给改一下吧

没有复杂的动西 实现的特别简单 复制下来应改不会花费太多的时间的

谢谢~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
duanlong 2004-12-01
  • 打赏
  • 举报
回复
晕啊
怎么都叫长啊
这才是一个服务器端的程序啊
还有客户端的呢
写个这样的程序不长也不行啊
看了别人的都写了好几百K呢
我这还不是很多吧 几k


下面的是客户端的程序 别闲长啊 昨天晚上突然停电没有贴上去


import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.net.*;

/*<applet code=wzqClient height=600 width=600>
</applet>
*/

public class wzqClient extends JApplet implements ActionListener
{
JButton b1;
JButton b2;
JButton b3;

Panel p1;

qipan q=new qipan();

public wzqClient()
{

}

public void init()
{

getContentPane().setLayout(new BorderLayout());
b1=new JButton("Start");
b2=new JButton("Exit");
b3=new JButton("Last");

b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);

p1=new Panel();

p1.add(b1);
p1.add(b2);
p1.add(b3);

getContentPane().add("North",p1);
getContentPane().add("Center",q);

}

public void paint(Graphics g)
{

}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{

}

if(e.getSource()==b2)
{

}

if(e.getSource()==b3)
{

}
}
}

class qipan extends JApplet implements MouseListener //canvas
{
int maxHeight;
int maxWidth;

int x1=0;
int y1=0; // 自己下的棋子的坐标

String x2=new String();
String y2=new String(); //对方下的棋子的坐标

boolean mark[][]=new boolean [15][15];

int x[]=new int[15];
int y[]=new int[15];

int position_x=0;
int position_y=0;

boolean win=false;

BufferedReader in;
PrintWriter out;

// Socket client=null;

public qipan()
{
addMouseListener(this);

for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
mark[i][j]=false;
}

/* try{
client=new Socket("219.246.84.224",9999);
System.out.println(client);
}catch(Exception e){System.out.println(client);}*/
}

public void init()
{
System.out.println("dddddddddddddddddddddddddddddd");
}

public void paint(Graphics g)
{
maxHeight=getHeight();
maxWidth=getWidth();
System.out.println(""+maxHeight);
System.out.println(""+maxWidth);

// setBackground(Color.gray);

x[0]=50;
y[0]=50;

for(int i=1;i<15;i++)
{
x[i]=x[i-1]+35;
y[i]=y[i-1]+35;
}

for(int i=0;i<15;i++)
{
g.drawLine(x[0],y[i],x[14],y[i]);
g.drawLine(x[i],y[0],x[i],y[14]);
}
}

public void update(Graphics g)
{
g.setColor(Color.black); //客户端和服务器端设置不同的颜色
draw(position_x,position_y,g);
try{
send(x1,y1);
receive();
g.setColor(Color.white);
draw(x[Integer.parseInt(x2)],y[Integer.parseInt(y2)],g);
}catch(Exception e){System.out.println(e);}
}

public void draw(int x,int y,Graphics g)
{
g.fillArc(position_x-8,position_y-8,16,16,0,360);

isWin();
}

public void send(int x,int y)throws IOException //客户端发送消息
{
Socket client=null;
try
{
client=new Socket("219.246.84.229",9999);
System.out.println("start:"+client);
System.out.println("connecting :"+client);

out=new PrintWriter(client.getOutputStream());

String str=null;

out.println(Integer.toString(x));
out.println(Integer.toString(y));

System.out.println(""+x+y);

client.close();
out.close();
}
catch(Exception e){System.out.println("2"+e);}
finally
{

}
}

public void receive()throws IOException
{
Socket client=null;
try
{
client=new Socket("219.246.84.224",9999);
System.out.println("start:"+client);
System.out.println("connecting :"+client);

in=new BufferedReader(
new InputStreamReader(client.getInputStream()));

x2 = in.readLine();
y2 = in.readLine();

System.out.println(x2+y2);

in.close();
client.close();
}
catch(Exception e){System.out.println("1"+e);}
finally
{

}
}

public void isWin()
{
int num=0; //竖

for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(mark[i][j]==true)
num=num+1;
}
if(num>=5)
{
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); //赢了之后的操作
}
num=0;
}

int num1=0; //横

for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
if(mark[j][i]==true)
num1++;
}
if(num1>=5)
{
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); //赢了之后的操作
}
num1=0;
}

int num2=0; //左上右下 下

int m;
for(int j=0;j<11;j++)
{
m=j;
for(int i=0;i<15-j;i++,j++)
{
if(mark[i][j]==true)
System.out.println("*******************************");
num2++;
}
if(num2>=5)
{
//System.out.println("*******************************"); //赢了之后的操作
}
num2=0;
j=m;
}
}

public void mouseClicked(MouseEvent e)
{
for(int i=0;i<15;i++)
{
if(x[i]-10<e.getX()&&e.getX()<x[i]+10)
{
position_x=x[i];
x1=i;
break;
}
}


for(int i=0;i<15;i++)
{
if(y[i]-10<e.getY()&&e.getY()<y[i]+10)
{
position_y=y[i];
y1=i;
break;
}
}

mark[x1][y1]=true;

repaint();
}

public void mousePressed(MouseEvent e)
{

}

public void mouseReleased(MouseEvent e)
{

}

public void mouseEntered(MouseEvent e)
{

}

public void mouseExited(MouseEvent e)
{

}
}
zzzhc 2004-12-01
  • 打赏
  • 举报
回复
你的处理方式有点怪,开一个serversocket接受一个连接,处理后就关闭太没必要,建议用DatagramSocket,多开一个线程处理接收发送
lkenshin 2004-12-01
  • 打赏
  • 举报
回复
好长啊
jFresH_MaN 2004-12-01
  • 打赏
  • 举报
回复
好长啊
帮你顶吧
drugon 2004-12-01
  • 打赏
  • 举报
回复
代码太长了,你又没有说出现什么问题,没时间帮你看。
不徻写代码 2004-12-01
  • 打赏
  • 举报
回复
UP
Avampire 2004-12-01
  • 打赏
  • 举报
回复
public void send(int x,int y)throws IOException //客户端发送消息
{
try{
ServerSocket server=new ServerSocket(9999);
System.out.println("start:"+server);
Socket s=server.accept();
System.out.println("connecting :"+s);
.
.
客户端不是这样连服务器端的,
try{
Socket client = new Socket(服务器名或服务器ip,服务程序的监听端口);
.
.
这样就可以了


zhaohao19853 2004-12-01
  • 打赏
  • 举报
回复
你这个程序是客户端的还是什么的啊
我怎么复制下来编译用不起来呢
射天狼 2004-12-01
  • 打赏
  • 举报
回复
这么长的代码没有时间帮你弄啊~~~~
duanlong 2004-12-01
  • 打赏
  • 举报
回复
具体问题就是出现在了
数据的接受和发送的地方
客户端和服务器端总是联接不上
duanlong 2004-12-01
  • 打赏
  • 举报
回复
楼上什么意思啊?
帮忙看看才是......

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧