Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Johan Gustav Thrane-Nielsen
inf101.v20.sem2.losning
Commits
a59b1dc2
Commit
a59b1dc2
authored
Apr 18, 2020
by
Anna Maria Eilertsen
Browse files
moved clicks to GUI and allow repeating clicks
parent
70c6702b
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/inf101/v20/sem2/mnkgames/GUI/GUIBoard.java
View file @
a59b1dc2
...
...
@@ -13,22 +13,20 @@ import inf101.v20.sem2.twoPlayerGame.TwoPlayerGame;
public
class
GUIBoard
extends
JPanel
{
private
Grid
<
GUIPiece
>
clickablePanels
;
private
TwoPlayerGame
<
MNKGame
>
game
;
private
MNKGameGUI
gui
;
private
static
final
long
serialVersionUID
=
1L
;
public
GUIBoard
(
MNKGameGUI
gui
,
TwoPlayerGame
<
MNKGame
>
game
)
{
this
.
game
=
game
;
this
.
gui
=
gui
;
setLayout
(
new
GridLayout
(
game
.
height
(),
game
.
width
()));
setBorder
(
BorderFactory
.
createEmptyBorder
(
50
,
50
,
50
,
50
));
setRequestFocusEnabled
(
true
);
requestFocus
();
fillWithClickable
(
game
.
height
(),
game
.
width
());
fillWithClickable
(
game
.
height
(),
game
.
width
()
,
game
);
}
public
void
updateToGame
()
{
public
void
updateToGame
(
TwoPlayerGame
<
MNKGame
>
game
)
{
for
(
int
y
=
0
;
y
<
clickablePanels
.
getHeight
();
y
++)
{
for
(
int
x
=
0
;
x
<
clickablePanels
.
getWidth
();
x
++)
{
clickablePanels
.
get
(
x
,
y
).
updateToGame
(
game
.
pieceAt
(
x
,
y
));
...
...
@@ -36,7 +34,7 @@ public class GUIBoard extends JPanel{
}
}
private
void
fillWithClickable
(
int
height
,
int
width
)
{
private
void
fillWithClickable
(
int
height
,
int
width
,
TwoPlayerGame
<
MNKGame
>
game
)
{
this
.
clickablePanels
=
new
Grid
<
GUIPiece
>(
width
,
height
,
null
);
for
(
int
y
=
0
;
y
<
height
;
y
++)
{
for
(
int
x
=
0
;
x
<
width
;
x
++)
{
...
...
@@ -64,9 +62,7 @@ public class GUIBoard extends JPanel{
class
GameClickListener
extends
MouseAdapter
{
public
void
mousePressed
(
MouseEvent
me
){
GUIPiece
clickedPanel
=(
GUIPiece
)
me
.
getSource
();
// get the reference to the box that was clicked
game
.
takeTurnforHumanPlayer
(
clickedPanel
.
X
,
clickedPanel
.
Y
);
updateToGame
();
gui
.
endOfTurn
();
gui
.
registerClick
(
clickedPanel
.
X
,
clickedPanel
.
Y
);
}
}
}
...
...
src/main/java/inf101/v20/sem2/mnkgames/GUI/GUIPiece.java
View file @
a59b1dc2
...
...
@@ -38,9 +38,9 @@ public class GUIPiece extends JPanel{
}
void
updateToGame
(
Piece
p
)
{
if
(
p
!=
Piece
.
NONE
)
{
makeUnClickable
();
}
//
if(p != Piece.NONE) {
//
makeUnClickable();
//
}
c
=
pieceToColor
(
p
);
}
...
...
src/main/java/inf101/v20/sem2/mnkgames/GUI/MNKGameGUI.java
View file @
a59b1dc2
...
...
@@ -75,10 +75,14 @@ s * Initializes a JFrame in which we place the game
this
.
updateUI
();
}
/**
* Should be called after a click to update the UI to reflect the current game state
*/
public
void
endOfTurn
()
{
public
void
registerClick
(
int
x
,
int
y
)
{
try
{
game
.
takeTurnforHumanPlayer
(
x
,
y
);
}
catch
(
Exception
e
){
//GUI ERROR HANDLING
}
board
.
updateToGame
(
game
);
updateMessage
();
MNKGameGUI
.
super
.
updateUI
();
if
(!
game
.
hasPossibleMoves
())
{
...
...
src/main/java/inf101/v20/sem2/mnkgames/MNKGame.java
View file @
a59b1dc2
...
...
@@ -86,7 +86,7 @@ public abstract class MNKGame {
if
(
x
>=
width
||
x
<
0
||
y
>=
height
||
y
<
0
)
throw
new
IllegalArgumentException
(
"Cannot add a piece to this position"
);
if
(
grid
.
get
(
x
,
y
)
!=
Piece
.
NONE
)
throw
new
IllegalArgumentException
(
"Position is already filled"
);
return
;
//
throw new IllegalArgumentException("Position is already filled");
grid
.
set
(
x
,
y
,
currentPlayer
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a 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