Commit b023b54e authored by doc. Ing. Jaroslav Porubän PhD.'s avatar doc. Ing. Jaroslav Porubän PhD.
Browse files

4. prednaska

parent c23befca
......@@ -5,9 +5,12 @@
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.postgresql:postgresql:42.2.19" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.checkerframework:checker-qual:3.5.0" level="project" />
</component>
</module>
\ No newline at end of file
......@@ -22,6 +22,11 @@
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.19</version>
</dependency>
</dependencies>
</project>
......@@ -5,7 +5,7 @@ import sk.tuke.gamestudio.game.mines.core.Field;
public class Main {
public static void main(String[] args) {
Field field = new Field(9, 9, 10);
Field field = new Field(9, 9, 1);
ConsoleUI ui = new ConsoleUI(field);
ui.play();
}
......
......@@ -4,23 +4,33 @@ import sk.tuke.gamestudio.game.mines.core.Clue;
import sk.tuke.gamestudio.game.mines.core.Field;
import sk.tuke.gamestudio.game.mines.core.GameState;
import sk.tuke.gamestudio.game.mines.core.Tile;
import sk.tuke.gamestudio.game.mines.entity.Score;
import sk.tuke.gamestudio.game.mines.service.ScoreService;
import sk.tuke.gamestudio.game.mines.service.ScoreServiceJDBC;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConsoleUI {
public static final String GAME_NAME = "mines";
private static final Pattern COMMAND_PATTERN = Pattern.compile("([OM])([A-I])([1-9])");
private final Field field;
private final Scanner scanner = new Scanner(System.in);
private ScoreService scoreService = new ScoreServiceJDBC();
public ConsoleUI(Field field) {
this.field = field;
}
public void play() {
printTopScores();
do {
printField();
processInput();
......@@ -32,6 +42,7 @@ public class ConsoleUI {
System.out.println("Game failed!");
} else {
System.out.println("Game solved!");
scoreService.addScore(new Score("mines", System.getProperty("user.name"), field.getScore(), new Date()));
}
}
......@@ -90,4 +101,11 @@ public class ConsoleUI {
System.err.println("Wrong input " + line);
}
}
private void printTopScores() {
List<Score> scores = scoreService.getTopScores(GAME_NAME);
for(Score score : scores) {
System.out.printf("%s %d\n", score.getPlayer(), score.getPoints());
}
}
}
......@@ -15,6 +15,8 @@ public class Field {
private int numberOfOpenTiles;
private long startMillis;
public Field(int rowCount, int columnCount, int mineCount) {
if (rowCount * columnCount <= mineCount)
throw new IllegalArgumentException("Illegal setting");
......@@ -29,6 +31,7 @@ public class Field {
private void generate() {
generateMines();
fillWithClues();
startMillis = System.currentTimeMillis();
}
private void generateMines() {
......@@ -155,5 +158,8 @@ public class Field {
tile.setState(TileState.CLOSED);
}
}
}
public int getScore() {
return rowCount * columnCount * 3 - (int) (System.currentTimeMillis() - startMillis) / 1000;
}
}
package sk.tuke.gamestudio.game.mines.entity;
import java.util.Date;
/**
CREATE TABLE score (game VARCHAR(32) NOT NULL, player VARCHAR(32) NOT NULL, points INTEGER NOT NULL, playedAt TIMESTAMP NOT NULL)
*/
public class Score {
private String game;
private String player;
private int points;
private Date playedAt;
public Score(String game, String player, int points, Date playedAt) {
this.game = game;
this.player = player;
this.points = points;
this.playedAt = playedAt;
}
public String getGame() {
return game;
}
public void setGame(String game) {
this.game = game;
}
public String getPlayer() {
return player;
}
public void setPlayer(String player) {
this.player = player;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public Date getPlayedAt() {
return playedAt;
}
public void setPlayedAt(Date playedAt) {
this.playedAt = playedAt;
}
@Override
public String toString() {
return "Score{" +
"game='" + game + '\'' +
", player='" + player + '\'' +
", points=" + points +
", playedAt=" + playedAt +
'}';
}
}
package sk.tuke.gamestudio.game.mines.service;
public class GamestudioException extends RuntimeException {
public GamestudioException() {
}
public GamestudioException(String message) {
super(message);
}
public GamestudioException(String message, Throwable cause) {
super(message, cause);
}
}
package sk.tuke.gamestudio.game.mines.service;
import sk.tuke.gamestudio.game.mines.entity.Score;
import java.util.List;
public interface ScoreService {
void addScore(Score score);
List<Score> getTopScores(String game);
void reset();
}
package sk.tuke.gamestudio.game.mines.service;
import sk.tuke.gamestudio.game.mines.entity.Score;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ScoreServiceJDBC implements ScoreService {
public static final String URL = "jdbc:postgresql://localhost/gamestudio";
public static final String USER = "postgres";
public static final String PASSWORD = "postgres";
public static final String SELECT = "SELECT game, player, points, playedAt FROM score WHERE game = ? ORDER BY points DESC LIMIT 10";
public static final String DELETE = "DELETE FROM score";
public static final String INSERT = "INSERT INTO score (game, player, points, playedAt) VALUES (?, ?, ?, ?)";
@Override
public void addScore(Score score) {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = connection.prepareStatement(INSERT)
) {
statement.setString(1, score.getGame());
statement.setString(2, score.getPlayer());
statement.setInt(3, score.getPoints());
statement.setTimestamp(4, new Timestamp(score.getPlayedAt().getTime()));
statement.executeUpdate();
} catch (SQLException e) {
throw new GamestudioException("Problem inserting score", e);
}
}
@Override
public List<Score> getTopScores(String game) {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = connection.prepareStatement(SELECT);
) {
statement.setString(1, game);
try (ResultSet rs = statement.executeQuery()) {
List<Score> scores = new ArrayList<>();
while (rs.next()) {
scores.add(new Score(rs.getString(1), rs.getString(2), rs.getInt(3), rs.getTimestamp(4)));
}
return scores;
}
} catch (SQLException e) {
throw new GamestudioException("Problem selecting score", e);
}
}
@Override
public void reset() {
try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);
Statement statement = connection.createStatement();
) {
statement.executeUpdate(DELETE);
} catch (SQLException e) {
throw new GamestudioException("Problem deleting score", e);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment