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

编程入门 行业动态 更新时间:2024-10-18 16:45:31
本文介绍了如何基于二维数组上的特定位置获取网格单元格的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

考虑一个二维网格,其中包含 n行和 n列(此处为75x75)。在鼠标单击时,每个单元格中都会绘制符号(令牌)。下面的代码用于在单元格内绘制网格线和符号:

class DrawCanvas extends JPanel { @Override public void paintComponent(Graphics g){ super.paintComponent(g); setBackground(Color.WHITE); // Lines g.setColor(Color.BLACK); for(int ligne = 1; ligne< ROWS; ++ ligne){ g.fillRoundRect(0,cellSize * ligne - halfGridWidth,canvasWidth - 1, gridWidth,gridWidth,gridWidth ); } for(int colnene = 1; colnene COLS; ++ colnene){ g.fillRoundRect(cellSize * colnene - halfGridWidth,0 $ b $,gridWidth,canvasHeight - 1, gridWidth,gridWidth); } //符号 Graphics2D g2d =(Graphics2D)g; g2d.setStroke(new BasicStroke(symbolStrokeWidth, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)); (int ligne = 0; ligne 代码如下,我可以找到给定单元格的所有邻居:

private void neighbors(int col,int row){ //找到所有serouding单元格,通过为col和row添加+/- 1 (int colNum = col - 1; colNum< =(col + 1); colNum + = 1) { for(int rowNum = row - 1; rowNum <=(row + 1); rowNum + = 1){ //如果不是中心单元格$ ((colNum == col)&&(rowNum == row))){ //确保它在网格内 if(withinGrid( colNum,rowNum)){ System.out.println(Neighbor of+ col ++ row + - + colNum ++ rowNum); } } } } } //定义由colNum表示的单元格,rowNum在网格内 private boolean withinGrid(int colNum,int rowNum){ if((colNum <0)||(rowNum

考虑单元格内容:

public enum Token { VIDE,CERCLE_BLEU,CERCLE_ROUGE }

现在我想知道是否有方法确定单元格包含的内容:是否为空: Token.VIDE ,具有 Token.CERCLE_BLEU 或者具有 Token.CERCLE_ROUGE。以及我如何实现这一点。

UPDATE:下面是我的代码:

import javax.swing.JFrame; import java.awt。*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing。*; public final class Pha extends JFrame { public static int ROWS = 75; public static int COLS = 75; public static int cellSize = 15; public static int canvasWidth = cellSize * COLS +(ROWS * 4); public static int canvasHeight = cellSize * ROWS; public static int gridWidth = 1; public static int halfGridWidth = gridWidth / 2; public static int cellPadding = cellSize / 5; public static int symbolSize = cellSize - cellPadding * 2; public static int symbolStrokeWidth = 3; public enum GameState { JOUE,NUL,CERCLE_ROUGE_GAGNE,CERCLE_BLEU_GAGNE } $ b $ private GameState actualState; 公共枚举令牌{ VIDE,CERCLE_ROUGE,CERCLE_BLEU } 私有令牌actualPlayer; 私人令牌[] []板; 私人最终DrawCanvas画布; 私有JLabel statusBar; public Pha(){ canvas = new DrawCanvas(); canvas.setPreferredSize(new Dimension(canvasWidth,canvasHeight)); canvas.addMouseListener(new MouseAdapter(){ @Override public void mouseClicked(MouseEvent e){ int x = e.getX( ); int y = e.getY(); int selectedRow = y / cellSize; int selectedCol; selectedCol = x / cellSize; $ b $ (actualState == GameState.JOUE){ if(selectedRow> = 0&& selectedRow< ROWS&&&selectedCol> = 0 b&& amp; amp; selectedCol< COLS&& board [selectedRow] [selectedCol] == Token.VIDE){ board [selectedRow] [selectedCol] = actualPlayer; updateGame (actualPlayer,selectedRow,selectedCol); actualPlayer =(actualPlayer == Token.CERCLE_BLEU)?Token.CERCLE_ROUGE:Token.CERCLE_BLEU; neighbors(selectedRow,selectedCol); } } el se { initGame(); } repaint(); } }); statusBar = new JLabel(); statusBar.setFont(new Font(Font.DIALOG_INPUT,Font.ITALIC,15)); statusBar.setBorder(BorderFactory.createEmptyBorder(2,5,4,5)); 容器cp = getContentPane(); cp.setLayout(new BorderLayout()); cp.add(canvas,BorderLayout.EAST); cp.add(statusBar,BorderLayout.NORTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setTitle(Pha par esQmo); setVisible(true); 板=新令牌[ROWS] [COLS]; initGame(); } public void initGame(){(int intne = 0; ligne< ROWS; ++ ligne) ; colonne COLS; ++ colonne){ board [ligne] [colonne] = Token.VIDE; } } actualState = GameState.JOUE; actualPlayer = Token.CERCLE_ROUGE; $ b $ public void updateGame(Token theSeed,int ligneSelectionnee,int colonneSelectionnee){ if(aGagne(theSeed,ligneSelectionnee,colonneSelectionnee)){ actualState =( theSeed == Token.CERCLE_ROUGE)? GameState.CERCLE_ROUGE_GAGNE:GameState.CERCLE_BLEU_GAGNE; } else if(estNul()){ actualState = GameState.CERCLE_BLEU_GAGNE; $ b public boolean estNul(){ / * for(int row = 0; row< ROWS; ++ row){$ b $如果(board [row] [col] == Token.VIDE){ return false; b为(int col = 0; col< COLS; ++ col){ if } } } * / return false; public boolean aGagne(Token token,int ligneSelectionnee,int colonneSelectionnee){ return false; $ b public void neighbors(int row,int col){ for(int colNum = col - 1; colNum (int rowNum = row_1;rowNum≤=(row + 1); rowNum + = 1){$ b(colNum + = 1){ $($) $ if(!((colNum == col)&&(rowNum == row))){ if(withinGrid(rowNum,colNum)){ System.out.println(Neighbor of+ row ++ col +is+ rowNum ++ colNum); $ b私有布尔内的网格(int colNum,int rowNum){$ b ((colNum <0)||(rowNum <0)){返回false; $ b if ((colNum> = COLS)||(rowNum> = ROWS)){ return false; } if } 返回true; } class DrawCanvas extends JPanel { $ b @Override public void paintComponent(Graphics g){//通过repaint()获得Invoqué super.paintComponent(g); //倒入remplir l'arriere计划 setBackground(Color.WHITE); // Defini la couleur de l'arriere计划 g.setColor(Color.BLACK); for(int ligne = 1; ligne< ROWS; ++ ligne){ g.fillRoundRect(0,cellSize * ligne - halfGridWidth,canvasWidth - 1, gridWidth,gridWidth,gridWidth ); } for(int colnene = 1; colnene COLS; ++ colnene){ g.fillRoundRect(cellSize * colnene - halfGridWidth,0 $ b $,gridWidth,canvasHeight - 1, gridWidth,gridWidth); } Graphics2D g2d =(Graphics2D)g; g2d.setStroke(new BasicStroke(symbolStrokeWidth, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND)); (int ligne = 0; ligne

解决方案

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

System.out.println(Neighbor of+ row ++ col +is+ rowNum + + colNum);

在 neighbors() p>

System.out.println(邻居+行++ col +是+ rowNum ++ colNum + 包含+ board [rowNum] [colNum]);

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

打印出来:

17 19的邻居是16 18包含CERCLE_ROUGE 17的邻居是 17 18包含CERCLE_BLEU 17的邻居是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

Consider a 2D grid with n rows and n columns (here 75x75). The symbols (tokens) are drawn in each cell 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); setBackground(Color.WHITE); //Lines g.setColor(Color.BLACK); for(int ligne = 1; ligne < ROWS; ++ligne){ g.fillRoundRect(0, cellSize * ligne - halfGridWidth, canvasWidth - 1, gridWidth, gridWidth, gridWidth); } for(int colonne = 1; colonne < COLS; ++colonne){ g.fillRoundRect(cellSize * colonne - halfGridWidth, 0 , gridWidth, canvasHeight - 1, gridWidth, gridWidth); } //Symbols Graphics2D g2d = (Graphics2D)g; g2d.setStroke(new BasicStroke(symbolStrokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); for(int ligne = 0; ligne < ROWS; ++ligne){ for(int colonne = 0; colonne < COLS; ++colonne){ int x1 = colonne * cellSize + cellPadding; int y1 = ligne * cellSize + cellPadding; if(board[ligne][colonne] == Token.CERCLE_ROUGE){ g2d.setColor(Color.RED); g2d.drawOval(x1, y1, symbolSize, symbolSize); g2d.fillOval(x1, y1, symbolSize, symbolSize); } else if(board[ligne][colonne] == Token.CERCLE_BLEU){ int x2 = colonne * cellSize + cellPadding; g2d.setColor(Color.BLUE); g2d.drawOval(x1, y1, symbolSize, symbolSize); g2d.fillOval(x2, y1, symbolSize, symbolSize); } } }

With the code below, I can find all neighbors of a 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 ) { for (int rowNum = row - 1 ; rowNum <= (row + 1) ; rowNum +=1 ) { //if not the center cell if(! ((colNum == col) && (rowNum == row))) { //make sure it is within grid if(withinGrid (colNum, rowNum)) { System.out.println("Neighbor of "+ col+ " "+ row + " - " + colNum +" " + rowNum ); } } } } } //define if cell represented by colNum, rowNum is inside grid private boolean withinGrid(int colNum, int rowNum) { if((colNum < 0) || (rowNum <0) ) { return false; //false if row or col are negative } if((colNum >= COLS) || (rowNum >= ROWS)) { return false; //false if row or col are > 75 } return true; }

Consider cells contents:

public enum Token{ VIDE, CERCLE_BLEU, CERCLE_ROUGE }

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

UPDATE: Below is my code:

import javax.swing.JFrame; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; public final class Pha extends JFrame { public static int ROWS = 75; public static int COLS = 75; public static int cellSize = 15; public static int canvasWidth = cellSize * COLS + (ROWS *4) ; public static int canvasHeight = cellSize * ROWS ; public static int gridWidth = 1; public static int halfGridWidth = gridWidth / 2; public static int cellPadding = cellSize / 5; public static int symbolSize = cellSize - cellPadding * 2; public static int symbolStrokeWidth = 3; public enum GameState{ JOUE, NUL, CERCLE_ROUGE_GAGNE, CERCLE_BLEU_GAGNE } private GameState actualState; public enum Token{ VIDE, CERCLE_ROUGE, CERCLE_BLEU } private Token actualPlayer; private Token[][] board; private final DrawCanvas canvas; private JLabel statusBar; public Pha(){ canvas = new DrawCanvas(); canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight)); canvas.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); int selectedRow = y / cellSize; int selectedCol; selectedCol = x / cellSize; if(actualState == GameState.JOUE){ if(selectedRow >= 0 && selectedRow < ROWS && selectedCol >= 0 && selectedCol < COLS && board[selectedRow][selectedCol] == Token.VIDE){ board[selectedRow][selectedCol] = actualPlayer; updateGame(actualPlayer, selectedRow, selectedCol); actualPlayer = (actualPlayer == Token.CERCLE_BLEU)? Token.CERCLE_ROUGE : Token.CERCLE_BLEU; neighbours(selectedRow, selectedCol); } } else { initGame(); } repaint(); } }); statusBar = new JLabel(" "); statusBar.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 15)); statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, 4, 5)); Container cp = getContentPane(); cp.setLayout(new BorderLayout()); cp.add(canvas, BorderLayout.EAST); cp.add(statusBar, BorderLayout.NORTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setTitle("Pha par esQmo"); setVisible(true); board = new Token[ROWS][COLS]; initGame(); } public void initGame(){ for(int ligne = 0; ligne < ROWS; ++ligne){ for(int colonne = 0; colonne < COLS; ++colonne){ board[ligne][colonne] = Token.VIDE; } } actualState = GameState.JOUE; actualPlayer = Token.CERCLE_ROUGE; } public void updateGame(Token theSeed, int ligneSelectionnee, int colonneSelectionnee) { if (aGagne(theSeed, ligneSelectionnee, colonneSelectionnee)) { actualState= (theSeed == Token.CERCLE_ROUGE) ? GameState.CERCLE_ROUGE_GAGNE : GameState.CERCLE_BLEU_GAGNE; } else if (estNul()) { actualState = GameState.CERCLE_BLEU_GAGNE; } } public boolean estNul() { /*for (int row = 0; row < ROWS; ++row) { for (int col = 0; col < COLS; ++col) { if (board[row][col] == Token.VIDE) { return false; } } }*/ return false; } public boolean aGagne(Token token, int ligneSelectionnee, int colonneSelectionnee) { return false; } public void neighbours(int row, int col) { for (int colNum = col - 1 ; colNum <= (col + 1) ; colNum +=1 ) { for (int rowNum = row - 1 ; rowNum <= (row + 1) ; rowNum +=1 ) { if(!((colNum == col) && (rowNum == row))) { if(withinGrid (rowNum, colNum )) { System.out.println("Neighbor of "+ row + " " + col + " is " + rowNum +" " + colNum ); } } } } } private boolean withinGrid(int colNum, int rowNum) { if((colNum < 0) || (rowNum <0) ) { return false; } if((colNum >= COLS) || (rowNum >= ROWS)) { return false; } return true; } class DrawCanvas extends JPanel{ @Override public void paintComponent(Graphics g){ //Invoqué via repaint() super.paintComponent(g); //Pour remplir l'arriere plan setBackground(Color.WHITE); //Defini la couleur de l'arriere plan g.setColor(Color.BLACK); for(int ligne = 1; ligne < ROWS; ++ligne){ g.fillRoundRect(0, cellSize * ligne - halfGridWidth, canvasWidth - 1, gridWidth, gridWidth, gridWidth); } for(int colonne = 1; colonne < COLS; ++colonne){ g.fillRoundRect(cellSize * colonne - halfGridWidth, 0 , gridWidth, canvasHeight - 1, gridWidth, gridWidth); } Graphics2D g2d = (Graphics2D)g; g2d.setStroke(new BasicStroke(symbolStrokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); for(int ligne = 0; ligne < ROWS; ++ligne){ for(int colonne = 0; colonne < COLS; ++colonne){ int x1 = colonne * cellSize + cellPadding; int y1 = ligne * cellSize + cellPadding; if(board[ligne][colonne] == Token.CERCLE_ROUGE){ g2d.setColor(Color.RED); g2d.drawOval(x1, y1, symbolSize, symbolSize); g2d.fillOval(x1, y1, symbolSize, symbolSize); } else if(board[ligne][colonne] == Token.CERCLE_BLEU){ int x2 = colonne * cellSize + cellPadding; g2d.setColor(Color.BLUE); g2d.drawOval(x1, y1, symbolSize, symbolSize); g2d.fillOval(x2, y1, symbolSize, symbolSize); } } } if(actualState == GameState.JOUE){ if(actualPlayer == Token.CERCLE_ROUGE){ statusBar.setText("ROUGE, c'est votre tour"); statusBar.setForeground(Color.RED); } else { statusBar.setText("BLEU, c'est votre tour"); statusBar.setForeground(Color.BLUE); statusBar.addMouseMotionListener(null); } } else if(actualState == GameState.NUL){ statusBar.setForeground(Color.yellow); statusBar.setText("Match nul! Cliquez pour rejouer"); } else if(actualState == GameState.CERCLE_ROUGE_GAGNE){ statusBar.setText("Le jouer X a remporté la partie, cliquez pour rejouer"); statusBar.setForeground(Color.RED); } else if(actualState == GameState.CERCLE_BLEU_GAGNE){ statusBar.setForeground(Color.BLUE); statusBar.setText("Le joueur O a remporté la partie, cliquez pour rejouer"); } } } public static void main(String[] args){ SwingUtilities.invokeLater(() -> { Pha pha = new Pha(); }); } }

解决方案

In board array you store the Token for each cell. To retrieve this information you could change

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

In neighbors() to:

System.out.println("Neighbor of "+ row + " " + col + " is " + rowNum +" " + colNum + " Contains "+ board[rowNum][colNum]);

Clicking the middle cell (17,19):

Prints out:

Neighbor of 17 19 is 16 18 Contains CERCLE_ROUGE Neighbor of 17 19 is 17 18 Contains CERCLE_BLEU 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 19 is 17 20 Contains CERCLE_BLEU Neighbor of 17 19 is 18 20 Contains CERCLE_ROUGE

更多推荐

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

本文发布于:2023-11-29 21:35:40,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   数组   单元格   状态   位置

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!