溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java實現(xiàn)兩人五子棋游戲(六) 行棋方變換

發(fā)布時間:2020-09-06 05:14:49 來源:腳本之家 閱讀:132 作者:v_xchen_v 欄目:編程語言

之前的兩篇文章:Java實現(xiàn)兩人五子棋游戲(二) 畫出棋盤;Java實現(xiàn)兩人五子棋游戲(三) 畫出棋子;Java實現(xiàn)兩人五子棋游戲(四) 落子動作的實現(xiàn),可以點擊查看。

Java實現(xiàn)兩人五子棋游戲(六) 行棋方變換

前面我們已經(jīng)畫好棋盤和棋子,并可以自由落子,且可以判斷棋盤中是否有五連珠。接下來的工作是,每次落子后交換行棋方(即將下一棋子顏色由白->黑或黑->白),并將勝出判斷增加顏色判斷條件(棋盤上存在五連珠->棋盤上存在同色的五連珠)。

Java實現(xiàn)兩人五子棋游戲(六) 行棋方變換

主模塊Main.java和棋子類Chessman.java不變,我們要對DrawChessBoard.java中的落子函數(shù)mousePressed和五連珠判斷函數(shù)isWin做一下調(diào)整。

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函數(shù),使得每次落子后,都將行棋方讓給對家(即每次落子都改變棋子顏色)

DrawChessBoard.java中的mousePressed函數(shù)部分,只要每次都修改chessColor的值即可。

@Override 
//當用戶按下鼠標按鈕時發(fā)生 
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會在中間點出現(xiàn)兩個匹配值 
        { 
          //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(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!!!!!"); 
    } 
  } 
} 

運行一下

Java實現(xiàn)兩人五子棋游戲(六) 行棋方變換

第二步,將勝出判斷增加顏色判斷條件(棋盤上存在五連珠->棋盤上存在同色的五連珠)

在isWin函數(shù)的判斷條件上,加上對Chessman.getColor的顏色判斷

isWin函數(shù)代碼

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) 
      { 
        int matchColor = chessStatus[i][j].getColor(); 
        //向右側(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+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; 
            }   
          } 
        } 
        //向左側(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-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<ROWS+1;i++) 
  { 
    for(int j=0;j<ROWS+1;j++) 
    { 
      //縱向 
      if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) 
      { 
        int matchColor = chessStatus[i][j].getColor(); 
        //向下查找,左上角為坐標原點,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+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<ROWS+1;i++) 
  { 
    for(int j=0;j<ROWS+1;j++) 
    { 
      //左上 
      if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) 
      { 
        int matchColor = chessStatus[i][j].getColor(); 
        //向下查找,左上角為坐標原點,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-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<ROWS+1;i++) 
  { 
    for(int j=0;j<ROWS+1;j++) 
    { 
      //左下 
      if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) 
      { 
        int matchColor = chessStatus[i][j].getColor(); 
        //向下查找,左上角為坐標原點,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-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實現(xiàn)兩人五子棋游戲(六) 行棋方變換

完整的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<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 
  //當用戶按下鼠標按鈕時發(fā)生 
  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會在中間點出現(xiàn)兩個匹配值 
          { 
            //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; 
          } 
        } 
      } 
       
      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 
  //當用戶按下并松開鼠標按鈕時發(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<ROWS+1;i++) 
    { 
      for(int j=0;j<ROWS+1;j++) 
      { 
        //橫向查找 
        if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) 
        { 
          int matchColor = chessStatus[i][j].getColor(); 
          //向右側(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+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; 
              }   
            } 
          } 
          //向左側(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-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<ROWS+1;i++) 
    { 
      for(int j=0;j<ROWS+1;j++) 
      { 
        //縱向 
        if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) 
        { 
          int matchColor = chessStatus[i][j].getColor(); 
          //向下查找,左上角為坐標原點,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+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<ROWS+1;i++) 
    { 
      for(int j=0;j<ROWS+1;j++) 
      { 
        //左上 
        if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) 
        { 
          int matchColor = chessStatus[i][j].getColor(); 
          //向下查找,左上角為坐標原點,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-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<ROWS+1;i++) 
    { 
      for(int j=0;j<ROWS+1;j++) 
      { 
        //左下 
        if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) 
        { 
          int matchColor = chessStatus[i][j].getColor(); 
          //向下查找,左上角為坐標原點,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-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;   
  } 
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI