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
3cce1188
Commit
3cce1188
authored
Mar 25, 2020
by
Anna Maria Eilertsen
Browse files
cleanup: added AI interface, reordered methods
parent
e5af9e87
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/main/java/inf101/v20/sem2/mnkgames/AI/AIPlayer.java
0 → 100644
View file @
3cce1188
package
inf101.v20.sem2.mnkgames.AI
;
import
inf101.v20.sem2.mnkgames.MNKGame
;
public
interface
AIPlayer
{
public
void
makeMove
(
MNKGame
game
);
}
src/main/java/inf101/v20/sem2/mnkgames/AI/SimpleStatelessAIPlayer.java
View file @
3cce1188
...
...
@@ -2,7 +2,7 @@ package inf101.v20.sem2.mnkgames.AI;
import
inf101.v20.sem2.mnkgames.MNKGame
;
public
class
SimpleStatelessAIPlayer
{
public
class
SimpleStatelessAIPlayer
implements
AIPlayer
{
public
SimpleStatelessAIPlayer
()
{
}
...
...
src/main/java/inf101/v20/sem2/mnkgames/GUI/MNKGameGUI.java
View file @
3cce1188
...
...
@@ -20,6 +20,7 @@ import javax.swing.JPanel;
import
inf101.v20.lab4.grid.Grid
;
import
inf101.v20.sem2.mnkgames.MNKGame
;
import
inf101.v20.sem2.mnkgames.MNKGame.Piece
;
import
inf101.v20.sem2.mnkgames.AI.AIPlayer
;
import
inf101.v20.sem2.mnkgames.AI.SimpleStatelessAIPlayer
;
/**
...
...
@@ -45,54 +46,6 @@ s * Initializes a JFrame in which we place the game
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
;
...
...
@@ -103,10 +56,11 @@ s * Initializes a JFrame in which we place the game
private
String
player1_screenName
;
private
String
player2_screenName
;
private
boolean
multiplayer
;
private
SimpleStateless
AIPlayer
ai
;
private
AIPlayer
ai
;
public
MNKGameGUI
(
GameSupplier
gameSupplier
)
{
this
.
gameSupplier
=
gameSupplier
;
//initialize to default values
this
.
game
=
gameSupplier
.
getTicTacToeGame
();
this
.
ai
=
new
SimpleStatelessAIPlayer
();
}
...
...
@@ -315,4 +269,52 @@ s * Initializes a JFrame in which we place the game
statusMessage
.
setText
(
"Next move from "
+
getPlayerName
(
game
.
getCurrent
()));
}
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'
;
}
}
\ 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