Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 5th, 2006, 10:57 AM   #1
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
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);
   } 
}
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Oct 5th, 2006, 11:12 AM   #2
Gilward Kukel
Newbie
 
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0 Gilward Kukel is on a distinguished road
add every string twice to the list?
Gilward Kukel is offline   Reply With Quote
Old Oct 5th, 2006, 11:22 AM   #3
ReggaetonKing
Sexy Programmer
 
ReggaetonKing's Avatar
 
Join Date: Nov 2005
Location: New Jersey
Posts: 891
Rep Power: 3 ReggaetonKing is on a distinguished road
Send a message via AIM to ReggaetonKing
Quote:
Originally Posted by Gilward Kukel View Post
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
__________________
I would love to change the world, but they won't give me the source code!
ReggaetonKing is offline   Reply With Quote
Old Oct 5th, 2006, 11:29 AM   #4
Gilward Kukel
Newbie
 
Join Date: Jun 2005
Location: Vienna, Austria
Posts: 15
Rep Power: 0 Gilward Kukel is on a distinguished road
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.
Gilward Kukel is offline   Reply With Quote
Old Oct 5th, 2006, 11:29 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:25 AM.

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