diff --git a/Board.java b/Board.java index b0cebb09ffacf38dc729a73171898dd386b99b7a..8747137bf19a5a87f5cfb33e7635315cde1083fd 100644 --- a/Board.java +++ b/Board.java @@ -1,6 +1,8 @@ package se.liu.ida.alere980.tddd78.tetris; import java.util.Random; +import static java.lang.Math.min; + public class Board { private SquareType[][] squares; @@ -21,6 +23,8 @@ public class Board } public static void main(String[] args) { + // Test coordinates, please ignore + //noinspection ResultOfObjectAllocationIgnored new Board(57,63); } @@ -36,22 +40,20 @@ public class Board } } - public SquareType getCellData(int x, int y){ + public SquareType getCellData(int inX, int inY){ SquareType[][] squaresWithFalling = squares.clone(); if(falling != null){ SquareType[][] fallingBlocks = falling.getBlocks(); - for (int squaresY = fallingY; squaresY < fallingY + fallingBlocks.length & squaresY < height ; squaresY++) { - for (int squaresX = fallingX; squaresX < fallingX + fallingBlocks[0].length & squaresX < width ; squaresX++) { - squaresWithFalling[squaresY][squaresX] = fallingBlocks[squaresY - fallingY][squaresX - fallingX]; + for (int y = fallingY; y < min(fallingY + fallingBlocks.length, height); y++) { + for (int x = fallingX; x < min(fallingX + fallingBlocks[0].length, width); x++) { + squaresWithFalling[y][x] = fallingBlocks[y - fallingY][x - fallingX]; } } } - - - return squaresWithFalling[y][x]; + return squaresWithFalling[inY][inX]; } public int getWidth() { diff --git a/BoardTest.java b/BoardTest.java index bcbe3d2c45ad872fea7a4c463376fc336eb629bc..28f15a18b8bf147903a5faedc5f69cb21aa37eea 100644 --- a/BoardTest.java +++ b/BoardTest.java @@ -4,9 +4,12 @@ import java.awt.event.ActionEvent; import javax.swing.*; -public class BoardTest +public final class BoardTest { + private BoardTest() {} + public static void main(String[] args) { + // test data, please ignore Board board = new Board(10,20); /* diff --git a/BoardToTextConverter.java b/BoardToTextConverter.java index 77bdab4a2ce55a990188523dc4b36603f5e0745c..caf71164076929aa84c9451a9ddb0bfe4f00e9d6 100644 --- a/BoardToTextConverter.java +++ b/BoardToTextConverter.java @@ -1,27 +1,30 @@ package se.liu.ida.alere980.tddd78.tetris; -public class BoardToTextConverter +public final class BoardToTextConverter { + private BoardToTextConverter() {} + public static String convertToText(Board board){ - String returnStr = ""; + StringBuilder strBuild = new StringBuilder(); + for (int y = 0; y < board.getHeight(); y++) { - returnStr += "│"; + strBuild.append("│"); for (int x = 0; x < board.getWidth() ; x++) { SquareType sq = board.getCellData(x,y); switch(sq){ case EMPTY: - returnStr += " "; + strBuild.append(" "); break; default: - returnStr += sq.toString(); + strBuild.append(sq.toString()); } } - returnStr += "│\n"; + strBuild.append("│\n"); } - return returnStr; + return strBuild.toString(); } }