Skip to content

Instantly share code, notes, and snippets.

@ebc-2in2crc
Created October 21, 2012 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebc-2in2crc/3926843 to your computer and use it in GitHub Desktop.
Save ebc-2in2crc/3926843 to your computer and use it in GitHub Desktop.
package tetromino;
public class Point implements Comparable<Point> {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public Point add(Point o) {
return new Point(x + o.x, y + o.y);
}
public Point sub(Point o) {
return new Point(x - o.x, y - o.y);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Point)) {
return false;
}
Point other = (Point) obj;
return x == other.x && y == other.y;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + x;
result = 31 * result + y;
return result;
}
@Override
public int compareTo(Point o) {
if (x < o.x) {
return -1;
} else if (x > o.x) {
return 1;
}
if (y < o.y) {
return -1;
} else if (y > o.y) {
return 1;
}
return 0;
}
}
package tetromino;
import java.util.Set;
import java.util.TreeSet;
public class Points {
private Set<Point> points;
public Points(Set<Point> points) {
this.points = points;
}
public static Points newInstance(String points) {
Set<Point> set = new TreeSet<>();
for (String s : points.split(",")) {
int x = Integer.parseInt(s.substring(0, 1));
int y = Integer.parseInt(s.substring(1, 2));
set.add(new Point(x, y));
}
return new Points(set);
}
public int size() {
return points.size();
}
public Points move(Point point) {
Set<Point> result = new TreeSet<>();
for (Point p : points) {
result.add(p.add(point));
}
return new Points(result);
}
public Points moveLeftTop() {
Point p = new Point(minX(), minY());
Point leftTop = new Point(0, 0);
return move(leftTop.sub(p));
}
private int minX() {
int min = Integer.MAX_VALUE;
for (Point p : this.points) {
if (min > p.x) {
min = p.x;
}
}
return min;
}
private int minY() {
int min = Integer.MAX_VALUE;
for (Point p : this.points) {
if (min > p.y) {
min = p.y;
}
}
return min;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Points)) {
return false;
}
Points other = (Points) obj;
return points.equals(other.points);
}
@Override
public int hashCode() {
return points.hashCode();
}
}
package tetromino;
import java.util.HashSet;
import java.util.Set;
public class Tetoromino {
static final Set<Points> patternL;
static final Set<Points> patternI;
static final Set<Points> patternS;
static final Set<Points> patternT;
static final Set<Points> patternO;
static {
// L
patternL = new HashSet<>();
patternL.add(Points.newInstance("00,01,02,12"));
patternL.add(Points.newInstance("00,01,10,20"));
patternL.add(Points.newInstance("00,10,11,12"));
patternL.add(Points.newInstance("01,11,20,21"));
patternL.add(Points.newInstance("02,10,11,12"));
patternL.add(Points.newInstance("00,01,11,21"));
patternL.add(Points.newInstance("00,01,02,10"));
patternL.add(Points.newInstance("00,10,20,21"));
// I
patternI = new HashSet<>();
patternI.add(Points.newInstance("00,10,20,30"));
patternI.add(Points.newInstance("00,01,02,03"));
// T
patternT = new HashSet<>();
patternT.add(Points.newInstance("01,10,11,21"));
patternT.add(Points.newInstance("00,01,02,11"));
patternT.add(Points.newInstance("00,10,11,20"));
patternT.add(Points.newInstance("01,10,11,12"));
// O
patternO = new HashSet<>();
patternO.add(Points.newInstance("00,01,10,11"));
// S
patternS = new HashSet<>();
patternS.add(Points.newInstance("00,01,11,12"));
patternS.add(Points.newInstance("01,10,11,20"));
patternS.add(Points.newInstance("01,02,10,11"));
patternS.add(Points.newInstance("00,10,11,21"));
}
public static TetorominoName getName(Points points) {
if (points.size() != 4) {
return TetorominoName.DASH;
}
Points leftTop = points.moveLeftTop();
if (patternL.contains(leftTop)) {
return TetorominoName.L;
} else if (patternI.contains(leftTop)) {
return TetorominoName.I;
} else if (patternT.contains(leftTop)) {
return TetorominoName.T;
} else if (patternO.contains(leftTop)) {
return TetorominoName.O;
} else if (patternS.contains(leftTop)) {
return TetorominoName.S;
} else {
return TetorominoName.DASH;
}
}
public static enum TetorominoName {
L, I, T, O, S, DASH;
}
}
package tetromino;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static tetromino.Tetoromino.TetorominoName.DASH;
import static tetromino.Tetoromino.TetorominoName.I;
import static tetromino.Tetoromino.TetorominoName.L;
import static tetromino.Tetoromino.TetorominoName.O;
import static tetromino.Tetoromino.TetorominoName.S;
import static tetromino.Tetoromino.TetorominoName.T;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import tetromino.Tetoromino.TetorominoName;
public class TetorominoTest {
@Test
public void テトロミノ認識() {
Map<String, TetorominoName> map = new HashMap<>();
map.put("03,11,13,01", DASH);
map.put("05,26,06,25", DASH);
map.put("11,20,00,21", DASH);
map.put("24,36,35,26", DASH);
map.put("27,27,27,27", DASH);
map.put("27,39,28,37", DASH);
map.put("36,56,45,35", DASH);
map.put("41,33,32,43", DASH);
map.put("43,45,41,42", DASH);
map.put("45,34,54,35", DASH);
map.put("49,45,46,48", DASH);
map.put("55,44,44,45", DASH);
map.put("55,55,55,55", DASH);
map.put("55,57,77,75", DASH);
map.put("59,79,69,48", DASH);
map.put("61,60,62,73", DASH);
map.put("63,63,52,72", DASH);
map.put("67,37,47,47", DASH);
map.put("70,73,71,71", DASH);
map.put("75,94,84,95", DASH);
map.put("84,86,84,95", DASH);
map.put("84,95,94,86", DASH);
map.put("87,57,97,67", DASH);
map.put("24,22,25,23", I);
map.put("49,69,59,79", I);
map.put("51,41,21,31", I);
map.put("64,63,62,65", I);
map.put("07,17,06,05", L);
map.put("13,23,03,24", L);
map.put("21,41,31,40", L);
map.put("48,49,57,47", L);
map.put("62,74,73,72", L);
map.put("69,89,79,68", L);
map.put("84,94,74,75", L);
map.put("90,82,91,92", L);
map.put("25,24,15,14", O);
map.put("68,57,58,67", O);
map.put("72,62,61,71", O);
map.put("12,11,22,01", S);
map.put("42,33,32,23", S);
map.put("43,54,53,42", S);
map.put("63,73,52,62", S);
map.put("66,57,67,58", S);
map.put("72,73,84,83", S);
map.put("76,68,77,67", S);
map.put("95,86,76,85", S);
map.put("12,10,21,11", T);
map.put("27,16,36,26", T);
map.put("32,41,43,42", T);
map.put("89,99,79,88", T);
for (String key : map.keySet()) {
Points p = Points.newInstance(key);
assertThat(Tetoromino.getName(p), is(map.get(key)));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment