|
--------------------------------------------------------------------------------
Suppose you have an altered chess board:
0 0 0 0 1 1 1 1
0 0 0 0 0 1 1 1
0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
where the 1's are spaces where nothing can be placed.
Now suppose you have four tetris shapes, where each shape contains only four 'units'
I need to create a fitness function where all four of the shapes are continuously and randomly added onto the board until no additional shapes can fit. The fitness function should return the # of spaces filled divided by the total number of spaces.
(Without using a GUI).
I was told to do this in such a way where each shape has the function:
f (shape, x coord., y coord., orientation) x 10 (So each shape has 4 ints)
Then, I was told to, using a 1-Dimensional Array of size 40 ints, add the shapes into the array for ten different iterations. For each iteration, the x and y coordinates as well as the orientation of each shape should be random.
After each iteration, the number of spaces filled should be printed.
I have the idea down, but have NO idea how to code this. Remember, it should return the # of spaces filled over the total number of spaces. Please help me! I have no idea how to represent the shapes, their coordinates, and their orientations. Thanks!
(This has to be done in java.)
|
|
|
import java.util.Random;
public class Tetris
{
Random seed;
char[][] grid;
char empty, filled, block;
int[] buffer;
final int
FLAT = 0,
VERT = 1,
TO_SE = 2,
TO_SW = 3;
public Tetris()
{
seed = new Random();
empty = '0';
filled = '*';
block = '1';
initGrid();
buffer = new int[40];
}
private void makeShapes()
{
int count = 0;
while(getEmptySpaces() > 0)
{
for(int j = 0; j < 10; j++)
{
int orientation = seed.nextInt(4);
int limit = grid.length * grid[0].length;
switch(orientation)
{
case FLAT:
limit -= 4;
break;
case VERT:
limit -= 3 * grid[0].length - 1;
break;
case TO_SE:
limit -= 3 * grid[0].length + 3;
break;
case TO_SW:
limit -= 3 * grid[0].length;
}
int pos = seed.nextInt(limit+1);
int y = pos / grid.length;
int x = pos - y * grid[0].length;
int[] shape = getShape(x, y, orientation);
System.arraycopy(shape, 0, buffer, j*shape.length, shape.length);
}
System.out.println("filled spaces = " + getNewSpacesFilled());
updateGrid();
count++;
}
printGrid();
int emptySpaces = getEmptySpaces();
System.out.println("emptySpaces = " + emptySpaces +
" after " + count + " iterations");
}
private int[] getShape(int x, int y, int orientation)
{
int start = x + y * grid.length;
int index = 0;
int[] shape = new int[4];
switch(orientation)
{
case FLAT:
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
{
int loc = k + j * grid.length;
if(loc >= start && index < 4)
shape[index++] = loc;
}
}
break;
case VERT:
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
{
int loc = k + j * grid.length;
if(loc >= start && k == x && index < 4)
shape[index++] = loc;
}
}
break;
case TO_SE:
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
{
int loc = k + j * grid.length;
int inc = index * grid[0].length + index;
if(loc == start + inc && index < 4)
shape[index++] = loc;
}
}
break;
case TO_SW:
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
{
int loc = k + j * grid.length;
int inc = index * grid[0].length - index;
if(loc == start + inc && index < 4)
shape[index++] = loc;
}
}
}
return shape;
}
private boolean contains(int target, int[] domain)
{
for(int j = 0; j < domain.length; j++)
if(domain[j] == target)
return true;
return false;
}
private int getNewSpacesFilled()
{
int count = 0;
for(int j = 0; j < grid.length; j++)
for(int k = 0; k < grid[0].length; k++)
{
int pos = k + j * grid[0].length;
if(grid[j][k] == empty && contains(pos, buffer))
count++;
}
return count;
}
private void updateGrid()
{
for(int j = 0; j < grid.length; j++)
for(int k = 0; k < grid[0].length; k++)
{
int pos = k + j * grid[0].length;
if(grid[j][k] == empty && contains(pos, buffer))
grid[j][k] = filled;
}
}
private int getEmptySpaces()
{
int total = 0;
for(int j = 0; j < grid.length; j++)
for(int k = 0; k < grid[0].length; k++)
if(grid[j][k] == empty)
total++;
return total;
}
private void printGrid()
{
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
System.out.print(" " + grid[j][k] + " ");
System.out.println();
}
}
private void initGrid()
{
grid = new char[8][8];
for(int j = 0; j < grid.length; j++)
for(int k = 0; k < grid[0].length; k++)
{
if(k >= grid[0].length/2 + j)
grid[j][k] = block;
else
grid[j][k] = empty;
}
}
public static void main(String[] args)
{
Tetris t = new Tetris();
t.printGrid();
t.makeShapes();
}
}
|
|
|
|
|
Wow, thanks so much...
What you did is really good. Can I credit you in anyway?
|
|
|
Only if you want to. The code is a gift to you.
|
|
|
|
|
|
|
|