如何根据二维数组上的特定位置获取网格单元的状态

2024-08-24Java开发问题
3

免费vpn 本文介绍了如何根据二维数组上的特定位置获取网格单元的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧! vpn下载 免费vpn

问题描述

考虑一个具有 n 免费vpn 行n 列(此处为 75x75)的 2D 网格.鼠标单击时在每个单元格中绘制符号(标记).下面的代码用于在单元格内绘制网格线和符号:

Consider 免费vpn下载 a 2D grid with n rows and n columns (here 75x75). The symbols (tokens) are drawn in free vpn each cell 免费vpn下载 on mouse click. The code below is used to draw grid lines and symbols within cells:

class DrawCanvas extends JPanel{

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);
         vpn free    setBackground(Color.WHITE);

            //Lines
            g.setColor(Color.BLACK);
            for(int ligne vpn下载 = 1; ligne < ROWS; ++ligne){
             vpn下载   vpn下载  g.fillRoundRect(0, cellSize * ligne - halfGridWidth, canvasWidth - 1,
 free vpn                  免费vpn       gridWidth, gridWidth, gridWidth);
            }
        免费vpn   vpn下载   for(int colonne = 1; colonne < COLS; ++colonne){
    free vpn   免费vpn下载   vpn下载 免费vpn   vpn下载       g.fillRoundRect(cellSize * colonne - halfGridWidth, 0
     free vpn                    free vpn , gridWidth, canvasHeight - 1,
           vpn下载              gridWidth, gridWidth);
            }

            //Symbols
            Graphics2D g2d = (Graphics2D)g;
  vpn下载   vpn下载 免费vpn下载         vpn free g2d.setStroke(new BasicStroke(symbolStrokeWidth,
  免费vpn                  vpn下载  BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 
    vpn下载         for(int ligne = 0; vpn下载 vpn free ligne < ROWS; ++ligne){
          vpn下载       for(int colonne = 0; colonne < COLS; ++colonne){
      免费vpn           免费vpn     int x1 = colonne * cellSize + cellPadding;
      vpn下载               int y1 vpn下载 = ligne * cellSize + cellPadding;

      vpn free               if(board[ligne][colonne] == Token.CERCLE_ROUGE){
 免费vpn        免费vpn          vpn free       免费vpn  g2d.setColor(Color.RED);
          free vpn             vpn下载   g2d.drawOval(x1, y1, symbolSize, symbolSize);
                  vpn下载       g2d.fillOval(x1, y1, symbolSize, symbolSize);
 免费vpn下载        免费vpn             } else
                 免费vpn        if(board[ligne][colonne] == Token.CERCLE_BLEU){
       vpn free      free vpn                 int x2 = colonne * cellSize + vpn下载 cellPadding;
 免费vpn  vpn free            vpn vpn下载 free                g2d.setColor(Color.BLUE);
   vpn free  免费vpn下载  vpn free                免费vpn下载         vpn下载 g2d.drawOval(x1, y1, symbolSize, symbolSize);
           免费vpn下载          免费vpn下载    免费vpn      g2d.fillOval(x2, y1, vpn free symbolSize, symbolSize);
      vpn free        vpn下载  vpn下载       vpn free     }
       免费vpn          }

           免费vpn 免费vpn下载  }

使用下面的代码,我可以找到给定单元格的所有邻居:

With the code below, I can 免费vpn下载 vpn下载 find all free vpn neighbors of a free vpn given cell:

private void neighbours(int  col, int row) {

     //find all serouding cell by adding +/- 1 to col and row 
    for (int colNum = col - 1 ; colNum <= (col + 1) ; colNum +=1  ) {

  vpn free       for (int rowNum = row - 1 ; 免费vpn free vpn rowNum <= (row + 1) ; rowNum vpn free +=1  ) {

             vpn free //if not the center cell 
     vpn下载        if(! ((colNum == col) && (rowNum == row))) {

                //make sure it is within vpn下载  grid
              vpn free  free vpn  if(withinGrid (colNum, rowNum)) {
         vpn free  vpn free           System.out.println("Neighbor of "+ col+ " "+ row + " - " + colNum vpn下载 +" " + rowNum free vpn );
        免费vpn         }
            }
        }
    }
}

 //define if cell represented by colNum, rowNum is inside grid
private boolean withinGrid(int colNum, int rowNum) {

    if((colNum < 0) || (rowNum <0) ) {
       free vpn  return false;    //false if row or col are negative
    }
    vpn 免费vpn下载 free if((colNum 免费vpn下载 >= COLS) || (rowNum >= ROWS)) 免费vpn {
        return false;    //false if row or col are 免费vpn下载 > 75
    }
   vpn free  return true;
}

考虑单元格内容:

public enum Token{
VIDE, CERCLE_BLEU, CERCLE_ROUGE
}

现在我想知道是否有办法确定单元格包含的内容:是否为空:Token.VIDE、有 Token.CERCLE_BLEU 还是有 vpn下载 Token.CERCLE_ROUGE. 以及如何实现.

Now I want to know if there is a way free vpn to determinate what a cell contains: is it empty: Token.VIDE, has Token.CERCLE_BLEU free vpn or has Token.CERCLE_ROUGE. And how I can achieve that.

更新:以下是我的代码:

UPDATE: Below is my free vpn code:

import javax.swing.JFrame;
import java.awt.*;
import 免费vpn下载 java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;


public final class Pha extends vpn下载 JFrame {

    public static int ROWS = 75;
    public static int COLS = vpn下载 75;


   free vpn  public static int cellSize = 15; 
    public static int canvasWidth = 免费vpn cellSize * COLS + (ROWS *4) ;
    public static int canvasHeight 免费vpn = cellSize * ROWS ; 
    public static int vpn free gridWidth = 1; 
    public static int halfGridWidth = gridWidth free vpn下载 vpn / 2;

    public static int cellPadding = cellSize / 5;
    public static int symbolSize vpn free = cellSize - cellPadding * 2; 
    public static int symbolStrokeWidth = 3; 

    vpn下载 public enum GameState{
        免费vpn JOUE, NUL, CERCLE_ROUGE_GAGNE, CERCLE_BLEU_GAGNE
  免费vpn   vpn free }

    vpn free private GameState actualState;

    public enum Token{
     vpn free    VIDE, CERCLE_ROUGE, CERCLE_BLEU
    }

 vpn free    private Token actualPlayer;

    private Token[][] board;
    private final DrawCanvas canvas; 
   免费vpn下载 vpn free  private JLabel statusBar; 

    public Pha(){

      免费vpn下载   canvas = new vpn下载 DrawCanvas(); 
      free vpn   canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));


        canvas.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) { 
    免费vpn     int free vpn x = e.getX();
     免费vpn    int y = e.getY();

      vpn free   int selectedRow = vpn free y / cellSize;
    vpn下载     int selectedCol;
   vpn下载      免费vpn     selectedCol = x / cellSize;


        if(actualState == GameState.JOUE){
 免费vpn   免费vpn       vpn下载    if(selectedRow >= 0 && selectedRow < ROWS && 免费vpn selectedCol vpn free >= 0
                免费vpn下载     免费vpn && selectedCol < COLS &&
     vpn下载                board[selectedRow][selectedCol] == Token.VIDE){
     vpn下载            board[selectedRow][selectedCol] = actualPlayer; free vpn 
        vpn free         updateGame(actualPlayer, selectedRow, selectedCol); 
        vpn下载    vpn 免费vpn free      actualPlayer = (actualPlayer == Token.CERCLE_BLEU)? Token.CERCLE_ROUGE : Token.CERCLE_BLEU;

            vpn free     neighbours(selectedRow, selectedCol);
      免费vpn下载       }
       免费vpn  } else { 
            initGame(); 
     免费vpn    }

        repaint();
    }

  });

 免费vpn   免费vpn  vpn free statusBar = new JLabel("  ");
    statusBar.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 15));
    statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, free vpn 4, 5));

    Container 免费vpn cp = getContentPane();
    cp.setLayout(new BorderLayout());
    cp.add(canvas, BorderLayout.EAST);
    cp.add(statusBar, BorderLayout.NORTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    pack(); 
    vpn下载 免费vpn setTitle("Pha par esQmo");
    setVisible(true); 

    免费vpn vpn free board = new Token[ROWS][COLS]; 
    initGame();   
}

 免费vpn    public void initGame(){
        for(int vpn下载 ligne = 0; ligne < ROWS; ++ligne){
            for(int colonne = 0; colonne < vpn free vpn下载 COLS; ++colonne){
   vpn free              board[ligne][colonne] = vpn free Token.VIDE; 
 免费vpn            }
        }
     vpn下载    actualState vpn下载 free vpn = GameState.JOUE;
   免费vpn    free vpn   actualPlayer = Token.CERCLE_ROUGE;
    }

 vpn free    public void updateGame(Token vpn下载 theSeed, int vpn free ligneSelectionnee, int colonneSelectionnee) {
      免费vpn if (aGagne(theSeed, ligneSelectionnee, colonneSelectionnee)) {  
    vpn free      actualState= (theSeed == Token.CERCLE_ROUGE) ? GameState.CERCLE_ROUGE_GAGNE : GameState.CERCLE_BLEU_GAGNE;
      } else if (estNul()) { 
         actualState vpn 免费vpn下载 free = GameState.CERCLE_BLEU_GAGNE;   免费vpn     
   免费vpn下载    }

 vpn下载   }
 public boolean estNul() {
      /*for (int row = 0; row < vpn free ROWS; 免费vpn下载 ++row) {
    vpn下载  vpn free     for (int col = 0; col < COLS; ++col) {
            if (board[row][col] == Token.VIDE) {
            免费vpn free vpn  免费vpn下载   return false; 
    vpn free      免费vpn下载   vpn free  }
       免费vpn  free vpn vpn下载 free vpn  }
      }*/
      return false; 
 vpn free   }

   public boolean aGagne(Token 免费vpn下载 token, int ligneSelectionnee, int colonneSelectionnee) {
      return false; 

}


   public void neighbours(int  row, int col) {

 免费vpn下载    for (int colNum = col - 1 ; colNum <= (col + 1) ; colNum +=1  ) {

        for (int rowNum = row - 1 ; rowNum <= (row + 1) ; rowNum +=1  ) {

 免费vpn            if(!((colNum == col) && (rowNum == row))) {

               vpn free  if(withinGrid (rowNum, colNum )) {

                    System.out.println("Neighbor of "+ row + " 免费vpn " + col + " is " + rowNum +" " + colNum );

                }
      免费vpn       }
    免费vpn     }
    }
}

private boolean withinGrid(int colNum, int rowNum) {

    if((colNum < 0) || (rowNum <0) ) {
        return false;
    }
    if((colNum >= COLS) || (rowNum >= ROWS)) {
        return false;
 免费vpn下载    }
    免费vpn return true;
}

class DrawCanvas 免费vpn extends JPanel{

        @Override
        public void paintComponent(Graphics g){ //Invoqué via repaint()
       vpn free  free vpn     super.paintComponent(g); //Pour remplir l'arriere plan
            setBackground(Color.WHITE); //Defini la couleur de l'arriere plan


 vpn free            g.setColor(Color.BLACK);
    vpn free vpn下载       vpn free vpn下载   免费vpn下载 for(int ligne = 1; ligne < ROWS; ++ligne){
      免费vpn下载    免费vpn      免费vpn   g.fillRoundRect(0, cellSize * vpn下载 ligne - halfGridWidth, canvasWidth - 1,
   free vpn                      gridWidth, free vpn gridWidth, gridWidth);
          vpn下载 免费vpn   vpn下载 免费vpn }
            for(int colonne = 1; colonne < COLS; ++colonne){
 免费vpn                g.fillRoundRect(cellSize * colonne - halfGridWidth, 0
         免费vpn               vpn下载  , gridWidth, canvasHeight - 1,
   vpn 免费vpn free           免费vpn            gridWidth, gridWidth);
   免费vpn          }


  免费vpn下载           Graphics2D g2d = (Graphics2D)g;
 vpn下载  免费vpn           g2d.setStroke(new 免费vpn下载 BasicStroke(symbolStrokeWidth,
                    vpn free BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 
            for(int ligne vpn free = 0; ligne < ROWS; ++ligne){
         vpn free     vpn free    for(int colonne = 0; colonne < COLS; ++colonne){
 vpn下载                vpn下载     int x1 = colonne * cellSize + cellPadding;
  vpn下载      免费vpn    免费vpn   free vpn         int y1 = ligne free vpn * cellSize + cellPadding;

    vpn free                 if(board[ligne][colonne] == Token.CERCLE_ROUGE){
                免费vpn下载         g2d.setColor(Color.RED);
          vpn下载               g2d.drawOval(x1, y1, symbolSize, symbolSize);
                  vpn下载       g2d.fillOval(x1, y1, symbolSize, vpn free symbolSize);
                    } else
       vpn下载     免费vpn              if(board[ligne][colonne] == Token.CERCLE_BLEU){
              vpn下载               int x2 = colonne * cellSize + cellPadding;
    免费vpn   vpn下载                       g2d.setColor(Color.BLUE);
     vpn下载                        g2d.drawOval(x1, y1, symbolSize, vpn free symbolSize);
        free 免费vpn下载 vpn    free vpn                  g2d.fillOval(x2, y1, symbolSize, symbolSize);
              免费vpn下载           }
       vpn free      vpn free     }

            }

            if(actualState == GameState.JOUE){
                if(actualPlayer == Token.CERCLE_ROUGE){
                    statusBar.setText("ROUGE, c'est votre tour");
            free vpn vpn free         statusBar.setForeground(Color.RED);

              vpn free   } else {
  vpn下载             vpn free       statusBar.setText("BLEU, c'est votre tour");
                    statusBar.setForeground(Color.BLUE);
                    statusBar.addMouseMotionListener(null);
               免费vpn下载  }
      vpn free    free vpn   免费vpn下载  } else
            vpn下载    免费vpn  if(actualState == GameState.NUL){
 免费vpn下载               vpn下载      statusBar.setForeground(Color.yellow);
         vpn下载    免费vpn         statusBar.setText("Match nul! Cliquez pour rejouer");
                } else
            free vpn         if(actualState == GameState.CERCLE_ROUGE_GAGNE){
     免费vpn      免费vpn         free vpn  免费vpn      statusBar.setText("Le jouer X a remporté la partie, cliquez pour rejouer");
      免费vpn下载     vpn free       vpn下载         statusBar.setForeground(Color.RED);
      vpn free       免费vpn    免费vpn  vpn free     } else
                        if(actualState == GameState.CERCLE_BLEU_GAGNE){
              free vpn             vpn free   vpn下载 statusBar.setForeground(Color.BLUE);
    vpn下载                免费vpn下载      免费vpn vpn下载     statusBar.setText("Le joueur vpn free O a remporté la vpn free partie, cliquez pour rejouer");
             免费vpn下载            }
   vpn下载      }
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(() -> {
       免费vpn      vpn 免费vpn free Pha pha = 免费vpn new Pha();
        });
  free vpn vpn下载   }
}

推荐答案

board 数组中存储每个单元格的 Token.
要检索此信息,您可以更改

In board array you store the Token for each cell.
To 免费vpn下载 免费vpn retrieve this information you vpn下载 could change

System.out.println("Neighbor of "+ row + " vpn下载 " + col + " vpn下载 is " 免费vpn + rowNum vpn free +" " + vpn free vpn下载 colNum );

neighbors()中:

System.out.println("Neighbor of "+ row + " vpn free " + col + " is " + rowNum +" " + colNum +
    vpn下载                免费vpn      " Contains "+ board[rowNum][colNum]);

单击中间单元格 (17,19):

Clicking the middle cell (17,19):

打印出来:

17 19 的邻居是 16 18 包含 CERCLE_ROUGE
17 19 vpn下载 的邻居是17 18 包含 CERCLE_BLEU
17 19 的邻居是 免费vpn 18 18 包含CERCLE_ROUGE
17 19 的邻居是 16 19 包含 CERCLE_BLEU
邻居17 19 是 18 19 包含 CERCLE_BLEU
17 19 的邻居是 16 20包含 CERCLE_ROUGE
17 19 的邻居是 17 20 包含 CERCLE_BLEU
17 19 的邻居是 18 20 包含 CERCLE_ROUGE

Neighbor of 17 19 is 16 18 Contains CERCLE_ROUGE
Neighbor of 17 免费vpn 19 is 17 18 Contains CERCLE_BLEU vpn free
Neighbor of 17 19 is 18 18 Contains CERCLE_ROUGE
Neighbor of 17 19 is 16 19 Contains CERCLE_BLEU
Neighbor of 17 19 is 18 19 Contains CERCLE_BLEU
Neighbor of 17 19 is 16 20 Contains CERCLE_ROUGE
Neighbor of 17 vpn下载 19 is 17 20 Contains CERCLE_BLEU 免费vpn
Neighbor of 17 19 is 18 免费vpn下载 20 免费vpn Contains CERCLE_ROUGE

这篇关于如何根据二维数组上的特定位置获取网格单元的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT vpn下载 using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a vpn下载 report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there vpn下载 any way 免费vpn下载 to detect an RTL language vpn free in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages 免费vpn from DB vpn下载 in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in vpn free Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13