Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   help filling array randomly, in a way (http://www.programmingforums.org/showthread.php?t=11505)

ReggaetonKing Oct 5th, 2006 11:57 AM

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;
  }
}

Here's the Board class.
:

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);
  }
}


Gilward Kukel Oct 5th, 2006 12:12 PM

add every string twice to the list?

ReggaetonKing Oct 5th, 2006 12:22 PM

Quote:

Originally Posted by Gilward Kukel (Post 115890)
add every string twice to the list?

yes and randomly and without knowing how many strings are in the FooList, assuming the number is even tho. Also

Gilward Kukel Oct 5th, 2006 12:29 PM

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.

DaWei Oct 5th, 2006 12:29 PM

Divide your tiles into two identical sets. Place each set randomly.
EDIT: Woops, missed the above post. Essentially the same thing.


All times are GMT -5. The time now is 1:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC