![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Sexy Programmer
|
help filling array randomly, in a way
I have a little problem with an algorthm. I have to make the game Concentration. It's a game in which tiles are placed face-down in rows on a "concentraction board". Each tile contains a string. For each tile, there is exactly one other tile like that, which is also faced down.
The problem I am having is filling the board up randomly with having only 2 of the each tile in the board. I know there has to be a simple way to do it but my mind justs goes blank. Any help fellows? public class Tile
{
private String name;
public Tile(String name)
{
this.name = name;
}
public String toString()
{
return name;
}
}Here is the code for the FooList A FooList is a class that defines a list of strings and a string length. All strings in a FooList have the same length. import java.util.ArrayList;
import java.util.Random;
public class FooList
{
private final int fooLength;
private ArrayList<String> availableFoos;
public FooList(int len)
{
fooLength = len;
availableFoos = new ArrayList<String>();
}
public String getFoo(int x)
{
return availableFoos.get(x);
}
public boolean found(String key)
{
for(int x = 0; x < availableFoos.size(); x++)
{
if(key.equals(availableFoos.get(x)))
return true;
}
return false;
}
public void addFoo(String foo)
{
if(found(foo) || foo.length() != fooLength)
return;
availableFoos.add(foo);
}
public String removeRandomFoo()
{
Random random = new Random();
int max = availableFoos.size() - 1;
int rnd = (random.nextInt() * max);
String foo = availableFoos.get(rnd);
availableFoos.remove(rnd);
return foo;
}
}import java.util.ArrayList;
public class Board
{
private Tile[] gameboard;
private int size;
private int rowlength;
int numberOfTilesFaceUp;
private FooList possibleTileValues;
public Board(int n, FooList list)
{
gameboard = new Tile[n * n];
size = gameboard.length;
numberOfTilesFaceUp = 0;
rowlength = 0;
possibleTileValues = list;
fillBoard();
}
private void fillBoard()
{
//so confused
}
public static void main(String args[])
{ //for debugging purposes
FooList list = new FooList(4);
list.addFoo("cats");
list.addFoo("dogs");
list.addFoo("bird");
list.addFoo("mice");
list.addFoo("fish");
Board board = new Board(4, list);
}
}
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0
![]() |
add every string twice to the list?
|
|
|
|
|
|
#3 |
|
Sexy Programmer
|
yes and randomly and without knowing how many strings are in the FooList, assuming the number is even tho. Also
__________________
I would love to change the world, but they won't give me the source code! |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0
![]() |
I meant: maybe you should just add every string twice to the foolist (in the method addFoo). Then you randomly take the strings out until the list is empty.
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Divide your tiles into two identical sets. Place each set randomly.
EDIT: Woops, missed the above post. Essentially the same thing.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 8:19 PM |
| Combining Values in an array | bigmitch | C++ | 4 | Feb 10th, 2006 7:29 AM |
| How to read unknown total of int data from text file to a 2-dim array in C++? | ladyscarlet99 | C++ | 2 | Sep 9th, 2005 1:01 AM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |
| Converting 1-dimensional array to 2-dimensional array | Tazz_Mission_13 | Java | 6 | Apr 8th, 2005 11:58 AM |