tags:

views:

74

answers:

1
+1  A: 

Let me try to get you started. I don't think I'm really understanding your given code at the moment, but let's break the problem down:

There are a couple main elements to the function:

  • Assign an image randomly to each tile inside loop
  • Keep track of which images have been assigned

There are at least two ways to accomplish this:

  1. Iterate over the board, and assign a random image to each tile. Keep track of how many times each image is used in a separate array, by index.
  2. Iterate through the possibleTileValues list, and randomly assign each one to a tile, twice. That takes care of the assignment counting, but you'll need to test each tile to make sure it hasn't been previously assigned an image.

Either way, you'll likely use java.util.Random to produce random integers. You can do this as follows:

Random randomizer = new Random();
int randomint = randomizer.nextInt(rowLength);

Obviously, for the first method above you'll need to generate one random per iteration of the inner loop, while with the second you'll generate two randoms per iteration to give you coordinates for the board.

Take the above and see where you can get with it. I'll be happy to help further.


Edit:

Ok, taking a look at your redone code, there are a few issues. I'm not sure what getFoo() and addFoo() are. If you're creating helper functions, please include them in the code.
Here's some pseudocode for you to follow:

int[rowSize] timesTileUsed

for x_index = 0 to rowLength - 1
  for y_index = 0 to rowLength - 1
  {
    do
      randomNumber = generator.nextInt(rowLength)
    while timesTileUsed[randomNumber] > 1
    board[x_index][y_index] = new Tile(possibleTileValues[randomNumber])
    timesTileUsed[randomNumber]++
  }

Now, note that this is a hideously naive way of doing this, but I don't think you're meant to be smart about it.

phoebus
Completely redid code...though I wasn't quite sure of what I was coding...private void fillBoard(){if(size%2==0gameBoard.addFoo(possibleTileValues.getFoo(i));}for(int i=0, i<size,i++){Random gen= new Random();int randomSpot=gen.nextInt(size);int temp = gameBoard[i];gameBoard[i]=gameBoard[gen];gameBoard[gen] = temp;}}}
David
Aw, sorry about the format.
David
Added your redone code to the above, looking at it.
phoebus
Whoops, in the last two lines of code, I meant to type randomSpot instead of gen.
David
Please take a look at my additional comments. I want to help but I don't feel right just giving you the code so I'm trying to be general but helpful. Please throw me an upvote if it's helping:)
phoebus
I definitely will. I forot to include the FooList class btw: http://www.mrmerlis.com/classes/APCS/labs/FooList.php
David
Sorry about any spelling mistakes... I'm typing everything on a mobile device. How do I increase reputation btw (I need 15 to upvote).
David