Java问题 请问为什么五子棋不能把达到横线五个黑色棋子消去?

importjava.applet.*;importjava.awt.*;importjava.awt.event.*;publicclassWuextendsApple... import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Wu extends Applet implements MouseListener,ActionListener
{
String str="退出游戏";
static int i,x,y,X,Y;
static int st[]=new int[225];
static int a=0,d,b;
static int s1[]=new int[16];
static int s2[]=new int[16];
Button b1;
public void init()
{
for(i=0;i<=224;i++) //横个每个点加个数,全部定义为0
{
st[i]=0;
}
for(x=0;x<15;x++) //给每个点的画棋子顶点定义坐标第一个点(x,y)=(s1[0]=5,s2[0]=5)
{
s2[y]=30;
for(y=0;y<15;y++)
{
s1[x]=30+30*x;
s2[y]=30+30*x;
}
}
b1=new Button("退出游戏");
this.add(b1);
b1.addActionListener(this);
this.addMouseListener(this);
setBackground(Color.orange);
}
Toolkit kit=Toolkit.getDefaultToolkit(); //初始化棋盘图片
Image back=kit.getImage("1.jpg");
public void paint(Graphics g) //绘画棋盘和棋子
{
g.drawImage(back,45,45,420,420,this);
for(i=0,b=0;i<=14;i++,b++) //对五子棋同色达到五颗的消除
{
i=i*15;
b=(b*15)+14;
d=0;
while(i<=b)
{
if(st[i]==1)
d++;
else
d=0;
if(d==5)
{
for(d=5;d>0;d--)
{
st[i]=0;
i--;
}
break;
}
i++;
}
d=0;
}
for(i=0;i<=224;i++)
{
if(st[i]==1)
{
s2[y]=i/15*30+30;
s1[x]=i%15*30+30;
g.setColor(Color.black);
g.drawOval(s1[x],s2[y],30,30); //以(s1[x],s2[y])为正方形顶点,在正方形中画直径为30的圆
g.fillOval(s1[x],s2[y],30,30);
}
}
for(i=0;i<=224;i++)
{
if(st[i]==2)
{
s2[y]=i/15*30+30;
s1[x]=(i%15+1)*30;
g.setColor(Color.white);
g.drawOval(s1[x],s2[y],30,30);
g.fillOval(s1[x],s2[y],30,30);
}
}
}
public void mouseClicked(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
X=e.getX();Y=e.getY();
if((X<=480&&Y<=480)&&(30<=X&&Y>=30))
{
X=X/30;Y=Y/30; //求出点的坐标在第一行第几格
i=(Y-1)*15+X-1;
if(st[i]==0)
{
if(a==0)
{
st[i]=1;
a=1;
}
else
{
st[i]=2;
a=0;
}
repaint();
}
}
}
public void mouseReleased(MouseEvent e){}
public void mouseExited(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void start()
{
repaint();
}
public void actionPerformed(ActionEvent ae)
{
if(str.equals("退出游戏"))
System.exit(0);
}
public static void main(String args[])
{
Wu applet=new Wu();
Frame f=new Frame();
f.add(applet);
applet.start();
applet.init();
f.setTitle("五子棋大战");
f.setSize(550,570);
f.setVisible(true);
}
}
谁能帮我修改一下,然后帮我说明一下~~·谢谢!
展开
 我来答
a863809965z
2010-04-03
知道答主
回答量:19
采纳率:0%
帮助的人:0
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.swing.JOptionPane.*;

public class FiveChess
{
public static void main(String[] args)
{
JFrame myFrame = new JFrame("快乐五子棋");
myFrame.getContentPane().add(new MyPanel());
Toolkit mykit = myFrame.getToolkit();
Dimension mysize = mykit.getScreenSize();
myFrame.setBounds(0,0,mysize.width,mysize.height - 40);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.show();
}
}

class MyPanel extends JPanel implements MouseListener
{
private Point start = new Point(0,0);
private Point next = new Point(0,0);
boolean[][] chessBo = new boolean[19][19];
boolean chessCo = true; //判断颜色的,true 黑
boolean winchess = true; //true,为胜利

MyPanel(){
addMouseListener(this);
setBackground(Color.blue);
}
public boolean winkill(int x,int y) //win ?
{
int countBlack = 0,countWhite = 0;
boolean winchess01 = false;
//x,y 取到r01,r02的值
for(int i = 0;i < 5;i++)//判断纵向下
{
if((x + i) > 20)
break;
if(chessCo && chessBo[x + i][y])
countBlack++;
else if(chessBo[x + i][y] && (chessCo == false))
countWhite++;
}
for(int i = 0;i < 5;i++)//判断纵向上
{
if((x - i) <= 0)
break;
if(chessCo && chessBo[x - i][y])
countBlack++;
else if(chessBo[x - i][y] && (chessCo == false))
countWhite++;
}
if((countBlack == 5) || (countWhite == 5))
winchess01 = true;
return winchess01;
}
public void paint(Graphics g)//画棋盘
{
Graphics2D g2D = (Graphics2D)g;

g2D.setPaint(Color.black);
float pay = 60.0f,pbx = 60.0f;
float lett = 25.0f;
Point2D.Float p1 = new Point2D.Float(60.0f,pay);
Point2D.Float p2 = new Point2D.Float(510.0f,pay);
Point2D.Float p3 = new Point2D.Float(pbx,60.0f);
Point2D.Float p4 = new Point2D.Float(pbx,510.0f);
for(int i=0;i<19;i++){
Line2D.Float lineH = new Line2D.Float(p1,p2);
Line2D.Float lineV = new Line2D.Float(p3,p4);
pay += lett;
p1 = new Point2D.Float(60.0f,pay);
p2 = new Point2D.Float(510.0f,pay);
pbx += lett;
p3 = new Point2D.Float(pbx,60.0f);
p4 = new Point2D.Float(pbx,510.0f);
g2D.draw(lineH);
g2D.draw(lineV);
}
}
public void mousePressed(MouseEvent evt)
{
}
public void mouseClicked(MouseEvent evt)
{
int x = evt.getX();
int y = evt.getY();
int clickCount = evt.getClickCount();
if(clickCount >=1 ){
if((x > 48 && x < 522) && (y > 48 && y < 522))
draw(x,y);
}
}

public void draw(int dx,int dy)
{
int r01 = 0,r02 = 0;
Graphics g = getGraphics();
start.x = dx;
start.y = dy;
r01 = (start.x + 13 - 60) / 25; //r01 、r02当前的格子
r02 = (start.y + 13 - 60) / 25;
//System.out.println(r01 + "-" + r02);
next.x = 25 * r01 + 60 - 11;
next.y = 25 * r02 + 60 - 11;
//System.out.println(next.x + "-" + next.y);
if(chessCo){
chessCo = false;
g.setColor(Color.black);
g.fillOval(next.x,next.y,20,20);
chessBo[next.x][next.y] = true;//用这个时,黑白子可交替出现,
//chessBo[r01][r02] = true;//用这个代替上面那个时黑白子不可交替了,不知道为什么会这样
//加入判断胜负的方法winkill()
if(winchess == winkill(r01,r02))
showMessage();
}
if(!chessCo){
chessCo = true;
g.setColor(Color.white);
g.fillOval(next.x,next.y,20,20);

chessBo[r01][r02] = true;
}
//g.drawOval(next.x,next.y,20,20);
g.dispose();
}

public void mouseReleased(MouseEvent evt)
{
}

public void mouseEntered(MouseEvent evt)
{
}

public void mouseExited(MouseEvent evt)
{
}
public void showMessage()
{
JOptionPane.showMessageDialog(null,
"You are win.",
"wide288 to Message!",
JOptionPane.INFORMATION_MESSAGE);
}
}
出错原因都写在程序内了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
cmsmtn
2010-04-16
知道答主
回答量:10
采纳率:0%
帮助的人:9.9万
展开全部
public void paint(Graphics g) 方法改为
public void paint(Graphics g) //绘画棋盘和棋子
{
boolean bolFlg=false;
int iSave=0;
int bSave=0;
g.drawImage(back,45,45,420,420,this);
for(i=0,b=0;i<=14;i++,b++) //对五子棋同色达到五颗的消除
{
if (bolFlg==true) {
bolFlg=false;
break;
}
iSave = i;
bSave=b;
i=i*15;
b=(b*15)+14;
d=0;

while(i<=b)
{

if(st[i]==1) {
d++;
}
else {
d=0;
}

if(d==5) {
for(d=5;d>0;d--) {
st[i]=0;
i--;
}
bolFlg=true;
break;
}
else {
i++;
}

}

i=iSave;
b=bSave;
d=0;

}

for(i=0;i<=224;i++)
{
if(st[i]==1)
{
s2[y]=i/15*30+30;
s1[x]=i%15*30+30;
g.setColor(Color.black);
g.drawOval(s1[x],s2[y],30,30); //以(s1[x],s2[y])为正方形顶点,在正方形中画直径为30的圆
g.fillOval(s1[x],s2[y],30,30);
}
}
for(i=0;i<=224;i++)
{
if(st[i]==2)
{
s2[y]=i/15*30+30;
s1[x]=(i%15+1)*30;
g.setColor(Color.white);
g.drawOval(s1[x],s2[y],30,30);
g.fillOval(s1[x],s2[y],30,30);
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式