为了提高我的代码效率,我尝试将GUI和游戏玩法分开。代码符合,但当我创建一个新的Game(); Gui正确显示,但当我尝试与它交互时(即更改板上的名称或单击按钮,方法不起作用。
GUI类构造函数:
public GUI()
{
makeFrame();
assignmines();
}
注意:分配地雷和制作帧是此类中唯一的方法。
游戏类:(来自构造函数)
public Game()
{
new GUI();
//UPDATES THE LABELS
updateGamesPlayed() ;
UpdateName();
}
// *********************************GAME CONTROLS************
private void quit()
{
System.exit(0);
}
//RESETS THE BOARD
public void reset()
{
frame.setVisible(false);
String tempName = Game.this.namelabel.getText();
frame.dispose();
Game gb= new Game();
gb.namelabel.setText(tempName);
score = 0;
}
// iNITIATES NEW GAME
public void newGame(){
frame.setVisible(false);
frame.dispose();
new Game();
}
// INCREASE THE ARRAY OF BUTTONS
public void biggerBoard(){
boardsize = 10;
reset();
}
// INCREASES NUMBER OF MINES ASSIGNED TO THE BOARD
public void changeDifficulty(){
numberOfMines = 15;
mine = 15;
minesLeft = 15;
reset();
}
}
// LOSING THE GAME ALERT
public void lose() {
status.setText("You're finished");
gamegoing = false;
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Sorry, you lost");
reset();
}
// WINNING THE GAME ALERT
public void win() {
status.setText("You won!");
gamegoing = false;
JFrame parent = new JFrame();
JOptionPane.showMessageDialog(parent, "Congratualations, you won!");
updateGamesWon();
Game.this.setVisible(false);
Game.this.dispose();
reset();
}
// UPDATING THE SCORE
public void updatescore() {
scorelabel.setText("" + score + " points");
if (100 - score <= 10) win();
}
public void gamesPlayed() {
gamesPlayed.setText("" + noGamesPlayed + " Games Played");
}
//UPDATES THE NAME BASED ON BUTTON CLICK
public void UpdateName() {
saveName.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
playername = nameEnter.getText();
namelabel.setText(playername);
}
});
}
//INCREMENTS THE NUMBER OF GAMES THAT HAVE BEEN PLAYED
public void updateGamesPlayed() {
noGamesPlayed ++;
}
//uPDATES HOW MANY GAMES HAVE BEEN WON
public void updateGamesWon() {
noGamesWon ++;
}
//WHAT VALUES THE CHARACTER HAVE
static char getUserChar(int cellValue) {
if(cellValue == Mine) {
return 'X';
} else if( cellValue == Empty) {
return '+';
} else if (cellValue == Flag || cellValue == FlaggedMine) {
return 'F';
} else if (cellValue == UncoveredMine) {
return 'X';
} else { String adjMines = Integer.toString(cellValue);
return adjMines.charAt(0);
}
}
//METHOD TO DISPLAY HOW MANY MINES AROUND THE BUTTON CLICKED
static int numAdjMines(int[][] mineBoard, int row, int col) {
int numMines = 0;
for(int dr = -1; dr <= 1; dr ++) {
for(int dc = -1; dc <= 1; dc++) {
if(row + dr >= 0 && row + dr < mineBoard.length &&
col+ dc >= 0 && col + dc < mineBoard[0].length) {
if(mineBoard[row+dr][col+dc] == Mine ||
mineBoard[row+dr][col+dc] == FlaggedMine) {
numMines++;
}
}
}
}
return numMines;
}
// TAKES X AND Y VALUE DESCRIBES WHAT HAPPENS ON CLICK
public void click(int row, int col) {
if(mineBoard[row][col] == Mine) {
buttons[row][col].setIcon( new ImageIcon( "images/bomb.gif" ) );
lose();
} else {
score += 1;
updatescore();
buttons[row][col].setText("" + numAdjMines(mineBoard, row, col));
buttons[row][col].setForeground(Color.GREEN);
mineBoard[row][col] = UncoveredEmpty;
//buttons[row][col].setText(Character.toString(getUserChar(mineBoard[row][col])));
if(numAdjMines(mineBoard, row, col) == Empty) {
for(int dr = -1; dr <= 1; dr ++) {
for(int dc = -1; dc <= 1; dc++) {
if(row+dr >= 1 && row+dr < 10 &&
col+dc >= 1 && col+dc < 10) {
if(mineBoard[row+dr][col+dc] == Empty) {
click(row+dr,col+dc);
}
}
}
}
}
}
}
//ACTION WHEN USER CLICKS ON A BUTTON
private class MouseListener extends MouseAdapter {
private int x = 0;
private int y = 0;
public MouseListener(int row, int col) {
this.x = row;
int i = 0;
this.y = col;
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == MouseEvent.BUTTON1) {
if((mineBoard[x][y] == Empty) && (Game.this.gamegoing == true)) {
Game.this.click(x, y);
} else if(mineBoard[x][y] == Mine) {
buttons[x][y].setIcon( new ImageIcon( "images/bomb.gif" ) );
Game.this.lose();
}} else if(e.getButton() == MouseEvent.BUTTON3) {
Game.this.buttons[x][y].setText("F");
}
}
}
}
如何链接GUI和功能?我认为我在构造函数中有错误的逻辑,但我不确定。 (我希望所有相关的代码都在那里,由于长度的原因,我省略了一些字段和方法。)