Difference between revisions of "Java - I/O und Exceptions"

From Coders.Bay Wiki
Jump to navigation Jump to search
 
(27 intermediate revisions by 2 users not shown)
Line 8: Line 8:


<font size=3><strong>Requirements</strong></font>
<font size=3><strong>Requirements</strong></font>
======InvaldName-Exception======   
======InvaldPersonName-Exception======   
Wenn ein Nutzer eine Person anlegt, die eine Zahl im Namen hat, soll eine "InvalidName-Exception" geworfen werden.<br>
Wenn ein Nutzer eine Person anlegt, die eine Zahl im Namen hat, soll eine "InvalidPersonName-Exception" geworfen werden.<br>
Die Anlegung des Objektes soll abgebrochen werden und der Nutzer informiert, dass dieser Name ungültig ist.<br>
Die Anlegung des Objektes soll abgebrochen werden und der Nutzer informiert, dass dieser Name ungültig ist.<br>
Das Programm soll nicht terminieren, sondern den Nutzer erneut bitten einen Namen einzugeben.<br>
Das Programm soll nicht terminieren, sondern den Nutzer erneut bitten einen Namen einzugeben.<br>
Line 23: Line 23:


==File I/O==
==File I/O==
====Aufgabe: Pokemon====
Der Spieler kann zu Beginn des Spiels ein Pokémon durch Angabe der
Index-Nummer oder dem Namen des Pokémons wählen, dieses wird für den
Kampf ausgewählt.


Gib auf der Konsole aus, um welches Pokemon es sich handelt und
<font size=4><strong>Aufgabe: Nutze die CSV-Dateien um ein Pokémon Arena Spiel zu erstellen</strong></font>
seine ASCII-Art Repräsentierung - falls es keine gibt, kannst du ein Fallback ASCII art einfügen ;)
Das gegnerische Pokemon wählt der Computer zufällig.


Die Pokémon sollen aus einer CSV Datei importiert werden (händisches Erfassen ist nicht notwendig).  
Mit den zur Verfügung gestellten Datensätzen soll der Arenakampf von Pokémon nachgebaut werden.<br>
Mögliche Quelle: https://docs.google.com/spreadsheets/d/1c9NgO5WBtgEhcG4fSl8PZqmAkCIf-uVY/edit?usp=sharing&ouid=117011915166077517693&rtpof=true&sd=true
Der User soll ein Pokémon Team auswählen und mithilfe verschiedener Attacken ein gegnerisches Pokémon Team, welches vom Computer gesteuert wird, bekämpfen.
https://gist.github.com/armgilles/194bcff35001e7eb53a2a8b441e8b2c6
(Andere Versionen sind natürlich auch möglich.)


=====Kampf=====
<font size=3><strong>Requirements</strong></font>
=====CSV Einlesen=====


Zu Beginn des Kampfs, werden dem Spieler die Stats seines Pokémons ausgegeben.  
Importiere die Pokémon CSV und Attack CSV aus dem Kompetenzcenter in dein Projekt.<br>
Das könnte z.B so aussehen:
Lese die Daten ein und erstelle daraus die notwendigen Klassen.<br>
Jedes Pokémon soll vier zufällige Attacken bekommen. Mindestens eine davon soll vom Typ Normal sein und mindestens eine Attacke soll dem Typ des Pokemon entsprechen.


{|
=====Auswahl=====
|Bulbasaur (80/100 ♥)||
|Squirtle (100/100 ♥)
|-
|(1) Overgrow||
|Torrent
|-
|(2) Chlorophyll||
|Rain Dish
|}


Bulbasaur ist dabei der Name des Pokémons. 80 seine aktuelle Gesundheit, 100 ist die maximale Gesundheit (HP google sheet)
Beide Spieler sollen sechs Pokémon erhalten. Das gleiche Pokémon kann mehrfach vorkommen.<br>
Overgrow und Chlorophyll sind dabei die beiden möglichen Attacken (abilities).  
Wenn ein Pokémon selektiert wird, wird sein Level zufällig von 10 bis 100 ausgewürfelt. Die Werte des Pokémon skalieren mit dem Level.<br>
Für jedes Level erhöhen sich die HP um 1/25 pro Level, die Att/Def/Sp.Att/Sp.Def um 1/50 pro Level und die Speed um 1/100 pro Level.<br>
Der Nutzer kann zu Beginn des Spiels das erste Pokémon durch Angabe der Index-Nummer oder dem Namen wählen, dieses wird für den Kampf ausgewählt.<br>
Anschließend werden fünf zufällige weitere hinzugefügt.<br>
Gib auf der Konsole aus, um welche Pokémon es sich handelt und welche Werte und mögliche Attacken es hat.<br>
Die gegnerischen Pokémon wählt der Computer zufällig.


Der Benutzer kann dann durch Angabe einer Zahl spezifizieren welche Attacke ausgewählt werden soll (1 oder 2 in unserem Fall).
=====Kampf=====
Bei einer fehlerhaften Angabe, solltest du den Benuter auffordern eine existierende Attacke zu wählen.
 
Auf der Konsole solltest du dann ausgeben welche Attacke gewählt wurde:
 
Bulbasur attacks with Overgrow. Bulbasur deals X damage.
 
 
Dann sollte Squirtles health um X verringert werden.
 
======Schadensberechnung======


Der Schaden den das Pokemon anrichtet errechnet sich aus dem `attack` Wert des angreifendes Pokemons multipliziert mit
Nach der Selektion soll der Kampf gestartet werden.<br>
einem zufälligen Modifikator (zwischen 0 und 1).  
Zu Beginn des Kampfs werden dem Nutzer die Werte all seiner Pokémon ausgegeben.<br>
Natürlich verteidigt sich das gegnerische Pokemon aus - der Verteidigungswert errechnet sich aus dem `defense` Wert des verteidigendes
Der Nutzer kann eines der Pokémon auswählen. Dieses kommt in den Kampf gegen ein zufälliges des gegnerischen Teams.<br>
Pokemons multipliziert mit einem zufälligen Modifikator (zwischen 0 und 0.5).  
Haben beide Spieler ein Pokémon ausgewählt, beginnt der Kampf.<br>


Wenn nun der Angriffswert - Verteidigungswert positiv ist, wird dieser Wert von den HP jenes Pokemons abgezogen, dass angegriffen wird.  
Dem Nutzer werden die vier Attacken aufgelistet. Zusätzlich noch die Möglichkeit ein neues Pokémon zu wählen und die Möglichkeit den Kampfverlauf automatisch zu beenden.<br>
Wählt der Nutzer einen Angriff aus der Liste, versucht das Pokémon anzugreifen.<br>
Wenn die gewählte Attacke keine Powerpoints (PP) hat, kann sie nicht ausgeführt werden.<br>
Wenn die Attacke ausgeführt wird, dann beginnt die Schadensberechnung und die Powerpoints reduzieren sich um 1.


Beispiel:
=====Schadensberechnung=====
Bulbasurs hat einen Angriffswert von 48. Squirtle eine verteidigung von 65.
Der zufällige Angriffsmodifikator ist 0.7, der Verteidigungsmodifikator 0.1.


0.7*48 - 0.1*65 = 27.1
Der Schaden berechnet sich wie folgt:


Also wird bei dieser Attacke Squirtle 27 HP verlieren.
Schaden = (Atk_Pw) * (APk_A / DPk_D) * (Lvl / 50) * Rnd * STAB


Das gegnerische Pokemon wählt seine Attacke zufällig.  
Atk_Pw = Power Wert der Attacke, die Ausgewählt wurde<br>
APk_A = Attack Wert des angreifenden Pokémon , falls der Angriff ein Physical kind ist. Special Attack Wert, falls es ein Special kind ist<br>
DPk_D = Defense Wert des verteidigenden Pokémon , falls der Angriff ein Physical kind ist. Special Defense Wert, falls der Angriff ein Special kind ist<br>
Lvl = Das Level des angreifenden Pokémon <br>
50 = Die Zahl 50<br>
Rnd = Zufälliger Wert zwischen 0,5 und 1,5<br>
STAB = 1,25 Falls der Type des Angriffs einem der Types des Pokémon entspricht, sonst 1<br>


Das Pokemon dass zu erst 0 oder weniger HP erreicht verliert.
Das Pokémon mit dem größeren Speed Wert greift zuerst an. Verliert das angegriffene Pokémon bevor es angegriffen hat, wird der Angriff übersprungen.


=====Zusatzaufgabe (sehr empfohlen 😜)=====
=====Zusatzaufgabe Effektivität=====


Ein Pokemon ist 1 - bis 2 Typen zugehörig -- siehe git.
Erweitere die Schadensberechnung und berücksichtige die Typen des Angriffs und des verteidigenden Pokémon.
Persistiere diese  zusätzlich in unserem Datenmodell. Ein Pokemon hat zusätzliche Modifikatoren für
den Angriff/die Verteidigung gegen Pokemons von bestimmten Typen - Spalten against_bug, against_grass, .. (siehe git).


Bulbasur ist ein grass und poison Pokemon. Squirtle ist ein water Pokemon.
Schaden = (Atk_Pw) * (APk_A / DPk_D) * (Lvl / 50) * Rnd * STAB * Eff1 * Eff2
Bulbasur hat einen Wert von 0.5 für against_water.
Dieser Modifikator muss auf die base attack angewandt werden d.h. bei unserem vorigen Beispiel würde sich der Angriffswert folgendermaßen ändern:


0.7*0.5*48 - 0.1*65 = 10.3
Eff1 = Effektivität des Angriffs gegen Typ1 des verteidigenden Pokémon <br>
Eff2 = Effektivität des Angriffs gegen Typ2 des verteidigenden Pokémon <br>


Das heißt Squirtle würde nur 10 HP verlieren.
Nutze die Effectiveness.csv Datei, um die Koeffizienten zu ermitteln.


=English=
=English=
Line 112: Line 96:


<font size=3><strong>Requirements</strong></font>
<font size=3><strong>Requirements</strong></font>
======InvaldName-Exception======   
======InvaldPersonName-Exception======   
When a user creates a person that has a number in the name, an "InvalidName-Exception" should be thrown.<br>
When a user creates a person that has a number in the name, an "InvalidPersonName-Exception" should be thrown.<br>
The creation of the object shall be aborted and the user should be informed that this name is invalid.<br>
The creation of the object shall be aborted and the user should be informed that this name is invalid.<br>
The program should not terminate, but ask the user to enter a new name.<br>
The program should not terminate, but ask the user to enter a new name.<br>
Line 125: Line 109:
======Termination======
======Termination======
The execution of the program shall only stop for the user input ":q!"
The execution of the program shall only stop for the user input ":q!"
==File I/O==
<font size=4><strong>Task: Use the CSV files to create a Pokémon Arena game</strong></font>
Use the provided data sets to recreate the arena battle of Pokemon.<br>
The user is to select a Pokemon team and use different attacks to fight an opposing Pokemon team controlled by the computer.
=====CSV Import=====
Import the Pokémon CSV and Attack CSV from the Competence Center into your project.<br>
Read the data and create the necessary classes from it.<br>
Each Pokemon should have four random attacks. At least one of them should be of type Normal and at least one attack should correspond to the Pokemon's type.
=====Select=====
Both players shall receive six Pokemon. The same Pokemon may appear more than once.<br>
When a Pokemon is selected, its level is randomly rolled from 10 to 100. The Pokémon 's stats scale with its level.<br>
For each level, HP increases by 1/25 per level, Att/Def/Sp.Att/Sp.Def increases by 1/50 per level, and Speed increases by 1/100 per level.<br>
The user can choose the first Pokémon at the beginning of the game by specifying the index number or name of the Pokémonm that will be selected for battle.<br>
After that, five random others are added.<br>
Display the selected team of the player on the console.<br>
The opposing Pokémon will be chosen randomly by the computer.
=====Fight=====
After the selection, the fight should be started.<br>
At the beginning of the fight, the user is given the stats of all his Pokémon.<br>
The user can select one of the Pokémon. The chosen one fights against a random one of the opposing team.<br>
When both players have selected a Pokémon, the battle starts.<br>
The four attacks are listed to the user. In addition, the possibility to choose a new Pokémon and the possibility to conclude the fight automatically.<br>
If the user chooses an attack from the list, the Pokémon tries to attack.<br>
If the selected attack has no power points (PP), it cannot be executed.<br>
If the attack is executed, then the damage calculation starts and power points decrease by 1.
=====DamageCalculation=====
Damage is calculated as follows:
Damage = (Atk_Pw) * (APk_A / DPk_D) * (Lvl / 50) * Rnd * STAB
Atk_Pw = Power value of the attack that was Selected<br>
APk_A = Attack value of the attacking Pokemon, if the attack is a Physical kind. Special Attack value, if it is a Special kind<br>
DPk_D = Defense value of the defending Pokemon, if the attack is a Physical kind. Special Defense value if the attack is a Special kind<br>
Lvl = The level of the attacking Pokemon<br>
50 = The number 50<br>
Rnd = Random value between 0.5 and 1.5<br>
STAB = 1.25 If the Type of the attack matches one of the Pokemon's Types, otherwise 1<br>
The Pokemon with the greater Speed value attacks first. If the attacked Pokemon loses before it has attacked, the attack is skipped.
=====Additional Task Effectivness=====
Expand the damage calculation to take into account the types of the attacking and defending Pokemon.
Damage = (Atk_Pw) * (APk_A / DPk_D) * (Lvl / 50) * Rnd * STAB * Eff1 * Eff2
Eff1 = Effectiveness of attack against type1 of defending Pokemon<br>
Eff2 = Effectiveness of attack against type2 of defending Pokemon<br>
Use the Effectiveness.csv file to determine the coefficients.

Latest revision as of 09:54, 28 March 2023

Exceptions[edit]

Aufgabe: Erweitere die Personenverwaltung um Exceptions

Die, in der früheren Aufgabe erstellte, Personenverwaltung, soll nun etwas sicherer für Fehler gemacht werden.
Erweitere den Code so, dass der User die Personen mit Hilfe eines Scanners erstellt.
Dabei soll auf ungültige Inputs vernünftig reagiert werden, ohne dass das Programm abstürzt.

Requirements

InvaldPersonName-Exception[edit]

Wenn ein Nutzer eine Person anlegt, die eine Zahl im Namen hat, soll eine "InvalidPersonName-Exception" geworfen werden.
Die Anlegung des Objektes soll abgebrochen werden und der Nutzer informiert, dass dieser Name ungültig ist.
Das Programm soll nicht terminieren, sondern den Nutzer erneut bitten einen Namen einzugeben.

NullpointerException[edit]

Der Nutzer soll Personen durch ihren Namen finden können.
Wird ein Name eingegeben, der nicht existiert, soll eine NullpointerException in der Personalverwaltung geworfen werden.
Der Stacktrace dieser Exception soll in der Main ausgegeben werden.
Das Programm soll nicht terminieren und weitere Inputs erhalten.

Terminierung[edit]

Das Programm soll nur durch die Eingabe von ":q!" terminierbar sein.

File I/O[edit]

Aufgabe: Nutze die CSV-Dateien um ein Pokémon Arena Spiel zu erstellen

Mit den zur Verfügung gestellten Datensätzen soll der Arenakampf von Pokémon nachgebaut werden.
Der User soll ein Pokémon Team auswählen und mithilfe verschiedener Attacken ein gegnerisches Pokémon Team, welches vom Computer gesteuert wird, bekämpfen.

Requirements

CSV Einlesen[edit]

Importiere die Pokémon CSV und Attack CSV aus dem Kompetenzcenter in dein Projekt.
Lese die Daten ein und erstelle daraus die notwendigen Klassen.
Jedes Pokémon soll vier zufällige Attacken bekommen. Mindestens eine davon soll vom Typ Normal sein und mindestens eine Attacke soll dem Typ des Pokemon entsprechen.

Auswahl[edit]

Beide Spieler sollen sechs Pokémon erhalten. Das gleiche Pokémon kann mehrfach vorkommen.
Wenn ein Pokémon selektiert wird, wird sein Level zufällig von 10 bis 100 ausgewürfelt. Die Werte des Pokémon skalieren mit dem Level.
Für jedes Level erhöhen sich die HP um 1/25 pro Level, die Att/Def/Sp.Att/Sp.Def um 1/50 pro Level und die Speed um 1/100 pro Level.
Der Nutzer kann zu Beginn des Spiels das erste Pokémon durch Angabe der Index-Nummer oder dem Namen wählen, dieses wird für den Kampf ausgewählt.
Anschließend werden fünf zufällige weitere hinzugefügt.
Gib auf der Konsole aus, um welche Pokémon es sich handelt und welche Werte und mögliche Attacken es hat.
Die gegnerischen Pokémon wählt der Computer zufällig.

Kampf[edit]

Nach der Selektion soll der Kampf gestartet werden.
Zu Beginn des Kampfs werden dem Nutzer die Werte all seiner Pokémon ausgegeben.
Der Nutzer kann eines der Pokémon auswählen. Dieses kommt in den Kampf gegen ein zufälliges des gegnerischen Teams.
Haben beide Spieler ein Pokémon ausgewählt, beginnt der Kampf.

Dem Nutzer werden die vier Attacken aufgelistet. Zusätzlich noch die Möglichkeit ein neues Pokémon zu wählen und die Möglichkeit den Kampfverlauf automatisch zu beenden.
Wählt der Nutzer einen Angriff aus der Liste, versucht das Pokémon anzugreifen.
Wenn die gewählte Attacke keine Powerpoints (PP) hat, kann sie nicht ausgeführt werden.
Wenn die Attacke ausgeführt wird, dann beginnt die Schadensberechnung und die Powerpoints reduzieren sich um 1.

Schadensberechnung[edit]

Der Schaden berechnet sich wie folgt:

Schaden = (Atk_Pw) * (APk_A / DPk_D) * (Lvl / 50) * Rnd * STAB

Atk_Pw = Power Wert der Attacke, die Ausgewählt wurde
APk_A = Attack Wert des angreifenden Pokémon , falls der Angriff ein Physical kind ist. Special Attack Wert, falls es ein Special kind ist
DPk_D = Defense Wert des verteidigenden Pokémon , falls der Angriff ein Physical kind ist. Special Defense Wert, falls der Angriff ein Special kind ist
Lvl = Das Level des angreifenden Pokémon
50 = Die Zahl 50
Rnd = Zufälliger Wert zwischen 0,5 und 1,5
STAB = 1,25 Falls der Type des Angriffs einem der Types des Pokémon entspricht, sonst 1

Das Pokémon mit dem größeren Speed Wert greift zuerst an. Verliert das angegriffene Pokémon bevor es angegriffen hat, wird der Angriff übersprungen.

Zusatzaufgabe Effektivität[edit]

Erweitere die Schadensberechnung und berücksichtige die Typen des Angriffs und des verteidigenden Pokémon.

Schaden = (Atk_Pw) * (APk_A / DPk_D) * (Lvl / 50) * Rnd * STAB * Eff1 * Eff2

Eff1 = Effektivität des Angriffs gegen Typ1 des verteidigenden Pokémon
Eff2 = Effektivität des Angriffs gegen Typ2 des verteidigenden Pokémon

Nutze die Effectiveness.csv Datei, um die Koeffizienten zu ermitteln.

English[edit]

Exceptions[edit]

Task: Add exceptions to the person management

The person administration, that was created in the previous task, should now be made more resilient to errors.
Extend the code so the user can create the people through a scanner.
React properly to invalid inputs by using Exceptions to change Control Flow.

Requirements

InvaldPersonName-Exception[edit]

When a user creates a person that has a number in the name, an "InvalidPersonName-Exception" should be thrown.
The creation of the object shall be aborted and the user should be informed that this name is invalid.
The program should not terminate, but ask the user to enter a new name.

NullpointerException[edit]

The user should be able to find people by name.
If a name is entered that does not exist, a NullpointerException should be thrown in the person management.
The stacktrace of this exception should be printed in the main method.
The program shall not terminate and receive new inputs from the user.

Termination[edit]

The execution of the program shall only stop for the user input ":q!"


File I/O[edit]

Task: Use the CSV files to create a Pokémon Arena game


Use the provided data sets to recreate the arena battle of Pokemon.
The user is to select a Pokemon team and use different attacks to fight an opposing Pokemon team controlled by the computer.

CSV Import[edit]

Import the Pokémon CSV and Attack CSV from the Competence Center into your project.
Read the data and create the necessary classes from it.
Each Pokemon should have four random attacks. At least one of them should be of type Normal and at least one attack should correspond to the Pokemon's type.

Select[edit]

Both players shall receive six Pokemon. The same Pokemon may appear more than once.
When a Pokemon is selected, its level is randomly rolled from 10 to 100. The Pokémon 's stats scale with its level.
For each level, HP increases by 1/25 per level, Att/Def/Sp.Att/Sp.Def increases by 1/50 per level, and Speed increases by 1/100 per level.
The user can choose the first Pokémon at the beginning of the game by specifying the index number or name of the Pokémonm that will be selected for battle.
After that, five random others are added.
Display the selected team of the player on the console.
The opposing Pokémon will be chosen randomly by the computer.

Fight[edit]

After the selection, the fight should be started.
At the beginning of the fight, the user is given the stats of all his Pokémon.
The user can select one of the Pokémon. The chosen one fights against a random one of the opposing team.
When both players have selected a Pokémon, the battle starts.

The four attacks are listed to the user. In addition, the possibility to choose a new Pokémon and the possibility to conclude the fight automatically.
If the user chooses an attack from the list, the Pokémon tries to attack.
If the selected attack has no power points (PP), it cannot be executed.
If the attack is executed, then the damage calculation starts and power points decrease by 1.

DamageCalculation[edit]

Damage is calculated as follows:

Damage = (Atk_Pw) * (APk_A / DPk_D) * (Lvl / 50) * Rnd * STAB

Atk_Pw = Power value of the attack that was Selected
APk_A = Attack value of the attacking Pokemon, if the attack is a Physical kind. Special Attack value, if it is a Special kind
DPk_D = Defense value of the defending Pokemon, if the attack is a Physical kind. Special Defense value if the attack is a Special kind
Lvl = The level of the attacking Pokemon
50 = The number 50
Rnd = Random value between 0.5 and 1.5
STAB = 1.25 If the Type of the attack matches one of the Pokemon's Types, otherwise 1

The Pokemon with the greater Speed value attacks first. If the attacked Pokemon loses before it has attacked, the attack is skipped.

Additional Task Effectivness[edit]

Expand the damage calculation to take into account the types of the attacking and defending Pokemon.

Damage = (Atk_Pw) * (APk_A / DPk_D) * (Lvl / 50) * Rnd * STAB * Eff1 * Eff2

Eff1 = Effectiveness of attack against type1 of defending Pokemon
Eff2 = Effectiveness of attack against type2 of defending Pokemon

Use the Effectiveness.csv file to determine the coefficients.