Fikser kodestil

This commit is contained in:
Kristian Knarvik 2020-03-16 20:06:35 +01:00
parent 49ee7dc896
commit b520178686
7 changed files with 74 additions and 68 deletions

View File

@ -16,8 +16,8 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
private final RoboRallyWrapper roboRallyWrapper; private final RoboRallyWrapper roboRallyWrapper;
private final OrthographicCamera camera; private final OrthographicCamera camera;
private CardRectangle cardRectangle; private final CardRectangle cardRectangle;
private ShapeRenderer shapeRenderer; private final ShapeRenderer shapeRenderer;
public CardChoiceScreen(final RoboRallyWrapper roboRallyWrapper) { public CardChoiceScreen(final RoboRallyWrapper roboRallyWrapper) {
this.roboRallyWrapper = roboRallyWrapper; this.roboRallyWrapper = roboRallyWrapper;
@ -28,7 +28,7 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
card1.y = 1200/2; card1.y = 1200/2;
card1.width = 100; card1.width = 100;
card1.height = 100; card1.height = 100;
cardRectangle = new CardRectangle(card1, false); cardRectangle = new CardRectangle(card1);
shapeRenderer = new ShapeRenderer(); shapeRenderer = new ShapeRenderer();
shapeRenderer.setAutoShapeType(true); shapeRenderer.setAutoShapeType(true);
Gdx.input.setInputProcessor(this); Gdx.input.setInputProcessor(this);
@ -36,7 +36,7 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
@Override @Override
public void show() { public void show() {
//Nothing to do
} }
@Override @Override
@ -61,27 +61,27 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
@Override @Override
public void resize(int i, int i1) { public void resize(int i, int i1) {
//Nothing to do
} }
@Override @Override
public void pause() { public void pause() {
//Nothing to do
} }
@Override @Override
public void resume() { public void resume() {
//Nothing to do
} }
@Override @Override
public void hide() { public void hide() {
//Nothing to do
} }
@Override @Override
public void dispose() { public void dispose() {
//Nothing to do
} }
@Override @Override
@ -99,11 +99,10 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
} }
class CardRectangle { class CardRectangle {
Rectangle rectangle; protected final Rectangle rectangle;
boolean selected; protected boolean selected = false;
CardRectangle(Rectangle rectangle, boolean selected) { CardRectangle(Rectangle rectangle) {
this.rectangle = rectangle; this.rectangle = rectangle;
this.selected = selected;
} }
} }

View File

@ -1,6 +1,11 @@
package inf112.fiasko.roborally.objects; package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.*; import inf112.fiasko.roborally.element_properties.Direction;
import inf112.fiasko.roborally.element_properties.Position;
import inf112.fiasko.roborally.element_properties.RobotID;
import inf112.fiasko.roborally.element_properties.TileType;
import inf112.fiasko.roborally.element_properties.WallType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -166,6 +171,21 @@ public class Board {
deadRobots = new ArrayList<>(); deadRobots = new ArrayList<>();
} }
/**
* Returns a robot id for a robot on a specific position if such a robot exists
* @param position The position to check
* @return The robot id of the robot on the position or null if there is no robot there
*/
public RobotID getRobotOnPosition(Position position) {
for (RobotID robotID : robots.keySet()) {
Robot robot = robots.get(robotID);
if (position.equals(robot.getPosition())) {
return robotID;
}
}
return null;
}
/** /**
* Checks if a specific robot is currently alive on the board * Checks if a specific robot is currently alive on the board
* @param robot the ID of the robot you want to check * @param robot the ID of the robot you want to check
@ -256,27 +276,12 @@ public class Board {
deadRobots.add(robot); deadRobots.add(robot);
} }
/**
* Returns a robot id for a robot on a specific position if such a robot exists
* @param position The position to check
* @return The robot id of the robot on the position or null if there is no robot there
*/
RobotID getRobotOnPosition(Position position) {
for (RobotID robotID : robots.keySet()) {
Robot robot = robots.get(robotID);
if (position.equals(robot.getPosition())) {
return robotID;
}
}
return null;
}
/** /**
* Checks whether there exists a robot on a specific position * Checks whether there exists a robot on a specific position
* @param position The position to check * @param position The position to check
* @return True if there is a robot on the specified position * @return True if there is a robot on the specified position
*/ */
boolean hasRobotOnPosition(Position position) { public boolean hasRobotOnPosition(Position position) {
return getRobotOnPosition(position) != null; return getRobotOnPosition(position) != null;
} }
@ -362,7 +367,7 @@ public class Board {
* @param walls The walls you want all positions for * @param walls The walls you want all positions for
* @return A list of BoardElementContainers * @return A list of BoardElementContainers
*/ */
public List<BoardElementContainer<Wall>> getPositionsOfWallOnBoard(WallType ... walls) { public List<BoardElementContainer<Wall>> getPositionsOfWallOnBoard(WallType... walls) {
List<BoardElementContainer<Wall>> combinedList = new ArrayList<>(); List<BoardElementContainer<Wall>> combinedList = new ArrayList<>();
for (WallType wall : walls) { for (WallType wall : walls) {
combinedList.addAll(makeTileList(wall, this.walls)); combinedList.addAll(makeTileList(wall, this.walls));

View File

@ -3,36 +3,36 @@ package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Position; import inf112.fiasko.roborally.element_properties.Position;
/** /**
* This class represents a type of object and its position * This class represents a board element and its position
* @param <K> The type of object * @param <K> The type of element
*/ */
public class BoardElementContainer <K>{ public class BoardElementContainer <K> {
K obj; private final K element;
private Position pos; private final Position position;
/** /**
* Initializes the BoardElementContainer * Initializes the BoardElementContainer
* @param obj The object * @param element The element
* @param pos The position * @param position The position
*/ */
BoardElementContainer(K obj, Position pos) { BoardElementContainer(K element, Position position) {
this.obj = obj; this.element = element;
this.pos = pos; this.position = position;
} }
/** /**
* Gets the object * Gets the element
* @return object * @return The element
*/ */
public K getObject() { public K getElement() {
return obj; return element;
} }
/** /**
* Gets the position * Gets the position
* @return position * @return The position
*/ */
public Position getPosition() { public Position getPosition() {
return pos; return position;
} }
} }

View File

@ -211,7 +211,7 @@ public class RoboRallyGame implements IDrawableGame {
continue; continue;
} }
sleep(); sleep();
if (cogwheel.obj.getTileType() == TileType.COGWHEEL_RIGHT) { if (cogwheel.getElement().getTileType() == TileType.COGWHEEL_RIGHT) {
gameBoard.rotateRobotRight(gameBoard.getRobotOnPosition(cogwheel.getPosition())); gameBoard.rotateRobotRight(gameBoard.getRobotOnPosition(cogwheel.getPosition()));
} else { } else {
gameBoard.rotateRobotLeft(gameBoard.getRobotOnPosition(cogwheel.getPosition())); gameBoard.rotateRobotLeft(gameBoard.getRobotOnPosition(cogwheel.getPosition()));
@ -258,7 +258,7 @@ public class RoboRallyGame implements IDrawableGame {
continue; continue;
} }
Position conveyorBeltPosition = conveyorBelt.getPosition(); Position conveyorBeltPosition = conveyorBelt.getPosition();
Tile conveyorBeltTile = conveyorBelt.getObject(); Tile conveyorBeltTile = conveyorBelt.getElement();
Position newPosition = gameBoard.getNewPosition(conveyorBeltPosition, conveyorBeltTile.getDirection()); Position newPosition = gameBoard.getNewPosition(conveyorBeltPosition, conveyorBeltTile.getDirection());
Tile nextTile = gameBoard.getTileOnPosition(newPosition); Tile nextTile = gameBoard.getTileOnPosition(newPosition);
@ -283,7 +283,7 @@ public class RoboRallyGame implements IDrawableGame {
Direction nextDirection = nextTile.getDirection(); Direction nextDirection = nextTile.getDirection();
sleep(); sleep();
gameBoard.moveRobot(robot, currentDirection); gameBoard.moveRobot(robot, currentDirection);
if (testPredicate(conveyorBelts, (container) -> container.getObject() == nextTile)) { if (testPredicate(conveyorBelts, (container) -> container.getElement() == nextTile)) {
if (Direction.getRightRotatedDirection(nextDirection) == currentDirection) { if (Direction.getRightRotatedDirection(nextDirection) == currentDirection) {
sleep(); sleep();
gameBoard.rotateRobotLeft(robot); gameBoard.rotateRobotLeft(robot);
@ -304,7 +304,7 @@ public class RoboRallyGame implements IDrawableGame {
Position flagPosition = flag.getPosition(); Position flagPosition = flag.getPosition();
if (gameBoard.hasRobotOnPosition(flagPosition)) { if (gameBoard.hasRobotOnPosition(flagPosition)) {
RobotID robot = gameBoard.getRobotOnPosition(flagPosition); RobotID robot = gameBoard.getRobotOnPosition(flagPosition);
gameBoard.updateFlagOnRobot(robot, flag.getObject().getTileType()); gameBoard.updateFlagOnRobot(robot, flag.getElement().getTileType());
} }
} }
} }

View File

@ -14,7 +14,7 @@ public class BoardElementContainerTest {
Position pos = new Position(1,2); Position pos = new Position(1,2);
Tile tile = new Tile(TileType.TILE, Direction.NORTH); Tile tile = new Tile(TileType.TILE, Direction.NORTH);
BoardElementContainer<Tile> element = new BoardElementContainer<>(tile, pos); BoardElementContainer<Tile> element = new BoardElementContainer<>(tile, pos);
assertEquals(tile, element.getObject()); assertEquals(tile, element.getElement());
} }
@Test @Test

View File

@ -35,8 +35,8 @@ public class BoardTest {
private List<Robot> robotList; private List<Robot> robotList;
private Board board; private Board board;
private Board boardWithDifferentAmountOfAllTypes; private Board boardWithDifferentAmountOfAllTypes;
private Map<WallType,Integer> wallTypeNumberMap = new HashMap<>(); private final Map<WallType,Integer> wallTypeNumberMap = new HashMap<>();
private Map<TileType,Integer> tileTypeNumberMap = new HashMap<>(); private final Map<TileType,Integer> tileTypeNumberMap = new HashMap<>();
@BeforeClass @BeforeClass
public static void globalSetUp() { public static void globalSetUp() {
@ -223,7 +223,7 @@ public class BoardTest {
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT); List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.COGWHEEL_LEFT);
for (BoardElementContainer<Tile> elem : boardElemList) { for (BoardElementContainer<Tile> elem : boardElemList) {
assertEquals(elem.getObject().getTileType(), TileType.COGWHEEL_LEFT); assertEquals(elem.getElement().getTileType(), TileType.COGWHEEL_LEFT);
} }
} }
@ -238,7 +238,7 @@ public class BoardTest {
List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.TILE); List<BoardElementContainer<Tile>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfTileOnBoard(TileType.TILE);
for (BoardElementContainer<Tile> elem : boardElemList) { for (BoardElementContainer<Tile> elem : boardElemList) {
assertEquals(elem.getObject().getTileType(), TileType.TILE); assertEquals(elem.getElement().getTileType(), TileType.TILE);
} }
} }
@ -253,7 +253,7 @@ public class BoardTest {
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_NORMAL); List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_NORMAL);
for (BoardElementContainer<Wall> elem : boardElemList) { for (BoardElementContainer<Wall> elem : boardElemList) {
assertEquals(elem.getObject().getWallType(), WallType.WALL_NORMAL); assertEquals(elem.getElement().getWallType(), WallType.WALL_NORMAL);
} }
} }
@ -268,14 +268,14 @@ public class BoardTest {
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER); List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER);
for (BoardElementContainer<Wall> elem : boardElemList) { for (BoardElementContainer<Wall> elem : boardElemList) {
assertEquals(elem.getObject().getWallType(), WallType.WALL_CORNER); assertEquals(elem.getElement().getWallType(), WallType.WALL_CORNER);
} }
} }
@Test @Test
public void getPositionsOfWallOnBoardHasCorrect() { public void getPositionsOfWallOnBoardHasCorrect() {
List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER); List<BoardElementContainer<Wall>> boardElemList = boardWithDifferentAmountOfAllTypes.getPositionsOfWallOnBoard(WallType.WALL_CORNER);
Predicate<BoardElementContainer<Wall>> pred = (element) -> element.getObject().getWallType() == WallType.WALL_CORNER; Predicate<BoardElementContainer<Wall>> pred = (element) -> element.getElement().getWallType() == WallType.WALL_CORNER;
boardElemList.removeIf(pred); boardElemList.removeIf(pred);
assertEquals(0, boardElemList.size()); assertEquals(0, boardElemList.size());
} }

View File

@ -8,12 +8,14 @@ import org.junit.Test;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class PlayerTest { public class PlayerTest {
private Player playerTest; private Player playerTest;
private List<ProgrammingCard> cards = new ArrayList(); private final List<ProgrammingCard> cards = new ArrayList<>();
@Before @Before
public void setUp() { public void setUp() {
@ -22,7 +24,6 @@ public class PlayerTest {
cards.add(new ProgrammingCard(30, Action.MOVE_3)); cards.add(new ProgrammingCard(30, Action.MOVE_3));
cards.add(new ProgrammingCard(40, Action.BACK_UP)); cards.add(new ProgrammingCard(40, Action.BACK_UP));
cards.add(new ProgrammingCard(50, Action.ROTATE_LEFT)); cards.add(new ProgrammingCard(50, Action.ROTATE_LEFT));
ProgrammingCardDeck playerDeck = new ProgrammingCardDeck(cards);
playerTest = new Player(RobotID.ROBOT_1, "TestPlayer" ); playerTest = new Player(RobotID.ROBOT_1, "TestPlayer" );
} }
@ -37,8 +38,9 @@ public class PlayerTest {
playerTest.setPowerDownNextRound(false); playerTest.setPowerDownNextRound(false);
assertFalse(playerTest.getPowerDownNextRound()); assertFalse(playerTest.getPowerDownNextRound());
} }
@Test @Test
public void testSetInProgram(){ public void testSetInProgram() {
playerTest.setInProgram(cards); playerTest.setInProgram(cards);
assertEquals(Action.MOVE_1, playerTest.getProgramFromPlayer().get(0).getAction()); assertEquals(Action.MOVE_1, playerTest.getProgramFromPlayer().get(0).getAction());
assertEquals(Action.MOVE_2, playerTest.getProgramFromPlayer().get(1).getAction()); assertEquals(Action.MOVE_2, playerTest.getProgramFromPlayer().get(1).getAction());
@ -47,13 +49,13 @@ public class PlayerTest {
} }
@Test (expected = IllegalArgumentException.class) @Test (expected = IllegalArgumentException.class)
public void testSetInProgramWithToManyCards(){ public void testSetInProgramWithToManyCards() {
cards.add(new ProgrammingCard(10,Action.ROTATE_LEFT)); cards.add(new ProgrammingCard(10,Action.ROTATE_LEFT));
playerTest.setInProgram(cards); playerTest.setInProgram(cards);
} }
@Test @Test
public void testSetInDeck(){ public void testSetInDeck() {
cards.add(new ProgrammingCard(10,Action.ROTATE_LEFT)); cards.add(new ProgrammingCard(10,Action.ROTATE_LEFT));
ProgrammingCardDeck playerDeck = new ProgrammingCardDeck(cards); ProgrammingCardDeck playerDeck = new ProgrammingCardDeck(cards);
playerTest.setPlayerDeck(playerDeck); playerTest.setPlayerDeck(playerDeck);
@ -61,17 +63,17 @@ public class PlayerTest {
} }
@Test @Test
public void GetPlayerRobotId(){ public void getPlayerRobotId() {
assertEquals(RobotID.ROBOT_1, playerTest.getRobotID()); assertEquals(RobotID.ROBOT_1, playerTest.getRobotID());
} }
@Test @Test
public void GetPlayerName(){ public void getPlayerName() {
assertEquals("TestPlayer", playerTest.getName()); assertEquals("TestPlayer", playerTest.getName());
} }
@Test @Test
public void GetProgramFromPlayer(){ public void getProgramFromPlayer() {
playerTest.setInProgram(cards); playerTest.setInProgram(cards);
assertEquals(cards,playerTest.getProgram()); assertEquals(cards,playerTest.getProgram());
} }