Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/achievements #2

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ hs_err_pid*
/.project
/.settings/
/tetris.log
.idea
tetris.iml
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: java
jdk:
- oraclejdk8
- oraclejdk14
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ You will need a Java JDK 8+ and maven 3+.

Execute **mvn clean package assembly:single** to build the release package.

# How to run it ?

**mvn clean**
**mvn package**
**mvn exec:java**

## How to play ?

- SPACE - Start a new game
Expand Down
25 changes: 25 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@
<tagNameFormat>@{project.version}</tagNameFormat>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>spypunk.tetris.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>

Expand Down Expand Up @@ -146,5 +154,22 @@
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.27.0</version>
</dependency>
</dependencies>
</project>
30 changes: 30 additions & 0 deletions src/main/java/spypunk/tetris/AchievementTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package spypunk.tetris;

import org.junit.jupiter.api.Test;
import spypunk.tetris.model.Tetris;
import spypunk.tetris.model.TetrisInstance;

import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class AchievementTest {

@Test
void testAchievementNotUnlocked() {

Tetris t = mock(Tetris.class);
TetrisInstance ti = mock(TetrisInstance.class);

assertEquals(ti.getAchievementCount(), 0);
assertFalse(ti.isAchievementUnlocked_ROW());
assertFalse(ti.isAchievementUnlocked_SCORE());

}

@Test
void testAchievementSCORE() {

}

}
65 changes: 65 additions & 0 deletions src/main/java/spypunk/tetris/model/Tetris.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.awt.Point;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -137,6 +138,70 @@ public void setCompletedRows(final int completedRows) {
tetrisInstance.setCompletedRows(completedRows);
}

public int getAchievementCount() {
return tetrisInstance.getAchievementCount();
}

public void setAchievementCount(final int achievementCount) {
tetrisInstance.setAchievementCount(achievementCount);
}

public ArrayList<TetrisAchievement> getAchievements() {
return tetrisInstance.getAchievements();
}

public void addAchievement(TetrisAchievement ta) {
tetrisInstance.addAchievement(ta);
}

public boolean isRowAboveN() {
return tetrisInstance.IsRowAboveN();
}

public void setRowAboveN() {
tetrisInstance.setRowAboveN();
}

public boolean isScoreAboveN() {
return tetrisInstance.IsScoreAboveN();
}

public void setScoreAboveN() {
tetrisInstance.setScoreAboveN();
}

public int getNForRow() {
return tetrisInstance.getNForRow();
}

public void setNForRow(int n) {
tetrisInstance.setNForRow(n);
}

public int getNForScore() {
return tetrisInstance.getNForScore();
}

public void setNForScore(int n) {
tetrisInstance.setNForScore(n);
}

public boolean getAchievementUnlocked_ROW() {
return tetrisInstance.isAchievementUnlocked_ROW();
}

public void setAchievementUnlocked_ROW() {
tetrisInstance.setAchievementUnlocked_ROW();
}

public boolean getAchievementUnlocked_SCORE() {
return tetrisInstance.isAchievementUnlocked_SCORE();
}

public void setAchievementUnlocked_SCORE() {
tetrisInstance.setAchievementUnlocked_SCORE();
}

public int getSpeed() {
return tetrisInstance.getSpeed();
}
Expand Down
66 changes: 66 additions & 0 deletions src/main/java/spypunk/tetris/model/TetrisAchievement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package spypunk.tetris.model;

import java.util.Map;

public class TetrisAchievement {
private Tetris tetris;
private String name;
private String message;
String achievementType;
private ShapeType shapeType;
private int threshold;
private boolean achievementUnlocked;

public ShapeType getShapeType() {
return shapeType;
}

public String getName() {
return name;
}

public Tetris getTetris() {
return tetris;
}

public String getAchievementType() {
return achievementType;
}

public String getMessage() {
return message;
}

public int getThreshold() {
return threshold;
}

public boolean isAchievementUnlocked() {
return achievementUnlocked;
}

public void setAchievementUnlocked(boolean exp) {
this.achievementUnlocked = exp;
}

public TetrisAchievement(String name, String message, String achievementType, int threshold) {
shapeType = null;
achievementUnlocked = false;
this.name = name;
this.message = message;
this.threshold = threshold;
}

public TetrisAchievement(String name, String message, ShapeType shapeType, int threshold) {
/*
In this type of achievement, programmer is able to count
how many times of using any shape type. Any type and any threshold value
can unlock such an achievement.
*/
achievementUnlocked = false;
this.name = name;
this.message = message;
this.threshold = threshold;
}

}
35 changes: 35 additions & 0 deletions src/main/java/spypunk/tetris/model/TetrisAchievementChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package spypunk.tetris.model;

import spypunk.tetris.ui.view.TetrisAchievementsView;

import java.util.Map;

public class TetrisAchievementChecker {
private TetrisAchievement ta;

public TetrisAchievementChecker(TetrisAchievement ta) {
this.ta = ta;
}

public void checkAchievement() {
if (ta.getShapeType() == null) { // that means this achievement is related to score or row
if (ta.getAchievementType().equals("SCORE")) {
ta.getTetris().setNForScore(ta.getThreshold());
// controls can be done in my previous methods.
}
if (ta.getAchievementType().equals("ROW")) {
ta.getTetris().setNForRow(ta.getThreshold());
}
}
else {
for (Map.Entry<ShapeType, Integer> entry: ta.getTetris().getStatistics().entrySet()) {
if (entry.getKey().equals(ta.getShapeType()) && entry.getValue() >= ta.getThreshold()) {
if (!ta.isAchievementUnlocked()) {
ta.setAchievementUnlocked(true);
ta.getTetris().addAchievement(ta);
}
}
}
}
}
}
82 changes: 78 additions & 4 deletions src/main/java/spypunk/tetris/model/TetrisInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
package spypunk.tetris.model;

import java.awt.Point;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -44,6 +41,19 @@ public class TetrisInstance {

private int completedRows;

private int achievementCount;

private ArrayList<TetrisAchievement> achievements = new ArrayList<>();
private boolean rowAboveN = false;
private int nForRow;
private boolean scoreAboveN = false;
private int nForScore;

// I need extra booleans in order for the game loop does
// not update the achievements when they are unlocked once.
private boolean achievementUnlocked_ROW = false;
private boolean achievementUnlocked_SCORE = false;

private int speed;

private int currentGravityFrame;
Expand Down Expand Up @@ -103,6 +113,70 @@ public int getScore() {
return score;
}

public int getAchievementCount() {
return achievementCount;
}

public void setAchievementCount(int achievementCount) {
this.achievementCount = achievementCount;
}

public boolean IsRowAboveN() {
return getNForRow() <= getCompletedRows();
}

public void setRowAboveN() {
this.rowAboveN = !this.rowAboveN;
}

public boolean IsScoreAboveN() {
return getNForScore() <= getScore();
}

public void setScoreAboveN() {
this.scoreAboveN = !this.scoreAboveN;
}

public int getNForRow() {
return nForRow;
}

public void setNForRow(int n) {
this.nForRow = n;
}

public int getNForScore() {
return nForScore;
}

public void setNForScore(int n) {
this.nForScore = n;
}

public boolean isAchievementUnlocked_ROW() {
return achievementUnlocked_ROW;
}

public void setAchievementUnlocked_ROW() {
achievementUnlocked_ROW = !achievementUnlocked_ROW;
}

public boolean isAchievementUnlocked_SCORE() {
return achievementUnlocked_SCORE;
}

public void setAchievementUnlocked_SCORE() {
achievementUnlocked_SCORE = !achievementUnlocked_SCORE;
}

public ArrayList<TetrisAchievement> getAchievements() {
return achievements;
}

public void addAchievement(TetrisAchievement ta) {
this.achievements.add(ta);
}

public void setScore(final int score) {
this.score = score;
}
Expand Down
Loading