Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mkhattat/bitcode-SEM into…
Browse files Browse the repository at this point in the history
… doc/assignment3
  • Loading branch information
avanderlinden committed Nov 3, 2017
2 parents 4fa9eaa + eb84617 commit cadd59a
Show file tree
Hide file tree
Showing 45 changed files with 2,606 additions and 537 deletions.
174 changes: 174 additions & 0 deletions src/main/java/nl/tudelft/item/EasyItemFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package nl.tudelft.item;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

/**
* Factory used to create items for the board in easy mode.
*/
public class EasyItemFactory implements ItemFactory {

/**
* The lower bound used for randomly generating items.
*/
private int lowerBound;

/**
* The upper bound used for randomly generating items.
*/
private int upperBound;

/**
* Random used to generate random items by generating random ints.
* These ints will be between the lower bound (inclusive) and the upper bound (exclusive).
*/
private Random intGen;

/**
* Array containing the itemnames this factory can use.
*/
private ArrayList<String> itemNames;

/**
* HashMap containing the items this factory can possibly create.
*/
private HashMap<String, Item> itemHashMap;

/**
* Constructor for StandardItemFactory objects.
*/
public EasyItemFactory() {
itemHashMap = new HashMap<>();
itemHashMap.put("axe", new Axe());
itemHashMap.put("bone", new Bone());
itemHashMap.put("eye", new Eye());
itemHashMap.put("leaf", new Leaf());
itemHashMap.put("mask", new Mask());
itemHashMap.put("mouth", new Mouth());

itemNames = new ArrayList<>();
itemNames.add("axe");
itemNames.add("bone");
itemNames.add("eye");
itemNames.add("leaf");
itemNames.add("mask");
itemNames.add("mouth");

resetRandom();
}

/**
* Resets the upper and lower bound to the default values.
* Also re-initializes the intGen.
*/
public void resetRandom() {
lowerBound = 0;
upperBound = itemHashMap.size();
intGen = new Random();
}

/**
* Retrieves the amount of different items.
* @return The amount of different items.
*/
public int getItemCount() {
return itemHashMap.size();
}

/**
* Retrieves the lower bound used for generating random items.
* @return The lower bound used for generating random items.
*/
public int getLowerBound() {
return lowerBound;
}

/**
* Sets the lower bound used for generating random items.
* @param newLowerBound The new lower bound used for generating random items.
* @throws IllegalArgumentException Is thrown if the new lower bound is larger than
* or equal to the current upper bound, or if the new lower bound is negative.
*/
public void setLowerBound(int newLowerBound) throws IllegalArgumentException {
if (newLowerBound >= upperBound) {
throw new IllegalArgumentException(
"The lower bound may not be larger than or equal to the upper bound."
+ "\nCurrent upper bound: " + upperBound
+ ", new lower bound: " + newLowerBound + ".");
}
if (newLowerBound < 0) {
throw new IllegalArgumentException("The lower bound may not be negative."
+ "\nThe new lower bound: " + newLowerBound + ".");
}
lowerBound = newLowerBound;
}

/**
* Retrieves the upper bound used for generating random items.
* @return The upper bound used for generating random items.
*/
public int getUpperBound() {
return upperBound;
}

/**
* Sets the upper bound used for generating random items.
* @param newUpperBound The new upper bound used for generating random items.
* @throws IllegalArgumentException Is thrown if the new upper bound is smaller than
* or equal to the current lower bound, or if the new upper bound is negative.
*/
public void setUpperBound(int newUpperBound) throws IllegalArgumentException {
if (newUpperBound < 0) {
throw new IllegalArgumentException("The upper bound may not be negative."
+ "\nThe new upper bound: " + newUpperBound + ".");
}
if (newUpperBound <= lowerBound) {
throw new IllegalArgumentException(
"The upper bound may not be smaller than or equal to the lower bound."
+ "\nNew upper bound: " + newUpperBound
+ ", current lower bound: " + lowerBound + ".");
}
upperBound = newUpperBound;
}

/**
* Retrieves the random int generator used for generating random items.
* @return The random int generator used for generating random items.
*/
public Random getIntGen() {
return intGen;
}

/**
* Randomly creates one of the following items:
* Axe, Bone, Eye, Leaf, Mask or Mouth.
* @return A random item.
*/
public Item createRandomItem() {
int r = (intGen.nextInt(upperBound - lowerBound) + lowerBound) % itemHashMap.size();
return itemHashMap.get(itemNames.get(r));
}

/**
* Creates a new item based on the provided string (ignoring case).
* "Axe" results in an axe item
* "Bone" results in a bone item
* "Eye" results in an eye item
* "Leaf" results in a leaf item
* "Mask" results in a mask item
* "Mouth" results in a mouth item
* @param itemName The string representing the desired item.
* @return The item corresponding to the input string.
* @throws IllegalArgumentException In case the provided string does not match any item name.
*/
public Item createItem(final String itemName) throws IllegalArgumentException {
Item res = itemHashMap.get(itemName.toLowerCase());
if (res == null) {
throw new IllegalArgumentException("The provided item name: " + itemName
+ "\nDoes not match any of the following: "
+ "axe, bone, eye, leaf, mask or mouth.");
}
return res;
}
}
67 changes: 35 additions & 32 deletions src/main/java/nl/tudelft/pooralien/Controller/BackgroundTile.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package nl.tudelft.pooralien.Controller;

import nl.tu.delft.defpro.exception.NotExistingVariableException;
import nl.tudelft.pooralien.Launcher;

import java.awt.Color;

/**
Expand All @@ -13,7 +10,7 @@ public class BackgroundTile {
private int coordinateX;
private int coordinateY;
private Color colorBackgroundTile;
private int maxWidthAndHeight;
private Integer maxWidthAndHeight;

/**
* @param coordinateX must be [0,10]
Expand All @@ -22,33 +19,48 @@ public class BackgroundTile {
*/
public BackgroundTile(int coordinateX, int coordinateY, Color colorBackgroundTile) {
initWidthHeight();
if (!(coordinateX >= 0 && coordinateX <= maxWidthAndHeight)) {
throw new IllegalArgumentException("Coordinate X must be between -1 and 11");
}
if (!(coordinateY >= 0 && coordinateY <= maxWidthAndHeight)) {
throw new IllegalArgumentException("Coordinate Y must be between -1 and 11");
}

checkCoordinate(coordinateX);
checkCoordinate(coordinateY);

// instance check is needed because a null would make the backgroundTiles hidden,
// and as a result the game would be unplayable
if ((colorBackgroundTile == null)) {
throw new IllegalArgumentException("colorBackgroundTile should be a Color object");
}
checkColorBackgroundTile(colorBackgroundTile);

this.coordinateX = coordinateX;
this.coordinateY = coordinateY;
this.colorBackgroundTile = colorBackgroundTile;
}

private void initWidthHeight() {
try {
//TODO: Implement Config Boundries
maxWidthAndHeight = Launcher.getGameCfg().getIntegerValueOf("maxBoardWidth");
} catch (NotExistingVariableException e) {
System.out.println(e.getMessage());
e.printStackTrace();
/**
* Check if the coordinate is not smaller than 0 or bigger than maxWidthAndHeight variable.
* @param coordinate to be checked.
*/
private void checkCoordinate(int coordinate) {
if (!(coordinate >= 0 && coordinate <= maxWidthAndHeight)) {
throw new IllegalArgumentException("Coordinate must be between -1 and "
+ (maxWidthAndHeight + 1));
}
}

/**
* Check if the colorBackgroundTile is not null. If it was null then the backgroundTiles,
* would be invisible which would break the game.
* @param colorBackgroundTile object to be tested.
*/
private void checkColorBackgroundTile(Color colorBackgroundTile) {
if ((colorBackgroundTile == null)) {
throw new IllegalArgumentException("colorBackgroundTile should be a Color object");
}
}

private void initWidthHeight() {
final int min = 5;
final int max = 20;
final int standard = 10;
maxWidthAndHeight = GameConfig.getInteger("maxBoardWidth", min, max, standard);
}

/**
* Pre: coordinateX is bigger or equal than 0 and smaller or equal than 10.
* @param coordinateX can be changed so that the board structure can be changed.
Expand Down Expand Up @@ -111,18 +123,9 @@ public boolean equals(Object object) {
}
BackgroundTile backgroundTile = (BackgroundTile) object;

if (!(backgroundTile.getCoordinateX() == this.getCoordinateX())) {
return false;
}
if (!(backgroundTile.getCoordinateY() == this.getCoordinateY())) {
return false;
}

if (!(backgroundTile.getColorBackgroundTile().equals(this.getColorBackgroundTile()))) {
return false;
}

return true;
return backgroundTile.getCoordinateX() == this.getCoordinateX()
&& backgroundTile.getCoordinateY() == this.getCoordinateY()
&& backgroundTile.getColorBackgroundTile().equals(this.getColorBackgroundTile());
}

@Override
Expand Down
Loading

0 comments on commit cadd59a

Please sign in to comment.