我目前正在尝试复制游戏反转(对于那些不熟悉的人,这里有一个例子: http: //www.mathsisfun.com/games/reversi.html)。我目前遇到的问题与我尝试更新游戏板有关。我有一个称为板的 2D 数组,当用户单击特定单元格时,板意味着与该玩家一起更新。然而事实并非如此。这是我到目前为止的代码:
/* 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;
}
具体问题与此方法有关:
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);
}
}
编辑*请原谅凌乱!