659 lines
28 KiB
C#
Raw Normal View History

2022-10-15 18:39:27 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Programmation_objet_TLESIO21.projet {
public class GameManager {
private string Path;
private List<Pokemon> Pkm = new List<Pokemon>();
private List<Character> Characters = new List<Character>();
private Player p1;
private Player p2;
private int p1CharacterChoosen;
private int p2CharacterChoosen;
2022-10-13 22:03:05 +02:00
public GameManager() {
2022-10-15 18:39:27 +02:00
this.p1CharacterChoosen = 0;
this.p2CharacterChoosen = 0;
2022-10-13 22:03:05 +02:00
this.Path = "../../../csv/";
2022-10-16 11:14:06 +02:00
//this.Path = "./";
2022-10-15 18:39:27 +02:00
}
private static bool StringIsValidInt(String s) {
foreach (char c in s) {
if (c < '0' || c > '9')
return false;
}
return true;
}
private static bool StringIsValidIntFight(String s) {
foreach (char c in s) {
if (c < '0' || c > '7')
return false;
}
return true;
}
private static bool TabStringIsValidInt(String[] tab) {
foreach(String s in tab) {
foreach (char c in s) {
if (c < '0' || c > '9') {
return false;
}
}
}
return true;
}
private static bool StringIsValidIntCharacters(String s) {
foreach(char c in s) {
if (c < '1' || c > '9') {
return false;
}
}
return true;
}
private static String DisplayTeam(Player p) {
String sb = "";
int i = 0;
foreach(Pokemon pkm in p.getTeam()) {
if(pkm.isAlive()) {
sb += "["+(i+1)+"]" + pkm.getName() + ". ";
} else {
sb += "[OUT]" + pkm.getName() + ". ";
}
++i;
}
return sb;
}
public void InitializeGame() {
StreamReader strm = new StreamReader(this.Path + "Title.txt");
String strmL;
//Read the first line of text
strmL = strm.ReadLine();
//Continue to read until you reach end of file
while (strmL != null) {
if (strmL[0] == '/') {
strmL = strm.ReadLine();
}
//write the line to console window
Console.WriteLine(strmL);
//Read the next line
strmL = strm.ReadLine();
}
//close the file
strm.Close();
Console.WriteLine(" ");
String entreeStandard;
bool p1 = false;
bool p2 = false;
while(!p1) {
Console.WriteLine("Merci d'entrer le nom du joueur 1 :");
entreeStandard = Console.ReadLine();
if(entreeStandard == "") {
Console.WriteLine("Veuillez resaisir. Nom incorrect.");
} else {
this.p1 = new Player(entreeStandard);
p1 = true;
}
}
while (!p2) {
Console.WriteLine("Merci d'entrer le nom du joueur 2 :");
entreeStandard = Console.ReadLine();
if (entreeStandard == "") {
Console.WriteLine("Veuillez resaisir. Nom incorrect.");
} else {
this.p2 = new Player(entreeStandard);
p2 = true;
}
}
2022-10-16 11:14:06 +02:00
Console.WriteLine("\n====================================================================================\n");
2022-10-15 18:39:27 +02:00
this.InitializePokemons();
this.ChooseTeams();
this.Combat();
}
private void ChooseTeams() {
String entreeStandard = "";
Console.WriteLine("Vous avez devant vous plusieurs dresseuses pokémon. Vous allez devoir choisir 6 pokémon parmis ceux de la dresseuse que vous allez choisir :\n");
int i = 0;
foreach(Character c in Characters) {
Console.WriteLine("[" + (i+1) + "] " + c.ToString());
++i;
}
Console.WriteLine("Choissez la dresseuse que vous pensez être la plus apte à vous prêter des pokémon pour combattre.\n" +
"Pour cela, rentrez le numéro affiché devant le nom de la dresseuse.");
bool correctChoice = false;
while (!correctChoice) {
entreeStandard = Console.ReadLine();
if(entreeStandard != "" && StringIsValidIntCharacters(entreeStandard)) {
correctChoice = true;
p1CharacterChoosen = int.Parse(entreeStandard.Substring(0,1))-1;
} else {
Console.WriteLine("Veuillez resaisir. Entrée incorrecte.");
}
}
p2CharacterChoosen = getRandom(9);
if (p2CharacterChoosen == 9) {
p2CharacterChoosen = 8;
}
2022-10-16 11:14:06 +02:00
Console.WriteLine("Vous avez choisi de faire confiance à " + Characters[p1CharacterChoosen].getName() + ".\n");
Console.WriteLine("Votre adversaire a choisis de faire confiance à " + Characters[p2CharacterChoosen].getName() + ".\n");
2022-10-15 18:39:27 +02:00
2022-10-16 11:14:06 +02:00
Console.WriteLine("Maintenant, vous allez devoir choisir 6 pokémons parmis ceux de " + Characters[p1CharacterChoosen].getName() + "\n");
2022-10-15 18:39:27 +02:00
i = 0;
foreach(Pokemon pkm in Characters[p1CharacterChoosen].getPC()) {
Console.WriteLine("[" + (i+1) + "] " + pkm.getStats());
++i;
}
Console.WriteLine("Entrez les 6 numéros des pokémons que vous souhaitez utiliser. Exemple : 1-12-5-6-10-2");
bool isTeamValid = false;
while(!isTeamValid) {
entreeStandard = Console.ReadLine();
String[] tab = entreeStandard.Split("-");
if (tab.Length == 6 && TabStringIsValidInt(tab)) {
foreach (String s in tab) {
p1.choosePokemon(Characters[p1CharacterChoosen].getPC()[int.Parse(s)-1]);
}
isTeamValid = true;
} else {
Console.WriteLine("Veuillez resaisir. Entrée incorrecte (Format attendu : 1-12-5-6-10-2).");
}
}
Console.Write("Votre équipe est constituée de : ");
foreach (Pokemon pkm in p1.getTeam()) {
Console.Write(pkm.getName() + ". ");
}
Console.WriteLine("");
for(int j = 0; j < 6; ++j) {
p2.choosePokemon(Characters[p2CharacterChoosen].getPC()[getRandom(Characters[p2CharacterChoosen].getPC().Count)]);
}
Console.Write("L'équipe de votre adversaire est constituée de : ");
foreach (Pokemon pkm in p2.getTeam()) {
Console.Write(pkm.getName() + ". ");
}
Console.WriteLine("\n\n Préparez vous, le combat va commencer. Vous allez envoyer " + p1.getTeam()[0].getName() +
"et votre adversaire va envoyer " + p2.getTeam()[0].getName() + ".");
p1.setCurrentPokemonIndex(0);
p2.setCurrentPokemonIndex(0);
}
private void Combat() {
bool isFighting = true;
bool currentTurnisValid = false;
String entreeStandard;
int p2Choice = getRandom(3);
while (isFighting) {
Console.WriteLine("\n\n\n");
while(!currentTurnisValid) {
displayCurrentPlayerTurn(p1);
Console.WriteLine("Pokemon adverse : " + p2.getCurrentPokemon() + " - Type : " + p2.getCurrentPokemon().getType());
entreeStandard = Console.ReadLine();
switch (entreeStandard) {
case "1":
Console.WriteLine(p1.getNom() + " décide que " + p1.getCurrentPokemon().getName() + " doit attaquer en physique.");
p1.getCurrentPokemon().PhysAttack(p2.getCurrentPokemon());
currentTurnisValid = true;
break;
case "2":
Console.WriteLine(p1.getNom() + " décide que " + p1.getCurrentPokemon().getName() + " doit attaquer en spécial.");
p1.getCurrentPokemon().SpeAttack(p2.getCurrentPokemon());
currentTurnisValid = true;
break;
case "3":
Console.WriteLine(p1.getNom() + " décide que " + p1.getCurrentPokemon().getName() + " revenir.");
displayTeamChange(p1);
entreeStandard = Console.ReadLine();
entreeStandard = entreeStandard.Substring(0, 1);
if(StringIsValidIntFight(entreeStandard)) {
p1.setCurrentPokemonIndex(int.Parse(entreeStandard)-1);
}
Console.WriteLine("À toi " + p2.getCurrentPokemon());
currentTurnisValid = true;
break;
default:
Console.WriteLine("Erreur de saisie. Veuillez recommencer.");
break;
}
}
currentTurnisValid = false;
if (!p2.getCurrentPokemon().isAlive()) {
bool correctSwitchChoice = false;
if (p2.canStillFight()) {
isFighting = false;
Console.WriteLine("La partie est terminée. " + p1.getNom() + " a gagné !");
correctSwitchChoice = true;
} else {
while (!correctSwitchChoice) {
Console.WriteLine(p2.getNom() + ", votre pokémon " + p2.getCurrentPokemon().getName() + " n'est plus capable de se battre. Vous devez changer de Pokémon !");
displayTeamChange(p2);
p2Choice = getRandom(6);
if (p2.getTeam()[p2Choice].isAlive()) {
Console.Write("Reviens " + p2.getCurrentPokemon());
p2.setCurrentPokemonIndex(p2Choice);
correctSwitchChoice = true;
Console.WriteLine("À toi " + p2.getCurrentPokemon());
}
}
}
}
while (!currentTurnisValid) {
p2Choice = getRandom(3);
switch (p2Choice) {
case 0:
Console.WriteLine(p2.getNom() + " décide que " + p2.getCurrentPokemon().getName() + " doit attaquer en physique.");
p2.getCurrentPokemon().PhysAttack(p1.getCurrentPokemon());
currentTurnisValid = true;
break;
case 1:
Console.WriteLine(p2.getNom() + " décide que " + p2.getCurrentPokemon().getName() + " doit attaquer en spécial.");
p2.getCurrentPokemon().SpeAttack(p1.getCurrentPokemon());
currentTurnisValid = true;
break;
case 2:
Console.WriteLine(p2.getNom() + " décide que " + p2.getCurrentPokemon().getName() + " revenir.");
displayTeamChange(p2);
p2.setCurrentPokemonIndex(getRandom(6));
currentTurnisValid = true;
Console.WriteLine("À toi " + p2.getCurrentPokemon());
break;
default:
Console.WriteLine("Erreur de saisie. Veuillez recommencer.");
break;
}
}
currentTurnisValid = false;
if (!p1.getCurrentPokemon().isAlive()) {
bool correctSwitchChoice = false;
if (p1.canStillFight()) {
isFighting = false;
Console.WriteLine("La partie est terminée. " + p2.getNom() + " a gagné !");
correctSwitchChoice = true;
} else {
while (!correctSwitchChoice) {
Console.WriteLine(p1.getNom() + ", votre pokémon " + p1.getCurrentPokemon().getName() + " n'est plus capable de se battre. Vous devez changer de Pokémon !");
displayTeamChange(p1);
entreeStandard = Console.ReadLine();
entreeStandard = entreeStandard.Substring(0, 1);
if (StringIsValidIntFight(entreeStandard) && p1.getTeam()[int.Parse(entreeStandard)-1].isAlive()) {
Console.Write("Reviens " + p1.getCurrentPokemon());
p1.setCurrentPokemonIndex(int.Parse(entreeStandard)-1);
correctSwitchChoice = true;
Console.WriteLine("À toi " + p1.getCurrentPokemon());
} else {
Console.WriteLine("Vous ne pouvez pas switch avec ce pokémon. Merci d'entrer une valeur correcte.");
}
}
}
}
}
}
private static void displayCurrentPlayerTurn(Player p) {
Console.WriteLine("C'est au tour de " + p.getNom() + " de combattre avec " + p.getCurrentPokemon().ToString() + " Type : " + p.getCurrentPokemon().getType() + ".");
Console.WriteLine("Que souhaitez vous faire : ");
Console.WriteLine("[1] Effectuer une attaque physique (ATK Physique "+ p.getCurrentPokemon().getATKPhys() +")");
Console.WriteLine("[2] Effectuer une attaque spéciale (ATK Spéciale "+ p.getCurrentPokemon().getATKSpe() +")");
Console.WriteLine("[3] Changer de pokémon");
}
private void displayTeamChange(Player p) {
Console.WriteLine("Avec quel Pokémon switch ?");
Console.WriteLine(DisplayTeam(p));
}
private int RandomMove() {
return getRandom(3);
}
2022-10-13 22:03:05 +02:00
private static int getRandom(int max) {
Random r = new Random();
return r.Next(max);
2022-10-15 18:39:27 +02:00
}
2022-10-13 22:03:05 +02:00
private Nature GenerateNature() {
switch (getRandom(25)) {
case 0:
return Nature.BOLD;
case 1:
return Nature.QUIRKY;
case 2:
return Nature.BRAVE;
case 3:
return Nature.CALM;
case 4:
return Nature.QUIET;
case 5:
return Nature.DOCILE;
case 6:
return Nature.MILD;
case 7:
return Nature.RASH;
case 8:
return Nature.GENTLE;
case 9:
return Nature.HARDY;
case 10:
return Nature.JOLLY;
case 11:
return Nature.LAX;
case 12:
return Nature.IMPISH;
case 13:
return Nature.SASSY;
case 14:
return Nature.NAUGHTY;
case 15:
return Nature.MODEST;
case 16:
return Nature.NAIVE;
case 17:
return Nature.HASTY;
case 18:
return Nature.CAREFUL;
case 19:
return Nature.BASHFUL;
case 20:
return Nature.RELAXED;
case 21:
return Nature.ADAMANT;
case 22:
return Nature.SERIOUS;
case 23:
return Nature.LONELY;
case 24:
return Nature.TIMID;
default:
return Nature.NULL;
}
2022-10-15 18:39:27 +02:00
}
2022-10-13 22:03:05 +02:00
private Pokemon? CreatePokemon(String s) {
String[] tab = s.Split(";");
switch (tab[1]) {
case "Normal":
return new PokemonNormal(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Grass":
return new PokemonGrass(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Fire":
return new PokemonFire(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Water":
return new PokemonWater(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Electric":
return new PokemonElectric(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Flying":
return new PokemonFlying(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Bug":
return new PokemonBug(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Rock":
return new PokemonRock(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Ground":
return new PokemonGround(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Psychic":
return new PokemonPsychic(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Poison":
return new PokemonPoison(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Ghost":
return new PokemonGhost(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Dark":
return new PokemonDark(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Steel":
return new PokemonSteel(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Fighting":
return new PokemonFighting(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Ice":
return new PokemonIce(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Dragon":
return new PokemonDragon(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
case "Fairy":
return new PokemonFairy(tab[0], int.Parse(tab[2]), int.Parse(tab[3]), int.Parse(tab[5]), int.Parse(tab[4]), int.Parse(tab[6]), int.Parse(tab[7]));
default:
return null;
}
2022-10-15 18:39:27 +02:00
}
private Character createCharacter(String s) {
2022-10-13 22:03:05 +02:00
String[] tab = s.Split(";");
List<Pokemon> pokemons = new List<Pokemon>();
for(int i = 0; i < getRandom(30)+7; ++i) {
2022-10-15 18:39:27 +02:00
pokemons.Add(Pkm.ElementAt(getRandom(1072)));
2022-10-13 22:03:05 +02:00
}
foreach (Pokemon pkm in pokemons) {
Nature n = GenerateNature();
pkm.SetNature(n);
this.ApplyNatureEffect(n, pkm);
this.ApplyPlayerEffect(tab[0], pkm);
}
2022-10-15 18:39:27 +02:00
return new Character(tab[0], tab[1] + ". " + tab[2], pokemons);
}
private void InitializePokemons() {
2022-10-13 22:03:05 +02:00
String line;
try {
//Pass the file path and file name to the StreamReader constructor
StreamReader srPkm = new StreamReader(this.Path + "Pokemon.csv");
StreamReader srPerso = new StreamReader(this.Path + "Characters.csv");
//Read the first line of text
line = srPkm.ReadLine();
//Continue to read until you reach end of file
while (line != null) {
if (line[0] == '/') {
line = srPkm.ReadLine();
}
//write the line to console window
2022-10-15 18:39:27 +02:00
Pkm.Add(CreatePokemon(line));
2022-10-13 22:03:05 +02:00
//Read the next line
line = srPkm.ReadLine();
}
//close the file
srPkm.Close();
//Read the first line of text
line = srPerso.ReadLine();
//Continue to read until you reach end of file
while (line != null) {
if (line[0] == '/') {
line = srPerso.ReadLine();
}
//write the line to console window
2022-10-15 18:39:27 +02:00
Characters.Add(createCharacter(line));
2022-10-13 22:03:05 +02:00
//Read the next line
line = srPerso.ReadLine();
}
//close the file
srPkm.Close();
} catch (Exception e) {
Console.WriteLine("Exception: " + e.Message);
}
2022-10-15 18:39:27 +02:00
}
private void AlterPokemonStat(String stat, double value, Pokemon p) {
switch(stat) {
case "PV":
p.setPV((int)Math.Floor(p.getPV() * value));
break;
case "ATKPhys":
p.setATKPhys(((int)Math.Floor(p.getATKPhys() * value)));
break;
case "ATKSpe":
p.setATKSpe(((int)Math.Floor(p.getATKSpe() * value)));
break;
case "DEFPhys":
p.setDefPhys(((int)Math.Floor(p.getDEFPhys() * value)));
break;
case "DEFSpe":
p.setDefSpe(((int)Math.Floor(p.getDEFSpe() * value)));
break;
case "Speed":
p.setSpeed(((int)Math.Floor(p.getSpeed() * value)));
break;
default:
break;
}
}
private void ApplyNatureEffect(Enum nature, Pokemon p) {
switch(nature) { //malus
case Nature.BOLD:
case Nature.CALM:
case Nature.MODEST:
case Nature.TIMID:
AlterPokemonStat("ATKPhys", 0.9, p);
break;
case Nature.MILD:
case Nature.GENTLE:
case Nature.HASTY:
case Nature.LONELY:
AlterPokemonStat("DEFPhys", 0.9, p);
break;
case Nature.JOLLY:
case Nature.IMPISH:
case Nature.CAREFUL:
case Nature.ADAMANT:
AlterPokemonStat("ATKSpe", 0.9, p);
break;
case Nature.RASH:
case Nature.LAX:
case Nature.NAUGHTY:
case Nature.NAIVE:
AlterPokemonStat("DEFSpe", 0.9, p);
break;
case Nature.BRAVE:
case Nature.QUIET:
case Nature.SASSY:
case Nature.RELAXED:
AlterPokemonStat("Speed", 0.9, p);
break;
default:
break;
}
switch(nature) { //bonus
case Nature.BRAVE:
case Nature.NAUGHTY:
case Nature.ADAMANT:
case Nature.LONELY:
AlterPokemonStat("ATKPhys", 1.1, p);
break;
case Nature.BOLD:
case Nature.LAX:
case Nature.IMPISH:
case Nature.RELAXED:
AlterPokemonStat("DEFPhys", 1.1, p);
break;
case Nature.QUIET:
case Nature.MILD:
case Nature.RASH:
case Nature.MODEST:
AlterPokemonStat("ATKSpe", 1.1, p);
break;
case Nature.CALM:
case Nature.GENTLE:
case Nature.SASSY:
case Nature.CAREFUL:
AlterPokemonStat("DEFSpe", 1.1, p);
break;
case Nature.JOLLY:
case Nature.NAIVE:
case Nature.HASTY:
case Nature.TIMID:
AlterPokemonStat("Speed", 1.1, p);
break;
default:
break;
}
}
private void ApplyPlayerEffect(String playerName, Pokemon p) {
switch(playerName) {
case "Applejack":
AlterPokemonStat("PV", 1.05, p);
break;
case "Rainbow Dash":
AlterPokemonStat("ATKPhys", 1.03, p);
break;
case "Twilight Sparkle":
AlterPokemonStat("Speed", 1.07, p);
break;
case "Pinkie Pie":
AlterPokemonStat("DEFPhys", 1.04, p);
break;
case "Fluttershy":
AlterPokemonStat("DEFPhys", 0.96, p);
break;
case "Izzy Moonbow":
AlterPokemonStat("PV", 1.05, p);
AlterPokemonStat("Speed", 0.95, p);
break;
case "Zipp Storm":
AlterPokemonStat("PV", 0.95, p);
AlterPokemonStat("Speed", 1.05, p);
break;
case "Sunny Starscout":
AlterPokemonStat("ATKSpe", 1.03, p);
break;
case "Derpy Hooves":
AlterPokemonStat("PV", 0.95, p);
break;
default:
break;
}
}
private List<Pokemon> GetPokemons() {
return this.Pkm;
}
private List<Character> GetCharacters() {
return this.Characters;
}
}
}