Java实现两人五子棋游戏(六)行棋方变换
之前的两篇文章:Java实现两人五子棋游戏(二) 画出棋盘;Java实现两人五子棋游戏(三) 画出棋子;Java实现两人五子棋游戏(四) 落子动作的实现,可以点击查看。
创新互联公司长期为超过千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为桐城企业提供专业的网站设计制作、成都网站建设,桐城网站改版等技术服务。拥有十载丰富建站经验和众多成功案例,为您定制开发。
前面我们已经画好棋盘和棋子,并可以自由落子,且可以判断棋盘中是否有五连珠。接下来的工作是,每次落子后交换行棋方(即将下一棋子颜色由白->黑或黑->白),并将胜出判断增加颜色判断条件(棋盘上存在五连珠->棋盘上存在同色的五连珠)。
主模块Main.java和棋子类Chessman.java不变,我们要对DrawChessBoard.java中的落子函数mousePressed和五连珠判断函数isWin做一下调整。
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); } }
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; } }
第一步,修改mousePressed函数,使得每次落子后,都将行棋方让给对家(即每次落子都改变棋子颜色)
DrawChessBoard.java中的mousePressed函数部分,只要每次都修改chessColor的值即可。
@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=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 =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(chessColor, true); chessStatus[status_x][status_y]=chessman; System.out.println("chess color:"+chessColor); if(chessColor==BLACK) { chessColor = WHITE; }else { chessColor = BLACK; } repaint(); if(isWin(status_x, status_y, chessStatus)) { System.out.println("WIN!!!!!"); } } }
运行一下
第二步,将胜出判断增加颜色判断条件(棋盘上存在五连珠->棋盘上存在同色的五连珠)
在isWin函数的判断条件上,加上对Chessman.getColor的颜色判断
isWin函数代码
boolean isWin(int point_x,int point_y,Chessman[][] cm) { for(int i=0;i=0)&&(i+n)<=ROWS) { if(chessStatus[i+n][j]!=null&&chessStatus[i+n][j].getPlaced()==true&&chessStatus[i+n][j].getColor()==matchColor) { 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-n][j].getColor()==matchColor) { 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 =0)&&(j+n)<=ROWS) { if(chessStatus[i][j+n]!=null&&chessStatus[i][j+n].getPlaced()==true&&chessStatus[i][j+n].getColor()==matchColor) { 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-n].getColor()==matchColor) { 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 =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-n][j-n].getColor()==matchColor) { 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+n][j+n].getColor()==matchColor) { 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 =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-n][j+n].getColor()==matchColor) { 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+n][j-n].getColor()==matchColor) { 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; }
运行一下!
完整的DrawChessBoard.java
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=x && point_x<=x+imgWidth && point_y>=y && point_y <= y+imgHeight) { //System.out.println("合法"); for(int i=0;i =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 =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; } } } if(chessStatus[status_x][status_y]==null||chessStatus[status_x][status_y].getPlaced()==false) { Chessman chessman = new Chessman(chessColor, true); chessStatus[status_x][status_y]=chessman; System.out.println("chess color:"+chessColor); if(chessColor==BLACK) { chessColor = WHITE; }else { chessColor = BLACK; } 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 =0)&&(i+n)<=ROWS) { if(chessStatus[i+n][j]!=null&&chessStatus[i+n][j].getPlaced()==true&&chessStatus[i+n][j].getColor()==matchColor) { 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-n][j].getColor()==matchColor) { 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 =0)&&(j+n)<=ROWS) { if(chessStatus[i][j+n]!=null&&chessStatus[i][j+n].getPlaced()==true&&chessStatus[i][j+n].getColor()==matchColor) { 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-n].getColor()==matchColor) { 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 =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-n][j-n].getColor()==matchColor) { 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+n][j+n].getColor()==matchColor) { 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 =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-n][j+n].getColor()==matchColor) { 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+n][j-n].getColor()==matchColor) { 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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
新闻名称:Java实现两人五子棋游戏(六)行棋方变换
文章出自:http://cdiso.cn/article/jhishg.html