我正在尝试做一个刽子手游戏,我想不出一种将单词存储在数组中并将其与用户输入进行比较的方法。
我使用了一个缓冲阅读器,一个随机发生器,它可以工作,它随机化单词并显示它们。
public static void main(String[] args) {
FileInputStream textFile;
BufferedReader readWords;
// Creates a string array
ArrayList<String> arrayWordList = new ArrayList<String>();
// Read in the text file and randomize the words
try {
textFile = new FileInputStream("dictionary.txt");
readWords = new BufferedReader(new InputStreamReader(textFile));
String line = readWords.readLine();
while (line != null) {
arrayWordList.add(line);
line = readWords.readLine();
}
textFile.close();
} catch (Exception e) {
}
Random rand = new Random();
int arrayIntNumber = rand.nextInt(arrayWordList.size());
// Initialize the GUI
new HangGUI();
System.out.println(arrayWordList.get(arrayIntNumber));
}
我想我应该添加一个 keylistener 并将正确的字母存储在一个 char 或一个字符串数组中,我不确定......
然后我需要将包含正确单词的字符串数组转换为字符串,然后使用我猜的 charAt(i) 将字符串与用户输入的字母进行比较?
这是我为刽子手设计的 GUI 类:
public class HangGUI extends JFrame {
JPanel panel, guessLeftPanel, statusPanel, guessPanel, missPanel;
JLabel guessLeftLabel, guessCountLabel, currentStatus, guess, misses;
JTextField statusText, guessText, missesText;
// Create two arrays
String[] arrayRightWord;
String[] arrayHiddenWord;
String[] getGuess = new String[1];
String missedGuesses = "";
public HangGUI() {
/* GUI */
// Creates the menu bar
JMenuBar menuBar = new JMenuBar();
// Creates the game menu and add it to the game menu
JMenu gameMenu = new JMenu("Game");
menuBar.add(gameMenu);
// Creates the game menu, the items and add it to the menu's
JMenuItem startGame = new JMenuItem("Start");
JMenuItem quitGame = new JMenuItem("Quit");
gameMenu.add(startGame);
gameMenu.add(quitGame);
// Creates the options menu and add it to the menu bar
JMenu optionsMenu = new JMenu("Options");
menuBar.add(optionsMenu);
// Creates a button group consisting of radio buttons and add it to the
// options menu
ButtonGroup radioButtons = new ButtonGroup();
JRadioButton easy = new JRadioButton("Easy");
JRadioButton medium = new JRadioButton("Medium");
JRadioButton hard = new JRadioButton("Hard");
radioButtons.add(easy);
radioButtons.add(medium);
radioButtons.add(hard);
optionsMenu.add(easy);
optionsMenu.add(medium);
optionsMenu.add(hard);
// Creates the main panel
panel = new JPanel(new GridLayout(4, 1, 5, 5));
// Creates the label's and textfield's
// Creates guesses left panel, label and text field
guessLeftPanel = new JPanel(new GridLayout(1, 2, 5, 5));
guessLeftLabel = new JLabel("Guesses left: ");
guessCountLabel = new JLabel();
// guessCountLabel.setText("sss");
// ActionListener that doesn't do anything at the moment
easy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int difficulty = 10;
guessCountLabel.setText("" + difficulty);
}
});
// ActionListener that doesn't do anything at the moment
medium.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int difficulty = 7;
guessCountLabel.setText("" + difficulty);
}
});
// ActionListener that doesn't do anything at the moment
hard.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int difficulty = 5;
guessCountLabel.setText("" + difficulty);
}
});
// Creates Current status panel and texfield
statusPanel = new JPanel(new GridLayout(1, 2, 0, 0));
currentStatus = new JLabel("Current status: ");
statusText = new JTextField();
statusText.setEditable(false);
/*
* createRightWord(arrayRandom());
*
*
* statusText.setText(returnHiddenWordtoString());
*/
// Creates the Guess panel and text field
guessPanel = new JPanel(new GridLayout(1, 2, 0, 0));
guess = new JLabel("Guess: ");
guessText = new JTextField();
guessText.setDocument(new JTextFieldLimit(1));
// Creates the Misses panel and text field
missPanel = new JPanel(new GridLayout(1, 2, 0, 0));
misses = new JLabel("Misses: ");
missesText = new JTextField();
missesText.setEditable(false);
// Make the label's, textfield's, menu and panel visible
add(menuBar);
add(panel);
guessLeftPanel.add(guessLeftLabel);
guessLeftPanel.add(guessCountLabel);
statusPanel.add(currentStatus);
statusPanel.add(statusText);
guessPanel.add(guess);
guessPanel.add(guessText);
missPanel.add(misses);
missPanel.add(missesText);
panel.add(guessLeftPanel);
panel.add(statusPanel);
panel.add(guessPanel);
panel.add(missPanel);
pack();
setVisible(true);
// Creates the frame and sets the menu bar
setSize(400, 200);
setTitle("Hangman");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(menuBar);
/*
* guessHandler guesshandler = new guessHandler();
* guessText.addActionListener(guesshandler);
*/
// Construct a new class that handles Quit's the game
quitHandler quithandler = new quitHandler();
quitGame.addActionListener(quithandler);
// Construct a new class that handles Starting the game
startHandler starthandler = new startHandler();
startGame.addActionListener(starthandler);
}
private class quitHandler implements ActionListener {
public void actionPerformed(ActionEvent quitEvent) {
System.exit(0);
}
}
private class startHandler implements ActionListener {
public void actionPerformed(ActionEvent startEvent) {
guessText.getText();
System.out.println("Hej");
}
}
所以基本上我想知道如何存储文本文件中的单词,我应该使用哪种类型的数组以及如何将用户的一个字母输入与正确单词的一个字符进行比较。