Skip to content

Commit

Permalink
Made size configurable, some refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-mauky committed May 5, 2015
1 parent d6f5712 commit 2009286
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 74 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Expand Up @@ -22,11 +22,13 @@ dependencies {

compile 'ch.qos.logback:logback-classic:1.1.2'

compile 'de.saxsys:mvvmfx:1.0.0'
compile 'de.saxsys:mvvmfx:1.2.0'

compile 'eu.lestard:grid:0.2.0'

compile 'eu.lestard:easy-di:0.2.0'
compile 'eu.lestard:easy-di:0.3.0'

compile 'eu.lestard:advanced-bindings:0.4.0'

// testing
testCompile "junit:junit:4.12"
Expand All @@ -37,6 +39,6 @@ dependencies {
}

task wrapper(type: Wrapper) {
gradleVersion = '2.2.1'
gradleVersion = '2.3'
}

4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Sat Feb 14 23:40:05 CET 2015
#Sat May 02 19:23:04 CEST 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
9 changes: 2 additions & 7 deletions src/main/java/eu/lestard/snakefx/config/Config.java
Expand Up @@ -11,12 +11,7 @@ public enum Config {
* The number of rows and columns of the grid. In this game the grid is a
* square and so the number of rows and columns are equal.
*/
ROW_AND_COLUMN_COUNT(20),

/**
* The size of the grid in pixel.
*/
GRID_SIZE_IN_PIXEL(500),
ROW_AND_COLUMN_COUNT(25),

/**
* The x coordinate of the starting point of the snake
Expand All @@ -37,7 +32,7 @@ public enum Config {

private Integer value;

private Config(final Integer value) {
Config(final Integer value) {
this.value = value;
}

Expand Down
25 changes: 8 additions & 17 deletions src/main/java/eu/lestard/snakefx/core/GameLoop.java
Expand Up @@ -11,9 +11,7 @@

import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

/**
* This class is the game loop of the game.
Expand All @@ -28,7 +26,7 @@ public class GameLoop {
private Timeline timeline;


private final List<Consumer<?>> actions = new ArrayList<>();
private final List<Runnable> actions = new ArrayList<>();

private final CentralViewModel viewModel;

Expand All @@ -45,10 +43,10 @@ public GameLoop(final CentralViewModel viewModel) {
/**
* Added Actions are called on every keyframe of the GameLoop. The order of invocation is not guaranteed.
*
* @param actions the action that gets called.
* @param action the action that gets called.
*/
public void addActions(final Consumer<?>... actions) {
this.actions.addAll(Arrays.asList(actions));
public void addAction(final Runnable action) {
this.actions.add(action);
}

/**
Expand All @@ -60,10 +58,8 @@ private void init() {

// in this place we can't use a direct binding as the ViewModel property
// can also be changed in other places.
timeline.statusProperty().addListener((observable, oldStatus,
newStatus) -> {
viewModel.gameloopStatus.set(newStatus);
});
timeline.statusProperty().addListener((observable, oldStatus, newStatus) ->
viewModel.gameloopStatus.set(newStatus));
}

/**
Expand All @@ -74,13 +70,8 @@ private KeyFrame buildKeyFrame() {
final int fps = viewModel.speed.get().getFps();
final Duration duration = Duration.millis(ONE_SECOND / fps);

final KeyFrame frame = new KeyFrame(duration, event -> {
actions.forEach(consumer -> {
consumer.accept(null);
});
});

return frame;
return new KeyFrame(duration, event ->
actions.forEach(Runnable::run));
}


Expand Down
6 changes: 3 additions & 3 deletions src/main/java/eu/lestard/snakefx/core/NewGameFunction.java
Expand Up @@ -5,15 +5,15 @@
import javafx.animation.Animation.Status;

import javax.inject.Singleton;
import java.util.function.Consumer;

/**
* The purpose of this function is to start a new Game.
*
* @author manuel.mauky
*/
@Singleton
public class NewGameFunction implements Consumer<Void> {
public class NewGameFunction implements Runnable {

private final CentralViewModel viewModel;
private final GridModel<State> gridModel;
private final Snake snake;
Expand All @@ -28,7 +28,7 @@ public NewGameFunction(final CentralViewModel viewModel, final GridModel<State>
}

@Override
public void accept(Void aVoid) {
public void run() {
viewModel.gameloopStatus.set(Status.STOPPED);

gridModel.getCells().forEach(cell -> cell.changeState(State.EMPTY));
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/eu/lestard/snakefx/core/Snake.java
Expand Up @@ -46,11 +46,10 @@ public Snake(final CentralViewModel viewModel, final GridModel<State> gridModel,

tail = new ArrayList<>();

gameLoop.addActions(x -> move());
gameLoop.addAction(this::move);

viewModel.snakeDirection.addListener( (observable,oldDirection,newDirection) -> {
Snake.this.changeDirection(newDirection);
});
viewModel.snakeDirection.addListener((observable,oldDirection,newDirection) ->
Snake.this.changeDirection(newDirection));
}

/**
Expand Down Expand Up @@ -79,10 +78,10 @@ public void init() {
* @param newDirection
*/
private void changeDirection(final Direction newDirection) {
if (!newDirection.hasSameOrientation(currentDirection)) {
nextDirection = newDirection;
}else{
if (newDirection.hasSameOrientation(currentDirection)) {
viewModel.snakeDirection.setValue(nextDirection);
} else {
nextDirection = newDirection;
}
}

Expand All @@ -99,9 +98,9 @@ void move() {
return;
}

boolean grow = false;
boolean snakeWillGrow = false;
if (newHead.getState().equals(State.FOOD)) {
grow = true;
snakeWillGrow = true;
}

Cell<State> lastField = head;
Expand All @@ -115,7 +114,7 @@ void move() {
lastField = f;
}

if (grow) {
if (snakeWillGrow) {
grow(lastField);
addPoints();
} else {
Expand Down
Expand Up @@ -35,12 +35,14 @@ public ListProperty<HighScoreEntry> highScoreEntries() {
return highScoreEntries;
}

public void addScore(final String name, final int points) {
public HighScoreEntry addScore(final String name, final int points) {
final HighScoreEntry entry = new HighScoreEntry(1, name, points);

highScoreEntries.add(entry);

updateList();

return entry;
}

private void updateList() {
Expand Down
Expand Up @@ -22,5 +22,9 @@ public class HighscoreView implements FxmlView<HighscoreViewModel>, Initializabl
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
tableView.setItems(viewModel.highScoreEntries());

viewModel.selectedEntry().addListener((observable, oldValue, newValue) -> {
tableView.getSelectionModel().select(newValue);
});
}
}
Expand Up @@ -6,7 +6,9 @@
import eu.lestard.snakefx.highscore.HighscoreManager;
import eu.lestard.snakefx.viewmodel.CentralViewModel;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;

import javax.inject.Singleton;
Expand All @@ -19,6 +21,8 @@ public class HighscoreViewModel implements ViewModel {
private final CentralViewModel centralViewModel;
private ListProperty<HighScoreEntry> highScoreEntries = new SimpleListProperty<>();

private ObjectProperty<HighScoreEntry> selectedEntry = new SimpleObjectProperty<>();

public HighscoreViewModel(CentralViewModel centralViewModel, HighscoreManager highscoreManager) {
this.centralViewModel = centralViewModel;

Expand Down Expand Up @@ -51,4 +55,8 @@ void gameFinished(){
public ObservableList<HighScoreEntry> highScoreEntries(){
return highScoreEntries;
}

public ObjectProperty<HighScoreEntry> selectedEntry() {
return selectedEntry;
}
}
Expand Up @@ -2,6 +2,7 @@


import de.saxsys.mvvmfx.ViewModel;
import eu.lestard.snakefx.highscore.HighScoreEntry;
import eu.lestard.snakefx.highscore.HighscoreManager;
import eu.lestard.snakefx.viewmodel.CentralViewModel;
import javafx.beans.property.BooleanProperty;
Expand All @@ -17,15 +18,17 @@
public class NewHighscoreViewModel implements ViewModel {

private final HighscoreManager highscoreManager;
private HighscoreViewModel highscoreViewModel;
private final CentralViewModel centralViewModel;

private StringProperty pointsLabelText = new SimpleStringProperty();

private BooleanProperty errorMessageVisible = new SimpleBooleanProperty();

public NewHighscoreViewModel(CentralViewModel centralViewModel, HighscoreManager highscoreManager){
public NewHighscoreViewModel(CentralViewModel centralViewModel, HighscoreManager highscoreManager, HighscoreViewModel highscoreViewModel){
this.centralViewModel = centralViewModel;
this.highscoreManager = highscoreManager;
this.highscoreViewModel = highscoreViewModel;
}

public ObservableStringValue pointsLabelText(){
Expand All @@ -37,33 +40,24 @@ public ObservableBooleanValue errorMessageVisible(){
}

public void addEntry(String playerName) {
if (!isNameValid(playerName)) {
if (isNameValid(playerName)) {
errorMessageVisible.set(false);
} else {
errorMessageVisible.set(true);
return;
} else {
errorMessageVisible.set(false);
}

highscoreManager.addScore(playerName, centralViewModel.points.get());
final HighScoreEntry highScoreEntry = highscoreManager.addScore(playerName, centralViewModel.points.get());
highscoreViewModel.selectedEntry().setValue(highScoreEntry);


centralViewModel.newHighscoreWindowOpen.set(false);
centralViewModel.highscoreWindowOpen.set(true);

}

private boolean isNameValid(final String name) {
if (name == null) {
return false;
}
if (name.isEmpty()) {
return false;
}
if (name.contains(",")) {
return false;
}

if (name.contains(";")) {
return false;
}

return true;
final boolean invalid = name == null || name.isEmpty() || name.contains(",") || name.contains(";");
return !invalid;
}
}
14 changes: 9 additions & 5 deletions src/main/java/eu/lestard/snakefx/view/main/MainViewModel.java
Expand Up @@ -2,9 +2,9 @@

import de.saxsys.mvvmfx.ViewModel;
import eu.lestard.grid.GridModel;
import eu.lestard.snakefx.config.Config;
import eu.lestard.snakefx.core.NewGameFunction;
import eu.lestard.snakefx.core.State;
import eu.lestard.snakefx.viewmodel.CentralViewModel;

import javax.inject.Singleton;

Expand All @@ -13,14 +13,18 @@ public class MainViewModel implements ViewModel {

private final GridModel<State> gridModel;

public MainViewModel( final GridModel<State> gridModel, NewGameFunction newGame) {
public MainViewModel(final CentralViewModel centralViewModel, final GridModel<State> gridModel, NewGameFunction newGame) {
this.gridModel = gridModel;

gridModel.setDefaultState(State.EMPTY);
gridModel.setNumberOfColumns(Config.ROW_AND_COLUMN_COUNT.get());
gridModel.setNumberOfRows(Config.ROW_AND_COLUMN_COUNT.get());

newGame.accept(null);
gridModel.numberOfColumns().bind(centralViewModel.gridSize);
gridModel.numberOfRows().bind(centralViewModel.gridSize);
//
// gridModel.setNumberOfColumns(Config.ROW_AND_COLUMN_COUNT.get());
// gridModel.setNumberOfRows(Config.ROW_AND_COLUMN_COUNT.get());

newGame.run();
}

public GridModel<State> getGridModel(){
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/eu/lestard/snakefx/view/menu/MenuView.java
Expand Up @@ -6,11 +6,13 @@
import eu.lestard.snakefx.view.about.AboutView;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.Window;

import java.net.URL;
import java.util.Optional;
import java.util.ResourceBundle;

public class MenuView implements FxmlView<MenuViewModel>, Initializable {
Expand All @@ -36,6 +38,20 @@ public void initialize(URL url, ResourceBundle resourceBundle) {

@FXML
public void newGame() {

ChoiceDialog<Integer> sizeChoiceDialog = new ChoiceDialog<>();

sizeChoiceDialog.setHeaderText("Enter the Size of the new Game");
sizeChoiceDialog.setTitle("New Game");
sizeChoiceDialog.setContentText("Size:");

sizeChoiceDialog.getItems().addAll(viewModel.sizeOptions());
sizeChoiceDialog.setSelectedItem(viewModel.newSize().get());

final Optional<Integer> newSizeOptional = sizeChoiceDialog.showAndWait();

newSizeOptional.ifPresent(viewModel.newSize()::setValue);

viewModel.newGame();
}

Expand Down

0 comments on commit 2009286

Please sign in to comment.