Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
doc. Ing. Jaroslav Porubän PhD.
gamestudio2020
Commits
e0d11829
Commit
e0d11829
authored
Mar 17, 2020
by
doc. Ing. Jaroslav Porubän PhD.
Browse files
4. prednaska
parent
7278f315
Changes
8
Hide whitespace changes
Inline
Side-by-side
pom.xml
View file @
e0d11829
...
...
@@ -20,4 +20,21 @@
<maven.compiler.source>
1.13
</maven.compiler.source>
<maven.compiler.target>
1.13
</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>
org.postgresql
</groupId>
<artifactId>
postgresql
</artifactId>
<version>
42.2.11
</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
4.13
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
</project>
src/main/java/sk/tuke/gamestudio/entity/Score.java
0 → 100644
View file @
e0d11829
package
sk.tuke.gamestudio.entity
;
import
java.io.Serializable
;
import
java.util.Date
;
import
java.util.Objects
;
public
class
Score
implements
Comparable
<
Score
>,
Serializable
{
private
String
player
;
private
int
points
;
private
String
game
;
private
Date
playedOn
;
public
Score
(
String
player
,
int
points
,
String
game
,
Date
playedOn
)
{
this
.
player
=
player
;
this
.
points
=
points
;
this
.
game
=
game
;
this
.
playedOn
=
playedOn
;
}
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
String
getGame
()
{
return
game
;
}
public
void
setGame
(
String
game
)
{
this
.
game
=
game
;
}
public
Date
getPlayedOn
()
{
return
playedOn
;
}
public
void
setPlayedOn
(
Date
playedOn
)
{
this
.
playedOn
=
playedOn
;
}
@Override
public
int
compareTo
(
Score
o
)
{
return
o
.
points
-
this
.
points
;
}
//Nasledujuce metody boli vygenerovane pomocou IntelliJ - Code / Generate ... / equals(), hashCode(), toString()
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
return
true
;
if
(
o
==
null
||
getClass
()
!=
o
.
getClass
())
return
false
;
Score
score
=
(
Score
)
o
;
return
points
==
score
.
points
&&
Objects
.
equals
(
player
,
score
.
player
)
&&
Objects
.
equals
(
game
,
score
.
game
)
&&
Objects
.
equals
(
playedOn
,
score
.
playedOn
);
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
player
,
points
,
game
,
playedOn
);
}
@Override
public
String
toString
()
{
return
"Score{"
+
"player='"
+
player
+
'\''
+
", points="
+
points
+
", game='"
+
game
+
'\''
+
", playedOn="
+
playedOn
+
'}'
;
}
}
src/main/java/sk/tuke/gamestudio/game/mines/consoleui/ConsoleUI.java
View file @
e0d11829
package
sk.tuke.gamestudio.game.mines.consoleui
;
import
sk.tuke.gamestudio.entity.Score
;
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.service.ScoreService
;
import
sk.tuke.gamestudio.service.ScoreServiceJDBC
;
import
java.util.Collections
;
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
{
private
static
final
String
GAME_NAME
=
"mines"
;
private
static
final
Pattern
INPUT_PATTERN
=
Pattern
.
compile
(
"([OM])([A-I])([1-9])"
);
private
final
Field
field
;
//private ScoreService scoreService = new ScoreServiceFile();
private
ScoreService
scoreService
=
new
ScoreServiceJDBC
();
public
ConsoleUI
(
Field
field
)
{
this
.
field
=
field
;
}
public
void
play
()
{
printScores
();
do
{
printField
();
processInput
();
...
...
@@ -26,6 +39,8 @@ public class ConsoleUI {
printField
();
if
(
field
.
getState
()
==
GameState
.
SOLVED
)
{
System
.
out
.
println
(
"You won!!!"
);
scoreService
.
addScore
(
new
Score
(
System
.
getProperty
(
"user.name"
),
field
.
getScore
(),
GAME_NAME
,
new
Date
()));
}
else
System
.
out
.
println
(
"You failed!!!"
);
}
...
...
@@ -103,4 +118,15 @@ public class ConsoleUI {
}
}
}
private
void
printScores
()
{
List
<
Score
>
scores
=
scoreService
.
getTopScores
(
GAME_NAME
);
Collections
.
sort
(
scores
);
System
.
out
.
println
(
"Top scores:"
);
for
(
Score
s
:
scores
)
{
System
.
out
.
println
(
s
);
}
}
}
src/main/java/sk/tuke/gamestudio/game/mines/core/Field.java
View file @
e0d11829
...
...
@@ -15,6 +15,8 @@ public class Field {
private
int
numberOfOpenTiles
;
private
long
startMillis
;
public
Field
(
int
rowCount
,
int
columnCount
,
int
mineCount
)
{
this
.
rowCount
=
rowCount
;
this
.
columnCount
=
columnCount
;
...
...
@@ -31,6 +33,7 @@ public class Field {
private
void
generate
()
{
generateMines
();
fillWithClues
();
startMillis
=
System
.
currentTimeMillis
();
}
private
void
generateMines
()
{
...
...
@@ -155,4 +158,12 @@ public class Field {
// }
// }
}
private
int
getPlayingTime
()
{
return
((
int
)
(
System
.
currentTimeMillis
()
-
startMillis
))
/
1000
;
}
public
int
getScore
()
{
return
rowCount
*
columnCount
*
3
-
getPlayingTime
();
}
}
src/main/java/sk/tuke/gamestudio/service/ScoreService.java
0 → 100644
View file @
e0d11829
package
sk.tuke.gamestudio.service
;
import
sk.tuke.gamestudio.entity.Score
;
import
java.util.List
;
public
interface
ScoreService
{
void
addScore
(
Score
score
);
List
<
Score
>
getTopScores
(
String
gameName
);
}
src/main/java/sk/tuke/gamestudio/service/ScoreServiceFile.java
0 → 100644
View file @
e0d11829
package
sk.tuke.gamestudio.service
;
import
sk.tuke.gamestudio.entity.Score
;
import
java.io.*
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
public
class
ScoreServiceFile
implements
ScoreService
{
private
static
final
String
FILE_NAME
=
"score.db"
;
@Override
public
void
addScore
(
Score
score
)
{
List
<
Score
>
scores
=
loadScore
();
scores
.
add
(
score
);
Collections
.
sort
(
scores
);
saveScore
(
scores
);
}
@Override
public
List
<
Score
>
getTopScores
(
String
gameName
)
{
List
<
Score
>
scores
=
loadScore
();
//TODO: vyber podla gameName
return
scores
;
}
private
void
saveScore
(
List
<
Score
>
scores
)
{
try
(
ObjectOutputStream
os
=
new
ObjectOutputStream
(
new
FileOutputStream
(
FILE_NAME
)))
{
os
.
writeObject
(
scores
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
private
List
<
Score
>
loadScore
()
{
try
(
ObjectInputStream
is
=
new
ObjectInputStream
(
new
FileInputStream
(
FILE_NAME
)))
{
return
(
List
<
Score
>)
is
.
readObject
();
}
catch
(
IOException
|
ClassNotFoundException
e
)
{
e
.
printStackTrace
();
}
return
new
ArrayList
<>();
}
}
src/main/java/sk/tuke/gamestudio/service/ScoreServiceJDBC.java
0 → 100644
View file @
e0d11829
package
sk.tuke.gamestudio.service
;
import
sk.tuke.gamestudio.entity.Score
;
import
java.sql.*
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
ScoreServiceJDBC
implements
ScoreService
{
private
static
final
String
URL
=
"jdbc:postgresql://localhost/gamestudio"
;
private
static
final
String
LOGIN
=
"postgres"
;
private
static
final
String
PASSWORD
=
"postgres"
;
private
static
final
String
CREATE_COMMAND
=
"CREATE TABLE score (player VARCHAR(64) NOT NULL, points INTEGER NOT NULL, game VARCHAR(64) NOT NULL, played_on TIMESTAMP NOT NULL)"
;
private
static
final
String
INSERT_COMMAND
=
"INSERT INTO score (player, points, game, played_on) VALUES (?, ?, ?, ?)"
;
private
static
final
String
SELECT_COMMAND
=
"SELECT player, points, game, played_on FROM score WHERE game = ? ORDER BY points DESC"
;
@Override
public
void
addScore
(
Score
score
)
{
try
(
Connection
connection
=
DriverManager
.
getConnection
(
URL
,
LOGIN
,
PASSWORD
);
PreparedStatement
stmt
=
connection
.
prepareStatement
(
INSERT_COMMAND
);
)
{
stmt
.
setString
(
1
,
score
.
getPlayer
());
stmt
.
setInt
(
2
,
score
.
getPoints
());
stmt
.
setString
(
3
,
score
.
getGame
());
stmt
.
setTimestamp
(
4
,
new
Timestamp
(
score
.
getPlayedOn
().
getTime
()));
stmt
.
executeUpdate
();
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
@Override
public
List
<
Score
>
getTopScores
(
String
gameName
)
{
List
<
Score
>
results
=
new
ArrayList
<>();
try
(
Connection
connection
=
DriverManager
.
getConnection
(
URL
,
LOGIN
,
PASSWORD
);
PreparedStatement
stmt
=
connection
.
prepareStatement
(
SELECT_COMMAND
);
)
{
stmt
.
setString
(
1
,
gameName
);
try
(
ResultSet
rs
=
stmt
.
executeQuery
())
{
while
(
rs
.
next
())
{
Score
score
=
new
Score
(
rs
.
getString
(
1
),
rs
.
getInt
(
2
),
rs
.
getString
(
3
),
rs
.
getTimestamp
(
4
));
results
.
add
(
score
);
}
}
}
catch
(
SQLException
e
)
{
throw
new
RuntimeException
(
e
);
}
return
results
;
}
}
src/test/java/sk/tuke/gamestudio/service/ScoreServiceTest.java
0 → 100644
View file @
e0d11829
package
sk.tuke.gamestudio.service
;
import
org.junit.Test
;
import
sk.tuke.gamestudio.entity.Score
;
import
java.util.Date
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
public
class
ScoreServiceTest
{
private
ScoreService
service
=
new
ScoreServiceFile
();
//private ScoreService service = new ScoreServiceJDBC();
@Test
public
void
addScore
()
{
Score
score
=
new
Score
(
"Zuzka"
,
200
,
"mines"
,
new
Date
());
service
.
addScore
(
score
);
List
<
Score
>
scores
=
service
.
getTopScores
(
"mines"
);
assertEquals
(
1
,
scores
.
size
());
assertEquals
(
"Zuzka"
,
scores
.
get
(
0
).
getPlayer
());
assertEquals
(
200
,
scores
.
get
(
0
).
getPoints
());
assertEquals
(
"mines"
,
scores
.
get
(
0
).
getGame
());
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment