0

So Im trying to make a boggle program. It loads a text file for the word list.
So my question is, How could I allow the user to specify a word and then have the program check if that word is on the game board.

Heres my code so far:

/*
 * Boggle
 */

import java.util.*; //imports scanner and other userful objects
import java.io.*;  //java input/output for file reading

public class BoggleStudent
{
    static String[][]game = new String[4][4];
    static String[]words = new String[5454];  //5454 - 4 letter words

    public static void main(String [] args) {

        Scanner input = new Scanner(System.in);  //instantiate scanner object
        readWords(words);
        makeBoard(game);
        printBoard(game);

    }

    
/*readWords reads in the list of all 4 letter words from words.txt file*/
    public static void readWords(String []words){
        File textFile = new File("words.txt");
        FileReader in;
        BufferedReader readFile;
        String line;
        try{
            in= new FileReader(textFile);
            readFile = new BufferedReader(in);
            for (int i=0; i<words.length; i++){
                line = readFile.readLine();
                words[i]=line;
            }  
            readFile.close();
            in.close();
        }catch(IOException e){  //handles file errors
            System.out.println("Error with input file");
        }
    }

    /*makeBoard creates a game board with random capital letters*/
    public static void makeBoard(String [][]game){
        //The ASCII codes for the letters A to Z are 65 to 90
        //for random integer use formula (90-65+1) + 1
        Random r = new Random();
        int num;
        for (int i=0; i<game.length; i++)
            for (int j=0; j<game[0].length;j++){
                num = r.nextInt(26)+65;  //random int grom 65 to 90
                game[i][j] = "" +(char)num; //cast integer as character
        }


    }

    /* printBoard prints the game board for the user */
    public static void printBoard(String [][]game){
        for (int i=0; i<game.length; i++){
            for (int j=0; j<game[0].length; j++)
                System.out.print(game[i][j]);
            System.out.println();
        }
    }
}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by NormR1
0

How could I allow the user to specify a word and then have the program check if that word is on the game board.

How/where are the words on the game board stored?
Given a String containing the word, you would search the location where the game board words are stored.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.