我正在尝试创建的程序是一个基本的游戏,用户输入一个网格大小,然后选择块以接收增加分数的奖品,从分数中夺走分数的强盗或炸弹以结束游戏。我收到堆栈流错误,我不知道为什么?
很抱歉,我无法找到大量代码!
这是我收到的堆栈溢出错误。输入网格大小后会发生这种情况(它的长度要长得多,因为您可以看到gameItems,blockHop和奖品不断重复:
java.lang.StackOverflowError
at java.lang.System.nanoTime(Native Method)
at java.util.Random.<init>(Random.java:62)
at BlockHop.<init>(BlockHop.java:12)
at GameItems.<init>(GameItems.java:20)
at Prize.<init>(Prize.java:9)
at BlockHop.<init>(BlockHop.java:27)
at GameItems.<init>(GameItems.java:20)
at Prize.<init>(Prize.java:9)
at BlockHop.<init>(BlockHop.java:27)
at GameItems.<init>(GameItems.java:20)
at Prize.<init>(Prize.java:9)
at BlockHop.<init>(BlockHop.java:27)
at GameItems.<init>(GameItems.java:20)
Block Hop GUI:
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class BlockHopGUI extends JFrame{
private BlockHop bh;
private JButton [][] board;
private JLabel scorePoints;
private PlayHandler ph; // listener for buttons
public BlockHopGUI( int gridSize ) {
super( "Click to uncover prizes" );
bh = new BlockHop( gridSize );
System.out.println( "gridsize: " + gridSize );
Container c = getContentPane( );
JPanel p = new JPanel( );
board = new JButton[gridSize][gridSize];
p.setLayout( new GridLayout( gridSize, gridSize ) );
ph = new PlayHandler( );
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board.length; col++ ) {
board[row][col] = new JButton( "" );
board[row][col].addActionListener(ph);
p.add( board[row][col] );
}
c.add( p, BorderLayout.CENTER );
JPanel scorePanel = new JPanel( );
JLabel scoreLabel = new JLabel( "Score: " );
scorePoints = new JLabel( Integer.toString( bh.getScore( ) ) );
scorePanel.add( scoreLabel );
scorePanel.add( scorePoints );
c.add( scorePanel, BorderLayout.SOUTH );
setSize( 500, 500 );
setVisible( true );
}
private class PlayHandler implements ActionListener {
public void actionPerformed( ActionEvent ae ) {
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board[0].length; col++ ) {
if ( ae.getSource( ) == board[row][col] ) {
bh.play( row, col );
board[row][col].setText( bh.getLabel( row, col ) );
board[row][col].removeActionListener( ph );
break;
}
}
scorePoints.setText( Integer.toString( bh.getScore( ) ) );
if ( bh.isGameOver( ) ) {
JOptionPane.showMessageDialog( null, "Game over! Final points: "
+ bh.getScore( ) );
System.exit( 1 );
}
街舞类:
import java.util.*;
import java.awt.*;
import javax.swing.*;
public class BlockHop {
Random rand = new Random();
private GameItems [] [] board;
int score = 100;
int row;
int col;
public BlockHop () {
board = new GameItems [1][1];
board[0][0] = new Prize( 0, 0, 'P') ;
}
public BlockHop( int gridSize ) {
board = new GameItems [gridSize][gridSize];
int end = 10;
int start = 1;
for ( int row = 0; row < board.length; row++ )
for ( int col = 0; col < board[row].length; col++ ) {
int numAssign = rand.nextInt( end - start + 1 ) + start;
// System.out.print("test");
switch (numAssign) {
case 1: case 2 : case 3 : case 4 : case 5 : case 6 :
board[row][col] = new Prize( row, col, 'P') ;
System.out.print("test");
break;
case 7:
board[row][col] = ( new Bomb( row, col, 'B') );
System.out.print("test");
break;
case 8: case 9 : case 10 :
board[row][col] = ( new Bandit( row, col, 'X') );
System.out.print("test");
break;
}
}
}
public void play( int row, int col ) {
String newID = getLabel(row, col);
//newID.adjustScore()
}
public int getScore( ) {
return score;
}
public String getLabel(int row, int col) {
if ((board[row][col]).equals('P'))
return "p";
else if ((board[row][col]).equals('X'))
return "x";
else
return "b";
}
public boolean isGameOver( ) {
if ( getScore() < 0)
return true;
else
return false;
}
}
游戏项目类别:
import java.awt.*;
import javax.swing.*;
import java.io.*;
public abstract class GameItems extends BlockHop {
private char ID; // block type
private int row; // row
private int col; // column
public GameItems (){
ID = ' ';
row = 0;
col = 0;
}
public GameItems (int newRow, int newCol, char newId) {
row = newRow;
col = newCol;
ID = newId;
}
public int adjustScore(char input) {
switch (input) {
case 'P' :
score += 10;
break;
case 'X':
score -= 5;
break;
case 'B' :
score -= 2000;
break;
}
return score;
}
}
奖项类别:
public class Prize extends GameItems{
public Prize () {
super();
}
public Prize (int row, int col, char id) {
super (row, col, id);
}
}
炸弹等级:
public class Bomb extends GameItems {
public Bomb () {
super();
}
public Bomb (int row, int col, char id) {
super (row, col, id);
}
}
强盗类:
public class Bandit extends GameItems {
public Bandit () {
super();
}
public Bandit (int row, int col, char id) {
super (row, col, id);
}
}
最佳答案
确实是构造函数中的堆栈溢出。
在BlockHop
中,您具有:
public BlockHop () {
board = new GameItems [1][1];
prize = new Prize(...);
}
但是,您将
GameItems
定义为:public abstract class GameItems extends BlockHop {
//...
}
每次构造从
GameItems
继承的类的实例时,它都会构造一个BlockHop
,该Prize
构造一个继承GameItems
的。为什么?因为:public class Prize extends GameItems {
//...
}
因此,现在每次您构造奖品时,它都会构造一个方块跃点,反之亦然。