Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  A better way to do this? (I'm sure there is.)  (Read 961 times)
0 Members and 1 Guest are viewing this topic.
Offline DecafJava

Senior Newbie





« Posted 2007-12-14 05:33:39 »

Some of you may remember me from my thread about the "implements" keyword not working. I finally did get my program to work, but it's design isn't so good. I made a vocabulary tester to hone my Spanish skills (I guess it could work with any language) but I think I kind of designed it weird. The way it works is, there are two arrays, one containing Spanish words and the other containing the English equivalent. A random number generator stores an index in a variable arrayInt and then uses that index to pick a random word from the Spanish array. Once something is typed in the text field, the program checks to see if it exists in the English array. If it does, it gets it's index and compares it with the index of the Spanish word. If they match, it's counted as correct and the program continues.

The problem is, though, some Spanish words can mean different things. For example, alfrombra can mean carpet or rug. My program can only test for one of those because of the arrays. I know I probably went about this all wrong, but I just see it as an opportunity to improve. Grin Any help is greatly appreciated. Thanks! (Here's my code. Remember, I probably broke about a million code design rules, but I'm still learning. Smiley)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame implements ActionListener {
   
   
   Random rand = new Random();
   
   ArrayList<String> dictionary = new ArrayList<String>();
   ArrayList<String> englishDict = new ArrayList<String>();
   
   int arrayInt;
   
   JLabel label1 = new JLabel();
   JTextField searchBar = new JTextField(25);
   
   public Test() {
      super("Tester");
     
      setSize(300, 100);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
      searchBar.addActionListener(this);
     
      FlowLayout flo = new FlowLayout();
      setLayout(flo);
     
      add(label1);
      add(searchBar);
     
      // Spanish array
     dictionary.add("casa");

                // English array
               englishDict("house");
     
      arrayInt = rand.nextInt(dictionary.size());
     
      label1.setText(dictionary.get(arrayInt));
     
      setVisible(true);
   }
   
   public void actionPerformed(ActionEvent e)
   {
     
      if (englishDict.indexOf(searchBar.getText()) == (dictionary.indexOf(dictionary.get(arrayInt)))) {
         JOptionPane.showMessageDialog(null, "Correct");
         searchBar.setText(null);
         arrayInt = rand.nextInt(dictionary.size());
         label1.setText(dictionary.get(arrayInt));
      }
     
      else {
         JOptionPane.showMessageDialog(null, "Incorrect, \"" + dictionary.get(arrayInt) + "\" means \"" + englishDict.get(arrayInt) + "\" in English.");
                searchBar.setText(null);
         
      }
     
   }
   
   public static void main(String[] args) {
      try {
         UIManager.setLookAndFeel(new
         com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
      }
      catch (Exception e) {
      }
         
      Test test = new Test();
     
   }
}
Offline cylab

JGO Ninja


Medals: 38



« Reply #1 - Posted 2007-12-14 12:50:19 »

For this kind of one-to-one mapping you could have utilized the HashMap class, which allows for key-value mapping. The most straight forward way to implement your one-to-many mapping would be to use use spanish words as a key and a ArrayList with the english meanings as the value.

Mathias - I Know What [you] Did Last Summer!
Offline DecafJava

Senior Newbie





« Reply #2 - Posted 2007-12-15 08:42:44 »

Thanks. But how do I get random keys? I need to use a random number generator, but I can't because all of my keys are strings.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 38



« Reply #3 - Posted 2007-12-15 14:23:36 »

Ah ok, I see - overlooked this detail. It's a bit unfurtunate, that the Map-interface does not provide a get(position) method. I am afraid you have to use a for loop with the entrySet iterator:

1  
2  
3  
4  
5  
6  
7  
8  
Map<String,ArrayList<String>>.Entry entry;
Iterator it= dictionary.entrySet().iterator();
arrayInt = rand.nextInt(dictionary.size());
for(int i=0; i<arrayInt;i++) entry= it.next();

// now entry contains the spanish word as entry.getKey() and the english word list as entry.getValue()
String spanishWord= entry.getKey();
ArrayList<String> englishMeanings= entry.getValue();


There are however myriad other ways to achieve what you want and if you will have a big dictionary, you might be better of using a database.


Mathias - I Know What [you] Did Last Summer!
Offline SimonH
« Reply #4 - Posted 2007-12-15 14:56:57 »

Why can't you use String.hashCode()?

People make games and games make people
Offline broumbroum

Junior Member





« Reply #5 - Posted 2007-12-16 00:16:43 »

(...)
The problem is, though, some Spanish words can mean different things. For example, alfrombra can mean carpet or rug. My program can only test for one of those because of the arrays. I know I probably went about this all wrong, but I just see it as an opportunity to improve. Grin Any help is greatly appreciated. Thanks! (Here's my code. Remember, I probably broke about a million code design rules, but I'm still learning. Smiley)

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
// (...)
  Random rand = new Random();
   
   ArrayList<String> dictionary = new ArrayList<String>();
   ArrayList<String> englishDict = new ArrayList<String>();
   
   int arrayInt;
   
   JLabel label1 = new JLabel();
   JTextField searchBar = new JTextField(25);
   
   // (...)
     // Spanish array
     dictionary.add("casa");

                // English array
               englishDict("house");
     
      arrayInt = rand.nextInt(dictionary.size());
     
      label1.setText(dictionary.get(arrayInt));
      // (...)

(List).indexOf((String)) is quite correct, I can certify since I'm using the same feature in my apps. But your random must fit the exact array bounds range, which are [0,arrayList.size() - 1]. So the random must be substracted by one unit. Cheesy

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (34 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!