在上篇文章的基础下,来继续写,主要目标是实现
- 存储棋子
- 实现 开始游戏
- 实现 悔棋
存储棋子:
if (xx>=0&&xx<=(row-1)&&yy>=0&&yy<=(column-1)){//当该位置没有棋子的时候,可以下棋if (isAvail[xx][yy] == 0) {if (count == 1){//count=1为黑棋isAvail[xx][yy] = 1;g.setColor(Color.BLACK);count++;} else if (count == 2) {//count=1时为白棋g.setColor(Color.WHITE);isAvail[xx][yy] = 2;count--;}g.fillOval(xx * size + x - size / 2, yy * size + y - size / 2, size, size);Setx[index] = xx;Sety[index] = yy;index++;
下棋需要下在两条直线正中间,所以要对棋子坐标进行更改,具体上一篇文章都有的。
if ((x1 - x) % size < size / 2) {xx = (x1 - x) / size;} else if ((x1 - x) % size > size / 2) {xx = (x1 - x) / size + 1;}if ((y1 - y) % size < size / 2) {yy = (y1 - y) / size;} else if ((y1 - y) % size > size / 2) {yy = (y1 - y) / size + 1;}
更改坐标
实现开始游戏:加入开始标记就欧克了
public void qizi(int[][] arr){for (int i=0;i<arr.length;i++){for (int j=0;j<arr.length;j++){arr[i][j]=0;}}}
qizi方法作用是在点击开始游戏之后保证棋盘是空,清空数组
switch (gbStr){case"开始游戏":begin=2;//开始游戏qizi(isAvail);//更新保存棋子的数组jf.repaint();count=1;//棋子颜色jf.repaint();break;
将begin=2条件放在棋子开始之前就可以实现了。
public void mousePressed(MouseEvent e) {x1 = e.getX();y1 = e.getY();if (begin == 2)
//后面为下棋子的步骤
}
实现悔棋:
switch (gbStr){
case"悔棋":
System.out.println("index"+index);if (index>0){index--;int x=Setx[index];int y=Sety[index];isAvail[x][y]=0;}if (index%2==1){g.setColor(Color.white);count++;}else {g.setColor(Color.BLACK);count--;}jf.repaint();break;
}
在加一个,判断游戏输赢:
需要在创建一个Win类
public class Win {public static int[][] isAvail;//在这里定义一个二维数组为棋子的落子情况public static boolean chessRow(int row, int column, int[][] isAvail) {//先定义一个计数器,用以去计算已下棋子的数量int count = 1;//算上已经点击的棋子int chess = isAvail[row][column];System.out.println("当前输出的棋子" + chess);//向右遍历for (int i = row + 1; i <isAvail.length; i++) {if (chess == isAvail[i][column]) {count++;} else {break;}}//向左遍历for (int i = row - 1; i > 0; i--) {if (chess == isAvail[i][column]) {count++;} else {break;}}return (count >= 5);}public static boolean up_Down(int row, int column, int isAvail[][]){int count=1;int chess=isAvail[row][column];//向上遍历for (int i=column+1;i<isAvail.length;i++){if (chess==isAvail[row][i]){count++;}else {break;}}//向下遍历ifor (int i=column-1;i>0;i--){if (chess==isAvail[row][i]){count++;}else {break;}}return (count>=5);}public static boolean leftUp_RightDown(int row,int column,int isAvail[][]){int count=1;int chess=isAvail[row][column];//向左上遍历for (int i=row-1,j=column-1;i>0&&j>0;i--,j--){if (chess==isAvail[i][j]){count++;}else {break;}}//向右下遍历for (int i=row+1,j=column+1;i<isAvail.length&&j<isAvail.length;i++,j++){if (chess==isAvail[i][j]){count++;}else {break;}}return (count>=5);}public static boolean leftDown_RightUp(int row,int column,int isAvail[][]){int count=1;int chess=isAvail[row][column];//向左下遍历for (int i=row+1,j=column-1;i<isAvail.length&&j>0;i++,j--){if (chess==isAvail[i][j]){count++;}else {break;}}//向右上遍历for (int i=row-1,j=column+1;i>0&&j<isAvail.length;i--,j++){if (chess==isAvail[i][j]){count++;}else {break;}}return (count>=5);}//检查五子连珠public static boolean isWin(int row,int column,int isAvail[][]){if (chessRow(row,column,isAvail)||up_Down(row,column,isAvail)||leftDown_RightUp(row,column,isAvail)||leftUp_RightDown(row,column,isAvail)){return true;}else{return false;}}}
Win.isAvail=isAvail;if(Win.isWin (xx,yy,isAvail)){if(isAvail[xx][yy]==1){System.out.println ("黑棋胜利!!");}else if(isAvail[xx][yy]==2){System.out.println ("白棋胜利!!");}}
写在监听器中,才会有效。