该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
定义棋盘及相关算法,代码多,度娘不给换行。
package fivechessgame;
import java.awt.Graphics;import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
public class CBoard extends JPanelimplements MouseListener{
private static final long serialVersionUID = 123654798932145422L;
ImageIcon ic1 = new ImageIcon("image/chessboard.jpg");
ImageIcon ic2 = new ImageIcon("image/b.gif");
ImageIcon ic3 = new ImageIcon("image/w.gif");
private int[][] cb = new int[15][15];
List list = new ArrayList();
private int whoClick = 0;
private boolean gameOver = true;
JLabel jl = new JLabel();
public CBoard(){this.add(jl);
}
public void gameStart(){
jl.setText("请黑方落子");
gameOver=false;
setMouseListener();
repaint();
}
public void gameOver() {if(gameOver){JOptionPane.showMessageDialog(null,"游戏没有开始或者已经结束!","不能认输",JOptionPane.DEFAULT_OPTION);}else if(list.size()>8){if(whoClick==1){JOptionPane.showMessageDialog(null,"白方认输","认输了",JOptionPane.DEFAULT_OPTION);}else if(whoClick==2){JOptionPane.showMessageDialog(null,"黑方认输","认输了",JOptionPane.DEFAULT_OPTION);}gameOver = true;cb = new int[15][15];list = new ArrayList();whoClick = 0;this.setMouseListener();}else if(list.size()<9){JOptionPane.showMessageDialog(null,"落子不足四粒!","不能认输",JOptionPane.DEFAULT_OPTION);}}
public void retract() {if(gameOver){JOptionPane.showMessageDialog(null,"游戏没有开始或者已经结束!","不能悔棋",JOptionPane.DEFAULT_OPTION);}else if(list.isEmpty()){JOptionPane.showMessageDialog(null,"还没有落字!","不能悔棋",JOptionPane.DEFAULT_OPTION);}if(!list.isEmpty()){CPiece cp = list.get(list.size()-1);list.remove(cp);whoClick = cb[cp.getX()][cp.getY()]-1;if(whoClick==0){whoClick = 2;}cb[cp.getX()][cp.getY()] = 0;if("请黑方落子".equals(jl.getText())){jl.setText("请白方落子");}else{jl.setText("请黑方落子");}repaint();}}public void setMouseListener(){if(!gameOver){this.addMouseListener(this);}else {this.removeMouseListener(this);}}
@Overrideprotected void paintComponent(Graphics g) {g.drawImage(ic1.getImage(),0,0,null);if(whoClick!=0){for(int i=0;i<15;i++){for(int j=0;j<15;j++){int m = cb[i][j];if(m==1){//画黑棋g.drawImage(ic2.getImage(),i*35+4, j*35+4, null);}else if(m==2){//画白棋g.drawImage(ic3.getImage(),i*35+4, j*35+4, null);}}}}}
public void mouseClicked(MouseEvent e) {Point p = e.getPoint(); int x = (int)p.getX()/35;int y = (int)p.getY()/35;if(cb[x][y]==0){whoClick++;if(whoClick>2)whoClick=1;if(whoClick==1){cb[x][y]=1;jl.setText("请白方落子");}else{cb[x][y]=2;jl.setText("请黑方落子");}CPiece cp = new CPiece(x,y);list.add(cp);repaint();int whoWin = judge(whoClick,x,y);if(whoWin!=0){String win="";if(whoWin==1){win = "执黑棋者胜!";}else{win = "执白棋者胜!";}JOptionPane.showMessageDialog(null,win,"赢了!",JOptionPane.DEFAULT_OPTION);gameOver = true;cb = new int[15][15];list = new ArrayList();whoClick = 0;this.setMouseListener();}}}
public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e) {}public void mousePressed(MouseEvent e) {}public void mouseReleased(MouseEvent e) {}public int judge(int whoWin,int i,int j){int count = 0;int x = i, y = j;//做横向判断while(x>0){x--;if(cb[x][y]==whoWin){count++;if(count==4){return whoWin;}}else{break;}}x=i;while(x<14){x++;if(cb[x][y]==whoWin){count++;if(count==4){return whoWin;}}else{break;}}count = 0;y=j;x=i;//做纵向判断while(y>0){y--;if(cb[x][y]==whoWin){count++;if(count==4){return whoWin;}}else{break;}}y=j;while(y<14){y++;if(cb[x][y]==whoWin){count++;if(count==4){return whoWin;}}else{break;}}//左斜判断x=i;y=j;count = 0;while(x>0&&y>0){x--;y--;if(cb[x][y]==whoWin){count++;if(count==4){return whoWin;}}else{break;}}x=i;y=j;while(x<14&&y<14){x++;y++;if(cb[x][y]==whoWin){count++;if(count==4){return whoWin;}}else{break;}}//右斜判断x=i;y=j;count = 0;while(x>0&&y<14){x--;y++;if(cb[x][y]==whoWin){count++;if(count==4){return whoWin;}}else{break;}}x=i;y=j;while(x<14&&y>0){x++;y--;if(cb[x][y]==whoWin){count++;if(count==4){return whoWin;}}else{break;}}return 0;}}