文档介绍:.my;
import .*;
import .*;
import ;
public class GobangPanel extends Panel{
int[][] chessArr = new int[15][15]; //棋盘数组 0 -无子 1-黑子 2-白子
boolean isBlack = true; //true为黑 false为白
public GobangPanel(){
init();
}
public void init(){
//添加面板上的鼠标点击事件
(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
gobangPnlMouseClicked(e);
}
});
}
//鼠标点击面板时,调用此方法
public void gobangPnlMouseClicked(MouseEvent evt){
//获取鼠标当前点击时的位置
int x = ();
int y = ();
//换算成数组对应的下标
int i = x/40;
int j = y/40;
//在棋盘外落子
if(i>14||j>14){
(this,"不能在棋盘外落子");
return ;
}
//重复棋子
if(chessArr[i][j]!=0){
(this,"对不起,此处已有棋子");
return;
}
//将对应的棋子颜色值存放棋盘数组
chessArr[i][j] = isBlack ? 1 : 2;
(); //手工重绘------>显示棋子
//判断是否赢了
boolean flag=isWin(i,j);
if(flag){
String msg=isBlack?"黑方获胜!":"白方获胜!";//显示消息框
(this,msg);
}
//切换对方
//if(isBlack)
// isBlack=false;
//else
// isBlack=true;
isBlack = !isBlack;
}
private boolean isWin(int xPos,int yPos){
int currColor = isBlack ? 1 : 2 ;
int count = 0 ;
for(int i=0;i<15;i++){
if(chessArr[i][yPos] ==currColor){
count++;
if(count == 5)
return true;
}else {
count