调用函数并返回正确的char问题

时间:2014-02-20 07:25:14

标签: java class methods client

基本上我创造的是一个简单的游戏,用户定义网格大小和点击框来收集点数,有三个选项奖励= 200分,强盗= -100分和炸弹= -10000(这将是结束比赛)。

我得到的问题是当用户选择一个炸弹出现的盒子时它只会减去100分,就像用户选择强盗一样

BlockHop:

import java.util.*;

public class BlockHop{
  Random rand = new Random();
  private GameItems[][] board;
  public static int score = 0;

  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 num = rand.nextInt(end - start + 1) + start;
        switch (num){
          case 1:
          case 2:
          case 3:
          case 4:
          case 5:
          case 6:
          board[row][col] = new Prize(row, col, 'P');
          break;
          case 7:
          board[row][col] = new Bomb(row, col, 'X');
          break;
          case 8:
          case 9:
          case 10:
          board[row][col] = new Bandit(row, col, 'B');
          break;
        }
      }
    }
  }
  public void play(int row, int col){
    String newID = getLabel(row, col);
    board[row][col].adjustScore(newID.charAt(0));
  }
  public int getScore(){
    return score;
  }
  public String getLabel(int row, int col){
    if ((board[row][col]).equals('P')){
      return "PRIZE!";
    }
    else if ((board[row][col]).equals('X')){
      return "Bomb!";
    }
    else{
      return "Bandit!";
    }
  }
  public boolean isGameOver(){
      return getScore() < 0;
  }
}

GameItems:

public abstract class GameItems {

  private final char ID;
  private final int row;      
  private final int col;    
  public GameItems(){
    row = 0;
    col = 0;
    ID = ' ';
  }
  public GameItems(int newRow, int newCol, char newId){
    row = newRow;
    col = newCol;
    ID = newId;
    }
  boolean equals(char c){
    return c == ID;
    }
  public int adjustScore(char input){
    switch (input){
      case 'P':
        BlockHop.score += 200;
        break;
      case 'X':
        BlockHop.score -= -10000;
        break;
      case 'B':
        BlockHop.score -= 100;
        break;
    }
    return BlockHop.score;
  }
}

BlockHopGUI:

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 );  
      }
    }
  }
  public static void main( String [] args ) {
    int gridSize = Integer.parseInt(
                JOptionPane.showInputDialog( null, "Enter the grid size" ) );
    BlockHopGUI bhg = new BlockHopGUI( gridSize );
    bhg.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  }
}

1 个答案:

答案 0 :(得分:1)

你的adjustScore()函数接受一个字符,并期望它是标记该对象在地图中的字符,即用于炸弹的X,用于强盗的B等等。但是如果你看看你在哪里在play()中调用它并不是你实际发送的内容,你从广场的标签中取出第一个字符。通常这会完全失败,但是因为强盗和炸弹都是以B开头而B是你期待的其中一个角色,它将两者视为强盗。你需要修复它,以便你调用一个返回你期望的char的函数,或者改变你的adjustScore函数来接受整个String标签。

调试的一些常规技巧是查看与问题相关的功能,并仔细检查他们是否正在按照您的预期进行操作,尝试逐步完成程序的操作并记下变量。更改。

希望这有帮助!