Java游戏板阵列未更新

Java游戏板阵列未更新,java,swing,multidimensional-array,Java,Swing,Multidimensional Array,我目前正在尝试制作游戏reversi的克隆(对于那些不熟悉的人,这里有一个例子:)。我现在的问题是我试图更新游戏板。我有一个称为board的2D数组,当用户点击一个特定的单元格时,board将使用该播放器进行更新。但事实并非如此。以下是迄今为止我掌握的代码: /* Main.java * * this is a simple program that will implement the game of reversi, it will only deal with the game

我目前正在尝试制作游戏reversi的克隆(对于那些不熟悉的人,这里有一个例子:)。我现在的问题是我试图更新游戏板。我有一个称为board的2D数组,当用户点击一个特定的单元格时,board将使用该播放器进行更新。但事实并非如此。以下是迄今为止我掌握的代码:

    /* Main.java
 *
 * this is a simple program that will implement the game of reversi, it will only deal with the game itself and a
 * simple scoreboard.
 */

/** includes **/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Rectangle2D;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JComponent;
import javax.swing.JFrame;

/** classes **/

public class Reverse extends JFrame {

    /** constructors **/
    public Reverse() {
        setSize(648, 670);
        setTitle("Reversi");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(true);
        setLocationRelativeTo(null);

        //Initialize Widget
        widget = new ReversiWidget();
        //Initialize Content Pane
        getContentPane().add(widget);
    }

    /** public functions **/
    public static void main(String[] args) {
        Reverse w = new Reverse();
        w.setVisible(true);
    }

    /** private fields **/
    ReversiWidget widget;   // where the game is being played
}

class ReversiWidget extends JComponent implements MouseListener {

    int width = getWidth();
    int height = getHeight();
    int column = 8;
    int row = 8;


    /** constructors **/
    public ReversiWidget() {
        black = this.black;
        cyan = this.cyan;
        white = this.white;

        //Initialize game state
        initialState();

        //add mouse listener
        addMouseListener(this);
    }

    /** public functions **/

    // method required by MouseListener. not used
    public void mouseClicked(MouseEvent event) {

    }

    // method required by MouseListener. not used
    public void mouseEntered(MouseEvent event) {

    }

    // method required by MouseListener. not used
    public void mouseExited(MouseEvent event) {

    }

    // will react to mouse press events on the widget
    public void mousePressed(MouseEvent event) {
        //get value of left button clicked
        if(event.getButton() == 3){


        }
    }

    // will react to mouse release events on the widget
    public void mouseReleased(MouseEvent event) {
        //determine value for newx and newy
        //get mouse pressed
        int x = event.getX();
        int y = event.getY();
        setX(x);
        setY(y);
        rowSelected = x / sqaure_size;
        colSelected = y / sqaure_size;
        current_player = 1;
        if (rowSelected >= 0 && rowSelected < row && colSelected >= 0 && colSelected < column){
            System.out.println("C Row && C Col: "+rowSelected+"  | "+colSelected);
            System.out.println(oldx+" | "+oldy);
            board[rowSelected][colSelected] = current_player;
            System.out.println("Board Test: "+board[0][0]+", "+board[0][1]+", "+board[0][2]+", "+board[0][3]+", ");
            attemptMove(rowSelected, colSelected, current_player);
        }

    }

    // repaints the widget when an update of any kind is made
    public void paintComponent(Graphics g) {
        //paint background
        //type cast graphics object

        Graphics2D g2d = (Graphics2D) g;
        g2d.setColor(Color.CYAN);
        g2d.fillRect(0,0,640, 640);
        //run draw grid method
        drawGrid(g2d);
        drawPieces(g2d);
    }


    /** private functions **/

    // will take in a position (x,y) a player and will attempt to make a move. if successful then it will place the
    // piece and update the game board.
    private void attemptMove(int x, int y, int player) {

        //repaint();

    }   

    // checks if there is a piece in a given position. returns 0 if empty, -1 if out of bounds, 1 for player 1, and
    // 2 for player 2
    private int checkPiece(int x, int y) {
        return -5;
    }

    // determines if a valid reverse chain can be made from the position (x, y) in the given direction (dx, dy) and a
    // given player
    private boolean determineChain(int x, int y, int dx, int dy, int player) {
        return false;
    }

    // determines if an end game state has been reached. this will happen if there are zero spaces on the board, if one
    // player has lost all of their pieces, or there are no valid moves left for either player
    private boolean determineEndGame() {
        return false;
    }

    // will draw the grid for the game. this assumes a 640 by 640 grid
    private void drawGrid(Graphics2D g2d) {
        //type cast graphics component
        //This is where it gets tricky

                //Here we're going to draw our grid
                int rowCount = width / row;
                int colCount  = height / column;
                int cell = 79;

                //Create Rows
                for(int a=0;a<row;a++){
                    g2d.setColor(Color.BLACK);
                    g2d.drawLine(0, colCount, 638, rowCount);
                    colCount = colCount + cell;
                    rowCount = rowCount + cell;
                }

                colCount = 79;
                rowCount = 79;
                //Create Columns
                for(int b=0;b<column;b++){
                    g2d.setColor(Color.BLACK);
                    //x1,y1  - x2,y2
                    System.out.println("ColCount: "+colCount);
                    g2d.drawLine(colCount, 0, rowCount, 675);
                    colCount = colCount + cell;
                    rowCount = rowCount + cell;

                }

                for (int rows = 0; rows < row; rows++) {
                 for (int col = 0; col < column; col++) {
                    board[row][column] = 0; // all cells empty
                    System.out.print(board[row][column]);
                 }
                 System.out.println();
              }

    }

    // will draw the pieces that are currently on the board. assumes a widget size of 640 square
    private void drawPieces(Graphics2D g2d) {

        int x = getX();
        int y = getY();
        System.out.println("drawPieces: "+x+" | "+y);
        //Traverse the entire board length to see if a move has been made

        //Black Initial Pieces
        g2d.setColor(Color.BLACK);
        g2d.fillOval(316, 237, 78,78);
        g2d.fillOval(238, 317, 78,78);

        //White Initial Pieces
        g2d.setColor(Color.WHITE);
        g2d.fillOval(238, 239, 78,78);
        g2d.fillOval(317, 317, 78,78);
        board[3][3] = 1;
        board[4][3] = 2;
        board[3][4] = 2;
        board[3][3] = 1;
        setBoard(3,3,1);
        setBoard(4,3,1);
        setBoard(3,4,1);
        //setBoard();

        //If move is made draw black piece

                        if(board[rowSelected][colSelected] == 2){
                           g2d.setColor(Color.BLACK);
                           g2d.fillOval(rowSelected, colSelected, 79, 79);
                        }


        //if move is made draw white pieces

                        if(board[rowSelected][colSelected] == 1){
                            g2d.setColor(Color.WHITE);
                            g2d.fillOval(rowSelected, colSelected, 79, 79);   
                        }





    }

    // will initialise the game board to the starting state
    private void initialState() {
        inPlay = true;
    }

    // given a position (x, y) and a player this will determine if there is a valid move to be made at the given
    // position by checking for the availability of a reverse chain in any direction
    private boolean reverseChainAvailable(int x, int y, int player) {
        return false;
    }   

    // given a position (x, y), direction (dx, dy) and a player this will reverse all opponents pieces in a given
    // direction. NOTE: this assumes that determineChain has been used first. method does not perform checks
    private void reversePieces(int x, int y, int dx, int dy, int player) {

    }

    // called at the end of a valid turn this will swap the current players
    private void swapPlayers() {

    }

    // updates the player scores after a piece has been placed
    private void updatePlayerScores() {

    }

    public void setX(int x){this.oldx = x;}
    public void setY(int y){this.oldy = y;}
    public void setBoard(int x, int y, int player){this.board[x][y] = board[x][y] = player;}

    /** private fields **/
    //initial board state
    public int board[][] = new int[9][9];
    int oldx;
    int oldy;   // denotes where the player clicked when he pressed the mouse button
    int current_player;                 // denotes who the current player is
    int player_1_score, player_2_score; // denotes the score each player has in the game thus far
    boolean inPlay;                     // indicates if the game is being played at the moment
    Color black, cyan, white;           // color objects that represent their named colours
    int sqaure_size = 79;
    int rowSelected = oldx / sqaure_size;
    int colSelected = oldy / sqaure_size;
}
/*Main.java
*
*这是一个实现reversi游戏的简单程序,它只处理游戏本身和
*简单的记分牌。
*/
/**包括**/
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.awt.geom.Rectangle2D;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Rectangle;
导入javax.swing.JComponent;
导入javax.swing.JFrame;
/**班级**/
公共类反向扩展JFrame{
/**建设者**/
公共反向{
设置大小(648670);
setTitle(“Reversi”);
setDefaultCloseOperation(关闭时退出);
可设置大小(真);
setLocationRelativeTo(空);
//初始化小部件
widget=newreversiWidget();
//初始化内容窗格
getContentPane().add(小部件);
}
/**公共职能**/
公共静态void main(字符串[]args){
反向w=新反向();
w、 setVisible(真);
}
/**私人领域**/
ReversiWidget;//在哪里玩游戏
}
类ReversiWidget扩展JComponent实现MouseListener{
int width=getWidth();
int height=getHeight();
int列=8;
int行=8;
/**建设者**/
公共ReversiWidget(){
黑色=这个;
青色=这个。青色;
白色=这个;
//初始化游戏状态
初始状态();
//添加鼠标侦听器
addMouseListener(这个);
}
/**公共职能**/
//MouseListener所需的方法。未使用
公共void mouseClicked(MouseEvent事件){
}
//MouseListener所需的方法。未使用
公共无效鼠标事件(鼠标事件){
}
//MouseListener所需的方法。未使用
public void mouseExited(MouseEvent事件){
}
//将对小部件上的鼠标按下事件作出反应
公共无效鼠标按下(鼠标事件){
//获取单击的左按钮的值
if(event.getButton()==3){
}
}
//将对小部件上的鼠标释放事件作出反应
公共无效MouseEvent事件(MouseEvent事件){
//确定newx和newy的值
//按下鼠标
intx=event.getX();
int y=event.getY();
setX(x);
赛蒂(y);
rowSelected=x/sqaure\u尺寸;
colSelected=y/sqaure\U尺寸;
当前_播放器=1;
if(rowSelected>=0&&rowSelected=0&&colSelected
编辑*请原谅混乱!

g2d.Fillova(rowSelected,colSelected,79,79)
将是一个问题,因为
rowSelected
&
colSelected
是电路板阵列的索引,不反映视图/虚拟电路板偏移,这些需要转换以反映图形网格

尝试使用
g2d.fi
public void mouseReleased(MouseEvent event) {
        //determine value for newx and newy
        //get mouse pressed
        int x = event.getX();
        int y = event.getY();
        setX(x);
        setY(y);
        rowSelected = x / sqaure_size;
        colSelected = y / sqaure_size;
        current_player = 1;
        if (rowSelected >= 0 && rowSelected < row && colSelected >= 0 && colSelected < column){
            System.out.println("C Row && C Col: "+rowSelected+"  | "+colSelected);
            System.out.println(oldx+" | "+oldy);
            board[rowSelected][colSelected] = current_player;
            System.out.println("Board Test: "+board[0][0]+", "+board[0][1]+", "+board[0][2]+", "+board[0][3]+", ");
            attemptMove(rowSelected, colSelected, current_player);
        }

    }
for (int y = 0; y < row; y++) {
    for (int x = 0; x < col; x++) {
        if(board[y][x] == 2){
           g2d.setColor(Color.BLACK);
           g2d.fillOval(y * 79, x * 79, 79, 79);
        } else if(board[y][x] == 1){
            g2d.setColor(Color.WHITE);
           g2d.fillOval(y * 79, x * 79, 79, 79);
        }
    }
}