之前的兩篇文章:Java實(shí)現(xiàn)兩人五子棋游戲(二) 畫(huà)出棋盤(pán);Java實(shí)現(xiàn)兩人五子棋游戲(三) 畫(huà)出棋子;Java實(shí)現(xiàn)兩人五子棋游戲(四) 落子動(dòng)作的實(shí)現(xiàn),可以點(diǎn)擊查看。
公司主營(yíng)業(yè)務(wù):網(wǎng)站設(shè)計(jì)、做網(wǎng)站、移動(dòng)網(wǎng)站開(kāi)發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競(jìng)爭(zhēng)能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開(kāi)放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對(duì)我們的高要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。成都創(chuàng)新互聯(lián)公司推出湘東免費(fèi)做網(wǎng)站回饋大家。
前面我們已經(jīng)畫(huà)好了棋盤(pán)、棋子并且可以自由的落子了,那么接下來(lái)要實(shí)現(xiàn)的功能是判斷是否有五連珠(暫時(shí)不考慮行棋方)。
我們采用遍歷棋盤(pán)已經(jīng)落子的位置,查看每個(gè)落子點(diǎn),在它的上下,左右,左下右上,左上右下四個(gè)方向的任一方向上是否有五個(gè)連續(xù)的棋子。
第一步,對(duì)棋子類進(jìn)行改造,之前我們的棋子類只有顏色信息和落子狀態(tài),現(xiàn)在要新增一個(gè)int型的數(shù)據(jù),用于記錄遍歷過(guò)程中當(dāng)前有幾個(gè)珠子已知連續(xù)。
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; } }
第二步,先從一個(gè)方向上判斷是否有五連珠,這里采用左右方向作為嘗試。
添加了一個(gè)isWin函數(shù),用遍歷整個(gè)棋盤(pán)上的有效棋子的方式,來(lái)進(jìn)行勝出判斷。
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); //畫(huà)橫線 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會(huì)在中間點(diǎn)出現(xiàn)兩個(gè)匹配值 { //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(BLACK, true); chessStatus[status_x][status_y]=chessman; repaint(); if(isWin(status_x, status_y, chessStatus)) { System.out.println("WIN!!!!!"); } } } @Override //當(dāng)用戶按下并松開(kāi)鼠標(biāo)按鈕時(shí)發(fā)生 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;//記錄連珠的數(shù)目 //橫向查找 for(int i=0;i =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; } } } //向左側(cè)查找 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; } }
第三步,主模塊不變,運(yùn)行測(cè)試一下我們的算法是否正確
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標(biāo)題 setTitle("單機(jī)五子棋"); Container containerPane =getContentPane(); containerPane.add(drawChessBoard); } public static void main(String[] args) { Main m = new Main(); m.setSize(800, 800); m.setVisible(true); } }
第四步,現(xiàn)在我們一個(gè)方向上的判斷已經(jīng)做好了,接下來(lái)補(bǔ)全其他三個(gè)方向上的判斷代碼
補(bǔ)足DrawChessBoard.java中的isWin()函數(shù)
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); //畫(huà)橫線 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會(huì)在中間點(diǎn)出現(xiàn)兩個(gè)匹配值 { //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(BLACK, true); chessStatus[status_x][status_y]=chessman; repaint(); if(isWin(status_x, status_y, chessStatus)) { System.out.println("WIN!!!!!"); } } } @Override //當(dāng)用戶按下并松開(kāi)鼠標(biāo)按鈕時(shí)發(fā)生 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][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; } } } //向左側(cè)查找 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 =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 =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 =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; } }
再運(yùn)行一下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。