数组越界(五子棋)

    技术2023-06-17  82

    package frame001;

    import javax.imageio.ImageIO; import javax.swing.; import java.awt.; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;

    public class Wuziqi extends JFrame implements MouseListener {

    int width= Toolkit.getDefaultToolkit().getScreenSize().width;//取得屏幕的宽度 int height= Toolkit.getDefaultToolkit().getScreenSize().height;//取得屏幕的高度 int x=0; int y=0; //保存之前下过的所有棋子的坐标,其中数据具体内容 //如果保存的是0,表示这个点没有其中,假设是1,表示这个点是黑子,假设是2,表示这个点是白子 int[][] allChess=new int[19][19]; //标识当前是黑棋,还是白棋 boolean chessColor=true; public Wuziqi() { this.setTitle("五子棋");//设置窗体的标题 this.setSize(500,500);//窗体的宽高

    // this.setLocation(200,150);//设置窗体位置 this.setResizable(false);//设置窗体是否可以改变大小 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体关闭方式,关闭窗体时,同时结束程序

    this.setLocation((width-500)/2,(height-500)/2);//设置窗体出现的位置 this.addMouseListener(this);//接口 this.setVisible(true);//设置窗体显示 } public void paint(Graphics g) { BufferedImage image = null; try { image = ImageIO.read(new File("C:\\Users\\Administrator\\Desktop\\素材\\timg.jpg"));//图片链接 } catch (IOException e) { e.printStackTrace(); } g.drawImage(image, -10, -10, this); g.setFont(new Font("宋体",0,20)); g.drawString("游戏信息:",100,60); g.setFont(new Font("宋体",0,16)); g.drawString("黑方时间:无限制",30,470); g.drawString("白方时间:无限制",250,470); //绘制棋盘 /* for (int a=75;a<=395;a+=20) { g.drawLine(15,a,395,a); } for (int a=15;a<=395;a+=20) { g.drawLine(a,75,a,395); } */ for (int a=1;a<19;a++) { g.drawLine(15,55+a*20,355,55+a*20); g.drawLine(-5+a*20,75,-5+a*20,415); }

    //标注点位 int d1=6; g.fillOval(75-d1/2,135-d1/2,d1,d1); g.fillOval(75-d1/2,255-d1/2,d1,d1); g.fillOval(75-d1/2,375-d1/2,d1,d1); g.fillOval(315-d1/2,135-d1/2,d1,d1); g.fillOval(315-d1/2,255-d1/2,d1,d1); g.fillOval(315-d1/2,375-d1/2,d1,d1); g.fillOval(195-d1/2,135-d1/2,d1,d1); g.fillOval(195-d1/2,375-d1/2,d1,d1); g.fillOval(195-d1/2,255-d1/2,d1,d1);

    // 绘制棋子 /* int d2=16; g.fillOval(x-d2/2,y-d2/2,d2,d2); */

    //绘制全部棋子 for(int i=0;i<19;i++) { for(int j=0;j<19;j++) { if(allChess[i][j]==1) { //黑 int x= i20+15; int y= j20+75; g.fillOval(x-8,y-8,16,16);

    } if (allChess[i][j]==2) { //白 int x= i*20+15; int y= j*20+75; g.setColor(Color.WHITE); g.fillOval(x-8,y-8,16,16); g.setColor(Color.BLACK); g.drawOval(x-8,y-8,16,16); } } } } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { System.out.println(e.getX()); System.out.println(e.getY()); //获取 x = e.getX(); y = e.getY(); if (x >= 15 && x <= 375 && y >= 75 && y <= 435) { float x1 = (x - 5) / 20; float y1 = (y - 65) / 20; int x = (int) x1; int y = (int) y1; if (allChess[x][y] == 0) { if (chessColor == true) { allChess[x][y] = 1; chessColor = false; } else { allChess[x][y] = 2; chessColor = true; } //判断这个棋子是否和其他棋子连成5个 boolean winFlag= this.checkWin(); if (winFlag==true) { JOptionPane.showMessageDialog(this,"游戏结束了"+(allChess[x][y]==1?"黑方":"白方")+"获胜"); } }else { JOptionPane.showMessageDialog(this,"当前位置已经有棋子了"); } this.repaint(); } } public void mouseReleased(MouseEvent e) { } //判断是否胜利 //判断一个米字 private boolean checkWin() { boolean flag=false; //判断横向的是否有5个相连,y轴不会改变 int count=1; int color = allChess[x][y]; int i=1; while (color==allChess[x+i][y]) { if (x+i>375) { break; } count++; i++; } i=1; while (color==allChess[x-i][y]) { if (x-i<0) { break; } count++; i++; } if(count>4) { flag=true; } return flag; }

    }

    出现的错误数组越界

    解决方案 float x1 = (x - 5) / 20; float y1 = (y - 65) / 20; x = (int) x1; y = (int) y1;

    把int去掉

    Processed: 0.023, SQL: 10