Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pingouins
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bariatti Francesco
pingouins
Commits
7c1972d7
Commit
7c1972d7
authored
8 years ago
by
Bariatti Francesco
Browse files
Options
Downloads
Patches
Plain Diff
GUI refactoring and cleaning
parent
b6ec98de
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
gui/src/controller/Controller.java
+40
-1
40 additions, 1 deletion
gui/src/controller/Controller.java
gui/src/controller/UpdateThread.java
+28
-57
28 additions, 57 deletions
gui/src/controller/UpdateThread.java
with
68 additions
and
58 deletions
gui/src/controller/Controller.java
+
40
−
1
View file @
7c1972d7
...
...
@@ -77,7 +77,7 @@ public class Controller implements Initializable
gameProcess
.
destroy
();
}
});
UpdateThread
upT
=
new
UpdateThread
(
gameProcess
,
this
.
gameState
,
this
.
board
,
this
.
boardView
,
scoreRedLabel
,
scoreBlueLabel
,
turnLabel
,
statusLabel
);
UpdateThread
upT
=
new
UpdateThread
(
gameProcess
,
this
,
this
.
gameState
,
statusLabel
);
upT
.
setDaemon
(
true
);
upT
.
start
();
}
catch
(
IOException
e
)
...
...
@@ -162,7 +162,46 @@ public class Controller implements Initializable
t
.
getFishLabel
().
setOnMouseClicked
(
gch
);
}
}
/**
* Updates model and view from the state
*/
public
void
updateModelAndView
()
{
for
(
int
i
=
0
;
i
<
board
.
length
;
i
++)
{
board
[
i
].
setNbFish
(
gameState
.
getNbFish
(
i
));
board
[
i
].
setPenguinPresence
(
Tile
.
PenguinPresence
.
NO_PENGUIN
);
}
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
board
[
gameState
.
getPenguinPos
(
Player
.
Red
,
i
)].
setPenguinPresence
(
Tile
.
PenguinPresence
.
RED_PENGUIN
);
board
[
gameState
.
getPenguinPos
(
Player
.
Blue
,
i
)].
setPenguinPresence
(
Tile
.
PenguinPresence
.
BLUE_PENGUIN
);
}
for
(
TileView
t
:
boardView
)
//No need to deselect or de-highlight tiles because it is done in the click listener
t
.
update
();
scoreRedLabel
.
setText
(
Integer
.
toString
(
gameState
.
getScore
(
Player
.
Red
)));
scoreBlueLabel
.
setText
(
Integer
.
toString
(
gameState
.
getScore
(
Player
.
Blue
)));
turnLabel
.
setText
(
gameState
.
getCurrent_player
()+
"'s turn"
);
}
//
//
public
void
gameEnd
()
{
Player
human
=
gameState
.
getHumanPlayer
();
Player
computer
=
human
.
equals
(
Player
.
Red
)
?
Player
.
Blue
:
Player
.
Red
;
String
message
=
""
;
if
(
gameState
.
getScore
(
human
)
>
gameState
.
getScore
(
computer
))
message
=
"You won!!! I can't believe it!"
;
else
if
(
gameState
.
getScore
(
human
)
==
gameState
.
getScore
(
computer
))
message
=
"That's a draw. Not bad!"
;
else
message
=
"You just lost the penguin game."
;
statusLabel
.
setText
(
"End of game"
);
Alert
alert
=
new
Alert
(
Alert
.
AlertType
.
INFORMATION
,
message
,
ButtonType
.
FINISH
);
alert
.
showAndWait
();
}
private
class
PlacePenguinClickHandler
implements
EventHandler
<
MouseEvent
>
...
...
This diff is collapsed.
Click to expand it.
gui/src/controller/UpdateThread.java
+
28
−
57
View file @
7c1972d7
...
...
@@ -6,31 +6,24 @@ import javafx.scene.control.ButtonType;
import
javafx.scene.control.Label
;
import
model.GameState
;
import
model.Player
;
import
model.Tile
;
import
view.TileView
;
import
java.io.*
;
public
class
UpdateThread
extends
Thread
{
BufferedReader
reader
;
PrintWriter
writer
;
Controller
controller
;
BufferedReader
gameReader
;
PrintWriter
gameWriter
;
GameState
gameState
;
Tile
[]
board
;
TileView
[]
boardView
;
Label
scoreRed
,
scoreBlue
,
turnLabel
,
statusLabel
;
Label
statusLabel
;
public
UpdateThread
(
Process
program
,
GameState
gameState
,
Tile
[]
board
,
TileView
[]
boardView
,
Label
scoreRed
,
Label
scoreBlue
,
Label
turnLabel
,
Label
statusLabel
)
public
UpdateThread
(
Process
program
,
Controller
controller
,
GameState
gameState
,
Label
statusLabel
)
{
this
.
r
eader
=
new
BufferedReader
(
new
InputStreamReader
(
program
.
getInputStream
()));
this
.
w
riter
=
new
PrintWriter
(
new
OutputStreamWriter
(
program
.
getOutputStream
()),
true
);
this
.
gameR
eader
=
new
BufferedReader
(
new
InputStreamReader
(
program
.
getInputStream
()));
this
.
gameW
riter
=
new
PrintWriter
(
new
OutputStreamWriter
(
program
.
getOutputStream
()),
true
);
this
.
gameState
=
gameState
;
this
.
board
=
board
;
this
.
boardView
=
boardView
;
this
.
scoreRed
=
scoreRed
;
this
.
scoreBlue
=
scoreBlue
;
this
.
turnLabel
=
turnLabel
;
this
.
statusLabel
=
statusLabel
;
this
.
controller
=
controller
;
}
public
void
run
()
...
...
@@ -40,63 +33,41 @@ public class UpdateThread extends Thread
{
try
{
String
line
=
r
eader
.
readLine
();
String
line
=
gameR
eader
.
readLine
();
System
.
out
.
println
(
line
);
if
(
line
==
null
)
if
(
line
==
null
)
//Normally this shouldn't happen (The game always end). So it is an error
{
gameRunning
=
false
;
Platform
.
runLater
(()
->
new
Alert
(
Alert
.
AlertType
.
ERROR
,
"That's it! I rage quit!"
,
ButtonType
.
FINISH
).
showAndWait
());
}
else
if
(
line
.
startsWith
(
Player
.
Red
+
" won"
)
||
line
.
startsWith
(
Player
.
Blue
+
" won"
)
||
line
.
startsWith
(
"draw"
))
{
//System.out.println("======= END OF GAME=======");
Platform
.
runLater
(()
->
{
Player
computer
=
gameState
.
getHumanPlayer
().
equals
(
Player
.
Red
)
?
Player
.
Blue
:
Player
.
Red
;
String
message
=
""
;
if
(
line
.
startsWith
(
gameState
.
getHumanPlayer
().
toString
()))
//Human won
message
=
"You won!!! I can't believe it!"
;
else
if
(
line
.
startsWith
(
computer
.
toString
()))
message
=
"You just lost the penguin game."
;
else
message
=
"That's a draw. Not bad!"
;
Alert
alert
=
new
Alert
(
Alert
.
AlertType
.
INFORMATION
,
message
,
ButtonType
.
OK
);
alert
.
showAndWait
();
});
gameRunning
=
false
;
Platform
.
runLater
(()
->
controller
.
gameEnd
());
}
else
if
(
line
.
contains
(
"{"
))
//Line contains JSON
{
//
UPDATE MODEL
//
gameState Update
gameState
.
update
(
line
.
substring
(
line
.
indexOf
(
"{"
),
line
.
lastIndexOf
(
"}"
)
+
1
));
//Extract JSON string
for
(
int
i
=
0
;
i
<
board
.
length
;
i
++)
{
board
[
i
].
setNbFish
(
gameState
.
getNbFish
(
i
));
board
[
i
].
setPenguinPresence
(
Tile
.
PenguinPresence
.
NO_PENGUIN
);
}
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
board
[
gameState
.
getPenguinPos
(
Player
.
Red
,
i
)].
setPenguinPresence
(
Tile
.
PenguinPresence
.
RED_PENGUIN
);
board
[
gameState
.
getPenguinPos
(
Player
.
Blue
,
i
)].
setPenguinPresence
(
Tile
.
PenguinPresence
.
BLUE_PENGUIN
);
}
//UPDATE VIEW
Platform
.
runLater
(()
->
{
for
(
int
i
=
0
;
i
<
boardView
.
length
;
i
++)
boardView
[
i
].
update
();
scoreRed
.
setText
(
Integer
.
toString
(
gameState
.
getScore
(
Player
.
Red
)));
scoreBlue
.
setText
(
Integer
.
toString
(
gameState
.
getScore
(
Player
.
Blue
)));
turnLabel
.
setText
(
gameState
.
getCurrent_player
()+
"'s turn"
);
});
if
(
gameState
.
getCurrent_player
()
==
gameState
.
getHumanPlayer
())
Platform
.
runLater
(()
->
controller
.
updateModelAndView
());
//If we can't play
if
(
gameState
.
getCurrent_player
().
equals
(
gameState
.
getHumanPlayer
()))
{
if
(!
gameState
.
getCanPlay
(
gameState
.
getHumanPlayer
()))
{
Platform
.
runLater
(()
->
{
Alert
alert
=
new
Alert
(
Alert
.
AlertType
.
INFORMATION
,
"You can't play any move!"
,
ButtonType
.
OK
);
alert
.
showAndWait
();
writer
.
println
(
"0"
);
//This pass the turn
Platform
.
runLater
(()
->
{
statusLabel
.
setText
(
"You can't play any move!"
);
try
{
sleep
(
2000
);
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
gameWriter
.
println
(
"0"
);
//This pass the turn
});
}
}
}
else
if
(
line
.
startsWith
(
"("
)
&&
line
.
contains
(
"value:"
))
//lines with values (estimation) of the computer winning chances
{
//We want to show a little message to the user depending on if we win or not
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment