Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Tomas Beranek
WarGames
Commits
1c7fb44c
Commit
1c7fb44c
authored
May 24, 2022
by
Tomas Beranek
Browse files
Merge branch 'tomaber-main-patch-94441' into 'main'
Deleted src/main/java/BattleSimulation/Army.java,... See merge request
!6
parents
6e2f7272
bfa4892d
Changes
18
Hide whitespace changes
Inline
Side-by-side
src/main/java/BattleSimulation/Army.java
deleted
100644 → 0
View file @
6e2f7272
package
BattleSimulation
;
import
SpecificUnits.*
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Objects
;
import
java.util.stream.Collectors
;
/**
* Class representing a full army, with different methods for handling the mechanics of an army
*/
public
class
Army
{
private
List
<
Unit
>
units
;
private
String
name
;
/**
* A constructor with a list, and the name of that list
* @param name army name
*/
public
Army
(
String
name
)
{
units
=
new
ArrayList
<>();
this
.
name
=
name
;
}
/**
* A constructor created with a list and a name of the army object created
* @param name name of army
* @param units list of units
*/
public
Army
(
String
name
,
List
<
Unit
>
units
){
this
.
name
=
name
;
this
.
units
=
units
;
}
/**
* the get method for the name of the army
* @return String with the name of the army
*/
public
String
getName
()
{
return
name
;
}
/**
* method for adding a unit to the army
* @param unit chosen unit to add to army
*/
public
void
add
(
Unit
unit
){
units
.
add
(
unit
);
}
/**
* method for adding multiple units at one
* @param newUnits the list of the new units
*/
public
void
addAll
(
List
<
Unit
>
newUnits
){
units
.
addAll
(
newUnits
);
}
/**
* method for removing a specific unit in the army
* @param unit the unit to remove
*/
public
void
remove
(
Unit
unit
){
units
.
remove
(
unit
);
}
/**
* method that return true if a list is empty or not
* @return true if the list is empty, false if it has units
*/
public
boolean
hasUnits
(){
if
(
units
.
size
()
>
0
){
return
true
;
}
return
false
;
}
/**
* method for getting all units
* @return a list of the unit
*/
public
List
<
Unit
>
getAllUnits
(){
return
units
;
}
/**
* method for getting a random unit for a list
* @return a random unit
*/
public
Unit
getRandom
(){
java
.
util
.
Random
random
=
new
java
.
util
.
Random
();
int
randomNum
=
random
.
nextInt
(
units
.
size
());
return
units
.
get
(
randomNum
);
}
/**
* this is a getmethod for getting the list of all units in an army
* @return list of unit in the army
*/
public
List
<
Unit
>
getUnits
()
{
return
units
;
}
/**
* methods for getting all infantryUnits in a army
* @return a new list of all infantryUnits
*/
public
List
<
Unit
>
getInfantryUnits
(){
return
getUnits
().
stream
()
.
filter
(
u
->
u
instanceof
InfantryUnit
)
.
collect
(
Collectors
.
toCollection
(
ArrayList:
:
new
));
}
/**
* methods for getting all RangeUnitsUnits in a army
* @return a new list of all RangeUnits
*/
public
List
<
Unit
>
getRangeUnits
(){
return
getUnits
().
stream
()
.
filter
(
u
->
u
instanceof
RangedUnit
)
.
collect
(
Collectors
.
toCollection
(
ArrayList:
:
new
));
}
/**
* methods for getting all CavalryUnits in a army
* @return a new list of all CavalryUnits
*/
public
List
<
Unit
>
getCavalryUnits
(){
return
getUnits
().
stream
()
.
filter
(
u
->
u
instanceof
CavalryUnit
&&
!(
u
instanceof
CommanderUnit
))
.
collect
(
Collectors
.
toCollection
(
ArrayList:
:
new
));
}
/**
* methods for getting all CommanderUnits in a army
* @return a new list of all CommanderUnits
*/
public
List
<
Unit
>
getCommanderUnits
(){
return
getUnits
().
stream
()
.
filter
(
u
->
u
instanceof
CommanderUnit
)
.
collect
(
Collectors
.
toCollection
(
ArrayList:
:
new
));
}
/**
* if a object is the same as a different unit
* @param o object to check
* @return true if the units are equal, false if not
*/
@Override
public
boolean
equals
(
Object
o
)
{
if
(
this
==
o
)
return
true
;
if
(!(
o
instanceof
Army
))
return
false
;
Army
army
=
(
Army
)
o
;
if
(!
Objects
.
equals
(
units
,
army
.
units
))
return
false
;
return
getName
()
!=
null
?
getName
().
equals
(
army
.
getName
())
:
army
.
getName
()
==
null
;
}
/**
* method that makes a hashcode for an object
* @return int as a hashcode for the different objects
*/
@Override
public
int
hashCode
()
{
int
result
=
units
!=
null
?
units
.
hashCode
()
:
0
;
result
=
31
*
result
+
(
getName
()
!=
null
?
getName
().
hashCode
()
:
0
);
return
result
;
}
/**
* a toString method for printing a units list
* @return a string of units
*/
@Override
public
String
toString
()
{
return
name
+
"\nunits="
+
units
;
}
}
src/main/java/BattleSimulation/Battle.java
deleted
100644 → 0
View file @
6e2f7272
package
BattleSimulation
;
import
SpecificUnits.Unit
;
import
java.util.Random
;
/**
* this class represents the simulation of a battle
* the battle is going to be between two armies from the army class
* this class consists of different methods and variables
*/
public
class
Battle
{
Army
attackingArmy
;
Army
defendingArmy
;
Army
armyOne
;
Army
armyTwo
;
/**
* this is a constructor for the battle class that takes two classes.
* @param armyOne
* @param armyTwo
*/
public
Battle
(
Army
armyOne
,
Army
armyTwo
)
{
this
.
armyOne
=
armyOne
;
this
.
armyTwo
=
armyTwo
;
}
/**
* this method is the simulation of the battle, two armies attack each other, and one wins
* @return return the winning army as an Army object
*/
public
Army
simulate
(){
Army
winningArmy
;
Random
random
=
new
Random
();
boolean
randomBool
=
random
.
nextBoolean
();
if
(
randomBool
){
attackingArmy
=
armyOne
;
defendingArmy
=
armyTwo
;
}
else
{
attackingArmy
=
armyTwo
;
defendingArmy
=
armyOne
;
}
boolean
winner
;
while
(
armyOne
.
hasUnits
()
&&
armyTwo
.
hasUnits
()){
Unit
attackingUnit
=
attackingArmy
.
getRandom
();
Unit
defendingUnit
=
defendingArmy
.
getRandom
();
winner
=
false
;
while
(!
winner
){
attackingUnit
.
attack
(
defendingUnit
);
if
(
defendingUnit
.
getHealth
()
<=
0
){
defendingArmy
.
remove
(
defendingUnit
);
winner
=
true
;
}
else
{
defendingUnit
.
attack
(
attackingUnit
);
if
(
attackingUnit
.
getHealth
()
<=
0
){
attackingArmy
.
remove
(
attackingUnit
);
winner
=
true
;
}
}
}
}
if
(
armyOne
.
hasUnits
()){
winningArmy
=
armyOne
;
}
else
{
winningArmy
=
armyTwo
;
}
return
winningArmy
;
}
/**
* this method chooses the starting/attacking and defending army
*/
public
void
whoStarts
(){
}
/**
* the toString method that print out both armies
* @return both armies units
*/
@Override
public
String
toString
()
{
return
armyOne
.
getName
()
+
"\n"
+
armyOne
.
getAllUnits
()
+
"\n\n"
+
armyTwo
.
getName
()
+
"\n"
+
armyTwo
.
getAllUnits
();
}
}
\ No newline at end of file
src/main/java/BattleSimulation/armyFiles.java
deleted
100644 → 0
View file @
6e2f7272
package
BattleSimulation
;
import
SpecificUnits.*
;
import
java.io.*
;
import
java.nio.file.Files
;
import
java.nio.file.NoSuchFileException
;
import
java.nio.file.Path
;
import
java.util.ArrayList
;
import
java.util.Objects
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
public
class
armyFiles
{
/**
* method that takes a unit and converts it to a string of csv type
* @param u the unit you want to convert
* @return string of csv type of the specific unit
*/
public
static
String
makeCSVString
(
Unit
u
){
return
u
.
getClass
().
getSimpleName
()
+
","
+
u
.
getName
()+
","
+
u
.
getHealth
()+
","
+
u
.
getAttack
()+
","
+
u
.
getArmor
()
+
","
+
u
.
getMelee
()+
"\n"
;
}
/**
* method that creates a file from an army with the armys name
* @param army the army you want to create a file of
* @return File with the new army
* @throws IOException if file not found, or file is wrong throws exception
*/
public
static
File
makeCSVFile
(
Army
army
)
throws
IOException
{
//makes a new file in the armyFile folder with the army name
File
armyFile
=
new
File
(
"src/main/resources/armyFiles/"
+
army
.
getName
().
trim
());
//checks if file is already in registry
if
(
armyFile
.
exists
()
&&
!
armyFile
.
isDirectory
())
{
throw
new
IllegalArgumentException
(
"This ArmyFile is already in the registry"
);
}
Writer
writer
=
new
FileWriter
(
armyFile
.
getAbsolutePath
());
writer
.
write
(
armyFile
.
getName
()+
"\n"
);
// puts all units in a army to a specific list
army
.
getUnits
()
.
forEach
(
unit
->
{
try
{
writer
.
write
(
makeCSVString
(
unit
));
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
});
writer
.
close
();
return
armyFile
;
}
/**
* method that takes a file and creates an army form its context
* @param armyName the file you are looking for also the name of the army
* @return the new Army created from the file
* @throws IOException any mistakes with the file will throw an exception
*/
public
static
Army
readFromCSV
(
String
armyName
)
throws
IOException
{
// finds path to wanted file
Path
path
=
Path
.
of
(
"src/main/resources/armyFiles/"
+
armyName
);
if
(
Path
.
of
(
"src/main/resources/armyFiles/"
+
armyName
).
getFileName
()
==
null
)
throw
new
NoSuchFileException
(
"File not found"
);
Army
newArmy
=
new
Army
(
armyName
);
ArrayList
<
Unit
>
newUnits
=
new
ArrayList
<>();
Files
.
lines
(
path
)
.
skip
(
1
)
.
forEach
(
line
->
{
//checkForCharacters(line);
checkForUnit
(
line
,
newUnits
);
});
if
(
newUnits
.
size
()
==
0
){
throw
new
NullPointerException
(
"This file doesnt contain any units"
);
}
newArmy
.
addAll
(
newUnits
);
return
newArmy
;
}
/**
* method that checks which unitType a line from a file is
* @param line line from a file
* @param newUnits arrayList with the new Units created
*/
public
static
void
checkForUnit
(
String
line
,
ArrayList
<
Unit
>
newUnits
){
String
[]
column
=
line
.
split
(
","
);
if
(
column
.
length
<
6
)
throw
new
IllegalArgumentException
(
"File is not readable"
);
// all 4 different unit types passed to an arraylist of units
String
unitType
=
column
[
0
];
if
(
Objects
.
equals
(
unitType
,
"InfantryUnit"
)){
newUnits
.
add
(
new
InfantryUnit
(
column
[
1
],
Integer
.
parseInt
(
column
[
2
]),
Integer
.
parseInt
(
column
[
2
])
,
Integer
.
parseInt
(
column
[
3
]),
Integer
.
parseInt
(
column
[
4
])));
}
else
if
(
Objects
.
equals
(
unitType
,
"RangedUnit"
)){
newUnits
.
add
(
new
RangedUnit
(
column
[
1
],
Integer
.
parseInt
(
column
[
2
]),
Integer
.
parseInt
(
column
[
2
])
,
Integer
.
parseInt
(
column
[
3
]),
Integer
.
parseInt
(
column
[
4
])));
}
else
if
(
Objects
.
equals
(
unitType
,
"CavalryUnit"
)){
newUnits
.
add
(
new
CavalryUnit
(
column
[
1
],
Integer
.
parseInt
(
column
[
2
]),
Integer
.
parseInt
(
column
[
2
])
,
Integer
.
parseInt
(
column
[
3
]),
Integer
.
parseInt
(
column
[
4
])));
}
else
if
(
Objects
.
equals
(
unitType
,
"CommanderUnit"
)){
newUnits
.
add
(
new
CommanderUnit
(
column
[
1
],
Integer
.
parseInt
(
column
[
2
]),
Integer
.
parseInt
(
column
[
2
])
,
Integer
.
parseInt
(
column
[
3
]),
Integer
.
parseInt
(
column
[
4
])));
}
}
/*
public static void checkForCharacters(String lines) throws IllegalArgumentException{
String removeCharacters = "[\\d]";
Pattern pt = Pattern.compile(removeCharacters);
Matcher mt = pt.matcher(lines);
boolean result = mt.matches();
if (result) throw new IllegalArgumentException("Illegal characters found in file");
}
*/
}
\ No newline at end of file
src/main/java/GameHub.java
deleted
100644 → 0
View file @
6e2f7272
import
SpecificUnits.*
;
import
BattleSimulation.*
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Objects
;
import
static
javax
.
swing
.
JOptionPane
.*;
/**
* this is the client for my warGame
*/
public
class
GameHub
{
public
static
List
<
Unit
>
unitList
=
new
ArrayList
<>();
public
static
List
<
Army
>
armyList
=
new
ArrayList
<>();
/**
* the main method that starts the game
* @param args
*/
public
static
void
main
(
String
[]
args
)
{
start
();
}
public
static
boolean
finished
=
true
;
/**
* the start f the game, where you can choose what you want to do in the game
*/
public
static
void
start
()
{
List
<
Army
>
armyList
=
new
ArrayList
<>();
while
(
finished
){
try
{
String
[]
menuInput
=
{
"Register new Unit"
,
"Register new Army"
,
"Add all units"
,
"Multiply unit count"
,
"Remove all units registered"
,
"Run simulation"
,
"See all Units"
,
"End Game"
,
};
final
int
REGISTER_NEW
=
0
;
final
int
REGISTER_ARMY
=
1
;
final
int
ADD_ALL
=
2
;
final
int
MULTIPY_UNIT
=
3
;
final
int
REMOVE_ALL
=
4
;
final
int
RUN_SIMULATION
=
5
;
final
int
SHOW_ALL_UNITS
=
6
;
final
int
EXIT
=
7
;
int
menuSelection
=
showOptionDialog
(
null
,
"****WarGames****"
+
"\n Choose function"
,
"Project Idatt2001"
,
YES_NO_OPTION
,
INFORMATION_MESSAGE
,
null
,
menuInput
,
menuInput
[
0
]);
switch
(
menuSelection
)
{
case
REGISTER_NEW
->
{
REGISTERNEW
();
}
case
REGISTER_ARMY
->
{
REGISTERARMY
();
}
case
ADD_ALL
->
{
ADDALL
();
}
case
MULTIPY_UNIT
->
{
MULTIPYUNIT
();
}
case
REMOVE_ALL
->
{
REMOVEALL
();
}
case
RUN_SIMULATION
->
{
RUNSIMULATION
();
}
case
SHOW_ALL_UNITS
->
{
SHOWALLUNITS
();
}
case
EXIT
->
{
System
.
out
.
println
(
"""
Thanks of using this Game
Bye
"""
);
finished
=
false
;
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
}
/**
* method where you register new units
*/
private
static
void
REGISTERNEW
()
{
String
typeRead
=
showInputDialog
(
"what type of unit is it\n"
+
"1. Infantry"
+
"\n2. Range"
+
"\n3. Cavalry"
+
"\n4. Commander"
);
int
type
=
Integer
.
parseInt
(
typeRead
);
String
newName
=
showInputDialog
(
"What is the name of the unit"
);
String
newDamageRead
=
showInputDialog
(
"How much damage does it deal"
);
int
newDamage
=
Integer
.
parseInt
(
newDamageRead
);
String
newArmorRead
=
showInputDialog
(
"How much Armor does it have"
);
int
newArmor
=
Integer
.
parseInt
(
newArmorRead
);
String
newHealthRead
=
showInputDialog
(
"How much health does it have"
);
int
newHealth
=
Integer
.
parseInt
(
newHealthRead
);
String
newMeleeRead
=
showInputDialog
(
"How much melee damage does it deal"
);
int
newMelee
=
Integer
.
parseInt
(
newMeleeRead
);
if
(
type
==
1
){
unitList
.
add
(
new
InfantryUnit
(
newName
,
newHealth
,
newDamage
,
newArmor
,
newMelee
));
}
else
if
(
type
==
2
){
unitList
.
add
(
new
RangedUnit
(
newName
,
newHealth
,
newDamage
,
newArmor
,
newMelee
));
}
else
if
(
type
==
3
){
unitList
.
add
(
new
CavalryUnit
(
newName
,
newHealth
,
newDamage
,
newArmor
,
newMelee
));
}
else
if
(
type
==
4
){
unitList
.
add
(
new
CommanderUnit
(
newName
,
newHealth
,
newDamage
,
newArmor
,
newMelee
));
}
}
/**
* method for registering new army
*/
private
static
void
REGISTERARMY
()
{
String
newName
=
showInputDialog
(
"Write the name of the army"
);
Army
army
=
new
Army
(
newName
);
armyList
.
add
(
army
);
}
/**
* method for adding all units to an army
*/
private
static
void
ADDALL
()
{
String
armyName
=
showInputDialog
(
"what is the name of the army you want to add all these units to"
);
for
(
Army
a:
armyList
)
{
if
(
Objects
.
equals
(
a
.
getName
(),
armyName
)){
a
.
addAll
(
unitList
);
}
}
}
/**
* method for multiplying a specific unit based on type
*/
private
static
void
MULTIPYUNIT
()
{
String
typeRead
=
showInputDialog
(
"what type of unit is it\n"
+
"1. Infantry"
+
"\n2. Range"
+
"\n3. Cavalry"
+
"\n4. Commander"
);
int
type
=
Integer
.
parseInt
(
typeRead
);
String
nameMultiple
=
showInputDialog
(
"What is the name of unit you want to multiple"
);
String
howManyTimes
=
showInputDialog
(
"How many do you want"
);
int
multipleNum
=
Integer
.
parseInt
(
howManyTimes
);
if
(
type
==
1
)
{
for
(
Unit
inf
:
unitList
)
{
if
(
Objects
.
equals
(
inf
.
getName
(),
nameMultiple
))
{
for
(
int
i
=
0
;
i
<=
multipleNum
;
i
++)
{
unitList
.
add
(
new
InfantryUnit
(
inf
.
getName
(),
inf
.
getHealth
(),
inf
.
getAttack
(),
inf
.
getArmor
(),
inf
.
getMelee
()));
}
}
}
}
else
if
(
type
==
2
){
for
(
Unit
rng:
unitList
)
{
if
(
Objects
.
equals
(
rng
.
getName
(),
nameMultiple
)){
for
(
int
i
=
0
;
i
<
multipleNum
;
i
++)
{
unitList
.
add
(
new
RangedUnit
(
rng
.
getName
(),
rng
.
getHealth
(),
rng
.
getAttack
(),
rng
.
getArmor
(),
rng
.
getMelee
()));
}
}
}
}
else
if
(
type
==
3
){
for
(
Unit
cav
:
unitList
){
if
(
Objects
.
equals
(
cav
.
getName
(),
nameMultiple
)){
for
(
int
i
=
0
;
i
<=
multipleNum
;
i
++)
{
unitList
.
add
(
new
CavalryUnit
(
cav
.
getName
(),
cav
.
getHealth
(),
cav
.
getAttack
(),
cav
.
getArmor
(),
cav
.
getMelee
()));
}
}
}
}
else
if
(
type
==
4
){
for
(
Unit
com
:
unitList
){
if
(
Objects
.
equals
(
com
.
getName
(),
nameMultiple
)){
for
(
int
i
=
0
;
i
<=
multipleNum
;
i
++)
{
unitList
.
add
(
new
CommanderUnit
(
com
.
getName
(),
com
.
getHealth
(),
com
.
getAttack
(),
com
.
getArmor
(),
com
.
getMelee
()));
}
}
}
}
}
/**
* remove all units in the list, so you can make a new army
*/
private
static
void
REMOVEALL
()
{