简体   繁体   English

如何在8x8黑白棋(othello)游戏板上涂漆?

[英]How can I paint to my 8x8 reversi(othello) gameboard?

I am creating a reversi game. 我正在创建一个黑白棋游戏。 I decided to create my board using JLabels. 我决定使用JLabels创建我的董事会。 The problem I am having is getting the four starting pieces onto the board. 我遇到的问题是将四个起始部分放到板上。 This is only a 2 player game, and it is 1v1. 这只是一个2人游戏,它是1v1。 At the beginning of the game each player starts off with 2 pieces in the very middle of the board. 在游戏开始时,每个玩家在棋盘的正中间开始有2个棋子。 In my program I go to create 2 blue and 2 red pieces for the 2 players but no matter what I do I can't get the pieces to show up on the game board. 在我的程序中,我为2个玩家创建2个蓝色和2个红色棋子,但是无论如何我都无法将这些棋子显示在游戏板上。 When I ran my debugger it looked like i was never giving each BoardSquare the correct x and y values. 当我运行调试器时,似乎从未给每个BoardSquare正确的x和y值。 I am not sure how to get the x and y values of the BoardSquares. 我不确定如何获取BoardSquares的x和y值。 Any and all help/criticism is welcome. 任何帮助/批评都值得欢迎。 I have been stuck trying to fix this for hours. 我一直被困在试图修复这个问题上几个小时。 Thanks. 谢谢。

public class ReversiRemake {

    public static void main(String[] args) {
        PlayGame game = new PlayGame();
    }
}

class BoardSquare extends JLabel {

    private int x;
    private int y;
    private int row;
    private int column;
    private MyShape circle;
    private Border b = BorderFactory.createLineBorder(Color.BLACK, 2);

    BoardSquare(int row, int column) {
        addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == 1) {

                    }

                }
            });
        this.row = row;
        this.column = column;
        setBorder(b);


    }
    public void setShape(Player p){

        if(p instanceof HumanPlayer){
            circle = new MyShape(x, y, Color.blue); 
        }
        if(p instanceof aiPlayer){
            circle = new MyShape(x, y, Color.red);
        }

    }
    public void setX(int x){
        this.x = x;
    }
    public void setY(int y){
        this.y = y;
    }
    public MyShape getShape() {
        return circle;
    }

    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        circle.drawShape(g2);
    }

}

class MyShape {

    private Color color;
    private int x;
    private int y;

    MyShape(int x, int y, Color color) {
        this.x = x;
        this.y = y;
        this.color = color;
    }

    MyShape(Color color) {
        this.color = color;
    }

    public void drawShape(Graphics2D g) {

        g.setPaint(color);
        g.drawOval(x + 5, y + 2, 50, 50);
        g.fillOval(x + 5, y + 2, 50, 50);

    }

    public void setColor(Player p) {
        if (p instanceof HumanPlayer) {
            color = Color.BLUE;
        }
        if (p instanceof aiPlayer) {
            color = Color.red;
        }
    }
}

class GameBoard extends JPanel {

    BoardSquare[][] BoardSquares = new BoardSquare[8][8];
    private BoardSquare bs;

    GameBoard() {
        for (int row = 0; row < 8; row++) {
            for (int column = 0; column < 8; column++) {
                BoardSquares[row][column] = new BoardSquare(row, column);
                add(BoardSquares[row][column]);

            }
        }
    }

    public void StartingPieces(HumanPlayer p1, aiPlayer p2) {
        for (int row = 0; row < 8; row++) {
            for (int column = 0; column < 8; column++) {
                BoardSquares[row][column] = bs;
                if (row == 3 && column == 3) {
                    bs.setShape(p1);
                }
                if (row == 3 && column == 4) {
                    bs.setShape(p2);
                }
                if (row == 4 && column == 3) {
                    bs.setShape(p2);
                }
                if (row == 4 && column == 4) {
                    bs.setShape(p1);
                }
            }
        }
    }
}

abstract class Player {

    int playerType;
    Color color;
    int score;

    Player(int playerType, Color color, int score) {
        this.playerType = playerType;
        this.color = color;
        this.score = score;
    }

}

class HumanPlayer extends Player {

    HumanPlayer() {
        super(1, Color.BLUE, 0);

    }
}

class aiPlayer extends Player {

    aiPlayer() {
        super(2, Color.RED, 0);
    }

}

class PlayGame {

    private HumanPlayer p1 = new HumanPlayer();
    private aiPlayer p2 = new aiPlayer();
    private GameBoard gameBoard = new GameBoard();
    private boolean p1Turn;
    private boolean p2Turn;

    PlayGame() {
        setStartingPieces();
    }

    public void setStartingPieces() {
        gameBoard.StartingPieces(p1, p2);
    }
}

I couldn't run your code as it is posted, but I believe upon visual inspection the line 发布后我无法运行您的代码,但我相信在进行目视检查时

BoardSquares[row][column] = bs;

should be 应该

bs = BoardSquares[row][column];

As it is written above, a NullPointerException would occur and bs is never actually getting set. 如上文所述,将发生NullPointerException,并且bs从未真正设置。 The revision I have offered would allow the shape to be set in the GameBoard class. 我提供的修订允许在GameBoard类中设置形状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM