Having a problem getting my code to work correctly. I can't get the char array (myArray) to display in the GUI. When it works correctly I have to display the array, if the letters are used next I have to re-display the array with the letters used gone. Continue this until the array of letters is empty.I placed in quotes what the instructions for the assignment. I am currently working with Eclipse.
Develop the Game – based on “Wordmole” Show the user a grid of letters (read from a file). Let the user enter a word using an input dialog. If the word that was entered is valid, then check if all the letters of the word appear in the grid. else use a message dialog to display an error message. If all the letters of the word are in the grid, then award the player the appropriate points for the word, and delete the letters from the gird. else use a message dialog to tell the user the word is not there. Let the player continue playing until The user quits the game by not entering a word or by clicking on cancel. The grid has no more letters in it.
import java.awt.GridLayout;
//import java.util.StringTokenizer;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Project2 {
public static String inputWord;
public static String matrix = "Words\n\n";
public static String total = "Total\n\n";
public static String newWord;
public static char[][] myArray;
public static void main(String[] args){
myArray = fillArray("project2a.txt");
while(true){
int count = 0;
int sum = 0;
inputWord = JOptionPane.showInputDialog(null,"Enter a word:");
if(inputWord == null || inputWord.length() == 0) break;
newWord = inputWord.toUpperCase();
if(IsValidWord(newWord)){
for(int x = 0; x < newWord.length(); x++){
for(int i = 0; i < 10; i ++){
for(int j = 0; j < 10; j++){
if(newWord.charAt(x) == myArray[i][j]) count++;
}
}
}
if(count == newWord.length()){
for(int c = 0; c < newWord.length(); c++)
sum += scoreValue(newWord.charAt(c));
for(int x = 0; x < newWord.length(); x++){
for(int i = 0; i < 10; i ++){
for(int j = 0; j < 10; j++){
if(newWord.charAt(x) == myArray[i][j]) myArray[i][j] = (' ');
}
}
}
matrix = matrix + newWord + "\n";
total = total + sum + "\n";
}
else
JOptionPane.showMessageDialog(null,"The word is not there!!!");
} // IF
else
JOptionPane.showMessageDialog(null,"ERROR: The word is invalid!!!");
} //while
createAndShowGUI();
}
public static char[][] fillArray(String myFile){
TextFileInput tfi = new TextFileInput(myFile);
String line = tfi.readLine();
System.out.println(line);
int rows, cols;
rows = Integer.parseInt(line);
cols = Integer.parseInt(line);
char[][] rtn = new char[rows][cols];
for(int i=0; i < rows; i++) {
line = tfi.readLine();
System.out.println(line);
for(int j= 0; j < cols; j++) {
rtn[i][j] = line.charAt(j);
} // For
} // For
return rtn;
} // FillArray
public static boolean IsValidWord(String myWord){
for(int i = 0; i < myWord.length(); i++)
if(!Character.isLetter(myWord.charAt(i))) return false;
return true;
} // IsValidWord
public static int scoreValue(char letters){
if(Character.isLetter(letters)){
if(letters == 'A' || letters == 'E' || letters == 'I' || letters == 'O' || letters == 'U')
return 0;
else if(letters == 'K' || letters == 'V' || letters == 'F' || letters == 'W') return 5;
else if(letters == 'X' || letters == 'Q') return 10;
} // If statement
return 1;
} // scoreValue
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("Word Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 100,100);//width, height);
frame.setLocation(200,200);//x, y);
frame.setLayout(new GridLayout(1,2));
JTextArea textArea = new JTextArea(5, 20);
JTextArea textArea1 = new JTextArea(5, 20);
textArea.setEditable(false);
textArea1.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
JScrollPane scrollPane2 = new JScrollPane(textArea1);
frame.getContentPane().add(scrollPane);
frame.getContentPane().add(scrollPane2);
textArea.setText(matrix);
textArea1.setText(total);
//Display the window.
frame.pack();
frame.setVisible(true);
}// createAndShowGUI
public static void createAndShowGUI2() {
//Create and set up the window.
JFrame frame = new JFrame("Letters");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize( 100,100);//width, height);
frame.setLocation(200,200);//x, y);
frame.setLayout(new GridLayout(1,2));
JTextArea textArea = new JTextArea(5, 20);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
frame.getContentPane().add(scrollPane);
textArea.setText(myArray);
//Display the window.
frame.pack();
frame.setVisible(true);
}// createAndShowGUI
}