Java tutorial
/* Copyright [2014] [James Thomas Hayes] 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 http://www.apache.org/licenses/LICENSE-2.0 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.codefiddler.libgdx.tetris.domain; import java.util.ArrayList; import java.util.List; import java.util.Random; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.math.Vector2; import com.codefiddler.libgdx.tetris.RenderBlock; public class Tetrimino { public static final int NUMBER_OF_SHAPES = 7; private Vector2 position = new Vector2(0, 0); private int[][][] blocks; private int blockSet = 0; private final Color color; private Tetrimino(int[][][] blocks, Color color) { this.blocks = blocks; this.color = color; } public int[][][] getBlocks() { return blocks; } public void setBlocks(int[][][] blocks) { this.blocks = blocks; } public Color getColor() { return color; } public boolean tick(SettledPlayBoard settledPlayboard) { position.add(0, -1); if (isInFreeBlock(settledPlayboard)) { return true; } position.add(0, 1); return false; } private boolean isInFreeBlock(SettledPlayBoard settledPlayboard) { return isWithinBoard(settledPlayboard) && !overlappingBlockIn(settledPlayboard); }; private boolean isWithinBoard(SettledPlayBoard settledPlayboard) { for (Vector2 vector : getCurrentPosition()) { if (vector.x > settledPlayboard.getWidth() - 1 || vector.x < 0) { return false; } if (vector.y < 0) { return false; } } return true; } private boolean overlappingBlockIn(SettledPlayBoard settledPlayboard) { List<Vector2> currentPosition = getCurrentPosition(); for (Vector2 point : currentPosition) { if (settledPlayboard.containsBlockAt(point)) { return true; } } if (getBottomYPosition() < settledPlayboard.getBottomPoint()) { return true; } return false; } public void right(SettledPlayBoard board) { position.add(1, 0); if (!isInFreeBlock(board)) { position.add(-1, 0); } }; public void left(SettledPlayBoard board) { position.add(-1, 0); if (!isInFreeBlock(board)) { position.add(1, 0); } }; public void down(SettledPlayBoard board) { position.add(0, -1); if (!isInFreeBlock(board)) { position.add(0, 1); } } public void drop(SettledPlayBoard board) { while (isInFreeBlock(board)) { position.add(0, -1); } position.add(0, 1); } public Vector2 getPosition() { return position; } public void setPosition(Vector2 position) { this.position = position; } public boolean turn(SettledPlayBoard board) { blockSet = (blockSet + 1) % (blocks.length); if (!isInFreeBlock(board)) { blockSet = (blockSet + blocks.length - 1) % (blocks.length); } return false; } public List<Vector2> getPoints() { ArrayList<Vector2> points = new ArrayList<Vector2>(); for (int[] point : blocks[blockSet]) { points.add(new Vector2(point[0], point[1])); } return points; } private static int[][][] Z = new int[][][] { { { 0, 0 }, { 1, 0 }, { 1, -1 }, { 2, -1 } }, { { 1, 0 }, { 1, -1 }, { 0, -1 }, { 0, -2 } } }; public static Tetrimino getZ() { return new Tetrimino(Z, Color.CYAN); } private static int[][][] S = new int[][][] { { { 2, 0 }, { 1, 0 }, { 1, -1 }, { 0, -1 } }, { { 2, -2 }, { 2, -1 }, { 1, -1 }, { 1, 0 } } }; public static Tetrimino getS() { return new Tetrimino(S, Color.GREEN); } private static int[][][] L = new int[][][] { { { 1, 0 }, { 1, -1 }, { 1, -2 }, { 2, -2 } }, { { 0, -2 }, { 0, -1 }, { 1, -1 }, { 2, -1 } }, { { 1, 0 }, { 0, 0 }, { 1, -1 }, { 1, -2 } }, { { 0, -1 }, { 1, -1 }, { 2, -1 }, { 2, 0 } } }; public static Tetrimino getL() { return new Tetrimino(L, Color.MAGENTA); } private static int[][][] J = new int[][][] { { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 2, -1 } }, { { 1, 0 }, { 1, -1 }, { 1, -2 }, { 0, -2 } }, { { 0, 0 }, { 0, -1 }, { 1, -1 }, { 2, -1 } }, { { 2, 0 }, { 1, 0 }, { 1, -1 }, { 1, -2 } } }; public static Tetrimino getJ() { return new Tetrimino(J, Color.GRAY); } private static int[][][] T = new int[][][] { { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 1, 1 } }, { { 1, 0 }, { 1, 1 }, { 1, 2 }, { 2, 1 } }, { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 1, -1 } }, { { 1, 0 }, { 1, 1 }, { 1, 2 }, { 0, 1 } } }; public static Tetrimino getT() { return new Tetrimino(T, new Color(0x139 / 255f, 0x69 / 255f, 0x19 / 255f, 0)); } private static int[][][] I = new int[][][] { { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 } }, { { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 } } }; public static Tetrimino getI() { return new Tetrimino(I, Color.RED); } private static int[][][] O = new int[][][] { { { 0, 0 }, { 1, 0 }, { 1, 1 }, { 0, 1 } } }; public static Tetrimino getO() { return new Tetrimino(O, Color.YELLOW); } private static int[][][] BLOCK = new int[][][] { { { 0, 0 } } }; public static Tetrimino getBlock(Vector2 position, Color color) { Tetrimino block = new Tetrimino(BLOCK, color); block.setPosition(position); return block; } public List<Vector2> getCurrentPosition() { List<Vector2> vector2s = getPoints(); List<Vector2> currentPoints = new ArrayList<Vector2>(); for (Vector2 point : vector2s) { currentPoints.add(point.add(getPosition())); } return currentPoints; } public void drawIn(SettledPlayBoard settledPlayboard) { for (Vector2 vector : getCurrentPosition()) { settledPlayboard.addBlock(Tetrimino.getBlock(vector, getColor())); } } public int[][] deepCopy(int[][] src) { int length = src.length; int[][] target = new int[length][src[0].length]; for (int i = 0; i < length; i++) { System.arraycopy(src[i], 0, target[i], 0, src[i].length); } return target; } @Override public String toString() { return "Tetrimino [position=" + position + "]"; } public int getWidth() { int max = 0; for (Vector2 vector : getPoints()) { max = Math.max((int) vector.x, max); } return max; } private float getBottomYPosition() { float min = Float.MAX_VALUE; for (Vector2 vector : getCurrentPosition()) { min = Math.min((int) vector.y, min); } return min; } private static Random random = new Random(); public static Tetrimino getNext() { return get(random.nextInt(NUMBER_OF_SHAPES)); } public static Tetrimino get(Integer index) { switch (index) { case 0: { return getI(); } case 1: { return getO(); } case 2: { return getT(); } case 3: { return getJ(); } case 4: { return getL(); } case 5: { return getS(); } case 6: { return getZ(); } } throw new RuntimeException(); } public void forEachBlock(RenderBlock renderBlock) { List<Vector2> position = getCurrentPosition(); for (Vector2 point : position) { renderBlock.renderBlock(point.x, point.y, getColor()); } } }