Skip to content

Achievement System Added #29

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

Open
wants to merge 2 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
25 changes: 25 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="bin/main" path="src/main/java">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/main" path="src/main/resources">
<attributes>
<attribute name="gradle_scope" value="main"/>
<attribute name="gradle_used_by_scope" value="main,test"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/java">
<attributes>
<attribute name="gradle_scope" value="test"/>
<attribute name="gradle_used_by_scope" value="test"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
34 changes: 34 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaCheckers</name>
<comment>Project JavaCheckers-master created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
<filteredResources>
<filter>
<id>1617895653003</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
13 changes: 13 additions & 0 deletions .settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
arguments=
auto.sync=false
build.scans.enabled=false
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=C\:/Program Files/Java/jdk-14.0.2
jvm.arguments=
offline.mode=false
override.workspace.settings=true
show.console.view=true
show.executions.view=true
4 changes: 4 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.source=1.8
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
Binary file added bin/main/achievements/AllUnlockedAch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/achievements/LongGameAch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/achievements/NumberOfMovesAch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/achievements/ShortGameAch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/board.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pawns/BLACK_PAWN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pawns/BLACK_QUEEN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pawns/WHITE_PAWN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pawns/WHITE_QUEEN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pawns/light_BLACK_PAWN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pawns/light_BLACK_QUEEN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pawns/light_WHITE_PAWN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pawns/light_WHITE_QUEEN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/main/pl/nogacz/checkers/Checkers.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added bin/main/pl/nogacz/checkers/board/Board$1.class
Binary file not shown.
Binary file added bin/main/pl/nogacz/checkers/board/Board$2.class
Binary file not shown.
Binary file added bin/main/pl/nogacz/checkers/board/Board.class
Binary file not shown.
Binary file not shown.
Binary file added bin/main/pl/nogacz/checkers/pawns/Pawn.class
Binary file not shown.
Binary file added bin/main/pl/nogacz/checkers/pawns/PawnClass.class
Binary file not shown.
Binary file added bin/main/pl/nogacz/checkers/pawns/PawnColor.class
Binary file not shown.
Binary file added bin/main/pl/nogacz/checkers/pawns/PawnMoves.class
Binary file not shown.
Binary file added bin/main/screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ repositories {
}

dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
// Use JUnit Jupiter API for testing.
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'

// Use JUnit Jupiter Engine for testing.
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.2'
implementation 'javax.xml.bind:jaxb-api:2.3.1'

implementation 'commons-cli:commons-cli:1.4'
Expand Down Expand Up @@ -59,6 +63,8 @@ test {
showStandardStreams = true
}

useJUnitPlatform()

finalizedBy jacocoTestReport
}

Expand Down
146 changes: 146 additions & 0 deletions src/main/java/pl/nogacz/checkers/achievements/Achievement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package pl.nogacz.checkers.achievements;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.*;


public class Achievement {

private String AchName;
private boolean isAchUnlocked;

public Achievement(){}

public Achievement(String AchName, boolean isAchUnlocked){
this.AchName = AchName;
this.isAchUnlocked = isAchUnlocked;
}

public void checkAchievement(Achievement [] achievements, int countOfMoves, long countOfTime){}

public String getAchName(){
return AchName;
}

public boolean getisAchUnlocked(){
return isAchUnlocked;
}

public void UnlockAch(){
this.isAchUnlocked = true;
}

public void LockAch(){
this.isAchUnlocked = false;
}

public void showAchievement(){
try{
JPanel panel = new JPanel();
BufferedImage image = ImageIO.read(new File( "./src/main/resources/achievements/" + AchName + ".png" ));
JLabel label = new JLabel(new ImageIcon(image));
panel.add(label);

JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}catch(Exception e){};

}

public static void generateAchievements(int countOfMoves, long countOfTime){
Achievement [] allAchievements = new Achievement[4];
allAchievements[0] = new AllUnlockedAch("AllUnlockedAch",false);
allAchievements[1] = new LongGameAch("LongGameAch",false);
allAchievements[2] = new NumberOfMovesAch("NumberOfMovesAch",false);
allAchievements[3] = new ShortGameAch("ShortGameAch",false);

readSaveFile(allAchievements);

for(int i=1 ; i<4 ; i++)
allAchievements[i].checkAchievement(allAchievements, countOfMoves, countOfTime);

allAchievements[0].checkAchievement(allAchievements, countOfMoves, countOfTime);

writeSaveFile(allAchievements);

}

public static void readSaveFile(Achievement [] achievements){
try{
File f = new File("Achievements_Save.txt");
String [] lineList = new String [5];

if(f.exists()){
BufferedReader br = new BufferedReader(new FileReader(f));
String line="";

for(int i=0; ((line = br.readLine()) != null) ; i++){
lineList[i]=line;
}

String fileContents="";

for(int i=0; i<4; i++){
fileContents = fileContents + lineList[i];
if( lineList[i].substring( lineList[i].indexOf(':') + 1 ).equals("Unlocked") ){
achievements[i].UnlockAch();
}
}

if( !(""+fileContents.hashCode()).equals( lineList[5] ) ){
for (Achievement ach : achievements) {
ach.LockAch();
}

f.delete();
br.close();
throw new Exception("Save file corrupted.");
}

br.close();

}
}catch(Exception e){};
}

public static void writeSaveFile(Achievement [] achievements){
try{
File f = new File("Achievements_Save.txt");
f.delete();
f.createNewFile();

FileOutputStream fos = new FileOutputStream(f);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

String fileContents="";

for( Achievement ach : achievements){
bw.write(ach.getAchName()+":");
fileContents = fileContents + ach.getAchName()+":";

if(ach.getisAchUnlocked()){
bw.write("Unlocked");
fileContents = fileContents + "Unlocked";
}else{
bw.write("Locked");
fileContents = fileContents + "Locked";
}

bw.newLine();
}

bw.write(""+fileContents.hashCode());

bw.close();

}catch(Exception e){};
}

}
26 changes: 26 additions & 0 deletions src/main/java/pl/nogacz/checkers/achievements/AllUnlockedAch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pl.nogacz.checkers.achievements;

public class AllUnlockedAch extends Achievement{

public AllUnlockedAch(String AchName, boolean isAchUnlocked){
super(AchName, isAchUnlocked);
}

public void checkAchievement(Achievement [] achievements, int countOfMoves, long countOfTime){
if(achievements == null)
throw new IllegalArgumentException();

boolean unlock = true;

for(int i=1 ; i< achievements.length ; i++){
if(achievements[i].getisAchUnlocked() == false)
unlock = false;
}

if(unlock == true && getisAchUnlocked() == false){
UnlockAch();
showAchievement();
}
}

}
24 changes: 24 additions & 0 deletions src/main/java/pl/nogacz/checkers/achievements/LongGameAch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package pl.nogacz.checkers.achievements;

public class LongGameAch extends Achievement{

public LongGameAch(String AchName, boolean isAchUnlocked){
super(AchName, isAchUnlocked);
}

public void checkAchievement(Achievement [] achievements, int countOfMoves, long countOfTime){
if(achievements == null)
throw new IllegalArgumentException();

boolean unlock = false;

if(countOfTime > 120)
unlock = true;

if(unlock == true && getisAchUnlocked() == false){
UnlockAch();
showAchievement();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pl.nogacz.checkers.achievements;

public class NumberOfMovesAch extends Achievement{

public NumberOfMovesAch(String AchName, boolean isAchUnlocked){
super(AchName, isAchUnlocked);
}

public void checkAchievement(Achievement [] achievements, int countOfMoves, long countOfTime){
if(achievements == null)
throw new IllegalArgumentException();

boolean unlock = false;

if(countOfMoves > 25)
unlock = true;

if(unlock == true && getisAchUnlocked() == false){
UnlockAch();
showAchievement();
}

}

}
25 changes: 25 additions & 0 deletions src/main/java/pl/nogacz/checkers/achievements/ShortGameAch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package pl.nogacz.checkers.achievements;

public class ShortGameAch extends Achievement{

public ShortGameAch(String AchName, boolean isAchUnlocked){
super(AchName, isAchUnlocked);
}

public void checkAchievement(Achievement [] achievements, int countOfMoves, long countOfTime){
if(achievements == null)
throw new IllegalArgumentException();

boolean unlock = false;

if(countOfTime < 45)
unlock = true;

if(unlock == true && getisAchUnlocked() == false){
UnlockAch();
showAchievement();
}

}

}
Loading