• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Game programming question

bigwilliestyle711

[H]ard|Gawd
Joined
Aug 10, 2001
Messages
1,708
Making a game for a final. I want to make a game where you have a big word like "battlements" and the user has to enter in as many words possible within 60 seconds that they can make from it like "at" "battle" "mate" and so on.

I have most of it working. well kind of. But how should i compare what the user enters to the list of words. I have all the possible words that can be made for each word. So do i just basically compare the users input to what is in the array of possible words? Since the game contains 8 big words, would i need 8 separate arrays for each possible word bank?

im just trying to get this part done and then i will be back later with GUI questions, although i know how to do applets, im trying to make an application which i don't know how to do very well. Ill be back, please respond with whatever you can. Thanks

ps. im not asking you guys to do any work for me, im just asking for a helpful hint. If you wanna do work for me its much appreciated . Just kidding, just help me out a little, point me in the right direction.
 
This is just a suggestion for now, but I would put the big words, and all possible words that can be made from it, in a seperate file that is read at runtime. That way, it's easier to add/change words later on. Probably the best way to do it is to read all possible words into an ArrayList as strings, and itterate through the list looking for the user's entry. You only really need one ArrayList, as the user will only be interacting with one big word at a time. This is yet another area where a seperate file would be handy; you can read in the words as you need 'em. =)

I suppose it would be more efficient to read all the data into a tree, and just do a tree traversal, but I don't think we need to get that fancy with it. =P
 
To me the most obvious way to go would be to create eight arrays, "lists", one for each set of "answers". This way you can match the answers the user gives to a smaller array instead of continually traversing through one large array. Whether or not this is the best way to do it is another question, but that's where I'd start. Hope this helps.
 
I say you use a Hashtable. They are cool :cool:

It's also the most efficient way of doing it. Plus, did I mention Hashtables are cool? :cool:

Also, like Velox suggested, using text files would be the best way to handle things.
 
I like velox's idea. This way you only need to create one array of the words you want to match against.
 
If you're going to stick with an array, I would suggest testing that what the user provides is a possible match (based on the characters in the small word all having a corresponding character in the big word) before scanning an array to see if it is actually a word. Otherwise, someone could provide a pile of invalid gibberish that'd cause a lot of time to be wasted scanning the array for entries that couldn't possibly derive from the big word (z, zz, zzz, etc.). On the plus side, you'd be able to use a single array and KNOW that if you find the word in there when you look for it, it derives from the big word (else someone could provide words derived from the other big words and they'd be accepted).

Also, watch out for duplicate answers provided by the user. That's the first weakness I'd try to exploit if I were testing it.

On a small scale, using an array shouldn't do too much damage (just be careful which big words you use), but if you were attempting to do it on a large scale, you'd have to store the data differently (like in a hashtable) to get acceptable performance.
 
Code:
import jpb.* ;
public class Game2
{
    public static void main(String[] args) {
        int wordMade = 0;
        int wordRem;
        String[] bank = new String[8];
        bank [5] = "BATTLEMENTS";
        bank [0] = "WHAT";
        bank [2] = "COMPUTERS";
        bank [7] = "SPECIFICATION";
        bank [4] = "KEYBOARD";
        bank [3] = "DRAWBRIDGE";
        bank [6] = "ACCELERATION";
        bank [1] = "RACECAR";
        String[] list1 = new String[4];
        list1 [0] = "AT";
        list1 [1] = "HAT";
        list1 [2] = "THAW";
        list1 [3] = "A";
        String[] list2 = new String[13];
        list2 [0] = "RACE";
        list2 [1] = "CARE";
        list2 [2] = "CAR";
        list2 [3] = "ARE";
        list2 [4] = "ACE";
        list2 [5] = "ERA";
        list2 [6] = "EAR";
        list2 [7] = "RARE";
        list2 [8] = "REAR";
        list2 [9] = "RACER";
        list2 [10] = "ACRE";
        list2 [11] = "ARC";
        list2 [12] = "A";
        String[] list3 = new String[17];
        list3 [0] = "KEY";
        list3 [1] = "BOARD";
        list3 [2] = "RED";
        list3 [3] = "RAD";
        list3 [4] = "RAY";
        list3 [5] = "BAR";
        list3 [6] = "DO";
        list3 [7] = "BOA";
        list3 [8] = "BAY";
        list3 [9] = "YARD";
        list3 [10] = "BAD";
        list3 [11] = "BED";
        list3 [12] = "BREAD";
        list3 [13] = "BEAD";
        list3 [14] = "ARE";
        list3 [15] = "ERA";
        list3 [16] = "A";
        String[] list4 = new String[28];
        list4 [0] = "DRAW";
        list4 [1] = "WIG";
        list4 [2] = "BRIDGE";
        list4 [3] = "RIDGE";
        list4 [4] = "WAD";
        list4 [5] = "DIG";
        list4 [6] = "RIG";
        list4 [7] = "RAW";
        list4 [8] = "RAG";
        list4 [9] = "WAR";
        list4 [10] = "BID";
        list4 [11] = "BIG";
        list4 [12] = "BAR";
        list4 [13] = "RAD";
        list4 [14] = "BAD";
        list4 [15] = "DAB";
        list4 [16] = "BREAD";
        list4 [17] = "WEAR";
        list4 [18] = "AWE";
        list4 [19] = "ARE";
        list4 [20] = "EAR";
        list4 [21] = "ERA";
        list4 [22] = "BEAR";
        list4 [23] = "BARE";
        list4 [24] = "DEAR";
        list4 [25] = "DARE";
        list4 [26] = "I";
        list4 [27] = "A";
        String[] list5 = new String[19];
        list5 [0] = "STEM";
        list5 [1] = "ROM";
        list5 [2] = "POT";
        list5 [3] = "SPOT";
        list5 [4] = "ROT";
        list5 [5] = "MET";
        list5 [6] = "SO";
        list5 [7] = "COT";
        list5 [8] = "MUTE";
        list5 [9] = "MORE";
        list5 [10] = "SMORE";
        list5 [11] = "CORE";
        list5 [12] = "PORE";
        list5 [13] = "TORE";
        list5 [14] = "STORE";
        list5 [15] = "SUM";
        list5 [16] = "COME";
        list5 [17] = "TOME";
        list5 [18] = "SOME";
        String[] list6 = new String[38];
        list6 [0] = "TEAL";
        list6 [1] = "SEAL";
        list6 [2] = "MEAT";
        list6 [3] = "BELT";
        list6 [4] = "MELT";
        list6 [5] = "SMELT";
        list6 [6] = "SALT";
        list6 [7] = "MALT";
        list6 [8] = "LET";
        list6 [9] = "LAST";
        list6 [10] = "BLAST";
        list6 [11] = "MEAL";
        list6 [12] = "BATTLE";
        list6 [13] = "BENT";
        list6 [14] = "AT";
        list6 [15] = "TENT";
        list6 [16] = "TENTS";
        list6 [17] = "LEAN";
        list6 [18] = "LAME";
        list6 [19] = "LAB";
        list6 [20] = "BAT";
        list6 [21] = "MET";
        list6 [22] = "MEET";
        list6 [23] = "NEAT";
        list6 [24] = "STEM";
        list6 [25] = "SEAT";
        list6 [26] = "TEN";
        list6 [27] = "BAN";
        list6 [28] = "LENT";
        list6 [29] = "SAT";
        list6 [30] = "MAN";
        list6 [31] = "MEN";
        list6 [32] = "MAT";
        list6 [33] = "LANE";
        list6 [34] = "TASTE";
        list6 [35] = "MATTE";
        list6 [36] = "MATE";
        list6 [37] = "A";
        String[] list7 = new String[49];
        list7 [0] = "RAN";
        list7 [1] = "CAN";
        list7 [2] = "LANE";
        list7 [3] = "CANE";
        list7 [4] = "CAIN";
        list7 [5] = "RAIN";
        list7 [6] = "TEAL";
        list7 [7] = "TORE";
        list7 [8] = "TEEN";
        list7 [9] = "TAR";
        list7 [10] = "RATIO";
        list7 [11] = "RATION";
        list7 [12] = "NOT";
        list7 [13] = "ION";
        list7 [14] = "AT";
        list7 [15] = "IN";
        list7 [16] = "EAR";
        list7 [17] = "ACTION";
        list7 [18] = "ERA";
        list7 [19] = "ARE";
        list7 [20] = "ON";
        list7 [21] = "RAT";
        list7 [22] = "ROT";
        list7 [23] = "COT";
        list7 [24] = "CAR";
        list7 [25] = "NO";
        list7 [26] = "ELECTION";
        list7 [27] = "ALTER";
        list7 [28] = "REAL";
        list7 [29] = "RATE";
        list7 [30] = "CARE";
        list7 [31] = "NEAT";
        list7 [32] = "NEAR";
        list7 [33] = "LEAN";
        list7 [34] = "COLT";
        list7 [35] = "LIT";
        list7 [36] = "ACT";
        list7 [37] = "TAN";
        list7 [38] = "LITE";
        list7 [39] = "RITE";
        list7 [40] = "TEAR";
        list7 [41] = "IT";
        list7 [42] = "ATE";
        list7 [43] = "EAT";
        list7 [44] = "CITE";
        list7 [45] = "ACRE";
        list7 [46] = "ARC";
        list7 [47] = "A";
        list7 [48] = "I";
        String[] list8 = new String[51];
        list8 [0] = "PIN";
        list8 [1] = "PACT";
        list8 [2] = "PEN";
        list8 [3] = "PAN";
        list8 [4] = "SPAN";
        list8 [5] = "SET";
        list8 [6] = "SAT";
        list8 [7] = "PAT";
        list8 [8] = "FACT";
        list8 [9] = "TON";
        list8 [10] = "ONE";
        list8 [11] = "ON";
        list8 [12] = "PAST";
        list8 [13] = "FAST";
        list8 [14] = "AT";
        list8 [15] = "POT";
        list8 [16] = "FICTION";
        list8 [17] = "FACTION";
        list8 [18] = "PIT";
        list8 [19] = "ACTION";
        list8 [20] = "EAT";
        list8 [21] = "NAT";
        list8 [22] = "FAT";
        list8 [23] = "CAT";
        list8 [24] = "COT";
        list8 [25] = "NOT";
        list8 [26] = "SAT";
        list8 [27] = "SIT";
        list8 [28] = "ACT";
        list8 [29] = "SPAT";
        list8 [30] = "SPIT";
        list8 [31] = "SPOT";
        list8 [32] = "TAN";
        list8 [33] = "FAN";
        list8 [34] = "CAN";
        list8 [35] = "SNAP";
        list8 [36] = "ION";
        list8 [37] = "CANE";
        list8 [38] = "PANE";
        list8 [39] = "PAIN";
        list8 [40] = "SANE";
        list8 [41] = "NO";
        list8 [42] = "RATE";
        list8 [43] = "IT";
        list8 [44] = "ATE";
        list8 [45] = "AN";
        list8 [46] = "CAIN";
        list8 [47] = "RITE";
        list8 [48] = "CITE";
        list8 [49] = "I";
        list8 [50] = "A";
        SimpleIO.prompt("Enter Level 1-8 / Enter 0 to Exit: ");
        String command = SimpleIO.readLine().trim();
        if (command.equalsIgnoreCase("1")) {
            wordRem = 4;
            System.out.println("Your word is : " + bank[0]);
            do {
                SimpleIO.prompt("Enter word : ");
                String userGuess = SimpleIO.readLine().trim();
                for (int j = 0; j < 4 ; j++) {
                    if (userGuess.equals(list1[j])) {
                        wordMade = wordMade+1;
                        System.out.println("Word made: " + wordMade);
                        wordRem = wordRem-1;
                        System.out.println("Word remaining: " + wordRem);
                    }
                }
            }
            while (wordRem > 0);
            System.out.println(wordMade);
        }
        else if (command.equalsIgnoreCase("2")) {
            wordRem = 13;
            System.out.println("Your word is : " + bank[1]);
            do {
                SimpleIO.prompt("Enter word : ");
                String userGuess = SimpleIO.readLine().trim();
                for (int j = 0; j < 13 ; j++) {
                    if (userGuess.equals(list2[j])) {
                        wordMade = wordMade+1;
                        System.out.println("Word made: " + wordMade);
                        wordRem = wordRem-1;
                        System.out.println("Word remaining: " + wordRem);
                    }
                }
            }
            while (wordRem > 0);
            System.out.println(wordMade);
        }
        else if (command.equalsIgnoreCase("3")) {
            wordRem = 17;
            System.out.println("Your word is : " + bank[2]);
            do {
                SimpleIO.prompt("Enter word : ");
                String userGuess = SimpleIO.readLine().trim();
                for (int j = 0; j < 17 ; j++) {
                    if (userGuess.equals(list3[j])) {
                        wordMade = wordMade+1;
                        System.out.println("Word made: " + wordMade);
                        wordRem = wordRem-1;
                        System.out.println("Word remaining: " + wordRem);
                    }
                }
            }
            while (wordRem > 0);
           System.out.println(wordMade);
        }
        else if (command.equalsIgnoreCase("4")) {
            wordRem = 28;
            System.out.println("Your word is : " + bank[3]);
            do {
                SimpleIO.prompt("Enter word : ");
                String userGuess = SimpleIO.readLine().trim();
                for (int j = 0; j < 28 ; j++) {
                    if (userGuess.equals(list4[j])) {
                        wordMade = wordMade+1;
                        System.out.println("Word made: " + wordMade);
                        wordRem = wordRem-1;
                        System.out.println("Word remaining: " + wordRem);
                    }
                }
            }
            while (wordRem > 0);
            System.out.println(wordMade);
        }
        else if (command.equalsIgnoreCase("5")) {
            wordRem = 19;
            System.out.println("Your word is : " + bank[4]);
            do {
                SimpleIO.prompt("Enter word : ");
                String userGuess = SimpleIO.readLine().trim();
                for (int j = 0; j < 19 ; j++) {
                    if (userGuess.equals(list5[j])) {
                        wordMade = wordMade+1;
                        System.out.println("Word made: " + wordMade);
                        wordRem = wordRem-1;
                        System.out.println("Word remaining: " + wordRem);
                    }
                }
            }
            while (wordRem > 0);
            System.out.println(wordMade);
        }
        else if (command.equalsIgnoreCase("6")) {
            wordRem = 38;
            System.out.println("Your word is : " + bank[5]);
            do {
                SimpleIO.prompt("Enter word : ");
                String userGuess = SimpleIO.readLine().trim();
                for (int j = 0; j < 38 ; j++) {
                    if (userGuess.equals(list6[j])) {
                        wordMade = wordMade+1;
                        System.out.println("Word made: " + wordMade);
                        wordRem = wordRem-1;
                        System.out.println("Word remaining: " + wordRem);
                    }
                }
            }
            while (wordRem > 0);
            System.out.println(wordMade);
        }
        else if (command.equalsIgnoreCase("7")) {
            wordRem = 49;
            System.out.println("Your word is : " + bank[6]);
            do {
                SimpleIO.prompt("Enter word : ");
                String userGuess = SimpleIO.readLine().trim();
                for (int j = 0; j < 49 ; j++) {
                    if (userGuess.equals(list7[j])) {
                        wordMade = wordMade+1;
                        System.out.println("Word made: " + wordMade);
                        wordRem = wordRem-1;
                        System.out.println("Word remaining: " + wordRem);
                    }
                }
            }
            while (wordRem > 0);
            System.out.println(wordMade);
        }
        else if (command.equalsIgnoreCase("8")) {
            wordRem = 51;
            System.out.println("Your word is : " + bank[7]);
            do {
                SimpleIO.prompt("Enter word : ");
                String userGuess = SimpleIO.readLine().trim();
                for (int j = 0; j < 51 ; j++) {
                    if (userGuess.equals(list8[j])) {
                        wordMade = wordMade+1;
                        System.out.println("Word made: " + wordMade);
                        wordRem = wordRem-1;
                        System.out.println("Word remaining: " + wordRem);
                    }
                }
            }
            while (wordRem > 0);
            System.out.println(wordMade);
        }
        else if (command.equalsIgnoreCase("0")) {
            System.out.println("Your lose for quitting");
        } else {
            System.out.println("Command was not recognized; please try again.");
        }
        System.out.println();
    }
}

Thats the code for now. I know how bad it is. I have it all in main, but it works, sorta. I still need to check for a person's entry because they can continuously enter one word and get it to keep removing words from the list. I have to figure out to check for invalid entries. It checks them and just skips over them if they aren't in the array which is good but i want to display how many words are still remaining so a person knows they got it wrong. After that i want to try to add a 60 second timer to the game but i have no idea how to because we never learned it, but i really really want to have a timer make the game a little more challenging. After that i will try to hit the graphics part. I took the ap test and while i did AP our teacher taught the kids that didn't take the test GUI so i have no idea how to do GUI at all. This is my background for the game
groundofback.jpg


i have no idea how im going to do it. Any suggestions? thanks
 
Suggestions:

-Don't waste your sanity trying to do a fancy GUI in Java until everything else works perfectly.

-Store words made in an array and check that array after a match to make sure the match hasn't already been made.
 
i wish i could remember how to do it, ive been trying to make objects and everytime i try i get an error saying that classname is not compatible with string. So i have to cast it right?
 
Doing a linear search through an array feels wrong to me. But if you're afraid the instructor will penalize you for not limiting yourself to only the things taught in class you don't really have much of a choice.
 
[Nemezer]
> I say you use a Hashtable. They are cool
> It's also the most efficient way of doing it.

This is incorrect.

The most efficient data structure for determining whether a given word is in a dictionary is a trie. Querying whether a word of length M is in the dictionary then takes O(M) time, regardless of how many words are in the dictionary or how long they are. Hash tables do not offer such a nice worst-case guarantee.
 
Back
Top