Java实现两人五子棋游戏(五) 判断是否有一方胜出
程序员文章站
2023-11-17 17:00:58
之前的两篇文章:java实现两人五子棋游戏(二) 画出棋盘;java实现两人五子棋游戏(三) 画出棋子;java实现两人五子棋游戏(四) 落子动作的实现,可以点击查看。...
之前的两篇文章:java实现两人五子棋游戏(二) 画出棋盘;java实现两人五子棋游戏(三) 画出棋子;java实现两人五子棋游戏(四) 落子动作的实现,可以点击查看。
前面我们已经画好了棋盘、棋子并且可以*的落子了,那么接下来要实现的功能是判断是否有五连珠(暂时不考虑行棋方)。
我们采用遍历棋盘已经落子的位置,查看每个落子点,在它的上下,左右,左下右上,左上右下四个方向的任一方向上是否有五个连续的棋子。
第一步,对棋子类进行改造,之前我们的棋子类只有颜色信息和落子状态,现在要新增一个int型的数据,用于记录遍历过程中当前有几个珠子已知连续。
chessman.java
package xchen.test.simplegobang; public class chessman { private int color;//1-white,0-black private boolean placed = false; int matchcount = 1; public chessman(int color,boolean placed){ this.color=color; this.placed=placed; } public boolean getplaced() { return placed; } public void setplaced(boolean placed) { this.placed = placed; } public int getcolor() { return color; } public void setcolor(int color) { this.color = color; } }
第二步,先从一个方向上判断是否有五连珠,这里采用左右方向作为尝试。
添加了一个iswin函数,用遍历整个棋盘上的有效棋子的方式,来进行胜出判断。
drawchessboard.java
package xchen.test.simplegobang; import java.awt.graphics; import java.awt.graphics2d; import java.awt.radialgradientpaint; import java.awt.image; import java.awt.toolkit; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.color; import javax.swing.jpanel; public class drawchessboard extends jpanel implements mouselistener{ final static int black=0; final static int white=1; public int chesscolor = black; int chessman_width=30; public image boardimg; final private int rows = 19; chessman[][] chessstatus=new chessman[rows+1][rows+1]; public drawchessboard() { boardimg = toolkit.getdefaulttoolkit().getimage("res/drawable/chessboard2.png"); if(boardimg == null) system.err.println("png do not exist"); addmouselistener(this); } @override protected void paintcomponent(graphics g) { // todo auto-generated method stub super.paintcomponent(g); int imgwidth = boardimg.getheight(this); int imgheight = boardimg.getwidth(this); int fwidth = getwidth(); int fheight= getheight(); int x=(fwidth-imgwidth)/2; int y=(fheight-imgheight)/2; int span_x=imgwidth/rows; int span_y=imgheight/rows; g.drawimage(boardimg, x, y, null); //画横线 for(int i=0;i<rows;i++) { g.drawline(x, y+i*span_y, fwidth-x,y+i*span_y); } //画竖线 for(int i=0;i<rows;i++) { g.drawline(x+i*span_x, y, x+i*span_x,fheight-y); } //画棋子 for(int i=0;i<rows+1;i++) { for(int j=0;j<rows+1;j++) { if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true) { //system.out.println("draw chessman "+i+" "+j); int pos_x=x+i*span_x; int pos_y=y+j*span_y; float radius_b=40; float radius_w=80; float[] fractions = new float[]{0f,1f}; java.awt.color[] colors_b = new java.awt.color[]{color.black,color.white}; color[] colors_w = new color[]{color.white,color.black}; radialgradientpaint paint; if(chessstatus[i][j].getcolor()==1) { //system.out.println("draw white chess"); paint = new radialgradientpaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w); }else{ //system.out.println("draw black chess"); paint = new radialgradientpaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b); } ((graphics2d)g).setpaint(paint); ((graphics2d)g).filloval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width); } } } } @override //当用户按下鼠标按钮时发生 public void mousepressed(mouseevent e) { int point_x=e.getx(); int point_y=e.gety(); int imgwidth = boardimg.getheight(this); int imgheight = boardimg.getwidth(this); int fwidth = getwidth(); int fheight= getheight(); int x=(fwidth-imgwidth)/2; int y=(fheight-imgheight)/2; int span_x=imgwidth/rows; int span_y=imgheight/rows; //system.out.println("press"); int status_x = 0; int status_y = 0; if(point_x>=x && point_x<=x+imgwidth && point_y>=y && point_y <= y+imgheight) { //system.out.println("合法"); for(int i=0;i<rows+1;i++) { if(point_x>=x-chessman_width/2+1+i*span_x) { if(point_x<=x+chessman_width/2-1+i*span_x)//如果是width/2会在中间点出现两个匹配值 { //system.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; } } } for(int i=0;i<rows+1;i++) { if(point_y>=y-chessman_width/2+1+i*span_y) { if(point_y <= y+chessman_width/2-1+i*span_y) { //system.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_y = i; } } } chessman chessman = new chessman(black, true); chessstatus[status_x][status_y]=chessman; repaint(); if(iswin(status_x, status_y, chessstatus)) { system.out.println("win!!!!!"); } } } @override //当用户按下并松开鼠标按钮时发生 public void mouseclicked(mouseevent e) { // todo auto-generated method stub } @override public void mousereleased(mouseevent e) { // todo auto-generated method stub } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } boolean iswin(int point_x,int point_y,chessman[][] cm) { //int matchcount = 1;//记录连珠的数目 //横向查找 for(int i=0;i<rows+1;i++) { for(int j=0;j<rows+1;j++) { if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true) { //system.out.println("iswin:"+i+" "+j); //向右侧查找 for(int n=1;n<=4;n++) { if((i+n>=0)&&(i+n)<=rows) { if(chessstatus[i+n][j]!=null&&chessstatus[i+n][j].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" right count++:"+(i+n)+" "+j+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { break; } } } //向左侧查找 for(int n=1;n<=4;n++) { if((i-n>=0)&&(i-n)<=rows) { if(chessstatus[i-n][j]!=null&&chessstatus[i-n][j].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i-n)+" "+j+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { if(chessstatus[i-n][j]!=null) { chessstatus[i][j].matchcount = 1; } break; } } } chessstatus[i][j].matchcount=1;//refresh count } } } return false; } }
第三步,主模块不变,运行测试一下我们的算法是否正确
main.java
package xchen.test.simplegobang; import java.awt.container; import javax.swing.jframe; import xchen.test.simplegobang.drawchessboard; public class main extends jframe{ private drawchessboard drawchessboard; public main() { drawchessboard = new drawchessboard(); //frame标题 settitle("单机五子棋"); container containerpane =getcontentpane(); containerpane.add(drawchessboard); } public static void main(string[] args) { main m = new main(); m.setsize(800, 800); m.setvisible(true); } }
第四步,现在我们一个方向上的判断已经做好了,接下来补全其他三个方向上的判断代码
补足drawchessboard.java中的iswin()函数
package xchen.test.simplegobang; import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.radialgradientpaint; import java.awt.toolkit; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import javax.swing.jpanel; public class drawchessboard extends jpanel implements mouselistener{ final static int black=0; final static int white=1; public int chesscolor = black; int chessman_width=30; public image boardimg; final private int rows = 19; chessman[][] chessstatus=new chessman[rows+1][rows+1]; public drawchessboard() { boardimg = toolkit.getdefaulttoolkit().getimage("res/drawable/chessboard2.png"); if(boardimg == null) system.err.println("png do not exist"); addmouselistener(this); } @override protected void paintcomponent(graphics g) { // todo auto-generated method stub super.paintcomponent(g); int imgwidth = boardimg.getheight(this); int imgheight = boardimg.getwidth(this); int fwidth = getwidth(); int fheight= getheight(); int x=(fwidth-imgwidth)/2; int y=(fheight-imgheight)/2; int span_x=imgwidth/rows; int span_y=imgheight/rows; g.drawimage(boardimg, x, y, null); //画横线 for(int i=0;i<rows;i++) { g.drawline(x, y+i*span_y, fwidth-x,y+i*span_y); } //画竖线 for(int i=0;i<rows;i++) { g.drawline(x+i*span_x, y, x+i*span_x,fheight-y); } //画棋子 for(int i=0;i<rows+1;i++) { for(int j=0;j<rows+1;j++) { if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true) { //system.out.println("draw chessman "+i+" "+j); int pos_x=x+i*span_x; int pos_y=y+j*span_y; float radius_b=40; float radius_w=80; float[] fractions = new float[]{0f,1f}; java.awt.color[] colors_b = new java.awt.color[]{color.black,color.white}; color[] colors_w = new color[]{color.white,color.black}; radialgradientpaint paint; if(chessstatus[i][j].getcolor()==1) { //system.out.println("draw white chess"); paint = new radialgradientpaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w); }else{ //system.out.println("draw black chess"); paint = new radialgradientpaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b); } ((graphics2d)g).setpaint(paint); ((graphics2d)g).filloval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width); } } } } @override //当用户按下鼠标按钮时发生 public void mousepressed(mouseevent e) { int point_x=e.getx(); int point_y=e.gety(); int imgwidth = boardimg.getheight(this); int imgheight = boardimg.getwidth(this); int fwidth = getwidth(); int fheight= getheight(); int x=(fwidth-imgwidth)/2; int y=(fheight-imgheight)/2; int span_x=imgwidth/rows; int span_y=imgheight/rows; //system.out.println("press"); int status_x = 0; int status_y = 0; if(point_x>=x && point_x<=x+imgwidth && point_y>=y && point_y <= y+imgheight) { //system.out.println("合法"); for(int i=0;i<rows+1;i++) { if(point_x>=x-chessman_width/2+1+i*span_x) { if(point_x<=x+chessman_width/2-1+i*span_x)//如果是width/2会在中间点出现两个匹配值 { //system.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); status_x = i; } } } for(int i=0;i<rows+1;i++) { if(point_y>=y-chessman_width/2+1+i*span_y) { if(point_y <= y+chessman_width/2-1+i*span_y) { //system.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_y = i; } } } chessman chessman = new chessman(black, true); chessstatus[status_x][status_y]=chessman; repaint(); if(iswin(status_x, status_y, chessstatus)) { system.out.println("win!!!!!"); } } } @override //当用户按下并松开鼠标按钮时发生 public void mouseclicked(mouseevent e) { // todo auto-generated method stub } @override public void mousereleased(mouseevent e) { // todo auto-generated method stub } @override public void mouseentered(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { // todo auto-generated method stub } boolean iswin(int point_x,int point_y,chessman[][] cm) { for(int i=0;i<rows+1;i++) { for(int j=0;j<rows+1;j++) { //横向查找 if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true) { //向右侧查找 for(int n=1;n<=4;n++) { if((i+n>=0)&&(i+n)<=rows) { if(chessstatus[i+n][j]!=null&&chessstatus[i+n][j].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" right count++:"+(i+n)+" "+j+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { break; } } } //向左侧查找 for(int n=1;n<=4;n++) { if((i-n>=0)&&(i-n)<=rows) { if(chessstatus[i-n][j]!=null&&chessstatus[i-n][j].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i-n)+" "+j+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { if(chessstatus[i-n][j]!=null) { chessstatus[i][j].matchcount = 1; } break; } } } chessstatus[i][j].matchcount=1;//refresh count } } } for(int i=0;i<rows+1;i++) { for(int j=0;j<rows+1;j++) { //纵向 if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true) { //向下查找,左上角为坐标原点,y轴正方向向下 for(int n=1;n<=4;n++) { if((j+n>=0)&&(j+n)<=rows) { if(chessstatus[i][j+n]!=null&&chessstatus[i][j+n].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" up count++:"+(i)+" "+(j+n)+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { break; } } } //向上查找 for(int n=1;n<=4;n++) { if((j-n>=0)&&(j-n)<=rows) { if(chessstatus[i][j-n]!=null&&chessstatus[i][j-n].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i)+" "+(j-n)+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { if(chessstatus[i][j-n]!=null) { chessstatus[i][j].matchcount = 1; } break; } } } chessstatus[i][j].matchcount=1;//refresh count } } } //方向:左上右下 for(int i=0;i<rows+1;i++) { for(int j=0;j<rows+1;j++) { //左上 if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true) { //向下查找,左上角为坐标原点,y轴正方向向下 for(int n=1;n<=4;n++) { if((j-n>=0)&&(j-n)<=rows&&(i-n)>=0&&(i-n)<=rows) { if(chessstatus[i-n][j-n]!=null&&chessstatus[i-n][j-n].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" up count++:"+(i-n)+" "+(j-n)+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { break; } } } //右下 for(int n=1;n<=4;n++) { if((j+n>=0)&&(j+n)<=rows&&(i+n)>=0&&(i+n)<=rows) { if(chessstatus[i+n][j+n]!=null&&chessstatus[i+n][j+n].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i+n)+" "+(j+n)+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { if(chessstatus[i+n][j+n]!=null) { chessstatus[i][j].matchcount = 1; } break; } } } chessstatus[i][j].matchcount=1;//refresh count } } } //方向:左下右上 for(int i=0;i<rows+1;i++) { for(int j=0;j<rows+1;j++) { //左下 if(chessstatus[i][j]!=null&&chessstatus[i][j].getplaced()==true) { //向下查找,左上角为坐标原点,y轴正方向向下 for(int n=1;n<=4;n++) { if((j+n>=0)&&(j+n)<=rows&&(i-n)>=0&&(i-n)<=rows) { if(chessstatus[i-n][j+n]!=null&&chessstatus[i-n][j+n].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" up count++:"+(i-n)+" "+(j+n)+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { break; } } } //右上 for(int n=1;n<=4;n++) { if((j-n>=0)&&(j-n)<=rows&&(i+n)>=0&&(i+n)<=rows) { if(chessstatus[i+n][j-n]!=null&&chessstatus[i+n][j-n].getplaced()==true) { chessstatus[i][j].matchcount++; system.out.println("pos:"+i+" "+j+" "+"left count++:"+(i+n)+" "+(j-n)+" count:"+chessstatus[i][j].matchcount); if(chessstatus[i][j].matchcount==5) { return true; } }else { if(chessstatus[i+n][j-n]!=null) { chessstatus[i][j].matchcount = 1; } break; } } } chessstatus[i][j].matchcount=1;//refresh count } } } return false; } }
再运行一下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 关键词词库筛选与建立