Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions trampoline/src/main/java/com/iluwatar/trampoline/Trampoline.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import java.util.stream.Stream;

/**
* <p>Trampoline pattern allows to define recursive algorithms by iterative loop </p>
* Trampoline pattern allows to define recursive algorithms by iterative loop.
*
* <p>When get is called on the returned Trampoline, internally it will iterate calling ‘jump’
* on the returned Trampoline as long as the concrete instance returned is {@link #more(Trampoline)},
* stopping once the returned instance is {@link #done(Object)}.</p>
* on the returned Trampoline as long as the concrete instance returned is {@link
* #more(Trampoline)}, stopping once the returned instance is {@link #done(Object)}.
*
* <p>Essential we convert looping via recursion into iteration,
* the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.</p>
* the key enabling mechanism is the fact that {@link #more(Trampoline)} is a lazy operation.
*
* @param <T> is type for returning result.
*/
Expand All @@ -40,6 +42,8 @@ public interface Trampoline<T> {


/**
* Jump to next stage.
*
* @return next stage
*/
default Trampoline<T> jump() {
Expand All @@ -52,14 +56,16 @@ default T result() {
}

/**
* Checks if complete.
*
* @return true if complete
*/
default boolean complete() {
return true;
}

/**
* Created a completed Trampoline
* Created a completed Trampoline.
*
* @param result Completed result
* @return Completed Trampoline
Expand All @@ -70,7 +76,7 @@ static <T> Trampoline<T> done(final T result) {


/**
* Create a Trampoline that has more work to do
* Create a Trampoline that has more work to do.
*
* @param trampoline Next stage in Trampoline
* @return Trampoline with more work
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,21 @@

package com.iluwatar.trampoline;


import lombok.extern.slf4j.Slf4j;

/**
* <p>Trampoline pattern allows to define recursive algorithms by iterative loop </p>
* <p>it is possible to implement algorithms recursively in Java without blowing the stack
* and to interleave the execution of functions without hard coding them together or even using threads.</p>
* Trampoline pattern allows to define recursive algorithms by iterative loop.
*
* <p>It is possible to implement algorithms recursively in Java without blowing the stack
* and to interleave the execution of functions without hard coding them together or even using
* threads.
*/
@Slf4j
public class TrampolineApp {

/**
* Main program for showing pattern. It does loop with factorial function.
* */
*/
public static void main(String[] args) {
log.info("start pattern");
Integer result = loop(10, 1).result();
Expand Down
10 changes: 5 additions & 5 deletions twin/src/main/java/com/iluwatar/twin/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
/**
* Twin pattern is a design pattern which provides a standard solution to simulate multiple
* inheritance in java.
* <p>
* In this example, the essence of the Twin pattern is the {@link BallItem} class and
* {@link BallThread} class represent the twin objects to coordinate with each other(via the twin
*
* <p>In this example, the essence of the Twin pattern is the {@link BallItem} class and {@link
* BallThread} class represent the twin objects to coordinate with each other(via the twin
* reference) like a single class inheriting from {@link GameItem} and {@link Thread}.
*/

public class App {

/**
* Program entry point
*
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) throws Exception {
Expand Down
3 changes: 1 addition & 2 deletions twin/src/main/java/com/iluwatar/twin/BallThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
/**
* This class is a UI thread for drawing the {@link BallItem}, and provide the method for suspend
* and resume. It hold the reference of {@link BallItem} to delegate the draw task.
*
*/

public class BallThread extends Thread {
Expand All @@ -47,7 +46,7 @@ public void setTwin(BallItem twin) {
}

/**
* Run the thread
* Run the thread.
*/
public void run() {

Expand Down
2 changes: 1 addition & 1 deletion twin/src/main/java/com/iluwatar/twin/GameItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public abstract class GameItem {
private static final Logger LOGGER = LoggerFactory.getLogger(GameItem.class);

/**
* Template method, do some common logic before draw
* Template method, do some common logic before draw.
*/
public void draw() {
LOGGER.info("draw");
Expand Down
15 changes: 9 additions & 6 deletions typeobjectpattern/src/main/java/com/iluwatar/typeobject/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**<p>Type object pattern is the pattern we use when the OOP concept of creating a base class and
* inheriting from it just doesn't work for the case in hand. This happens when we either don't know
/**
* <p>Type object pattern is the pattern we use when the OOP concept of creating a base class and
* inheriting from it just doesn't work for the case in hand. This happens when we either don't know
* what types we will need upfront, or want to be able to modify or add new types conveniently w/o
* recompiling repeatedly. The pattern provides a solution by allowing flexible creation of required
* objects by creating one class, which has a field which represents the 'type' of the object.</p>
Expand All @@ -40,17 +41,19 @@
* Type. We have a json file {@link candy} which contains the details about the candies, and this is
* parsed to get all the different candies in {@link JsonParser}. The {@link Cell} class is what the
* game matrix is made of, which has the candies that are to be crushed, and contains information on
* how crushing can be done, how the matrix is to be reconfigured and how points are to be gained.
* how crushing can be done, how the matrix is to be reconfigured and how points are to be gained.
* The {@link CellPool} class is a pool which reuses the candy cells that have been crushed instead
* of making new ones repeatedly. The {@link CandyGame} class has the rules for the continuation of
* the game and the {@link App} class has the game itself.</p>
* the game and the {@link App} class has the game itself.</p>
*/

public class App {

private static final Logger LOGGER = LoggerFactory.getLogger(App.class);

/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
Expand All @@ -61,7 +64,7 @@ public static void main(String[] args) throws FileNotFoundException, IOException
var start = System.currentTimeMillis();
var end = System.currentTimeMillis();
var round = 0;
while (pointsWon < toWin && end - start < givenTime) {
while (pointsWon < toWin && end - start < givenTime) {
round++;
var pool = new CellPool(numOfRows * numOfRows + 5);
var cg = new CandyGame(numOfRows, pool);
Expand All @@ -72,7 +75,7 @@ public static void main(String[] args) throws FileNotFoundException, IOException
}
cg.printGameStatus();
end = System.currentTimeMillis();
cg.round((int)(end - start), givenTime);
cg.round((int) (end - start), givenTime);
pointsWon += cg.totalPoints;
end = System.currentTimeMillis();
}
Expand Down
22 changes: 12 additions & 10 deletions typeobjectpattern/src/main/java/com/iluwatar/typeobject/Candy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,38 @@
package com.iluwatar.typeobject;

/**
* The Candy class has a field type, which represents the 'type' of candy. The objects
* are created by parsing the candy.json file.
* The Candy class has a field type, which represents the 'type' of candy. The objects are created
* by parsing the candy.json file.
*/

public class Candy {

enum Type { crushableCandy, rewardFruit };


enum Type {
crushableCandy,
rewardFruit
}

String name;
Candy parent;
String parentName;
private int points;
private Type type;

Candy(String name, String parentName, Type type, int points) {
this.name = name;
this.parent = null;
this.type = type;
this.points = points;
this.parentName = parentName;
}

int getPoints() {
return this.points;
}

void setPoints(int a) {
this.points = a;
}

Type getType() {
return this.type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,45 @@

package com.iluwatar.typeobject;

import java.util.ArrayList;
import com.iluwatar.typeobject.Candy.Type;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The CandyGame class contains the rules for the continuation of the game and has
* the game matrix (field 'cells') and totalPoints gained during the game.
* The CandyGame class contains the rules for the continuation of the game and has the game matrix
* (field 'cells') and totalPoints gained during the game.
*/

public class CandyGame {

private static final Logger LOGGER = LoggerFactory.getLogger(CandyGame.class);

Cell[][] cells;
CellPool pool;
int totalPoints;

CandyGame(int num, CellPool pool) {
this.cells = new Cell[num][num];
this.pool = pool;
this.totalPoints = 0;
for (var i = 0; i < num; i++) {
for (var j = 0; j < num; j++) {
this.cells[i][j] = this.pool.getNewCell();
this.cells[i][j].xIndex = j;
this.cells[i][j].yIndex = i;
this.cells[i][j].positionX = j;
this.cells[i][j].positionY = i;
}
}
}

static String numOfSpaces(int num) {
String result = "";
for (var i = 0; i < num; i++) {
result += " ";
}
return result;
}

void printGameStatus() {
LOGGER.info("");
for (var i = 0; i < cells.length; i++) {
Expand All @@ -79,32 +79,32 @@ void printGameStatus() {
}
LOGGER.info("");
}
ArrayList<Cell> adjacentCells(int yIndex, int xIndex) {

ArrayList<Cell> adjacentCells(int y, int x) {
ArrayList<Cell> adjacent = new ArrayList<Cell>();
if (yIndex == 0) {
adjacent.add(this.cells[1][xIndex]);
if (y == 0) {
adjacent.add(this.cells[1][x]);
}
if (xIndex == 0) {
adjacent.add(this.cells[yIndex][1]);
if (x == 0) {
adjacent.add(this.cells[y][1]);
}
if (yIndex == cells.length - 1) {
adjacent.add(this.cells[cells.length - 2][xIndex]);
if (y == cells.length - 1) {
adjacent.add(this.cells[cells.length - 2][x]);
}
if (xIndex == cells.length - 1) {
adjacent.add(this.cells[yIndex][cells.length - 2]);
if (x == cells.length - 1) {
adjacent.add(this.cells[y][cells.length - 2]);
}
if (yIndex > 0 && yIndex < cells.length - 1) {
adjacent.add(this.cells[yIndex - 1][xIndex]);
adjacent.add(this.cells[yIndex + 1][xIndex]);
if (y > 0 && y < cells.length - 1) {
adjacent.add(this.cells[y - 1][x]);
adjacent.add(this.cells[y + 1][x]);
}
if (xIndex > 0 && xIndex < cells.length - 1) {
adjacent.add(this.cells[yIndex][xIndex - 1]);
adjacent.add(this.cells[yIndex][xIndex + 1]);
if (x > 0 && x < cells.length - 1) {
adjacent.add(this.cells[y][x - 1]);
adjacent.add(this.cells[y][x + 1]);
}
return adjacent;
}

boolean continueRound() {
for (var i = 0; i < this.cells.length; i++) {
if (this.cells[cells.length - 1][i].candy.getType().equals(Type.rewardFruit)) {
Expand All @@ -114,7 +114,7 @@ boolean continueRound() {
for (var i = 0; i < this.cells.length; i++) {
for (var j = 0; j < this.cells.length; j++) {
if (!this.cells[i][j].candy.getType().equals(Type.rewardFruit)) {
var adj = adjacentCells(i,j);
var adj = adjacentCells(i, j);
for (var a = 0; a < adj.size(); a++) {
if (this.cells[i][j].candy.name.equals(adj.get(a).candy.name)) {
return true;
Expand All @@ -125,13 +125,13 @@ boolean continueRound() {
}
return false;
}

void handleChange(int points) {
LOGGER.info("+" + points + " points!");
this.totalPoints += points;
this.totalPoints += points;
printGameStatus();
}

void round(int timeSoFar, int totalTime) {
var start = System.currentTimeMillis();
var end = System.currentTimeMillis();
Expand All @@ -148,9 +148,9 @@ void round(int timeSoFar, int totalTime) {
for (var i = 0; i < this.cells.length; i++) {
var j = cells.length - 1;
var points = 0;
while (j > 0) {
while (j > 0) {
points = this.cells[j][i].interact(this.cells[j - 1][i], this.pool, this.cells);
if (points != 0) {
if (points != 0) {
handleChange(points);
} else {
j = j - 1;
Expand All @@ -160,7 +160,7 @@ void round(int timeSoFar, int totalTime) {
for (var i = 0; i < this.cells.length; i++) {
var j = 0;
var points = 0;
while (j < cells.length - 1) {
while (j < cells.length - 1) {
points = this.cells[i][j].interact(this.cells[i][j + 1], this.pool, this.cells);
if (points != 0) {
handleChange(points);
Expand All @@ -172,5 +172,5 @@ void round(int timeSoFar, int totalTime) {
end = System.currentTimeMillis();
}
}

}
Loading