From fc0552b49d325b632868d323eca3d22508a819e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugho389@student.liu.se> Date: Mon, 13 Feb 2017 16:54:01 +0100 Subject: [PATCH] fixed some inspection --- Board.java | 16 +++++++++------- BoardTest.java | 5 ++++- BoardToTextConverter.java | 17 ++++++++++------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Board.java b/Board.java index b0cebb0..8747137 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 bcbe3d2..28f15a1 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 77bdab4..caf7116 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(); } } -- GitLab