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.Collections; import java.util.List; import java.util.Stack; import com.badlogic.gdx.math.Vector2; public class ActivePlayBoard { private Tetrimino activePiece; public SettledPlayBoard settledPlayboard; private int score = 0; private int level = 1; private int rows = 0; private Stack<Tetrimino> nextShapes = new Stack<Tetrimino>(); private boolean gameRunning = true; public ActivePlayBoard() { settledPlayboard = new SettledPlayBoard(); init(); } public ActivePlayBoard(char[][] initialBoard) { settledPlayboard = new SettledPlayBoard(); init(); } private void init() { generateNextShapes(); activePiece = nextShapes.pop(); setInitialPosition(activePiece); } public void tick() { if (gameRunning == false) { return; } if (!activePiece.tick(settledPlayboard)) { drawActiveShapeInSettledBoardAndGetNextShape(); } ; } public void drawActiveShapeInSettledBoardAndGetNextShape() { activePiece.drawIn(settledPlayboard); addScore(activePiece); activePiece = nextShapes.pop(); if (nextShapes.isEmpty()) { generateNextShapes(); } activePiece.setPosition(new Vector2(0, 0)); setInitialPosition(activePiece); int cleanUpPoints = settledPlayboard.cleanUp(); if (cleanUpPoints > 0) { addScore(cleanUpPoints); } if (!activePiece.tick(settledPlayboard)) { gameRunning = false; } } public Tetrimino getNextShape() { return nextShapes.peek(); } private void generateNextShapes() { List<Integer> values = new ArrayList<Integer>(); for (int i = 0; i < Tetrimino.NUMBER_OF_SHAPES; i++) { values.add(i); } Collections.shuffle(values); for (int i : values) { nextShapes.add(Tetrimino.get(i)); } } private void addScore(Tetrimino settledPiece) { int newPoints = settledPiece.getPoints().size() * 10; score = score + newPoints; } private void addScore(int cleanUpPoints) { rows += cleanUpPoints; if (getRowsNeededForNextLevel() <= rows) { rows = 0; levelUp(); } int newPoints = cleanUpPoints * 10; score = score + newPoints; } private int getRowsNeededForNextLevel() { return level * 5; } private void setInitialPosition(Tetrimino activePiece) { Vector2 position = new Vector2(settledPlayboard.getWidth() / 2, settledPlayboard.getHeight() - 1); int halfWidthOfPiece = activePiece.getWidth() / 2; position.x -= halfWidthOfPiece; activePiece.setPosition(position); } public boolean turn(SettledPlayBoard settledPlayboard) { return activePiece.turn(settledPlayboard); } public Tetrimino getActivePiece() { return activePiece; } public String toString() { return settledPlayboard.toString(); } public List<Tetrimino> getSettledPieces() { return settledPlayboard.getBlocks(); } public int getScore() { return score; } public boolean isGameRunning() { return gameRunning; } public int getLevel() { return level; } public void levelUp() { level++; } }