将单元格的内容放入网格中“监听”最近的单元格

时间:2016-10-14 21:31:19

标签: java swing

我是编程的新手,我需要一些灯光和帮助。我正在开发一种游戏,其中两个玩家必须通过将它们放置在单元格(75x75网格)中来玩令牌(比如红色和蓝色)。目标是通过围绕它们来“捕捉”对手的代币。 (参见图像,这是实际的游戏输出,周围是手工绘制的)

要做到这一点,我需要让令牌“听”到邻居,这意味着网格中的其他单元格。令牌必须在网格中检查自己(它在网格中的位置是什么)并检查是否有另一个令牌靠近它,检查颜色(蓝色或红色)然后,在某些条件下,触发捕获机制< / p>

[![带有令牌的游戏板] [1]] [1]

我在技术上做了什么:

  1. 创建网格(网格/纸板是令牌对象的二维数组。)
  2. 令牌(枚举:EMPTY,BLUE_TOKEN,RED_TOKEN。
  3. currentPlayer 也是令牌。 当用户转动时,他们选择一个他们指向的空单元格。他们将 currentPlayer 放置在单元格 rowSelected,colSelected 的网格/电路板中,然后使用网格/电路板中新添加的单元格重新绘制画布。
  4. 现在我坚持如何让令牌听他们周围的下一个单元格来查看是否有对手或盟友。

    PS:我在这里发布了相同的内容,因为我不尊重规则而被降级(我不知道)

    这是我的代码:

    import javax.swing.JFrame;
        import java.awt.*;
        import java.awt.event.MouseAdapter;
        import java.awt.event.MouseEvent;
        import javax.swing.*;
    
        public class Phagocyte extends JFrame {
    
           public static final int ROWS = 75;  
           public static final int COLS = 75;
    
    
           public static final int CELL_SIZE = 18; 
           public static final int CANVAS_WIDTH = CELL_SIZE * COLS; 
           public static final int CANVAS_HEIGHT = CELL_SIZE * ROWS;
           public static final int GRID_WIDTH = 8;                   
           public static final int GRID_WIDHT_HALF = GRID_WIDTH / 2; 
    
           // Symbols (Blue token/Red Token) are displayed inside a cell with padding from borders
           public static final int CELL_PADDING = CELL_SIZE / 5;
           public static final int SYMBOL_SIZE = CELL_SIZE - CELL_PADDING * 2;
           public static final int SYMBOL_STROKE_WIDTH = 3; 
    
           //This represent the various states of the game
           public enum GameState {
              PLAYING, DRAW, BLUE_TOKEN_WON, RED_TOKEN_WON   //Haven't created a scenario yet
           }
    
           private GameState currentState;  // The current state of the game
    
           //This represent the Tokens and cell contents
           public enum Token {
              EMPTY, BLUE_TOKEN, RED_TOKEN
           }
           private Token currentPlayer;  // The current player (whether red or blue)
    
           private Token[][] board   ; // This is the game board of cells (ROWS by COLS)
           private DrawCanvas canvas; 
           private JLabel statusBar;  
    
           /**The components of the the game and the GUI are setup here */
           public Phagocyte() {
              canvas = new DrawCanvas();  
              canvas.setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
              canvas.addMouseListener(new MouseAdapter() {
                 @Override
                 public void mouseClicked(MouseEvent e) {  
                    int mouseX = e.getX();
                    int mouseY = e.getY();
    
                    // Here to get the row and column that is clicked
                    int rowSelected = mouseY / CELL_SIZE;
                    int colSelected = mouseX / CELL_SIZE;
    
                    if (currentState == GameState.PLAYING) {
                       if (rowSelected >= 0 && rowSelected < ROWS && colSelected >= 0
                             && colSelected < COLS && board[rowSelected][colSelected] == TOKEN.EMPTY) {
                          board[rowSelected][colSelected] = currentPlayer; 
                          updateGame(currentPlayer, rowSelected, colSelected); 
    
                          // Here's to switch player
                          currentPlayer = (currentPlayer == Token.BLUE_TOKEN) ? Token.RED_TOKEN : Token.BLUE_TOKEN;
                       }
                    } else {       
                       initGame();
                    }
                    // Drawing canvas are refresh
                    repaint();
                 }
              });
    
              // Setup the status bar (JLabel) to display status message
              statusBar = new JLabel("  ");
              statusBar.setFont(new Font(Font.DIALOG_INPUT, Font.BOLD, 15));
              statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, 4, 5));
    
              Container cp = getContentPane();
              cp.setLayout(new BorderLayout());
              cp.add(canvas, BorderLayout.CENTER);
              cp.add(statusBar, BorderLayout.NORTH);
    
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              pack();  
              setTitle("Phagocyte by esQmo");
              setVisible(true);
    
              board = new Token[ROWS][COLS];
              initGame();
           }
    
           /** The game board contents and the status are initialised here*/
           public void initGame() {
              for (int row = 0; row < ROWS; ++row) {
                 for (int col = 0; col < COLS; ++col) {
                    board[row][col] = Token.EMPTY;  
                 }
              }
              currentState = GameState.PLAYING; 
              currentPlayer = Token.RED_TOKEN;       
           }
    
           /*this part need some improvements since hasWon and isDraw are not yet definied*/
           public void updateGame(Token theToken, int rowSelected, int colSelected) {
              if (hasWon(theToken, rowSelected, colSelected)) {  
                 currentState = (theToken == Token.RED_TOKEN) ? GameState.RED_TOKEN_WON : GameState.BLUE_TOKEN_WON;
              } else if (isDraw()) {
                 currentState = GameState.DRAW;
              }
    
           }
    
           /** This is supposed to return true if it is a draw (no more empty cell for exemple) */
           /** need to be improved **/
           /* public boolean isDraw() {
              for (int row = 0; row < ROWS; ++row) {
                 for (int col = 0; col < COLS; ++col) {
                    if (board[row][col] == Token.EMPTY) {
                       return false; 
                    }
                 }
              }
              return true;  
           } */
    
           /**Need to implement a Win scenario as well **/
    
           public boolean hasWon(Token theToken, int rowSelected, int colSelected){
            //   
           }
    
          class DrawCanvas extends JPanel {
              @Override
              public void paintComponent(Graphics g) {  
                 super.paintComponent(g);    
                 setBackground(Color.WHITE); 
    
                 //Grid lines
                 g.setColor(Color.LIGHT_GRAY);
                 for (int row = 1; row < ROWS; ++row) {
                    g.fillRoundRect(0, CELL_SIZE * row - GRID_WIDHT_HALF,
                          CANVAS_WIDTH-1, GRID_WIDTH, GRID_WIDTH, GRID_WIDTH);
                 }
                 for (int col = 1; col < COLS; ++col) {
                    g.fillRoundRect(CELL_SIZE * col - GRID_WIDHT_HALF, 0,
                          GRID_WIDTH, CANVAS_HEIGHT-1, GRID_WIDTH, GRID_WIDTH);
                 }
    
                 // This draw the Tokkens in the cells if they are not empty
    
                 Graphics2D g2d = (Graphics2D)g;
                 g2d.setStroke(new BasicStroke(SYMBOL_STROKE_WIDTH, BasicStroke.CAP_ROUND,
                       BasicStroke.JOIN_ROUND));
                 for (int row = 0; row < ROWS; ++row) {
                    for (int col = 0; col < COLS; ++col) {
                       int x1 = col * CELL_SIZE + CELL_PADDING;
                       int y1 = row * CELL_SIZE + CELL_PADDING;
                       if (board[row][col] == Token.RED_TOKEN) {
                          int x2 = (col + 1) * CELL_SIZE - CELL_PADDING;
                          int y2 = (row + 1) * CELL_SIZE - CELL_PADDING;
                          g2d.setColor(Color.RED);
                          g2d.drawOval(x1, y1, SYMBOL_SIZE, SYMBOL_SIZE);
                          g2d.fillOval(x1, y1, SYMBOL_SIZE, SYMBOL_SIZE);
                       } else 
                           if (board[row][col] == Token.BLUE_TOKEN) {
                          g2d.setColor(Color.BLUE);
                          g2d.drawOval(x1, y1, SYMBOL_SIZE, SYMBOL_SIZE);
                          g2d.fillOval(x2, y1, SYMBOL_SIZE, SYMBOL_SIZE);
                       }
                    }
                 }
    
                 // Print status-bar message
                 if (currentState == GameState.PLAYING) {
                    statusBar.setForeground(Color.BLACK);
                    if (currentPlayer == Token.RED_TOKEN) {
                       statusBar.setText("Red, it's your move");
                       statusBar.setForeground(Color.RED);
                    } else {
                       statusBar.setText("Blue, it's your move");
                            statusBar.setForeground(Color.BLUE);
                            statusBar.addMouseMotionListener(null);
                    }
                 } else if (currentState == GameState.DRAW) {
                    statusBar.setForeground(Color.RED);
                    statusBar.setText("It's a draw!");
                 } else if (currentState == GameState.RED_TOKEN_WON) {
                    statusBar.setForeground(Color.RED);
                    statusBar.setText("Red wow!");
                 } else if (currentState == GameState.BLUE_TOKEN_WON) {
                    statusBar.setForeground(Color.BLUE);
                    statusBar.setText("Blue Won! ");
                 }
              }
           }
    
    
            public static void main(String[] args){
                SwingUtilities.invokeLater(() -> {
                    Phagocyte phagocyte = new Phagocyte();
    
              });
           }
    }
    

    由于我的声誉,我无法发布图片:'(

0 个答案:

没有答案
相关问题