diff --git a/src/ChessBord.java b/src/ChessBord.java new file mode 100644 index 0000000..72aaaef --- /dev/null +++ b/src/ChessBord.java @@ -0,0 +1,179 @@ +package 五子棋; + +import java.awt.*; +import java.awt.event.*; +import javax.swing.*; + +public class ChessBord extends JPanel implements MouseListener{//继承面板类和鼠标事件接口 + public static int MARGIN=30;//定义边距 + public static int ROWS=15;//定义行数 + public static int COLS=15;//定义列数 + public static int GRID_SPAN=35;//网格间距 + Chess[] chessList=new Chess[(ROWS+1)*(COLS+1)];//定义一个棋子数组 + String[][] board=new String[MARGIN*2+GRID_SPAN*COLS][MARGIN*2+GRID_SPAN*COLS];//声明一个字符串数组,用来判断输赢 + int chessCount;//棋子数目 + int xindex,yindex;//棋子的坐标索引 + boolean start=true;//开始默认黑子先下 + boolean GameOver=false;//定义是否游戏结束 + public ChessBord() {//棋盘类构造函数 + setBackground(Color.LIGHT_GRAY);//设置背景颜色 + addMouseListener(this);//将棋盘类添加到鼠标事件监听器 + addMouseMotionListener(new MouseMotionListener() {//匿名内部类 + @Override + public void mouseMoved(MouseEvent e) {//根据鼠标的移动所在的坐标来设置鼠标光标形状 + int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//对鼠标光标的x坐标进行转换 + int y1=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;//对鼠标光标的y坐标进行转换 + if(x1<0||x1>ROWS||y1<0||y1>COLS||GameOver||findchess(x1, y1)) { + setCursor(new Cursor(Cursor.DEFAULT_CURSOR));//设置鼠标光标为默认形状 + }else { + setCursor(new Cursor(Cursor.HAND_CURSOR));//设置鼠标光标为手型 + } + } + @Override + public void mouseDragged(MouseEvent e) { + } + }); + for(int i=0;iROWS||yindex<0||yindex>COLS) {//棋子在棋盘外不能下, + return ; + }else if(findchess( xindex, yindex)) {//所下位置已有棋子,不能下 + return ; + } + Chess po=new Chess(xindex,yindex,start?Color.black:Color.WHITE);//对棋子对象进行初始化 + chessList[chessCount++]=po;//将棋子对象添加到棋子数组中 + repaint();//重画图型 + if(win( xindex,yindex,start)) {//判断是否胜利 + String msg=String.format("恭喜 %s赢了",colorName); + JOptionPane.showMessageDialog(this, msg); + //gameOver=true; + GameOver=true; + }else if(chessCount==(COLS+1)*(ROWS+1)) {//判断是否全部下满 + String msg=String.format("恭喜 %s赢了",colorName); + JOptionPane.showMessageDialog(this, msg); + GameOver=true; + } + start=!start;//改变棋子先下棋状态 + } + @Override + public void mouseReleased(MouseEvent e) { + } + @Override + public void mouseEntered(MouseEvent e) { + } + @Override + public void mouseExited(MouseEvent e) { + } + @Override + protected void paintComponent(Graphics g) {//画棋盘和棋子 + super.paintComponent(g); + for(int i=0;i<=ROWS;i++) {//画横线 + g.drawLine(MARGIN, MARGIN+i*GRID_SPAN, MARGIN+COLS*GRID_SPAN, MARGIN+i*GRID_SPAN); + } + for(int j=0;j<=COLS;j++) {//画竖线 + g.drawLine(MARGIN+j*GRID_SPAN, MARGIN, MARGIN+j*GRID_SPAN, MARGIN+ROWS*GRID_SPAN); + } + for(int i=0;i=4&&x+y<=30){ + int i=(x+y<=19)?x+y:x+y-20; + if(x+y<=19){ + for(int k=0;k<=i-4;k++){ + if(board[k][i-k].equals(str)&&board[k+1][i-k-1].equals(str)&&board[k+2][i-k-2].equals(str)&&board[k+3][i-k-3].equals(str)&&board[k+4][i-k-4].equals(str)) + return true; + } + }else{ + for(int k=i;k<=15;k++){ + if(board[k][20-k].equals(str)&&board[k+1][20-k-1].equals(str)&&board[k+2][20-k-2].equals(str)&&board[k+3][20-k-3].equals(str)&&board[k+4][20-k-4].equals(str)) + return true; + } + } + } + //棋子所在捺行是否有五子相连的情况 + if(y-x<=15&&x-y<=15){ + int i=(x0) { + xindex=chessList[chessCount-1].getX(); + yindex=chessList[chessCount-1].getY(); + } + start=!start; + repaint(); + } + public void restartGame() {//重新开始函数 + for(int i=0;i