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
3d94d079
Commit
3d94d079
authored
Mar 25, 2020
by
Anna Maria Eilertsen
Browse files
added player name prompt and support
parent
82f05819
Changes
3
Show whitespace changes
Inline
Side-by-side
src/main/java/inf101/v20/sem2/mnkgames/GUI/GUIMain.java
View file @
3d94d079
package
inf101.v20.sem2.mnkgames.GUI
;
import
java.util.function.Supplier
;
import
inf101.v20.sem2.mnkgames.ConnectFour
;
import
inf101.v20.sem2.mnkgames.MNKGame
;
import
inf101.v20.sem2.mnkgames.TicTacToe
;
...
...
@@ -10,7 +9,6 @@ public class GUIMain {
public
static
void
main
(
String
[]
args
)
{
MNKGameGUI
.
run
(
new
GameSupplier
());
// MNKGameGUI.run(() -> new TicTacToe());
}
}
...
...
src/main/java/inf101/v20/sem2/mnkgames/GUI/MNKGameGUI.java
View file @
3d94d079
...
...
@@ -14,6 +14,7 @@ import javax.swing.BorderFactory;
import
javax.swing.JButton
;
import
javax.swing.JFrame
;
import
javax.swing.JLabel
;
import
javax.swing.JOptionPane
;
import
javax.swing.JPanel
;
import
inf101.v20.lab4.grid.Grid
;
...
...
@@ -36,12 +37,61 @@ s * Initializes a JFrame in which we place the game
JFrame
f
=
new
JFrame
(
"mnkGame"
);
f
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
MNKGameGUI
ap
=
new
MNKGameGUI
(
gameSupplier
);
ap
.
collectPlayerInfo
();
ap
.
initialize
();
f
.
add
(
"Center"
,
ap
);
f
.
pack
();
f
.
setVisible
(
true
);
}
public
void
collectPlayerInfo
()
{
JFrame
frame
=
new
JFrame
();
multiplayer
=
promptMultiplayer
(
frame
);
player1_screenName
=
promptPlayerName
(
frame
,
"Player 1"
);
if
(
multiplayer
)
{
player2_screenName
=
promptPlayerName
(
frame
,
"Player 2"
);
}
else
{
player2_screenName
=
"AI player"
;
}
}
public
String
promptPlayerName
(
JFrame
frame
,
String
player
)
{
String
name
=
(
String
)
JOptionPane
.
showInputDialog
(
frame
,
"Welcome "
+
player
+
":\n"
+
"What is your name?"
,
"MKGame StartUp"
,
JOptionPane
.
PLAIN_MESSAGE
,
null
,
null
,
null
);
//If a string was returned, say so.
if
((
name
!=
null
)
&&
(
name
.
length
()
>
0
))
{
System
.
out
.
println
(
"Received "
+
name
);
}
return
name
;
}
private
boolean
promptMultiplayer
(
JFrame
frame
)
{
Object
[]
possibilities
=
{
"Multiplayer"
,
"Single Player (against AI)"
};
String
s
=
(
String
)
JOptionPane
.
showInputDialog
(
frame
,
"Welcome:\n"
+
"Select one or two players"
,
"MKGame StartUp"
,
JOptionPane
.
PLAIN_MESSAGE
,
null
,
possibilities
,
null
);
//If a string was returned, say so.
if
((
s
!=
null
)
&&
(
s
.
length
()
>
0
))
{
System
.
out
.
println
(
"Received "
+
s
);
}
return
s
.
charAt
(
0
)==
'M'
;
}
// Fields
private
MNKGame
game
;
private
JButton
playConnectFourButton
;
...
...
@@ -49,6 +99,9 @@ s * Initializes a JFrame in which we place the game
private
JLabel
statusMessage
;
private
Grid
<
GamePanel
>
clickablePanels
;
private
GameSupplier
gameSupplier
;
private
String
player1_screenName
;
private
String
player2_screenName
;
private
boolean
multiplayer
;
public
MNKGameGUI
(
GameSupplier
gameSupplier
)
{
this
.
gameSupplier
=
gameSupplier
;
...
...
@@ -82,6 +135,7 @@ s * Initializes a JFrame in which we place the game
private
void
updateBoardUI
()
{
clickablePanels
.
forEach
(
item
->
item
.
updateToGame
());
updateMessage
();
MNKGameGUI
.
super
.
updateUI
();
if
(
game
.
hasWinner
())
{
System
.
out
.
println
(
"Winner!"
);
...
...
@@ -117,6 +171,23 @@ s * Initializes a JFrame in which we place the game
}
}
/**
* Maps from Piece values to players
*
* @param pieceAt The piece to be drawn
* @return The player name
*/
private
String
getPlayerName
(
Piece
pieceAt
)
{
if
(
pieceAt
.
equals
(
Piece
.
NONE
))
return
"No Player"
;
if
(
pieceAt
.
equals
(
MNKGame
.
getStartColor
()))
{
return
player1_screenName
;
}
else
{
return
player2_screenName
;
}
}
@Override
public
Dimension
getPreferredSize
()
{
return
new
Dimension
(
100
*
game
.
getHeight
(),
100
*
game
.
getWidth
());
...
...
@@ -213,7 +284,7 @@ s * Initializes a JFrame in which we place the game
playConnectFourButton
.
setText
(
"Connect Four"
);
statusMessage
=
new
JLabel
();
statusMessage
.
setText
(
"Welcome to this game!"
);
statusMessage
.
setText
(
"Welcome to this game!
"
+
player1_screenName
+
" begins.
"
);
JPanel
buttons
=
new
JPanel
();
buttons
.
setLayout
(
new
BorderLayout
());
...
...
@@ -224,9 +295,9 @@ s * Initializes a JFrame in which we place the game
private
void
updateMessage
()
{
if
(
game
.
hasWinner
())
statusMessage
.
setText
(
"The game is over!
"
);
//
The winner is " + game.getWinner()
.name().toLowerCase(
));
statusMessage
.
setText
(
"The game is over! The winner is "
+
getPlayerName
(
game
.
getWinner
()));
else
statusMessage
.
setText
(
"
Playing"
);
statusMessage
.
setText
(
"
Next move from "
+
getPlayerName
(
game
.
getCurrent
())
);
}
}
\ No newline at end of file
src/main/java/inf101/v20/sem2/mnkgames/MNKGame.java
View file @
3d94d079
...
...
@@ -277,4 +277,12 @@ public abstract class MNKGame {
return
r
;
}
public
static
Piece
getStartColor
()
{
return
Piece
.
BLACK
;
}
public
Piece
getCurrent
()
{
return
currentPlayer
;
}
}
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