Skip to content

Commit 0abb75a

Browse files
committed
docs: update type object
1 parent d26454a commit 0abb75a

File tree

1 file changed

+124
-8
lines changed

1 file changed

+124
-8
lines changed

type-object/README.md

Lines changed: 124 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ In plain words
3232

3333
> The Type Object pattern allows for the creation and management of flexible and extensible sets of related types dynamically, without modifying existing code.
3434
35-
Gameprogrammingpatterns.com says
35+
gameprogrammingpatterns.com says
3636

3737
> Define a type object class and a typed object class. Each type object instance represents a different logical type. Each typed object stores a reference to the type object that describes its type.
3838
@@ -44,7 +44,7 @@ In the provided code, the Type Object pattern is implemented in a mini candy-cru
4444

4545
Let's break down the key components of this implementation:
4646

47-
1. **Candy Class**: This class represents the 'type' object in this pattern. Each candy has a name, parent, points, and type. The type is an enum that can be either `CRUSHABLE_CANDY` or `REWARD_FRUIT`.
47+
1. **Candy Class**: This class represents the 'type' object in this pattern. Each `Candy` has a `name`, `parent`, `points`, and `type`. The `type` is an enum that can be either `CRUSHABLE_CANDY` or `REWARD_FRUIT`.
4848

4949
```java
5050
class Candy {
@@ -168,16 +168,129 @@ class CellPool {
168168
@Slf4j
169169
public class App {
170170
public static void main(String[] args) {
171-
// implementation
171+
var givenTime = 50; //50ms
172+
var toWin = 500; //points
173+
var pointsWon = 0;
174+
var numOfRows = 3;
175+
var start = System.currentTimeMillis();
176+
var end = System.currentTimeMillis();
177+
var round = 0;
178+
while (pointsWon < toWin && end - start < givenTime) {
179+
round++;
180+
var pool = new CellPool(numOfRows * numOfRows + 5);
181+
var cg = new CandyGame(numOfRows, pool);
182+
if (round > 1) {
183+
LOGGER.info("Refreshing..");
184+
} else {
185+
LOGGER.info("Starting game..");
186+
}
187+
cg.printGameStatus();
188+
end = System.currentTimeMillis();
189+
cg.round((int) (end - start), givenTime);
190+
pointsWon += cg.totalPoints;
191+
end = System.currentTimeMillis();
192+
}
193+
LOGGER.info("Game Over");
194+
if (pointsWon >= toWin) {
195+
LOGGER.info("" + pointsWon);
196+
LOGGER.info("You win!!");
197+
} else {
198+
LOGGER.info("" + pointsWon);
199+
LOGGER.info("Sorry, you lose!");
200+
}
172201
}
173202
}
174203
```
175204

176-
In this implementation, the Type Object pattern allows for the flexible creation of `Candy` objects. The type of each candy is determined at runtime by parsing a JSON file, which makes it easy to add, modify, or remove candy types without having to recompile the code.
205+
Let's break down what happens in `App` class.
206+
207+
1. The `main` method is the entry point of the application. It starts by initializing several variables:
208+
- `givenTime` is set to 50 milliseconds. This is the time limit for the game.
209+
- `toWin` is set to 500 points. This is the target score to win the game.
210+
- `pointsWon` is initialized to 0. This variable keeps track of the total points won so far.
211+
- `numOfRows` is set to 3. This is the number of rows in the game grid.
212+
- `start` and `end` are both set to the current system time in milliseconds. These variables are used to track the elapsed time.
213+
- `round` is initialized to 0. This variable keeps track of the current round number.
214+
215+
2. The game enters a loop that continues until either the player has won enough points (`pointsWon >= toWin`) or the time limit has been reached (`end - start < givenTime`).
216+
217+
3. At the start of each round, a new `CellPool` and `CandyGame` are created. The `CellPool` is initialized with a size based on the number of cells in the game grid (`numOfRows * numOfRows + 5`). The `CandyGame` is initialized with the number of rows and the `CellPool`.
218+
219+
4. If it's not the first round, a message "Refreshing.." is logged. If it is the first round, a message "Starting game.." is logged.
220+
221+
5. The current game status is printed by calling `cg.printGameStatus()`.
222+
223+
6. The `end` time is updated to the current system time.
224+
225+
7. The game round is played by calling `cg.round((int) (end - start), givenTime)`. The elapsed time and the time limit are passed as arguments.
226+
227+
8. The points won in the round are added to the total points.
177228

178-
## Class diagram
229+
9. The `end` time is updated again to the current system time.
179230

180-
![Type Object](./etc/typeobjectpattern.urm.png "Type Object")
231+
10. After the loop, a "Game Over" message is logged.
232+
233+
11. If the total points won is greater than or equal to the target score, a winning message is logged. Otherwise, a losing message is logged.
234+
235+
This is a simplified version of a game similar to Candy Crush, where the player tries to score as many points as possible within a given time limit. The game is played in rounds, and the player's score and the elapsed time are tracked throughout the game.
236+
237+
Console output:
238+
239+
```
240+
14:36:14.453 [main] INFO com.iluwatar.typeobject.App -- Starting game..
241+
14:36:14.455 [main] INFO com.iluwatar.typeobject.CandyGame --
242+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- cherry |
243+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- mango |
244+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- orange gum |
245+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame --
246+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- orange gum |
247+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- purple popsicle |
248+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- purple popsicle |
249+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame --
250+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- green jellybean |
251+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- green jellybean |
252+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame -- mango |
253+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame --
254+
14:36:14.458 [main] INFO com.iluwatar.typeobject.CandyGame --
255+
14:36:14.459 [main] INFO com.iluwatar.typeobject.CandyGame -- +20 points!
256+
...
257+
...
258+
...
259+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
260+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- green jellybean |
261+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- cherry |
262+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- green jellybean |
263+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
264+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- green jellybean |
265+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- green jellybean |
266+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- mango |
267+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
268+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- orange gum |
269+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- purple popsicle |
270+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- orange gum |
271+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
272+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
273+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- +20 points!
274+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
275+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- orange gum |
276+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- cherry |
277+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- green jellybean |
278+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
279+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- purple popsicle |
280+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- green jellybean |
281+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- mango |
282+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
283+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- orange gum |
284+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- purple popsicle |
285+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame -- orange gum |
286+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
287+
14:36:14.465 [main] INFO com.iluwatar.typeobject.CandyGame --
288+
14:36:14.465 [main] INFO com.iluwatar.typeobject.App -- Game Over
289+
14:36:14.465 [main] INFO com.iluwatar.typeobject.App -- 660
290+
14:36:14.465 [main] INFO com.iluwatar.typeobject.App -- You win!!
291+
```
292+
293+
In this implementation, the Type Object pattern allows for the flexible creation of `Candy` objects. The type of each candy is determined at runtime by parsing a JSON file, which makes it easy to add, modify, or remove candy types without having to recompile the code.
181294

182295
## Applicability
183296

@@ -188,6 +301,10 @@ This pattern can be used when:
188301
* Suitable for situations where the number of types is large and may change over time.
189302
* The difference between the different 'types' of objects is the data, not the behaviour.
190303

304+
## Tutorials
305+
306+
* [Types as Objects Pattern (Jon Pearce)](http://www.cs.sjsu.edu/~pearce/modules/patterns/analysis/top.htm)
307+
191308
## Known uses
192309

193310
* Java Collections Framework: Utilizing various collection types like List, Set, and Map.
@@ -218,5 +335,4 @@ Trade-offs:
218335
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
219336
* [Effective Java](https://amzn.to/4cGk2Jz)
220337
* [Java Design Patterns: A Hands-On Experience with Real-World Examples](https://amzn.to/3yhh525)
221-
* [Game Programming Patterns - Type Object](http://gameprogrammingpatterns.com/type-object.html)
222-
* [Types as Objects Pattern - Jon Pearce](http://www.cs.sjsu.edu/~pearce/modules/patterns/analysis/top.htm)
338+
* [Type Object (Game Programming Patterns)](http://gameprogrammingpatterns.com/type-object.html)

0 commit comments

Comments
 (0)