1

I have a problem with my homework assignment. I'm not asking for any code whatsoever. All I ask for is some advice on how to implement some parts of the assignment.

The assignment is the following: N number of players (n is given by the user) and a 'box' with letters (A-Z, unspecified number of letters or as it states in the problem: unlimited). Each letter has an index. A=0, B=1, .. , Z=25. Each player receives a random letter. The first player who forms a word of size p >= 3 ( p is given by the user ) with the following property: letter's indexes should be in an arithmetic progression. For instance if p=3, ABC or ZXV is a winning combination.

The program should show at each iteration every player's letters. When one player wins, the program should show the player who won and the winning word.

Suggestions are: using Math.random() and StringBuilder for working with strings.

The way I figured the entry point of this program is asking the user to give the number of players. Afterwards I should generate N number of objects (given a class Player). First stupid question: how do I generate N objects. Something like:

for (int i = 0 ; i < n ; i++) {
    Player player1 = new Player();
    //how do I allocate the other objects?
}

Is there a way to assign indexes to letters ? All I could think of is generating random letters with something like:

    Random r = new Random();
    box = new char[9999];
    for (int i = 0; i < 9999; i++){
        box[i]= (char)(r.nextInt(25)+65); //there are 26 letters and A starts on position 65
    }

Any other ideas? Would really appreciate any advice on how to THINK about a solution to this problem.

Bart
  • 19,692
  • 7
  • 68
  • 77
Constantin
  • 23
  • 1
  • 5
  • "and a 'box' with letters (A-Z, unspecified number of letters or as it states in the problem: unlimited)" ... wha? – Brian Roach Mar 04 '12 at 18:48
  • I assume an array with random letters in it, but of a reasonably large size. – Constantin Mar 04 '12 at 18:51
  • You probably don't need an array to actually *hold* all those random letters. Just generate a new one whenever you need one. – Greg Hewgill Mar 04 '12 at 18:53
  • @Greg Hegwill, meaning I allocate a random letter for each player at each iteration and store it in their arrays or what? – Constantin Mar 04 '12 at 19:00
  • That's right. Because the problem says the box you draw from is of "unlimited" size, the probability of drawing the next letter is always the same as randomly picking a letter. This would *not* be true if the box were of a finite size, since one you pick a letter such as `Q`, there will be one fewer `Q` next time (and thus the probabilities would change). – Greg Hewgill Mar 04 '12 at 19:03
  • ok. thanks for the tip ;) it's easier to implement it this way. – Constantin Mar 04 '12 at 19:07

4 Answers4

2

how do I generate N objects

Use a list or array. Most idiomatic Java code uses lists over arrays

List<Player> players = new ArrayList<Player>();
for (int i = 0; i < n; i++) {
    players.add(new Player());
}

Now players contains all the n players, and to reference a specific player k (from 0 to n - 1), you use players.get(k) e.g.

players.get(k).takeTurn();

would call takeTurn on the kth player.

Is there a way to assign indexes to letters?

I'm not quite sure what you mean by this, or why you are generating 9999 characters, but the way that you are generating 9999 random characters now won't work because it will exclude 'Z'. Whenever you call r.nextInt you actually specify 1 beyond the top limit you want. So, to generate 9999 random characters from 'A' to 'Z' you need

Random r = new Random();
box = new char[9999];
for (int i = 0; i < 9999; i++){
    box[i] = (char)(r.nextInt((int)'Z' - (int)'A' + 1) + 'A');
}

which takes advantage of the fact that chars share a lot of characteristics with integral numeric types in Java.

Adam Mihalcin
  • 14,242
  • 4
  • 36
  • 52
  • I'm not quite sure what my teacher actually wants from me. The problem states: "a box with English letters (A-Z, unlimited) with indexes assigned to each letter (i.e. A=0; B=1; .. ; Z=25) . – Constantin Mar 04 '12 at 18:56
  • when using List players = new ArrayList(); for (int i = 0; i < n; i++) { players.add(new Player()); } for example for n = 3, when I try System.out.println(players.size()); it returns 51. any ideas why I have 51 objects instead of 3? thank you! – Constantin Mar 05 '12 at 18:35
  • as far as I understand, AbstractList has a list of 48 default objects. so a solution I found when using another loop, I run the loop up to players.size()-48 . would like a more elengant solution if there is any. thanks – Constantin Mar 05 '12 at 18:47
  • @Constantin No, having 51 objects for n = 3 doesn't make sense at all. AbstractList doesn't have a list of 48 default objects (although it does preallocate some space when you construct a new list, that isn't visible to code that uses ArrayList). If I create a program whose main() method simply creates a new ArrayList and adds three elements, the size of the list is 3 and *not* 51. – Adam Mihalcin Mar 06 '12 at 00:40
1

To create multiple players, you need to use a container:

Vector<Player> players = new Vector<Player>();
for ( int i = 0 ; i < n ; i++ )
    players.add(new Player());

Vector is just an example, there are tons from which you can choose for.

For associating numbers with letters, I would also go with a Vector. Or possibly a HashMap from int to char. But since the integers are consecutive, a Vector would also do it.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

Regarding question 1: you can declare an array of Player objects:

Player[] players;

Then once you know the number of players, you can assign it an array of the appropriate size:

players = new Player[n];

This array is still all null, so you can initialize it with your loop using a subscript:

for (int i = 0; i < n; ++i) {
    players[i] = new Player();
}

(You can also use an ArrayList for this, but there's no advantage to this (other than learning about the collection framework) since you know exactly how many Player objects you need.)

Regarding your second question, there are various techniques. You can define a char array:

char[] chars = { 'A', 'B', /* etc */, 'Z' };

Then you can access each character by subscripting. Or you can define a String:

String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

You can then extract a single character as a char using chars.charAt(i) or as a one-character String with chars.substring(i, i + 1).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

You could put the instances of "Player" into an array (arraylist for dynamic size). Just saw that someone posted code for an arraylist. Check it out!

keyser
  • 18,829
  • 16
  • 59
  • 101