Java 二维阵列的邻域求法
我有一个75行乘75列的二维网格,我在用户点击时绘制了两种颜色(红色和蓝色)的符号,它工作正常,但我遇到了一个主要问题,我想找到这些符号的邻居:例如,当用户点击一个单元格时,它会打印一个符号(椭圆形),现在我想点击也会检查它的邻居,看看单元格是有人还是空,如果被占用,则使用哪个符号 我发现下面的算法似乎符合需要,但不知道如何适应我的代码:Java 二维阵列的邻域求法,java,algorithm,grid,2d,nearest-neighbor,Java,Algorithm,Grid,2d,Nearest Neighbor,我有一个75行乘75列的二维网格,我在用户点击时绘制了两种颜色(红色和蓝色)的符号,它工作正常,但我遇到了一个主要问题,我想找到这些符号的邻居:例如,当用户点击一个单元格时,它会打印一个符号(椭圆形),现在我想点击也会检查它的邻居,看看单元格是有人还是空,如果被占用,则使用哪个符号 我发现下面的算法似乎符合需要,但不知道如何适应我的代码: private static int[][] array = new int[3][3]; private static void initiali
private static int[][] array = new int[3][3];
private static void initialiseArray() {
int counter = 1;
for (int row = ; row < 3; row++) {
for (int col = ; col < 3; col++) {
array[row][col] = counter;
counter++;
}
}
}
public static void main(String args[]) {
initialiseArray();
for (int row = ; row < 3; row++) {
for (int col = ; col < 3; col++) {
System.out.print(array[row][col]);
if( col == 2 ){
System.out.println("\n");
}
}
}
neighbours();
}
public static void neighbours(){
int posX = 2;
int posY = 2;
for( int row = posX - 1; row <= posX + 1; row++){
for(int col = posY -1; col <= posY + 1; col++){
if( !(posX == row && posY == col) && row >= && col >= && row < 3 && col < 3 ){
System.out.println(array[row][col]);
}
}
}
}
private static int[]array=new int[3][3];
私有静态void initialiseArray(){
int计数器=1;
for(int行=;行<3;行++){
for(int col=;col<3;col++){
数组[行][列]=计数器;
计数器++;
}
}
}
公共静态void main(字符串参数[]){
initialiseArray();
for(int行=;行<3;行++){
for(int col=;col<3;col++){
系统输出打印(数组[行][列]);
如果(列==2){
System.out.println(“\n”);
}
}
}
邻居();
}
公共静态无效邻居({
int-posX=2;
int-posY=2;
对于(int row=posX-1;row=&row<3&&col<3){
System.out.println(数组[行][col]);
}
}
}
}
下面是我的代码,我需要你帮我解决这个问题。对不起打扰你了
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;
private JLabel indexIndicator;
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;
actualiseJeu(actualPlayer, selectedRow, selectedCol);
actualPlayer = (actualPlayer == Token.CERCLE_BLEU)? Token.CERCLE_ROUGE : Token.CERCLE_BLEU;
}
} 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() {
return false;
}
(rowSelected, colSelected) */
public boolean aGagne(Token theSeed, int ligneSelectionnee, int colonneSelectionnee) {
return false;
/* (planche[ligneSelectionnee][colonneSelectionnee] == Jeton.CERCLE_BLEU
&& planche[ligneSelectionnee][colonneSelectionnee - 1] == Jeton.CERCLE_BLEU
&& planche[ligneSelectionnee][colonneSelectionnee + 1] == Jeton.CERCLE_BLEU
|| planche[ligneSelectionnee - 1][colonneSelectionnee] == Jeton.CERCLE_BLEU
&& planche[1][colonneSelectionnee] == theSeed
&& planche[2][colonneSelectionnee] == theSeed
|| ligneSelectionnee == colonneSelectionnee
&& planche[0][0] == theSeed
&& planche[1][1] == theSeed
&& planche[2][2] == theSeed
|| ligneSelectionnee + colonneSelectionnee == 2
&& planche[0][2] == theSeed
&& planche[1][1] == theSeed
&& planche[2][0] == theSeed);*/
}
class DrawCanvas extends JPanel{
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
setBackground(Color.WHITE);
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();
});
}
}
公共最终类Pha扩展JFrame{
公共静态int行=75;
公共静态int COLS=75;
公共静态int cellSize=15;
public static int canvasWidth=cellSize*COLS+(行*4);
public static int canvashheight=单元格大小*行;
公共静态int gridWidth=1;
公共静态int-halfGridWidth=gridWidth/2;
公共静态int cellPadding=cellSize/5;
public static int symbolSize=cellSize-cellPadding*2;
公共静态int symbolStrokeWidth=3;
公共枚举博弈状态{
朱厄,努尔,塞尔克勒·鲁格·加涅,塞尔克勒·布鲁格·加涅
}
私有游戏状态实际状态;
公共枚举令牌{
维德,红血球,蓝血球
}
专用令牌层;
专用令牌[]板;
私人最终画布;
专用JLabel状态栏;
专用JLabel索引指示器;
公共Pha(){
画布=新的DrawCanvas();
canvas.setPreferredSize(新维度(画布宽度、画布高度));
canvas.addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
int x=e.getX();
int y=e.getY();
int selectedRow=y/单元格大小;
int-selectedCol;
selectedCol=x/单元格大小;
如果(实际状态==GameState.JOUE){
如果(selectedRow>=0&&selectedRow=0
&&selectedColfor (int nr = Math.max(0, r - 1); nr <= Math.min(r + 1, board.length - 1); ++nr){
for (int nc = Math.max(0, c - 1); nc <= Math.min(c + 1, board[0].length - 1); ++nc) {
if (!(nr==r && nc==c)) { // don't process board[r][c] itself
// board[nr][nc] is one of board[r][c]'s neighbors
}
}
}
//col , row : representing the cell you want to find neighbors to
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
//function used by neighbours()
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;
}
if(actualState == GameState.JOUE){
if((selectedRow >= 0) && (selectedRow < ROWS) && (selectedCol >= 0)
&& (selectedCol < COLS) &&
(board[selectedRow][selectedCol] == Token.VIDE)){
board[selectedRow][selectedCol] = actualPlayer;
//actualiseJeu(actualPlayer, selectedRow, selectedCol);
actualPlayer = (actualPlayer == Token.CERCLE_BLEU)? Token.CERCLE_ROUGE : Token.CERCLE_BLEU;
//add this to call function :
neighbours(selectedCol, selectedRow);
}
}