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
bb062a60
Commit
bb062a60
authored
Mar 25, 2020
by
Anna Maria Eilertsen
Browse files
added buttons for switching games. Includes debug statements
parent
7411f39e
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/inf101/v20/lab4/mnkgames/GUIMain.java
View file @
bb062a60
package
inf101.v20.lab4.mnkgames
;
import
java.util.function.Supplier
;
public
class
GUIMain
{
public
static
void
main
(
String
[]
args
)
{
MNKGameGUI
.
run
(
()
->
new
ConnectFou
r
());
MNKGameGUI
.
run
(
new
GameSupplie
r
());
// MNKGameGUI.run(() -> new TicTacToe());
}
}
class
GameSupplier
implements
Supplier
<
MNKGame
>{
@Override
public
MNKGame
get
()
{
return
null
;
}
public
MNKGame
getConnectFourGame
()
{
return
new
ConnectFour
();
}
public
MNKGame
getTicTacToeGame
()
{
return
new
TicTacToe
();
}
}
src/main/java/inf101/v20/lab4/mnkgames/MNKGame.java
View file @
bb062a60
...
...
@@ -82,15 +82,15 @@ public abstract class MNKGame {
if
(
current
==
Piece
.
NONE
)
return
Piece
.
NONE
;
//
System.out.println();
//
System.out.println("Currently at " + x + "," + y + " value " + current);
//
System.out.println();
System
.
out
.
println
();
System
.
out
.
println
(
"Currently at "
+
x
+
","
+
y
+
" value "
+
current
);
System
.
out
.
println
();
//horizontal
for
(
int
i
=
x
-
k
;
i
<
x
+
k
;
i
++)
{
if
(
i
<
0
||
i
>=
getWidth
())
continue
;
//
System.out.println("Horizontal Looking at \n" + debugToString(i, y) + ": count is " + count);
System
.
out
.
println
(
"Horizontal Looking at \n"
+
debugToString
(
i
,
y
)
+
": count is "
+
count
);
if
(
grid
.
get
(
i
,
y
)
!=
current
)
{
count
=
0
;
}
...
...
@@ -106,7 +106,7 @@ public abstract class MNKGame {
for
(
int
j
=
y
-
k
;
j
<
y
+
k
;
j
++)
{
if
(
j
<
0
||
j
>=
getHeight
())
continue
;
//
System.out.println("Vertical Looking at \n" + debugToString(x, j) + ": count is " + count);
System
.
out
.
println
(
"Vertical Looking at \n"
+
debugToString
(
x
,
j
)
+
": count is "
+
count
);
if
(
grid
.
get
(
x
,
j
)
!=
current
)
{
count
=
0
;
}
...
...
@@ -122,7 +122,7 @@ public abstract class MNKGame {
for
(
int
i
=
x
-
k
,
j
=
y
-
k
;
i
<
x
+
k
&&
j
<
y
+
k
;
i
++,
j
++)
{
if
(
i
<
0
||
j
<
0
||
i
>=
getWidth
()
||
j
>=
getHeight
())
continue
;
//
System.out.println("Diagonal TL-BR Looking at \n" + debugToString(i, j) + ": count is " + count);
System
.
out
.
println
(
"Diagonal TL-BR Looking at \n"
+
debugToString
(
i
,
j
)
+
": count is "
+
count
);
if
(
grid
.
get
(
i
,
j
)
!=
current
)
{
count
=
0
;
...
...
@@ -139,7 +139,7 @@ public abstract class MNKGame {
for
(
int
i
=
x
+
k
,
j
=
y
-
k
;
i
>
x
-
k
&&
j
<
y
+
k
;
i
--,
j
++)
{
if
(
i
<
0
||
j
<
0
||
i
>=
getWidth
()
||
j
>=
getHeight
())
continue
;
//
System.out.println("Diagonal TR-BL Looking at \n" + debugToString(i, j) + ": count is " + count);
System
.
out
.
println
(
"Diagonal TR-BL Looking at \n"
+
debugToString
(
i
,
j
)
+
": count is "
+
count
);
if
(
grid
.
get
(
i
,
j
)
!=
current
)
{
count
=
0
;
...
...
@@ -166,7 +166,7 @@ public abstract class MNKGame {
}
private
String
debugToString
(
int
x
,
int
y
)
{
//
System.out.println("x="+x+", y="+y);
System
.
out
.
println
(
"x="
+
x
+
", y="
+
y
);
String
r
=
""
;
for
(
int
j
=
0
;
j
<
height
;
j
++)
{
for
(
int
i
=
0
;
i
<
width
;
i
++)
{
...
...
src/main/java/inf101/v20/lab4/mnkgames/MNKGameGUI.java
View file @
bb062a60
...
...
@@ -30,12 +30,10 @@ public class MNKGameGUI extends JPanel implements ActionListener{
/**
*
* Initializes a JFrame in which we place the a CellAutomataGUI containing
* the given ILabyrinth.
*
* @param ca
* Initializes a JFrame in which we place the game
* @param gameSupplier A lambda function that provides new games
*/
public
static
void
run
(
Supplier
<
MNKGame
>
gameSupplier
)
{
public
static
void
run
(
Game
Supplier
gameSupplier
)
{
JFrame
f
=
new
JFrame
(
"mnkGame"
);
f
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
MNKGameGUI
ap
=
new
MNKGameGUI
(
gameSupplier
);
...
...
@@ -46,15 +44,17 @@ public class MNKGameGUI extends JPanel implements ActionListener{
}
MNKGame
game
;
private
JButton
setBoardButton
;
private
JLabel
message
;
private
Grid
<
GamePanel
>
grid
;
private
Supplier
<
MNKGame
>
gameSupplier
;
public
MNKGameGUI
(
Supplier
<
MNKGame
>
gameSupplier
)
{
private
JButton
resetBoardButton
;
private
JButton
playConnectFourButton
;
private
JButton
playTicTacToeButton
;
private
JLabel
statusMessage
;
private
Grid
<
GamePanel
>
clickablePanels
;
private
GameSupplier
gameSupplier
;
public
MNKGameGUI
(
GameSupplier
gameSupplier
)
{
this
.
gameSupplier
=
gameSupplier
;
this
.
game
=
gameSupplier
.
get
();
this
.
grid
=
new
Grid
<
GamePanel
>(
game
.
getWidth
(),
game
.
getHeight
(),
null
);
this
.
game
=
gameSupplier
.
get
TicTacToeGame
();
}
/*
...
...
@@ -68,21 +68,27 @@ public class MNKGameGUI extends JPanel implements ActionListener{
*/
@Override
public
void
actionPerformed
(
ActionEvent
e
)
{
if
(
e
.
getSource
()
==
setBoardButton
)
{
reset
();
}
// if (e.getSource() == resetBoardButton) {
// reset();
// }
if
(
e
.
getSource
()
==
playConnectFourButton
)
{
reset
(
gameSupplier
.
getConnectFourGame
());
}
if
(
e
.
getSource
()
==
playTicTacToeButton
)
{
reset
(
gameSupplier
.
getTicTacToeGame
());
}
}
private
void
reset
()
{
game
=
game
Supplier
.
get
()
;
private
void
reset
(
MNKGame
game
)
{
this
.
game
=
game
;
updateMessage
();
this
.
updateUI
();
removeAll
();
initialize
();
this
.
updateUI
();
}
private
void
updateBoardUI
()
{
grid
.
forEach
(
item
->
item
.
updateToGame
());
clickablePanels
.
forEach
(
item
->
item
.
updateToGame
());
MNKGameGUI
.
super
.
updateUI
();
if
(
game
.
hasWinner
())
{
System
.
out
.
println
(
"Winner!"
);
...
...
@@ -93,7 +99,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
private
void
stop
()
{
updateMessage
();
grid
.
forEach
(
item
->
item
.
removeMouseListeners
());
clickablePanels
.
forEach
(
item
->
item
.
removeMouseListeners
());
}
//Class that defines what happens (i.e: the color changes) when a panel is clicked
...
...
@@ -174,7 +180,7 @@ public class MNKGameGUI extends JPanel implements ActionListener{
JPanel
p
=
new
JPanel
();
p
.
setLayout
(
new
GridLayout
(
game
.
getHeight
(),
game
.
getWidth
()));
p
.
setBorder
(
BorderFactory
.
createEmptyBorder
(
50
,
50
,
50
,
50
));
this
.
clickablePanels
=
new
Grid
<
GamePanel
>(
game
.
getWidth
(),
game
.
getHeight
(),
null
);
for
(
int
y
=
0
;
y
<
game
.
getHeight
();
y
++)
{
for
(
int
x
=
0
;
x
<
game
.
getWidth
();
x
++)
{
GamePanel
pan
=
new
GamePanel
(
x
,
y
);
...
...
@@ -183,35 +189,49 @@ public class MNKGameGUI extends JPanel implements ActionListener{
pan
.
setBorder
(
BorderFactory
.
createLineBorder
(
Color
.
GRAY
));
pan
.
addMouseListener
(
new
BoxListener
());
// add a mouse listener to make the panels clickable
p
.
add
(
pan
);
grid
.
set
(
x
,
y
,
pan
);
clickablePanels
.
set
(
x
,
y
,
pan
);
}
}
p
.
setRequestFocusEnabled
(
true
);
p
.
requestFocus
();
setBoardButton
=
new
JButton
();
setBoardButton
.
addActionListener
(
this
);
setBoardButton
.
setText
(
"Reset Board"
);
message
=
new
JLabel
();
message
.
setText
(
"Welcome to this game!"
);
resetBoardButton
=
new
JButton
();
resetBoardButton
.
addActionListener
(
this
);
resetBoardButton
.
setText
(
"Reset Board"
);
playTicTacToeButton
=
new
JButton
();
playTicTacToeButton
.
addActionListener
(
this
);
playTicTacToeButton
.
setText
(
"Tic-Tac-Toe"
);
playConnectFourButton
=
new
JButton
();
playConnectFourButton
.
addActionListener
(
this
);
playConnectFourButton
.
setText
(
"Connect Four"
);
statusMessage
=
new
JLabel
();
statusMessage
.
setText
(
"Welcome to this game!"
);
JPanel
buttons
=
new
JPanel
();
buttons
.
setLayout
(
new
BorderLayout
());
buttons
.
add
(
resetBoardButton
,
BorderLayout
.
CENTER
);
buttons
.
add
(
playConnectFourButton
,
BorderLayout
.
WEST
);
buttons
.
add
(
playTicTacToeButton
,
BorderLayout
.
EAST
);
// p.add(leftButton);
// p.add(rightButton);
// p.add(upButton);
// p.add(downButton);
add
(
p
);
// add(gameComponent, BorderLayout.CENTER);
add
(
setBoardB
utton
,
BorderLayout
.
SOUTH
);
add
(
m
essage
,
BorderLayout
.
NORTH
);
add
(
b
utton
s
,
BorderLayout
.
SOUTH
);
add
(
statusM
essage
,
BorderLayout
.
NORTH
);
validate
();
}
private
void
updateMessage
()
{
if
(
game
.
hasWinner
())
m
essage
.
setText
(
"The game is over!"
);
// The winner is " + game.getWinner().name().toLowerCase());
statusM
essage
.
setText
(
"The game is over!"
);
// The winner is " + game.getWinner().name().toLowerCase());
else
m
essage
.
setText
(
"Playing"
);
statusM
essage
.
setText
(
"Playing"
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
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