Constantin
Constantin

Reputation: 23

java letter game [homework assignment]

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.

Upvotes: 1

Views: 1546

Answers (4)

Adam Mihalcin
Adam Mihalcin

Reputation: 14468

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.

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234847

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).

Upvotes: 1

keyser
keyser

Reputation: 19189

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!

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258628

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.

Upvotes: 1

Related Questions