/* (@)TetrisTileMatching.java */
/* Copyright 2010 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
/**
* Test for matching tetris tiles with bit operations.
*
* @version $Revision$ as of $Date$
* @author Sebastian Haufe
* @since Playground-3.8
*/
public class TetrisTileMatching {
/** @param args ignored */
@SuppressWarnings("boxing")
public static void main(String[] args) {
final long outerTile = toBits(new byte[][] { //
{ 1, 1, 1, 1, 1, 1, 1, }, //
{ 1, 1, 1, 1, 1, 1, 1, }, //
{ 1, 1, 1, 1, 1, 1, 1, }, //
{ 1, 1, 1, 1, 1, 1, 1, }, //
{ 1, 1, 1, 1, 1, 1, 1, }, //
}, 8);
long innerTile = toBits(new byte[][] { //
{ 0, 1, 1, 1, 0, 0, 0, }, //
{ 0, 1, 1, 1, 1, 1, 0, }, //
{ 0, 1, 1, 1, 1, 1, 0, }, //
{ 1, 1, 1, 0, 0, 0, 0, }, //
{ 1, 1, 1, 0, 0, 0, 0, }, //
}, 8);
final long right = 0x8080808080808080L;
final long bottom = 0xff00000000000000L;
boolean outBottom = false;
for (int offy = 0; offy < 8; offy++) {
boolean outRight = false;
for (int offx = 0; offx < 8; offx++) {
if (!outBottom && !outRight && (outerTile & innerTile) == innerTile) {
System.out.printf("Match at %dx%d%n", offx, offy);
}
innerTile <<= 1;
outRight = (innerTile & right) != 0;
}
outBottom = (innerTile & bottom) != 0;
}
}
private static long toBits(byte[][] bs, int maxTileSize) {
long v = 0;
for (int y = maxTileSize - 1; y >= 0; y--) {
for (int x = maxTileSize - 1; x >= 0; x--) {
final boolean b = y < bs.length && x < bs[y].length && bs[y][x] != 0;
v <<= 1;
v |= b ? 1 : 0;
}
}
return v;
}
}