From 97ac55329a3737792287906a9663b7bea28fa8b1 Mon Sep 17 00:00:00 2001 From: eamea Date: Mon, 21 Sep 2015 14:07:04 +0200 Subject: [PATCH] mta15344 solution Changes to SoccerTestClass and FriendlySoccerMatch, to solve the problems given in lecture 1. --- Lecture1/FriendlySoccerMatch.java | 130 +++++++++++++++++++++--------- Lecture1/SoccerTestClass.java | 9 ++- README.md | 2 +- 3 files changed, 99 insertions(+), 42 deletions(-) diff --git a/Lecture1/FriendlySoccerMatch.java b/Lecture1/FriendlySoccerMatch.java index f78ce10..38817a4 100644 --- a/Lecture1/FriendlySoccerMatch.java +++ b/Lecture1/FriendlySoccerMatch.java @@ -1,10 +1,15 @@ +package pcss.lecture01; + import java.util.Random; +import java.util.Scanner; public class FriendlySoccerMatch implements FriendlyMatch { + Scanner sc = new Scanner(System.in); private String nameHomeTeam; private String nameGuestTeam; private int pointsHome; private int pointsGuest; + private boolean halfTimeHappened = false; // Constructor public FriendlySoccerMatch(){ @@ -36,7 +41,7 @@ public int getGuestPoints() { public String getResultText() { return "The friendly game ends with \n\n"+nameHomeTeam+" - "+nameGuestTeam +" "+pointsHome+":"+pointsGuest+"."; } - + public void startGame(Team t1, Team t2){ nameHomeTeam = t1.getName(); nameGuestTeam = t2.getName(); @@ -62,6 +67,50 @@ public void startGame(Team t1, Team t2){ //******************************************* // A new action can take place ... time = time + nextAction; + + //INTERACTIVE! Switch-out can happen between 40 and 55 minutes, only once. + if(time > 40 && time < 55 && halfTimeHappened == false){ + halfTimeHappened = true; + System.out.println("\nHalf-time! Would you like to switch out? 1 for yes, 2 for no."); + + //Saving the answer from the scanner. + int answer = sc.nextInt(); + + //Checking the answer and acting accordingly + if(answer == 1){ + + //Printing out a list of the current players. + System.out.println("Who would you like to switch out? (write the player number)"); + Player[] teamList = t1.getPlayers(); + for(int i = 0; i < 10; i++) + System.out.println(i+1 + " " + teamList[i].getName()); + + //Saving the number of the switched out player. + int switchedOutPlayer = sc.nextInt(); + + //Printing out a list of available players and saving the answer in the scanner. + System.out.println("\nWho would you like to switch in?"); + for(int i = 10; i < 11; i++) + System.out.println(i+1 + " " + teamList[i].getName()); + int switchedInPlayer = sc.nextInt(); + + //Taking the old player and replacing it with the new. + t1.getPlayers()[switchedOutPlayer-1] = t1.getPlayers()[switchedInPlayer-1]; + + //Printing out the new player list. + System.out.println("New player list:"); + for(int i = 0; i < 10; i++) + System.out.print("\n" + (i+1) + " " + teamList[i].getName()); + + } + //The user answered "no", the game will continue. + else if (answer == 2) + System.out.println("Continuing game without switch-out."); + + //The user entered an invalid answer, and they'll get slapped for that. + else + System.out.println("Invalid answer, you're weird. Continuing game."); + } // influence of motivation on strength: float strength_1 = (t1.getStrength()/2.0f) + ((t1.getStrength()/2.0f)*(t1.getMotivation()/10.0f)); @@ -71,6 +120,11 @@ public void startGame(Team t1, Team t2){ int deviation = r.nextInt(2); if (strength_1 > t1.getTrainer().getExperience()) deviation = -deviation; + //Math.max(1, x) means that even if x is a negative number, + //it will become 1. You will never go below 1. + //Math.min(10, y) means that even if y is less than 10, + //it will become 10. + //So these numbers will always be between 1 and 10. strength_1 = Math.max(1, Math.min(10, strength_1 + deviation)); deviation = r.nextInt(2); if (strength_2 > t2.getTrainer().getExperience()) @@ -78,48 +132,46 @@ public void startGame(Team t1, Team t2){ strength_2 = Math.max(1, Math.min(10, strength_2 + deviation)); // randomly choose a player for next shot - int shooter = r.nextInt(10); if ((r.nextInt(Math.round(strength_1+strength_2))-strength_1)<=0){ - Player p = t1.getPlayers()[shooter]; - Keeper k = t2.getKeeper(); - int shot = p.shootsOnGoal(); - // check if shot is saved - boolean goal = !k.saveShot(shot); - System.out.println(); - System.out.println(time+".Minute: "); - System.out.println(" Chance for "+t1.getName()+" ..."); - System.out.println(" "+p.getName()+" shoots"); - if (goal) { - pointsHome++; - p.addGoal(); - System.out.println(" Goal!!! "+pointsHome+":"+ - pointsGuest+" "+p.getName()+"("+p.getGoals()+")"); - } - else { - System.out.println(" "+t2.getKeeper().getName() - +" saves briliantly."); - } + teamShoots(t1, t2, time, true); } // IF else{ - Player p = t2.getPlayers()[shooter]; - Keeper k = t1.getKeeper(); - int shot = p.shootsOnGoal(); - boolean goal = !k.saveShot(shot); - System.out.println(); - System.out.println(time+".Minute: "); - System.out.println(" Chance for "+t2.getName()+" ..."); - System.out.println(" "+p.getName()+" shoots"); - - if (goal) { - pointsGuest++; - p.addGoal(); - System.out.println(" GOAL!!! "+pointsHome+":"+ pointsGuest+" "+p.getName()+"("+p.getGoals()+")"); - } - else { - System.out.println(" "+t1.getKeeper().getName() - +" saves brilliantly."); - } + teamShoots(t2, t1, time, false); } // else } //WHILE } + + //teamShoots(attacking team, defending team, time, are they homeTeam or not?) + private void teamShoots(Team t1, Team t2, int time, boolean homeTeam){ + Random r = new Random(); + int comment = r.nextInt(5); + int shooter = r.nextInt(10); + + //String-arrays with different comments + String[] goalComment = new String[]{"GOOOOOAL!!", "Amazing score!", "HE SCORES! ALSO I SPILLED MY DRINK!!", "And it goes in the net!", "Did.. was there goal..? Technicians looking at i- there was a goal!!"}; + String[] saveComment = new String[]{t2.getKeeper().getName()+" saves brilliantly!", "But the ball is caught before it reaches the net!", "Amazing save from "+t2.getKeeper().getName()+"!", "BUT HE WAS DENIED!!", "But the lousy shot was easily saved by "+t2.getKeeper().getName()}; + + Player p = t1.getPlayers()[shooter]; + Keeper k = t2.getKeeper(); + int shot = p.shootsOnGoal(); + // check if shot is saved + boolean goal = !k.saveShot(shot); + System.out.println(); + System.out.println(time+".Minute: "); + System.out.println(" Chance for "+t1.getName()+" ..."); + System.out.println(" "+p.getName()+" shoots"); + if (goal) { + if(homeTeam) + pointsHome++; + else + pointsGuest++; + p.addGoal(); + System.out.println(goalComment[comment]+ " " +pointsHome+":"+ + pointsGuest+" "+p.getName()+"("+p.getGoals()+")"); + } + else { + System.out.println(saveComment[comment]); + } + } + } diff --git a/Lecture1/SoccerTestClass.java b/Lecture1/SoccerTestClass.java index 54878b3..b4f40da 100644 --- a/Lecture1/SoccerTestClass.java +++ b/Lecture1/SoccerTestClass.java @@ -1,3 +1,4 @@ +package pcss.lecture01; public class SoccerTestClass { @@ -9,7 +10,7 @@ public static void main(String[] args) { // Team 1 (Germany) Trainer t1 = new Trainer("Juergen Klinsmann", 34, 9); Keeper k1 = new Keeper("J. Lehmann", 36, 8, 1, 9, 7); - Player[] p1 = new Player[10]; + Player[] p1 = new Player[11]; p1[0] = new Player("P. Lahm", 23, 9, 5, 9); p1[1] = new Player("C. Metzelder", 25, 8, 2, 7); p1[2] = new Player("P. Mertesacker", 22, 9, 2, 8); @@ -20,13 +21,15 @@ public static void main(String[] args) { p1[7] = new Player("L. Podolski", 21, 7, 8, 9); p1[8] = new Player("M. Klose", 28, 10, 9, 7); p1[9] = new Player("O. Neuville", 33, 8, 8, 7); + + p1[10] = new Player("E. Ustrup", 22, 3, 1, 0); // ************************************************************* // ************************************************************* // Team 2 (Brazil) Trainer t2 = new Trainer("Carlos Alberto Parreira", 50, 3); Keeper k2 = new Keeper("Dida", 25, 9, 1, 6, 8); - Player[] p2 = new Player[10]; + Player[] p2 = new Player[11]; p2[0] = new Player("Cafu", 33, 8, 4, 6); p2[1] = new Player("R. Carlos", 32, 9, 9, 2); p2[2] = new Player("Lucio", 29, 10, 9, 9); @@ -37,6 +40,8 @@ public static void main(String[] args) { p2[7] = new Player("Adriano", 23, 8, 8, 4); p2[8] = new Player("Robinho", 19, 9, 8, 9); p2[9] = new Player("Ronaldo", 28, 4, 10, 2); + + p2[10] = new Player("M. Hagemann", 19, 1, 5, 8); // ************************************************************* Team tm1 = new Team("Germany 2006",t1,k1,p1); diff --git a/README.md b/README.md index 0b9db4c..b5f65b5 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# pcss +# pcss15344