Java- Object-Oriented and Concurrent Programming
src/Artifact.java
src/Artifact.java
class
Artifact
extends
Cave
{
public
int
creatureIndex
=
0
;
public
Artifact
(
int
index
,
String
type
,
int
creatureIndex
)
{
super
(
type
,
index
);
this
.
creatureIndex
=
creatureIndex
;
}
@
Override
public
String
toString
()
{
// Return neater output of the class data
return
"[Artifact]: \n\tOwner Index = "
+
Integer
.
toString
(
this
.
creatureIndex
)
+
" \n\tArtifact Type = "
+
this
.
type
;
}
}
src/Cave.java
src/Cave.java
public
class
Cave
{
public
int
index
=
0
;
public
String
type
=
null
;
public
String
name
=
null
;
/***
* Constructor
*
@param
index
*/
public
Cave
(
int
index
)
{
this
.
index
=
index
;
}
public
Cave
(
int
index
,
String
type
,
String
name
)
{
this
.
index
=
index
;
this
.
type
=
type
;
this
.
name
=
name
;
}
public
Cave
(
int
index
,
String
name
)
{
this
.
index
=
index
;
this
.
name
=
name
;
}
public
Cave
(
String
type
,
int
index
)
{
this
.
index
=
index
;
this
.
type
=
type
;
}
}
src/Creature.java
src/Creature.java
import
java
.
awt
.
BorderLayout
;
import
java
.
awt
.
event
.
ActionEvent
;
import
java
.
awt
.
event
.
ActionListener
;
import
java
.
util
.
ArrayList
;
import
javax
.
swing
.
JButton
;
import
javax
.
swing
.
JFrame
;
import
javax
.
swing
.
JLabel
;
import
javax
.
swing
.
JOptionPane
;
import
javax
.
swing
.
JPanel
;
import
javax
.
swing
.
JProgressBar
;
class
Creature
extends
Cave
implements
ActionListener
,
Runnable
{
public
int
partyIndex
=
0
;
public
int
empathyValue
;
public
int
fearValue
;
public
double
carryingCapacity
;
public
int
age
;
public
int
weight
;
public
double
height
;
public
ArrayList
<
Treasure
>
treasures
=
new
ArrayList
<
Treasure
>
();
public
ArrayList
<
Artifact
>
artifacts
=
new
ArrayList
<
Artifact
>
();
public
ArrayList
<
Jobs
>
jobs
=
new
ArrayList
<
Jobs
>
();
public
long
jobTime
=
0
;
public
Jobs
currentJob
=
null
;
public
volatile
boolean
runJob
=
false
;
public
volatile
boolean
cancelJob
=
false
;
JButton
cancelButton
=
new
JButton
(
"Cancel Job"
);
JButton
startButton
=
new
JButton
(
"Start Job"
);
public
Creature
(
int
index
,
String
type
,
String
name
,
int
partyIndex
,
int
empathyValue
,
int
fearValue
,
double
carryingCapacity
)
{
super
(
index
,
type
,
name
);
this
.
partyIndex
=
partyIndex
;
this
.
empathyValue
=
empathyValue
;
this
.
fearValue
=
fearValue
;
this
.
carryingCapacity
=
carryingCapacity
;
}
public
Creature
(
int
index
,
String
type
,
String
name
,
int
partyIndex
,
int
empathyValue
,
int
fearValue
,
double
carryingCapacity
,
int
age
,
int
weight
,
double
height
)
{
super
(
index
,
type
,
name
);
this
.
partyIndex
=
partyIndex
;
this
.
empathyValue
=
empathyValue
;
this
.
fearValue
=
fearValue
;
this
.
carryingCapacity
=
carryingCapacity
;
this
.
age
=
age
;
this
.
weight
=
weight
;
this
.
height
=
height
;
}
public
void
addTreature
(
Treasure
t
)
{
if
(
t
!=
null
&&
t
instanceof
Treasure
&&
t
.
creatureIndex
!=
0
)
{
this
.
treasures
.
add
(
t
);
}
}
public
void
addArtifact
(
Artifact
a
)
{
if
(
a
!=
null
&&
a
instanceof
Artifact
&&
a
.
creatureIndex
!=
0
)
{
this
.
artifacts
.
add
(
a
);
}
}
@
Override
public
String
toString
()
{
String
output
=
""
;
output
+=
"[Creature]: \n\tType = "
+
this
.
type
+
" \n\tName = "
+
this
.
name
+
" \n\tParty Index = "
+
Integer
.
toString
(
this
.
partyIndex
)
+
" \n\tEmpathy Value = "
+
Integer
.
toString
(
this
.
empathyValue
)
+
" \n\tFear Value = "
+
Integer
.
toString
(
this
.
fearValue
)
+
" \n\tCarrying Capacity = "
+
Double
.
toString
(
this
.
carryingCapacity
);
output
+=
"\n\t------Owned Items------\n"
;
for
(
Treasure
t
:
this
.
treasures
)
output
+=
"\n\t"
+
t
.
toString
();
for
(
Artifact
a
:
this
.
artifacts
)
output
+=
"\n\t"
+
a
.
toString
();
output
+=
"\n\t-----------------------\n"
;
return
output
;
}
public
void
addJob
(
long
time
,
Jobs
j
)
{
this
.
jobTime
=
time
;
this
.
currentJob
=
j
;
this
.
jobs
.
add
(
j
);
}
public
void
run
()
{
JProgressBar
progressBar
=
new
JProgressBar
();
progressBar
.
setStringPainted
(
true
);
cancelButton
.
addActionListener
(
this
);
startButton
.
addActionListener
(
this
);
JFrame
jf
=
new
JFrame
(
"Creature "
+
this
.
name
+
"'s Job"
);
jf
.
add
(
new
JLabel
(
" Performing Job: "
+
this
.
currentJob
.
name
),
BorderLayout
.
PAGE_START
);
jf
.
add
(
progressBar
,
BorderLayout
.
CENTER
);
JPanel
btnPanel
=
new
JPanel
();
btnPanel
.
add
(
startButton
);
btnPanel
.
add
(
cancelButton
);
jf
.
add
(
btnPanel
,
BorderLayout
.
SOUTH
);
jf
.
pack
();
jf
.
setVisible
(
true
);
jf
.
setDefaultCloseOperation
(
JFrame
.
DISPOSE_ON_CLOSE
);
jf
.
setLocationRelativeTo
(
null
);
System
.
out
.
println
(
"Starting Job: "
+
this
.
currentJob
.
name
);
System
.
out
.
println
(
"Job Duration: "
+
this
.
jobTime
);
Thread
jobThread
=
Thread
.
currentThread
();
while
(
!
this
.
runJob
)
{
jobThread
.
yield
();
}
long
currentTime
=
System
.
currentTimeMillis
();
long
startTime
=
currentTime
;
long
stopTime
=
currentTime
+
1000
*
jobTime
;
System
.
out
.
println
(
"Stop time: "
+
stopTime
);
double
duration
=
stopTime
-
currentTime
;
while
(
currentTime
<
stopTime
)
{
if
(
this
.
cancelJob
==
true
&&
jobThread
!=
null
)
{
jobThread
.
interrupt
();
JOptionPane
.
showMessageDialog
(
null
,
"Job Cancelled!"
);
jf
.
dispose
();
break
;
}
else
{
try
{
jobThread
.
sleep
(
100
);
}
catch
(
InterruptedException
e
)
{
}
progressBar
.
setValue
((
int
)
(((
currentTime
-
startTime
)
/
duration
)
*
100
));
currentTime
=
System
.
currentTimeMillis
();
}
}
if
(
!
this
.
cancelJob
)
{
progressBar
.
setValue
(
100
);
}
jf
.
dispose
();
}
@
Override
public
void
actionPerformed
(
ActionEvent
e
)
{
if
(
e
.
getSource
()
==
cancelButton
)
{
this
.
cancelJob
=
true
;
this
.
runJob
=
false
;
}
else
if
(
e
.
getSource
()
==
startButton
)
{
this
.
runJob
=
true
;
this
.
cancelJob
=
false
;
}
}
}
src/CreatureComp.java
src/CreatureComp.java
import
java
.
util
.
Comparator
;
public
class
CreatureComp
implements
Comparator
<
Creature
>
{
private
String
attribute
;
public
CreatureComp
(
String
attribute
)
{
this
.
attribute
=
attribute
;
}
public
int
compare
(
Creature
c1
,
Creature
c2
)
{
int
result
=
-
1
;
switch
(
attribute
.
charAt
(
0
))
{
case
'e'
:
if
(
c1
.
empathyValue
==
c2
.
empathyValue
)
{
result
=
0
;
}
else
if
(
c1
.
empathyValue
>
c2
.
empathyValue
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
case
'f'
:
if
(
c1
.
fearValue
==
c2
.
fearValue
)
{
result
=
0
;
}
else
if
(
c1
.
fearValue
>
c2
.
fearValue
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
case
'c'
:
if
(
c1
.
carryingCapacity
==
c2
.
carryingCapacity
)
{
result
=
0
;
}
else
if
(
c1
.
carryingCapacity
>
c2
.
carryingCapacity
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
case
'a'
:
if
(
c1
.
age
==
c2
.
age
)
{
result
=
0
;
}
else
if
(
c1
.
age
>
c2
.
age
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
case
'h'
:
if
(
c1
.
height
==
c2
.
height
)
{
result
=
0
;
}
else
if
(
c1
.
height
>
c2
.
height
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
case
'w'
:
if
(
c1
.
weight
==
c2
.
weight
)
{
result
=
0
;
}
else
if
(
c1
.
weight
>
c2
.
weight
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
default
:
if
(
c1
.
carryingCapacity
==
c2
.
carryingCapacity
)
{
result
=
0
;
}
else
if
(
c1
.
carryingCapacity
>
c2
.
carryingCapacity
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
}
return
result
;
}
}
src/Game.java
src/Game.java
import
java
.
io
.
File
;
import
java
.
io
.
IOException
;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
Collections
;
import
java
.
util
.
HashMap
;
import
java
.
util
.
Iterator
;
import
java
.
util
.
List
;
import
java
.
util
.
Map
;
import
java
.
util
.
Scanner
;
import
java
.
util
.
Set
;
public
class
Game
{
private
String
fileName
;
private
HashMap
<
Integer
,
Cave
>
caveElements
=
new
HashMap
<
Integer
,
Cave
>
();
public
ArrayList
<
Cave
>
loners
=
new
ArrayList
<
Cave
>
();
public
ArrayList
<
Party
>
parties
=
new
ArrayList
<
Party
>
();
Party
party
;
Creature
creature
;
Treasure
treasure
;
Artifact
artifact
;
public
Game
()
{
}
public
Game
(
String
dataFileName
)
throws
IOException
{
this
.
fileName
=
dataFileName
;
this
.
loadFileData
(
dataFileName
);
}
//searches the game elements for the input criteria
public
String
searchGame
(
String
searchInput
){
String
result
=
""
;
ArrayList
<
Cave
>
printElements
=
new
ArrayList
<
Cave
>
();
printElements
.
addAll
(
caveElements
.
values
());
printElements
.
addAll
(
loners
);
for
(
Cave
ge
:
printElements
){
if
(
ge
.
index
!=
0
&&
searchInput
!=
null
&&
Integer
.
toString
(
ge
.
index
).
equals
(
searchInput
)){
result
+=
ge
.
toString
()
+
"\n"
;
}
else
if
(
ge
.
type
!=
null
&&
searchInput
!=
null
&&
ge
.
type
.
toLowerCase
().
equals
(
searchInput
.
toLowerCase
())){
result
+=
ge
.
toString
()
+
"\n"
;
}
else
if
(
ge
.
name
!=
null
&&
searchInput
!=
null
&&
ge
.
name
.
toLowerCase
().
equals
(
searchInput
.
toLowerCase
())){
result
+=
ge
.
toString
()
+
"\n"
;
}
}
return
result
;
}
public
String
printAll
()
{
String
output
=
""
;
ArrayList
<
Cave
>
printElements
=
new
ArrayList
<
Cave
>
();
printElements
.
addAll
(
caveElements
.
values
());
for
(
Cave
ge
:
printElements
)
{
if
(
ge
instanceof
Party
)
{
Party
p
=
(
Party
)
ge
;
output
+=
p
.
toString
();
}
}
output
+=
"\n-----------------\nLoners\n-----------------\n"
;
for
(
Cave
ge
:
loners
)
{
output
+=
"\n"
+
ge
.
toString
();
}
return
output
;
}
public
void
setGameElements
(
String
fileName
)
throws
IOException
{
this
.
loadFileData
(
fileName
);
}
public
void
setFileName
(
String
fileName
)
{
this
.
fileName
=
fileName
;
}
public
String
getFileName
()
{
return
this
.
fileName
;
}
//Prints the elements, sorted by the critera and the object they belong to
public
String
printSortedElements
(
String
object
){
String
result
=
""
;
Set
elementSet
=
caveElements
.
entrySet
();
Iterator
i
=
elementSet
.
iterator
();
while
(
i
.
hasNext
()){
Map
.
Entry
g
=
(
Map
.
Entry
)
i
.
next
();
Cave
ge
=
(
Cave
)
g
.
getValue
();
if
(
ge
instanceof
Party
){
Party
p
=
(
Party
)
ge
;
if
(
"empathy"
.
equals
(
object
)
||
"carrying"
.
equals
(
object
)
||
"fear"
.
equals
(
object
)
||
"name"
.
equals
(
object
)
||
"age"
.
equals
(
object
)
||
"height"
.
equals
(
object
)
||
"weight"
.
equals
(
object
)){
Collections
.
sort
(
p
.
creatures
,
new
CreatureComp
(
object
));
}
if
(
"value"
.
equals
(
object
)){
for
(
Creature
c
:
p
.
creatures
){
Collections
.
sort
(
c
.
treasures
,
new
TreasureComp
(
object
));
}
}
result
+=
p
.
toString
();
}
}
result
+=
"\n---------------------\nLoners\n-------------------\n"
;
for
(
Cave
geLoner
:
loners
){
result
+=
geLoner
.
toString
();
}
return
result
;
}
public
void
addJob
(
int
index
,
String
name
,
int
creatureIndex
,
long
time
){
Jobs
j
=
new
Jobs
(
index
,
name
,
creatureIndex
,
time
);
caveElements
.
put
(
index
,
j
);
Creature
c
=
(
Creature
)
caveElements
.
get
(
creatureIndex
);
c
.
addJob
(
time
,
j
);
Thread
t
=
new
Thread
(
c
);
t
.
start
();
}
private
void
loadFileData
(
String
fileName
)
throws
IOException
{
List
<
String
>
lines
=
new
ArrayList
<
String
>
();
File
file
=
new
File
(
fileName
);
Scanner
scanner
=
new
Scanner
(
file
);
while
(
scanner
.
hasNext
()){
lines
.
add
(
scanner
.
nextLine
());
}
for
(
String
line
:
lines
)
{
line
=
line
.
replaceAll
(
"\\s"
,
""
);
if
(
!
line
.
startsWith
(
"//"
)
&&
line
.
length
()
>
0
)
{
String
[]
lineparts
=
line
.
split
(
":"
);
switch
(
lineparts
[
0
].
charAt
(
0
))
{
case
'p'
:
party
=
new
Party
(
Integer
.
parseInt
(
lineparts
[
1
]),
lineparts
[
2
]);
this
.
caveElements
.
put
(
party
.
index
,
party
);
parties
.
add
(
party
);
break
;
case
'c'
:
creature
=
new
Creature
(
Integer
.
parseInt
(
lineparts
[
1
]),
lineparts
[
2
],
lineparts
[
3
],
Integer
.
parseInt
(
lineparts
[
4
]),
Integer
.
parseInt
(
lineparts
[
5
]),
Integer
.
parseInt
(
lineparts
[
6
]),
Double
.
parseDouble
(
lineparts
[
7
]));
if
(
lineparts
.
length
>
8
&&
lineparts
[
8
]
!=
null
)
{
creature
.
age
=
Integer
.
parseInt
(
lineparts
[
8
]);
}
if
(
lineparts
.
length
>
9
&&
lineparts
[
9
]
!=
null
)
{
creature
.
height
=
Double
.
parseDouble
(
lineparts
[
9
]);
}
if
(
lineparts
.
length
>
10
&&
lineparts
[
10
]
!=
null
)
{
creature
.
weight
=
Integer
.
parseInt
(
lineparts
[
10
]);
}
Party
creatureParty
=
(
Party
)
caveElements
.
get
(
creature
.
partyIndex
);
if
(
creatureParty
==
null
||
creature
.
partyIndex
==
0
)
{
this
.
loners
.
add
(
creature
);
}
else
{
creatureParty
.
addCreature
(
creature
);
}
this
.
caveElements
.
put
(
creature
.
index
,
creature
);
break
;
case
't'
:
treasure
=
new
Treasure
(
Integer
.
parseInt
(
lineparts
[
1
]),
lineparts
[
2
],
Integer
.
parseInt
(
lineparts
[
3
]),
Double
.
parseDouble
(
lineparts
[
4
]),
Double
.
parseDouble
(
lineparts
[
5
]));
Creature
treasureCreature
=
(
Creature
)
caveElements
.
get
(
treasure
.
creatureIndex
);
if
(
treasureCreature
==
null
||
treasure
.
creatureIndex
==
0
)
{
this
.
loners
.
add
(
treasure
);
}
else
{
treasureCreature
.
addTreature
(
treasure
);
}
this
.
caveElements
.
put
(
treasure
.
index
,
treasure
);
break
;
case
'a'
:
artifact
=
new
Artifact
(
Integer
.
parseInt
(
lineparts
[
1
]),
lineparts
[
2
],
Integer
.
parseInt
(
lineparts
[
3
]));
Creature
artifactCreature
=
(
Creature
)
caveElements
.
get
(
artifact
.
creatureIndex
);
if
(
artifactCreature
==
null
||
artifact
.
creatureIndex
==
0
)
{
this
.
loners
.
add
(
artifact
);
}
else
if
(
artifact
.
creatureIndex
!=
0
)
{
artifactCreature
.
addArtifact
(
artifact
);
}
this
.
caveElements
.
put
(
artifact
.
index
,
artifact
);
break
;
case
'j'
:
addJob
(
Integer
.
parseInt
(
lineparts
[
1
]),
lineparts
[
2
],
Integer
.
parseInt
(
lineparts
[
3
]),
Long
.
parseLong
(
lineparts
[
4
]));
break
;
default
:
break
;
}
}
}
}
}
src/input.txt
// Jan 23, 2012 // Parties format: // p:<index>:<name> p : 10001 : Unity p : 10002 : Assemblage p : 10003 : Conglomeration // Creatures format: // c:<index>:<type>:<name>:<party>:<empathy>:<fear>:<carrying capacity> c : 20001 : Woman : Lucy :10001 : 17 : 22 : 20 c : 20002 : Woman : Jane :10001 : 22 : 15 : 25 c : 20003 : Woman : Nancy :10003 : 80 : 44 : 20 c : 20004 : Man : James :10002 : 35 : 13 : 50 c : 20005 : Troll : Fred :10002 : 03 : 05 : 150 c : 20006 : Warlock : Harry :10003 : 05 : 06 : 0 c : 20007 : Witch : Morgana:10001 : 10 : 15 : 0 // Treasures format: // t:<index>:<type>:<creature>:<weight>:<value> // creature = 0 means noone is carrying that treasure at the moment t : 30001 : Gold : 20004 : 50 : 2000 t : 30002 : Gold : 0 : 75 : 5000 t : 30003 : Gems : 20002 : 10 : 10000 t : 30004 : Silver : 20005 : 120 : 1000 // Artifacts format: // a:<index>:<type>:<creature>[:<name>] a : 40001 : Wand : 20007 : ElderWand a : 40002 : Wand : 20006 a : 40003 : Potion : 20007 j : 50001 : Heal Troll : 20007 : 10 : Wand : 1 : Stone : 2 : Scroll : 1
src/Jobs.java
src/Jobs.java
public
class
Jobs
extends
Cave
{
public
int
creatureIndex
=
0
;
public
long
time
=
0
;
/**
* Costructor
*
@param
index
*
@param
name
*
@param
creatureIndex
*
@param
time
*/
public
Jobs
(
int
index
,
String
name
,
int
creatureIndex
,
long
time
)
{
super
(
index
,
name
);
this
.
creatureIndex
=
creatureIndex
;
this
.
time
=
time
;
}
}
src/Main.java
src/Main.java
import
java
.
awt
.
BorderLayout
;
import
java
.
awt
.
event
.
ActionEvent
;
import
java
.
awt
.
event
.
ActionListener
;
import
java
.
io
.
File
;
import
java
.
io
.
IOException
;
import
java
.
util
.
logging
.
Level
;
import
java
.
util
.
logging
.
Logger
;
import
javax
.
swing
.
JButton
;
import
javax
.
swing
.
JComboBox
;
import
javax
.
swing
.
JFileChooser
;
import
javax
.
swing
.
JFrame
;
import
javax
.
swing
.
JLabel
;
import
javax
.
swing
.
JOptionPane
;
import
javax
.
swing
.
JPanel
;
import
javax
.
swing
.
JScrollPane
;
import
javax
.
swing
.
JTextArea
;
import
javax
.
swing
.
JTextField
;
import
javax
.
swing
.
JTree
;
import
javax
.
swing
.
border
.
EmptyBorder
;
import
javax
.
swing
.
event
.
TreeSelectionEvent
;
import
javax
.
swing
.
event
.
TreeSelectionListener
;
import
javax
.
swing
.
tree
.
DefaultMutableTreeNode
;
import
javax
.
swing
.
tree
.
DefaultTreeCellRenderer
;
public
class
Main
extends
JFrame
implements
ActionListener
{
/***/
private
JTextArea
output
;
/***/
private
JFileChooser
fileChooser
;
/***/
private
JButton
loadButton
;
/***/
private
JButton
showData
;
/***/
private
JScrollPane
completePanel
;
/***/
private
JPanel
panel
;
/***/
private
Game
game
;
JTextField
searchField
;
JButton
searchButton
;
JButton
sortButton
;
String
[]
sortStrings
=
{
"empathy"
,
"carrying"
,
"fear"
,
"weight"
,
"value"
,
"age"
,
"height"
};;
JComboBox
sList
;
JTree
gameTree
;
DefaultMutableTreeNode
partyNode
=
null
;
DefaultMutableTreeNode
creatureNode
=
null
;
DefaultMutableTreeNode
artifactNode
=
null
;
DefaultMutableTreeNode
treasureNode
=
null
;
DefaultMutableTreeNode
lNode
=
null
;
public
Main
(
String
title
)
{
searchField
=
new
JTextField
(
20
);
sortButton
=
new
JButton
(
"Sort"
);
searchButton
=
new
JButton
(
"Search"
);
sList
=
new
JComboBox
(
sortStrings
);
game
=
new
Game
();
panel
=
new
JPanel
();
fileChooser
=
new
JFileChooser
(
"."
);
output
=
new
JTextArea
();
showData
=
new
JButton
(
"Show Data"
);
loadButton
=
new
JButton
(
"Load File"
);
this
.
loadButton
.
addActionListener
(
this
);
this
.
showData
.
addActionListener
(
this
);
this
.
searchButton
.
addActionListener
(
this
);
this
.
sortButton
.
addActionListener
(
this
);
completePanel
=
new
JScrollPane
(
output
);
add
(
completePanel
,
BorderLayout
.
CENTER
);
panel
.
add
(
loadButton
);
panel
.
add
(
showData
);
panel
.
add
(
sortButton
);
panel
.
add
(
sList
);
panel
.
add
(
searchButton
);
panel
.
add
(
searchField
);
add
(
panel
,
BorderLayout
.
PAGE_END
);
setTitle
(
title
);
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
setLocationRelativeTo
(
null
);
setSize
(
400
,
400
);
setVisible
(
true
);
}
@
Override
public
void
actionPerformed
(
ActionEvent
e
)
{
if
(
e
.
getSource
()
==
loadButton
)
{
int
returnVal
=
fileChooser
.
showOpenDialog
(
this
);
if
(
returnVal
==
JFileChooser
.
APPROVE_OPTION
)
{
File
file
=
fileChooser
.
getSelectedFile
();
try
{
game
.
setGameElements
(
file
.
getAbsolutePath
());
JOptionPane
.
showMessageDialog
(
null
,
"Data Successfully Loaded!"
);
}
catch
(
IOException
ex
)
{
Logger
.
getLogger
(
Main
.
class
.
getName
()).
log
(
Level
.
SEVERE
,
null
,
ex
);
}
}
}
else
if
(
e
.
getSource
()
==
showData
)
{
DefaultTreeCellRenderer
render
=
new
DefaultTreeCellRenderer
();
render
.
setOpenIcon
(
null
);
render
.
setClosedIcon
(
null
);
render
.
setLeafIcon
(
null
);
final
DefaultMutableTreeNode
rootNode
=
new
DefaultMutableTreeNode
(
"Game/Cave"
,
true
);
gameTree
=
new
JTree
(
rootNode
);
gameTree
.
setCellRenderer
(
render
);
JScrollPane
sp
=
new
JScrollPane
(
gameTree
);
gameTree
.
addTreeSelectionListener
(
new
TreeSelectionListener
()
{
@
Override
public
void
valueChanged
(
TreeSelectionEvent
e
)
{
if
(
rootNode
!=
null
)
{
for
(
Party
p
:
game
.
parties
)
{
partyNode
=
new
DefaultMutableTreeNode
(
p
.
name
,
true
);
if
(
!
p
.
creatures
.
isEmpty
())
{
for
(
Creature
c
:
p
.
creatures
)
{
creatureNode
=
new
DefaultMutableTreeNode
(
c
.
name
,
true
);
if
(
!
c
.
artifacts
.
isEmpty
())
{
for
(
Artifact
a
:
c
.
artifacts
)
{
artifactNode
=
new
DefaultMutableTreeNode
(
a
.
type
,
false
);
creatureNode
.
add
(
artifactNode
);
}
}
if
(
!
c
.
treasures
.
isEmpty
())
{
for
(
Treasure
t
:
c
.
treasures
)
{
treasureNode
=
new
DefaultMutableTreeNode
(
t
.
type
,
false
);
creatureNode
.
add
(
treasureNode
);
}
}
partyNode
.
add
(
creatureNode
);
}
}
rootNode
.
add
(
partyNode
);
}
lNode
=
new
DefaultMutableTreeNode
(
"Loners"
,
true
);
for
(
Cave
ge
:
game
.
loners
)
{
if
(
ge
instanceof
Artifact
||
ge
instanceof
Treasure
)
{
DefaultMutableTreeNode
geNode
=
new
DefaultMutableTreeNode
(
ge
.
type
,
false
);
lNode
.
add
(
geNode
);
}
else
{
DefaultMutableTreeNode
geNode
=
new
DefaultMutableTreeNode
(
ge
.
name
,
false
);
lNode
.
add
(
geNode
);
}
}
rootNode
.
add
(
lNode
);
}
}
});
JLabel
lbl
=
new
JLabel
(
"Double-click the root node to load and view sub-nodes."
);
JFrame
showFrame
=
new
JFrame
(
"Show Data: Game Tree"
);
JPanel
lblPanel
=
new
JPanel
();
lblPanel
.
setBorder
(
new
EmptyBorder
(
10
,
10
,
10
,
10
));
lblPanel
.
add
(
lbl
);
showFrame
.
add
(
lblPanel
,
BorderLayout
.
NORTH
);
sp
.
setBorder
(
new
EmptyBorder
(
10
,
10
,
10
,
10
));
showFrame
.
add
(
sp
,
BorderLayout
.
SOUTH
);
showFrame
.
pack
();
showFrame
.
setVisible
(
true
);
showFrame
.
setLocationRelativeTo
(
null
);
showFrame
.
setDefaultCloseOperation
(
JFrame
.
DISPOSE_ON_CLOSE
);
}
else
if
(
e
.
getSource
()
==
searchButton
)
{
output
.
setText
(
""
);
String
resultText
=
"Search Results: \n------------\n"
;
String
searchInput
=
searchField
.
getText
();
resultText
+=
game
.
searchGame
(
searchInput
);
output
.
setText
(
resultText
);
}
else
if
(
e
.
getSource
()
==
sortButton
)
{
output
.
setText
(
""
);
String
sortInput
=
sList
.
getSelectedItem
().
toString
();
String
resultText
=
game
.
printSortedElements
(
sortInput
);
output
.
setText
(
resultText
);
}
}
public
static
void
main
(
String
[]
args
)
{
Main
p
=
new
Main
(
"New Game"
);
}
}
src/Party.java
src/Party.java
import
java
.
util
.
ArrayList
;
public
class
Party
extends
Cave
{
public
String
location
;
public
ArrayList
<
Creature
>
creatures
=
new
ArrayList
<
Creature
>
();
/***
* Constructor
*
@param
index
*
@param
name
*/
public
Party
(
int
index
,
String
name
)
{
super
(
index
,
name
);
}
/***
*
*
@param
c
*/
public
void
addCreature
(
Creature
c
)
{
if
(
c
!=
null
&&
c
instanceof
Creature
)
{
this
.
creatures
.
add
(
c
);
}
}
@
Override
public
String
toString
()
{
String
output
=
""
;
output
+=
"Party: \nName = "
+
this
.
name
+
" \nIndex = "
+
Integer
.
toString
(
this
.
index
);
output
+=
"\n---------Member Creatures---------\n"
;
for
(
Creature
c
:
this
.
creatures
)
output
+=
"\n\t"
+
c
.
toString
();
output
+=
"\n----------------------------------\n"
;
return
output
;
}
}
src/Treasure.java
src/Treasure.java
class
Treasure
extends
Cave
{
public
int
creatureIndex
=
0
;
public
double
weight
;
public
double
value
;
/***
* Constructor
*
@param
index
*
@param
type
*
@param
creatureIndex
*
@param
weight
*
@param
value
*/
public
Treasure
(
int
index
,
String
type
,
int
creatureIndex
,
double
weight
,
double
value
){
super
(
type
,
index
);
this
.
creatureIndex
=
creatureIndex
;
this
.
weight
=
weight
;
this
.
value
=
value
;
}
@
Override
public
String
toString
(){
return
"[Treasure]: \n\tType = "
+
this
.
type
+
" \n\tOwner Index = "
+
Integer
.
toString
(
this
.
creatureIndex
)
+
" \n\tWeight = "
+
Double
.
toString
(
this
.
weight
)
+
" \n\tValue = "
+
Double
.
toString
(
this
.
value
);
}
}
src/TreasureComp.java
src/TreasureComp.java
import
java
.
util
.
Comparator
;
public
class
TreasureComp
implements
Comparator
<
Treasure
>
{
private
String
attribute
;
public
TreasureComp
(
String
attribute
)
{
// set the member variable for the attribute we will sort by
this
.
attribute
=
attribute
;
}
@
Override
public
int
compare
(
Treasure
t1
,
Treasure
t2
)
{
int
result
=
-
1
;
// a switch statement for the member variable to handle multiple
// criteria in a single class
switch
(
attribute
.
charAt
(
0
))
{
case
'w'
:
if
(
t1
.
weight
==
t2
.
weight
)
{
result
=
0
;
}
else
if
(
t1
.
weight
>
t2
.
weight
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
case
'v'
:
if
(
t1
.
value
==
t2
.
value
)
{
result
=
0
;
}
else
if
(
t1
.
value
>
t2
.
value
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
default
:
if
(
t1
.
value
==
t2
.
value
)
{
result
=
0
;
}
else
if
(
t1
.
value
>
t2
.
value
)
{
result
=
1
;
}
else
{
result
=
-
1
;
}
break
;
}
return
result
;
}
}