初学者:无法完成游戏

时间:2016-01-13 00:22:52

标签: java

我在整理这个“穷人”刽子手时遇到了一些麻烦,这是我班上的一部分。我已经完成了它,但我有一件事阻止我完成。

我的问题是,我想我的

nextGuess = nextGuess.concat(String.valueOf( guess.charAt(i) ));

正在用空格替换我的“*”占位符。这个词必须用星号掩盖并间隔开,所以“披萨”将是“* * * * *”。

然而,当按下一个字母时,单词中的某些字符会从游戏区域中消失。有任何想法吗?

 package hang;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Hang {


    JButton stopButton    = null;
    JButton startButton = null;

    JLabel    gameField    = null;
    JLabel    messageArea = null;
    java.util.List keysArray= new ArrayList();
    Iterator keyIterator = null;

    boolean reset        = true;
    boolean disable      = false;





    String[] answerWord = {
        "pizza"
        };


    String guess;
    String currentWord;
    String newLetter;
    int numberWrong       = 0;

    int next              = 0;

    public void setUpNewGame() {
        numberWrong = 0;



        Iterator keyIterator = keysArray.iterator();
        while( keyIterator.hasNext() ) {
            ( (JButton)keyIterator.next() ).setEnabled( reset );
        }


        startButton.setEnabled( disable );


        gameField.setBackground(Color.lightGray);


        double numb = Math.random();
        next = (int)( numb * answerWord.length );
        currentWord  = answerWord[next];


        guess = "*";

        for( int i=0; i<currentWord.length()-1; i++) {
            guess = guess.concat(" " + "*");
        }
        gameField.setText( guess );



    }





    public void processAnswer(String answer) {         // Have Vanna turn the correct letters over
        char newCharacter = answer.charAt(0);


        String nextGuess    = "";
        boolean foundAMatch = false;
        for( int i=0; i<currentWord.length(); i++ ) {
            char characterToMatch = currentWord.charAt(i);
            if( characterToMatch == newCharacter ) {
                nextGuess = nextGuess.concat(String.valueOf(newCharacter));
                foundAMatch = true;
            }
            else {
                nextGuess = nextGuess.concat(String.valueOf
                                                  ( guess.charAt(i) ));

            }
        }
        guess = nextGuess;
        gameField.setText( guess );


        if( guess.equals( currentWord ) ) {

            Iterator keyIterator = keysArray.iterator();
            while( keyIterator.hasNext() ) {
                ( (JButton)keyIterator.next() ).setEnabled( disable );
            }

            startButton.setEnabled( reset );
            stopButton.setEnabled( reset );
        }

        else {
            if( !foundAMatch ) {
                numberWrong++;



            }

        }
    }

public Component wordPane() {
        JPanel pane = new JPanel();
        pane.setLayout( new BoxLayout( pane, BoxLayout.X_AXIS ) );
        pane.setBorder( BorderFactory.createEmptyBorder(0, 10, 10, 10) );
        pane.add(Box.createHorizontalGlue() );
        gameField = new JLabel("Welcome to Hangman");
        gameField.setFont( new Font("Helvetica", Font.PLAIN, 24) );
        gameField.setBackground(Color.lightGray);
        gameField.setForeground(Color.black);
        pane.add(gameField);
        pane.add(Box.createHorizontalGlue() );
        return pane;
    }




    public Component keyboardPane() {
        ActionListener alphabetButtonAction = new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                JButton buttonPushed = (JButton)e.getSource();
                buttonPushed.setEnabled( disable );
                processAnswer( buttonPushed.getText() );
            }
        };

        JPanel pane = new JPanel();
        pane.setBorder(BorderFactory.createLoweredBevelBorder());
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c  = new GridBagConstraints();
        pane.setLayout( gridbag );
        c.fill = GridBagConstraints.BOTH;


        JButton button = new JButton( "a" );

        c.gridx      = 0;
        c.gridy      = 0;
        c.gridheight = 1;

        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "b" );

        c.gridx      = 1;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "c" );

        c.gridx      = 2;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "d" );

        c.gridx      = 3;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "e" );

        c.gridx      = 4;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "f" );

        c.gridx      = 5;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );


        button = new JButton( "g" );

        c.gridx      = 6;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "h" );

        c.gridx      = 7;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "i" );

        c.gridx      = 8;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "j" );

        c.gridx      = 9;
        c.gridy      = 0;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "k" );

        c.gridx      = 0;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "l" );

        c.gridx      = 1;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "m" );

        c.gridx      = 2;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "n" );

        c.gridx      = 3;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "o" );

        c.gridx      = 4;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "p" );

        c.gridx      = 5;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "q" );

        c.gridx      = 6;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "r" );

        c.gridx      = 7;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "s" );

        c.gridx      = 8;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "t" );

        c.gridx      = 9;
        c.gridy      = 1;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "u" );

        c.gridx      = 2;
        c.gridy      = 3;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "v" );

        c.gridx      = 3;
        c.gridy      = 3;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "w" );

        c.gridx      = 4;
        c.gridy      = 3;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "x" );

        c.gridx      = 4;
        c.gridy      = 3;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "y" );

        c.gridx      = 5;
        c.gridy      = 3;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        button = new JButton( "z" );

        c.gridx      = 6;
        c.gridy      = 3;
        gridbag.setConstraints( button, c );
        button.addActionListener( alphabetButtonAction );
        pane.add( button );
        keysArray.add( button );

        keyIterator = keysArray.iterator();
        while( keyIterator.hasNext() ) {
            ( (JButton)keyIterator.next() ).setEnabled( disable );
        }
        return pane;
    }





    public Component menuPane() {
        ActionListener controlButtonListener = new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                JButton buttonPushed = (JButton)e.getSource();
                if( buttonPushed.getText().equals("Start") ) {
                    setUpNewGame();
                }
                else {
                    System.exit(0);
                }
            }//actionPerformed
        };//controlButtonListener

        JPanel pane = new JPanel();
        pane.setBorder(BorderFactory.createLoweredBevelBorder());
        pane.setLayout( new BoxLayout( pane, BoxLayout.Y_AXIS ) );

        startButton = new JButton( "Start" );
        startButton.setFont( new Font("Helvetica", Font.PLAIN, 18) );
        startButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        startButton.addActionListener( controlButtonListener );
        pane.add( startButton );

        pane.add( Box.createVerticalGlue() );

        stopButton = new JButton( "Stop" );
        stopButton.setFont( new Font("Helvetica", Font.PLAIN, 18) );
        stopButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        stopButton.addActionListener( controlButtonListener );
        pane.add( stopButton );


        return pane;
    }




    public Component createComponents() {
        JPanel pane = new JPanel();
        pane.setBorder(BorderFactory.createLoweredBevelBorder());
        pane.setLayout(new BorderLayout() );
        pane.add( wordPane(),  BorderLayout.NORTH );
    pane.add( keyboardPane(),   BorderLayout.CENTER );
        pane.add( menuPane(),   BorderLayout.SOUTH );

        return pane;
    }



    public static void main(String[] args) {
    String lookAndFeel;

    lookAndFeel=UIManager.getCrossPlatformLookAndFeelClassName();
    if ( args.length == 1 )
    {
        if ( args[0].equals("motif") )
           lookAndFeel =
            "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
        if ( args[0].equals("metal") )
           lookAndFeel =
            "javax.swing.plaf.metal.MetalLookAndFeel";
        else if ( args[0].equals("system") )
           lookAndFeel=
            UIManager.getSystemLookAndFeelClassName() ;
    }
        try {
            UIManager.setLookAndFeel( lookAndFeel);
        } catch (Exception e) { }

        //Create the top-level container and add contents to it.
        JFrame frame = new JFrame("Hangman");
        frame.setSize( new Dimension(800,480) );
        Hang game = new Hang();
        Component contents = game.createComponents();

        frame.getContentPane().add(contents);


        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

       frame.pack();
        frame.setVisible(true);
    }
}

1 个答案:

答案 0 :(得分:1)

这是将模型/数据与演示文稿/视图混合的典型示例

console.log(data.affectedColumns[0].name); 有更多字符,然后guess,但您尝试根据currentWord的索引将guess中的值连接到其中......这是不匹配的。

相反,我只是在currentWordguess之间保持1-1关系,因此它们具有相同数量的字符,只是改变它的显示方式......

因此,当您致电currentWord时,您需要先格式化文字,例如gameField.setText(guess);

gameField.setText(pad(guess));可能看起来像......

pad

只是在每个角色之间注入一个空格。

这也意味着当你这样做时

protected String pad(String value) {
    StringJoiner sj = new StringJoiner(" ");
    for (char c : value.toCharArray()) {
        sj.add(Character.toString(c));
    }
    return sj.toString();
}

您实际上会获得有效的结果(因为if (guess.equals(currentWord)) { 不等于p i z z a:P)