Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
235 changes: 0 additions & 235 deletions src/main/java/org/example/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,242 +8,7 @@

public class App {
public static void main(String[] args) throws IOException {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("jpa-hibernate-mysql");
EntityManager em = emf.createEntityManager();
new BootstrapService(em).initialize();
EntityTransaction tx = em.getTransaction();
boolean wonGame = false;
Traveler p1;
Traveler p2;
Traveler p3;
Traveler p4;

try {
tx.begin();
List<Transport> transportMethods = em.createQuery("select t from Transport t", Transport.class).getResultList();
int pAmount = 0;
String input = IO.readln("Welcome, how many players??? 2 - 4 ");
try {
pAmount = Integer.parseInt(input);
} catch (NumberFormatException e){
System.out.println("Not a number");
}

switch (pAmount){
case 2 ->{
String p1Name = IO.readln("Input name for player 1: ");
Location newDestP1 = randomLocation(em);
p1 = new Traveler(p1Name, randomLocation(em));
p1.setDestinationPos(newDestP1.getX(), newDestP1.getY());
p1.startJourney(newDestP1);
String p2Name = IO.readln("Input name for player 2: ");
Location newDestP2 = randomLocation(em);
p2 = new Traveler(p2Name, randomLocation(em));
p2.setDestinationPos(newDestP2.getX(), newDestP2.getY());
p2.startJourney(newDestP2);
em.persist(p1);
em.persist(p2);
while(!wonGame){
p1.playerTurn(transportMethods);
p2.playerTurn(transportMethods);
if (p1.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p1.setDestinationPos(newDest.getX(), newDest.getY());
}else if(p2.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p2.setDestinationPos(newDest.getX(), newDest.getY());
}
if(p1.checkScore()){
System.out.println(p1.getPlayerName() + " wins");
wonGame = true;
} else if(p2.checkScore()){
System.out.println(p2.getPlayerName() + " wins");
wonGame = true;
}
p1.updateJourney();
p2.updateJourney();
em.persist(p1);
em.persist(p2);
}

}case 3 ->{
String p1Name = IO.readln("Input name for player 1: ");
Location newDestP1 = randomLocation(em);
p1 = new Traveler(p1Name, randomLocation(em));
p1.setDestinationPos(newDestP1.getX(), newDestP1.getY());
p1.startJourney(newDestP1);
String p2Name = IO.readln("Input name for player 2: ");
Location newDestP2 = randomLocation(em);
p2 = new Traveler(p2Name, randomLocation(em));
p2.setDestinationPos(newDestP2.getX(), newDestP2.getY());
p2.startJourney(newDestP2);
String p3Name = IO.readln("Input name for player 2: ");
Location newDestP3 = randomLocation(em);
p3 = new Traveler(p3Name, randomLocation(em));
p3.setDestinationPos(newDestP3.getX(), newDestP3.getY());
p3.startJourney(newDestP3);
em.persist(p1);
em.persist(p2);
em.persist(p3);

while(!wonGame){
p1.playerTurn(transportMethods);
p2.playerTurn(transportMethods);
p3.playerTurn(transportMethods);
if (p1.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p1.setDestinationPos(newDest.getX(), newDest.getY());
}else if(p2.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p2.setDestinationPos(newDest.getX(), newDest.getY());
}else if(p3.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p3.setDestinationPos(newDest.getX(), newDest.getY());
}
if(p1.checkScore()){
System.out.println(p1.getPlayerName() + " wins");
wonGame = true;
} else if(p2.checkScore()){
System.out.println(p2.getPlayerName() + " wins");
wonGame = true;
} else if(p3.checkScore()){
System.out.println(p3.getPlayerName() + " wins");
wonGame = true;
}
p1.updateJourney();
p2.updateJourney();
p3.updateJourney();
em.persist(p1);
em.persist(p2);
em.persist(p3);
}

}case 4 ->{
String p1Name = IO.readln("Input name for player 1: ");
Location newDestP1 = randomLocation(em);
p1 = new Traveler(p1Name, randomLocation(em));
p1.setDestinationPos(newDestP1.getX(), newDestP1.getY());
p1.startJourney(newDestP1);
String p2Name = IO.readln("Input name for player 2: ");
Location newDestP2 = randomLocation(em);
p2 = new Traveler(p2Name, randomLocation(em));
p2.setDestinationPos(newDestP2.getX(), newDestP2.getY());
p2.startJourney(newDestP2);
String p3Name = IO.readln("Input name for player 2: ");
Location newDestP3 = randomLocation(em);
p3 = new Traveler(p3Name, randomLocation(em));
p3.setDestinationPos(newDestP3.getX(), newDestP3.getY());
p3.startJourney(newDestP3);
String p4Name = IO.readln("Input name for player 4: ");
Location newDestP4 = randomLocation(em);
p4 = new Traveler(p4Name, randomLocation(em));
p4.setDestinationPos(newDestP4.getX(), newDestP4.getY());
p4.startJourney(newDestP4);
em.persist(p1);
em.persist(p2);
em.persist(p3);
em.persist(p4);
while(!wonGame){
p1.playerTurn(transportMethods);
p2.playerTurn(transportMethods);
p3.playerTurn(transportMethods);
p4.playerTurn(transportMethods);
if (p1.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p1.setDestinationPos(newDest.getX(), newDest.getY());
}else if(p2.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p2.setDestinationPos(newDest.getX(), newDest.getY());
}else if(p3.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p3.setDestinationPos(newDest.getX(), newDest.getY());
}else if(p4.checkIfPlayerIsAtDestination()){
Location newDest = randomLocation(em);
p4.setDestinationPos(newDest.getX(), newDest.getY());
}
if(p1.checkScore()){
System.out.println(p1.getPlayerName() + " wins");
wonGame = true;
} else if(p2.checkScore()){
System.out.println(p2.getPlayerName() + " wins");
wonGame = true;
} else if(p3.checkScore()){
System.out.println(p3.getPlayerName() + " wins");
wonGame = true;
}else if(p4.checkScore()){
System.out.println(p4.getPlayerName() + " wins");
wonGame = true;
}
p1.updateJourney();
p2.updateJourney();
p3.updateJourney();
p4.updateJourney();
em.persist(p1);
em.persist(p2);
em.persist(p3);
em.persist(p4);
}
}
}

tx.commit();

} catch (Exception e) {
if (tx.isActive()) tx.rollback();
throw e;
} finally {
em.close();
}
}

//Randomize through all points for continent, country and locations to get a random location. If the country has no locations, loop again until it finds a location.
static public Location randomLocation(EntityManager em){

Long contCount = em.createQuery("Select count(c) from Continent c", Long.class).getSingleResult();

Continent randCont = em.createQuery("select c from Continent c", Continent.class).setFirstResult(randomIndex(contCount)).setMaxResults(1).getSingleResult();

while(randCont.getId() == 2){
randCont = em.createQuery("select c from Continent c", Continent.class).setFirstResult(randomIndex(contCount)).setMaxResults(1).getSingleResult();
}

Long countryCount = em.createQuery("Select count(c) from Country c where c.continent = :continent", Long.class)
.setParameter("continent", randCont)
.getSingleResult();

Country randCountry = em.createQuery("select c from Country c where continent = :continent", Country.class)
.setParameter("continent", randCont)
.setFirstResult(randomIndex(countryCount))
.setMaxResults(1).getSingleResult();

Long locCount = em.createQuery("select count(o) from Location o where o.country = :country", Long.class)
.setParameter("country", randCountry)
.getSingleResult();

while(locCount == 0){
randCountry = em.createQuery("select c from Country c where continent = :continent", Country.class)
.setParameter("continent", randCont)
.setFirstResult(randomIndex(countryCount))
.setMaxResults(1).getSingleResult();

locCount = em.createQuery("select count(o) from Location o where o.country = :country", Long.class)
.setParameter("country", randCountry)
.getSingleResult();
}

Location randLocation = em.createQuery("select l from Location l where l.country = :country", Location.class)
.setParameter("country", randCountry).setFirstResult(randomIndex(locCount)).setMaxResults(1).getSingleResult();

System.out.println(randLocation.getName() + " " + randLocation.getX() + " " + randLocation.getY());

return randLocation;
}

static public int randomIndex(long indexes){
if (indexes <= 0) {
throw new IllegalArgumentException("indexes must be > 0");
}
return (int) (Math.random() * indexes);
}
}

Loading
Loading