我正在尝试创建的程序是一个基本游戏,用户输入一个网格大小,选择块接收加入得分的奖品,一个从分数中拿走积分的强盗或炸弹结束游戏。我收到堆栈流错误,我无法弄清楚为什么?
很抱歉我发现大量代码无法找到问题!
这是我收到的堆栈溢出错误。输入网格大小后会发生这种情况(它比你看到的游戏项目,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 );
}
Block Hop Class:
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);
}
}
答案 0 :(得分:5)
它确实是构造函数中的堆栈溢出..
在BlockHop
中你有:
public BlockHop () {
board = new GameItems [1][1];
prize = new Prize(...);
}
但是您将GameItems
定义为:
public abstract class GameItems extends BlockHop {
//...
}
每次构造一个继承自GameItems
的类的实例时,它构造一个BlockHop
构造一个继承Prize
的{{1}}。为什么?这是因为:
GameItems
所以现在每次构建一个奖项时,它都会构造一个块跳,反之亦然。
答案 1 :(得分:1)
StackOverflowError本身告诉你程序有什么问题,如果你看到这些行正在制作一个模式
at Prize.<init>(Prize.java:9)
at BlockHop.<init>(BlockHop.java:27)
at GameItems.<init>(GameItems.java:20)
如果你检查这些行。所有这些都发生在这些类的对象创建时,清楚地表明for creation of GameItems you need BlackHop creation
和for BlackHop you need Prize Creation
以及for Prize you need GameItems creation
。这是由于您的继承层次结构。因此发生了堆栈溢出错误。