Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
af68db8
Add mvn exec:java target
cagdasgerede Nov 18, 2020
8066580
Update jdk14
cagdasgerede Nov 18, 2020
5586e52
Minor format update
cagdasgerede Dec 6, 2020
bf14533
Command type SHOW_SCORES is added.
abugraokkali Mar 31, 2021
3889d35
createShowScoresCommand added.(temporarily its content is same as pau…
abugraokkali Mar 31, 2021
41862e5
SHOW_SCORES command assigned to the H key.
abugraokkali Mar 31, 2021
7030a2d
empty openTable method is added.
abugraokkali Mar 31, 2021
f38f65f
openTable method is renamed
abugraokkali Mar 31, 2021
67774ba
showScores is filled, now it creates a new empty window when pressed …
abugraokkali Mar 31, 2021
b3f5fef
High-Scores window will open only while game is running
abugraokkali Mar 31, 2021
3556c0b
High-Scores window will open every time pressed H
abugraokkali Mar 31, 2021
14fd3d5
Dimensions and background color changed.
abugraokkali Mar 31, 2021
3c20b8d
Makefile is added
abugraokkali Mar 31, 2021
a849ff5
TetrisScoresView is created for high-score window.
abugraokkali Mar 31, 2021
5db1de1
showScores's content is moved to TetrisScoresView.
abugraokkali Mar 31, 2021
ab7bef3
Visibility bug fixed.
abugraokkali Mar 31, 2021
0a0a7f3
Minor changes.
abugraokkali Mar 31, 2021
2ad25e2
A test label added to score window.
abugraokkali Mar 31, 2021
860810b
Background and font color changed.
abugraokkali Apr 2, 2021
ee4341c
Score table is added.
abugraokkali Apr 2, 2021
2499da6
Score table reader method is added.
abugraokkali Apr 2, 2021
70a34ff
Score table type is changed to hash-map
abugraokkali Apr 2, 2021
fcde1c6
Some changes in the High-Score window.
abugraokkali Apr 3, 2021
0a84c04
Sort method added.
abugraokkali Apr 3, 2021
eba3d75
Minor changes.
abugraokkali Apr 4, 2021
f8be099
getMinScore added.
abugraokkali Apr 4, 2021
9c04fe1
Refactoring and detecting of game over state.
abugraokkali Apr 4, 2021
ad98323
NameInputView has been added.
abugraokkali Apr 4, 2021
f2df628
Preparation for a new method.
abugraokkali Apr 4, 2021
58f81a4
Minor changes to increase readability.
abugraokkali Apr 4, 2021
cc2bb96
To show NameInputView when game is over.
abugraokkali Apr 4, 2021
5375021
Condition added.
abugraokkali Apr 4, 2021
da36ba1
A text field and a button are added.
abugraokkali Apr 4, 2021
63deb09
To print score in the name input view.
abugraokkali Apr 4, 2021
84ab4ad
Submit button action is changed.
abugraokkali Apr 4, 2021
c06444f
removeMin and putEntry methods are added.
abugraokkali Apr 4, 2021
e38975d
Small changes.
abugraokkali Apr 5, 2021
4522e67
Update method is added.
abugraokkali Apr 5, 2021
5ad0856
Clean-up and some minor changes.
abugraokkali Apr 6, 2021
14ebafe
Exception handling
abugraokkali Apr 6, 2021
d0affd3
max size is added.
abugraokkali Apr 6, 2021
192e17b
control whether the name is added or not.
abugraokkali Apr 6, 2021
4bf1e61
Submit only works when name is not empty
abugraokkali Apr 6, 2021
18ada9b
isAdded control
abugraokkali Apr 6, 2021
367f348
Making the list persistent.
abugraokkali Apr 6, 2021
e4caf8e
isFull method is added
abugraokkali Apr 6, 2021
2515ada
bugfix
abugraokkali Apr 6, 2021
061e3ce
bugfix
abugraokkali Apr 6, 2021
051b216
Test cases are deleted.
abugraokkali Apr 8, 2021
65dab72
Test dependency added.
abugraokkali Apr 8, 2021
f9f70e6
Minor changes.
abugraokkali Apr 8, 2021
bad6a3c
Minor changes.
abugraokkali Apr 8, 2021
767b3ed
Unit tests are added.
abugraokkali Apr 8, 2021
a3130ad
unnecessary print deleted.
abugraokkali Apr 8, 2021
720b3d7
New option shortcut is added.
abugraokkali Apr 8, 2021
79e5a39
Comments
abugraokkali Apr 8, 2021
4462c7d
Changes on gui
abugraokkali Apr 8, 2021
4459bc5
New screenshots are added.
abugraokkali Apr 9, 2021
dd7463f
Makefile is deleted.
abugraokkali Apr 9, 2021
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: 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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@

![alt tag](img/screenshot_gameover.png)

![alt tag](img/screenshot_name_input.png)

![alt tag](img/screenshot_score_table.png)

## How to build it ?

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 All @@ -34,6 +48,8 @@ Execute **mvn clean package assembly:single** to build the release package.

- P - Pause the current game

- H - Show the high scores

- M - Mute sound

- PAGE UP - Increase the volume
Expand Down
Binary file added img/screenshot_name_input.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 img/screenshot_score_table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 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,11 @@
<artifactId>commons-lang3</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.4.2</version>
</dependency>

</dependencies>
</project>
Empty file added score_table.txt
Empty file.
5 changes: 5 additions & 0 deletions src/main/java/spypunk/tetris/service/TetrisService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public interface TetrisService {
void pause();

void mute();

void showScores();

void takeName();

}
33 changes: 32 additions & 1 deletion src/main/java/spypunk/tetris/service/TetrisServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import spypunk.tetris.model.ShapeType;
import spypunk.tetris.model.Tetris;
import spypunk.tetris.model.Tetris.State;
import spypunk.tetris.ui.view.TetrisNameInputView;
import spypunk.tetris.ui.view.TetrisScoresView;
import spypunk.tetris.model.TetrisEvent;
import spypunk.tetris.model.TetrisInstance;

Expand Down Expand Up @@ -79,7 +81,7 @@ public void update() {
public void pause() {
tetris.setState(tetris.getState().onPause());
}

@Override
public void move(final Movement movement) {
if (isMovementAllowed()) {
Expand Down Expand Up @@ -332,4 +334,33 @@ private int getLevelSpeed(final int level) {
private boolean isMovementAllowed() {
return isTetrisRunning() && !isCurrentShapeLocked();
}

TetrisScoresView tetrisScoresView=new TetrisScoresView();
@Override
public void showScores() {
if(tetris.getState().equals(State.RUNNING)){
tetris.setState(tetris.getState().onPause());
}
if(tetrisNameInputView.isAdded==false && !tetrisNameInputView.getName().equals("")){
String name=tetrisNameInputView.getName();
Integer score=tetris.getScore();
tetrisScoresView.putScoreAndName(name, score);
tetrisNameInputView.isAdded=true;
}
tetrisScoresView.show();
}

TetrisNameInputView tetrisNameInputView=new TetrisNameInputView();
@Override
public void takeName() {
if(tetrisScoresView.isFull()){
if(tetris.getScore()>tetrisScoresView.getMinScore()){
tetrisNameInputView.show(tetris.getScore());
}
}
else{
tetrisNameInputView.show(tetris.getScore());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public enum TetrisControllerCommandType {
OPEN_PROJECT_URL,
SHAPE_LOCKED,
GAME_OVER,
ROWS_COMPLETED
ROWS_COMPLETED,
SHOW_SCORES
}

TetrisControllerCommand getTetrisControllerCommand(TetrisControllerCommandType tetrisControllerCommandType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public TetrisControllerCommandCacheImpl(final TetrisService tetrisService,
tetrisControllerCommands.put(TetrisControllerCommandType.SHAPE_LOCKED, createShapeLockedCommand());
tetrisControllerCommands.put(TetrisControllerCommandType.GAME_OVER, createGameOverCommand());
tetrisControllerCommands.put(TetrisControllerCommandType.ROWS_COMPLETED, createRowsCompletedCommand());
tetrisControllerCommands.put(TetrisControllerCommandType.SHOW_SCORES, createShowScoresCommand());

}

@Override
Expand Down Expand Up @@ -93,6 +95,19 @@ private TetrisControllerCommand createPauseCommand() {
}
};
}
private TetrisControllerCommand createShowScoresCommand() {
return () -> {
tetrisService.showScores();

final State state = tetris.getState();

if (State.PAUSED.equals(state)) {
soundService.pauseMusic();
} else if (State.RUNNING.equals(state)) {
soundService.resumeMusic();
}
};
}

private TetrisControllerCommand createMoveCommand(final Movement movement) {
return () -> tetrisService.move(movement);
Expand All @@ -114,7 +129,10 @@ private TetrisControllerCommand createMuteCommand() {
}

private TetrisControllerCommand createGameOverCommand() {
return () -> soundService.playMusic(Sound.GAME_OVER);
return () -> {
soundService.playMusic(Sound.GAME_OVER);
tetrisService.takeName();
};
}

private TetrisControllerCommand createRowsCompletedCommand() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public TetrisControllerInputHandlerImpl(final TetrisControllerCommandCache tetri
releasedKeyEventCommandTypes.put(KeyEvent.VK_PAGE_UP, TetrisControllerCommandType.INCREASE_VOLUME);
releasedKeyEventCommandTypes.put(KeyEvent.VK_PAGE_DOWN, TetrisControllerCommandType.DECREASE_VOLUME);
releasedKeyEventCommandTypes.put(KeyEvent.VK_CONTROL, TetrisControllerCommandType.HARD_DROP);
releasedKeyEventCommandTypes.put(KeyEvent.VK_H, TetrisControllerCommandType.SHOW_SCORES);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ public TetrisMainViewImpl(final TetrisController tetrisController,
frame.add(centerPanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.pack();

frame.setLocationRelativeTo(null);
}

Expand Down
101 changes: 101 additions & 0 deletions src/main/java/spypunk/tetris/ui/view/TetrisNameInputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package spypunk.tetris.ui.view;

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Font;

import static spypunk.tetris.ui.constants.TetrisUIConstants.DEFAULT_FONT_COLOR;
import spypunk.tetris.ui.font.cache.FontCacheImpl;

public class TetrisNameInputView{

private final JFrame frame;
private final Container contentPane;
private final JPanel firstPanel;
private final JButton submitButton;
private final JTextField textField;
private final JTextArea textArea;

private final FontCacheImpl fontCache;
private final Font font;

private String name="";
public boolean isAdded=false;;

public TetrisNameInputView(){

frame=new JFrame();
contentPane=frame.getContentPane();
contentPane.setBackground(Color.BLACK);
frame.setPreferredSize(new Dimension(400, 200));
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);

firstPanel=new JPanel();
textField = new JTextField(10);
submitButton = new JButton("SUBMIT");
textArea=new JTextArea();

fontCache=new FontCacheImpl();
font=fontCache.getDefaultFont();
}

public void show(int score){
print(score);
frame.setVisible(true);
}

private void print(int score){

submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
name=textField.getText();
if(!name.equals("")){
if(isAdded){
isAdded=false;
}
frame.setVisible(false);
}
}
});

submitButton.setBackground(Color.BLACK);
submitButton.setForeground(DEFAULT_FONT_COLOR);
submitButton.setFont(font);

textField.setBackground(Color.BLACK);
textField.setForeground(DEFAULT_FONT_COLOR);
textField.setFont(font);
textField.setText("");

textArea.setBackground(Color.BLACK);
textArea.setForeground(DEFAULT_FONT_COLOR);
textArea.setFont(font);

textArea.setText("\n YOU ARE IN TOP 5 !\n\n SCORE : "+score+"\n\n PLEASE ENTER YOUR NAME\n");
textArea.setEditable(false);

firstPanel.setBackground(Color.BLACK);
firstPanel.add(textField);
firstPanel.add(submitButton);

frame.add(firstPanel,BorderLayout.CENTER);
frame.add(textArea,BorderLayout.NORTH);
}
public String getName(){
return name;
}

}
64 changes: 64 additions & 0 deletions src/main/java/spypunk/tetris/ui/view/TetrisScoresTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package spypunk.tetris.ui.view;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;

public class TetrisScoresTest {

private TetrisScoresView tsv;
@BeforeEach
public void setUp(){
tsv=new TetrisScoresView();
}
@Test
public void testGetMin(){
tsv.hashMap.put("A",2);
tsv.hashMap.put("B",1);
tsv.hashMap.put("C",3);
assertEquals(1,tsv.getMinScore());
}
@Test
public void testGetMinWithEmptyList(){
//If list is empty then minimum score is 0.
assertEquals(0,tsv.getMinScore());
}
@Test
public void testPutScoreAndName(){
//When the number of elements in the list is less than 5.
tsv.putScoreAndName("A",5);
assertTrue(tsv.hashMap.containsKey("A"));
}
@Test
public void testPutScoreAndNameWithFullList(){
//We expect that when list is full, if F is added then the element
// has minimum score is removed.
tsv.hashMap.put("A",1);
tsv.hashMap.put("B",2);
tsv.hashMap.put("C",3);
tsv.hashMap.put("D",4);
tsv.hashMap.put("E",5);
tsv.putScoreAndName("F",6);
assertTrue(tsv.hashMap.containsKey("F"));
assertFalse(tsv.hashMap.containsKey("A"));
}
@Test
public void testIsFull(){
//When the number of elements in the list is equal to 5.
tsv.hashMap.put("A",1);
tsv.hashMap.put("B",2);
tsv.hashMap.put("C",3);
tsv.hashMap.put("D",4);
tsv.hashMap.put("E",5);
assertTrue(tsv.isFull());
}
@Test
public void testRemoveMinScoreElement(){
tsv.hashMap.put("A",1);
tsv.hashMap.put("B",2);
tsv.hashMap.put("C",3);
tsv.removeMinScoreElement();
assertFalse(tsv.hashMap.containsKey("A"));
}

}
Loading