Skip to content

Commit

Permalink
Adicionar exercícios resolvidos da aula 2
Browse files Browse the repository at this point in the history
  • Loading branch information
nunofachada committed Oct 7, 2023
1 parent af5a726 commit 7ed237d
Show file tree
Hide file tree
Showing 26 changed files with 553 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Aula02/Aula02.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CatFromLastWeek", "CatFromLastWeek\CatFromLastWeek.csproj", "{68290AAD-FBA9-4823-9433-5187EA42F3DB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Polimorfismo", "Polimorfismo\Polimorfismo.csproj", "{EA07B964-981F-4369-AD7C-B31EECA78409}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OverrideVsNew", "OverrideVsNew\OverrideVsNew.csproj", "{651CD352-8B54-42A2-91B8-3E57E60D61D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClassVsStruct", "ClassVsStruct\ClassVsStruct.csproj", "{C1C8C05F-2FB4-45F2-BD1C-B46AA15CC1F0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GameUnits", "GameUnits\GameUnits.csproj", "{11933F5E-0FCC-42BC-8D40-8B45D2C2B73A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{68290AAD-FBA9-4823-9433-5187EA42F3DB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68290AAD-FBA9-4823-9433-5187EA42F3DB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68290AAD-FBA9-4823-9433-5187EA42F3DB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68290AAD-FBA9-4823-9433-5187EA42F3DB}.Release|Any CPU.Build.0 = Release|Any CPU
{EA07B964-981F-4369-AD7C-B31EECA78409}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA07B964-981F-4369-AD7C-B31EECA78409}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA07B964-981F-4369-AD7C-B31EECA78409}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA07B964-981F-4369-AD7C-B31EECA78409}.Release|Any CPU.Build.0 = Release|Any CPU
{651CD352-8B54-42A2-91B8-3E57E60D61D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{651CD352-8B54-42A2-91B8-3E57E60D61D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{651CD352-8B54-42A2-91B8-3E57E60D61D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{651CD352-8B54-42A2-91B8-3E57E60D61D8}.Release|Any CPU.Build.0 = Release|Any CPU
{C1C8C05F-2FB4-45F2-BD1C-B46AA15CC1F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C1C8C05F-2FB4-45F2-BD1C-B46AA15CC1F0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C1C8C05F-2FB4-45F2-BD1C-B46AA15CC1F0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C1C8C05F-2FB4-45F2-BD1C-B46AA15CC1F0}.Release|Any CPU.Build.0 = Release|Any CPU
{11933F5E-0FCC-42BC-8D40-8B45D2C2B73A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11933F5E-0FCC-42BC-8D40-8B45D2C2B73A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11933F5E-0FCC-42BC-8D40-8B45D2C2B73A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11933F5E-0FCC-42BC-8D40-8B45D2C2B73A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
106 changes: 106 additions & 0 deletions Aula02/CatFromLastWeek/Cat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;

namespace CatFromLastWeek
{
public class Cat
{
public static int MaxEnergy { get; set; } = 100;
public static int EnergyGainAfterSleep { get; set; } = 20;
public static int EnergyLossAfterPlay { get; set; } = 15;
public static int EnergyLossAfterMeow { get; set; } = 5;

private int energy;
private Feed feedStatus;
private Random random;
private Feed[] possibleFeedStatus;
private Mood[] possibleMoods;

public string Name { get; }

public int Energy
{
get => energy;
private set
{
energy = value;
if (energy < 0) energy = 0;
else if (energy > MaxEnergy) energy = MaxEnergy;
}
}

public Feed FeedStatus
{
get => feedStatus;
private set
{
feedStatus = value;
if (feedStatus < Feed.Starving)
feedStatus = Feed.Starving;
else if (feedStatus > Feed.AboutToExplode)
feedStatus = Feed.AboutToExplode;
}
}

public Mood MoodStatus { get; private set; }

private Cat()
{
random = new Random();
possibleFeedStatus = (Feed[])Enum.GetValues(typeof(Feed));
possibleMoods = (Mood[])Enum.GetValues(typeof(Mood));
}

public Cat(string name, int energy, Feed feedStatus, Mood moodStatus)
: this()
{
Name = name;
Energy = energy;
FeedStatus = feedStatus;
MoodStatus = moodStatus;
}

public Cat(string name) : this()
{
Name = name;
energy = random.Next(MaxEnergy + 1);
FeedStatus =
possibleFeedStatus[random.Next(possibleFeedStatus.Length)];
MoodStatus = RandomMoods();
}

private Mood RandomMoods()
{
Mood moods = 0;
int numMoods = random.Next(possibleMoods.Length) + 1;
for (int i = 0; i < numMoods; i++)
{
moods |= possibleMoods[random.Next(possibleMoods.Length)];
}
return moods;
}

public void Eat()
{
FeedStatus++;
}

public void Sleep()
{
Energy += EnergyGainAfterSleep;
FeedStatus--;
MoodStatus = RandomMoods();
}

public void Play()
{
Energy -= EnergyLossAfterPlay;
MoodStatus = Mood.Happy;
}

public void Meow()
{
Console.WriteLine($"{Name} says \"Meow\"!");
Energy -= EnergyLossAfterMeow;
}
}
}
9 changes: 9 additions & 0 deletions Aula02/CatFromLastWeek/CatFromLastWeek.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

</Project>
11 changes: 11 additions & 0 deletions Aula02/CatFromLastWeek/Feed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace CatFromLastWeek
{
public enum Feed
{
Starving,
Hungry,
Satisfied,
Full,
AboutToExplode
}
}
13 changes: 13 additions & 0 deletions Aula02/CatFromLastWeek/Mood.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace CatFromLastWeek
{
[Flags]
public enum Mood
{
Happy = 1 << 0, // Equivalente a 1 (0001)
Grumpy = 1 << 1, // Equivalente a 2 (0010)
IgnoringYou = 1 << 2, // Equivalente a 4 (0100)
HyperActive = 1 << 3, // Equivalente a 8 (1000)
}
}
55 changes: 55 additions & 0 deletions Aula02/CatFromLastWeek/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;

namespace CatFromLastWeek
{
public class Program
{
private static void Main(string[] args)
{
// Declarar os gatos
Cat cat1, cat2;

// Alterar os valores comuns a todos os gatos
Cat.MaxEnergy = 300;
Cat.EnergyGainAfterSleep = 25;
Cat.EnergyLossAfterMeow = 2;
Cat.EnergyLossAfterPlay = 14;

// Criar dois gatos usando os diferentes construtores
cat1 = new Cat("Lombriga");
cat2 = new Cat(
"Minhoca", 99, Feed.Full, Mood.HyperActive | Mood.Grumpy);

// Mostrar estado de cada gato antes das ações
Console.WriteLine("Estado inicial dos gatos:");

ShowCat(cat1);
ShowCat(cat2);

Console.WriteLine("Ações dos gatos:");

// Gato 1 brinca e diz miau
Console.WriteLine($"\t{cat1.Name} plays");
cat1.Play();
Console.Write("\t");
cat1.Meow();

// Gato 2 come e dorme
Console.WriteLine($"\t{cat2.Name} eats and sleeps");
cat2.Eat();
cat2.Sleep();

// Estado dos gatos após terem efetuado algumas ações
Console.WriteLine("Estado final dos gatos:");
ShowCat(cat1);
ShowCat(cat2);
}

private static void ShowCat(Cat cat)
{
Console.WriteLine(
"\tNome={0}, Energia={1}, Alimentação={2}, Disposição={3}",
cat.Name, cat.Energy, cat.FeedStatus, cat.MoodStatus);
}
}
}
9 changes: 9 additions & 0 deletions Aula02/ClassVsStruct/ClassVsStruct.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

</Project>
15 changes: 15 additions & 0 deletions Aula02/ClassVsStruct/Player.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace ClassVsStruct
{
public struct Player
{
public float Health { get; set; }
public float Armor { get; set; }

public Player(float health)
{
Health = health;
Armor = 101f;

}
}
}
28 changes: 28 additions & 0 deletions Aula02/ClassVsStruct/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;

namespace ClassVsStruct
{
public class Program
{
private static void Main(string[] args)
{
Player p1;
p1 = new Player() { Health = 21.2f, Armor = 53.9f };

Console.WriteLine("Valores iniciais:");
Console.WriteLine("\tP1: Health={0}, Armor={1}",
p1.Health, p1.Armor);

DupHealth(p1);

Console.WriteLine("Valores finais:");
Console.WriteLine("\tP1: Health={0}, Armor={1}",
p1.Health, p1.Armor);
}

private static void DupHealth(Player p)
{
p.Health *= 2;
}
}
}
9 changes: 9 additions & 0 deletions Aula02/GameUnits/GameUnits.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>8.0</LangVersion>
</PropertyGroup>

</Project>
27 changes: 27 additions & 0 deletions Aula02/GameUnits/MilitaryUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace GameUnits
{
public class MilitaryUnit : Unit
{
public int AttackPower { get; }
public int XP { get; set; }
public override int Health => base.Health + XP;
public override float Value => AttackPower + XP;

public MilitaryUnit(
int movement, int health,
int attackPower)
: base(movement, health)
{
AttackPower = attackPower;
XP = 0;
}

public void Attack(Unit u)
{
// Attack some other unit
}

public override string ToString() =>
base.ToString() + $", AP={AttackPower}, XP={XP}";
}
}
19 changes: 19 additions & 0 deletions Aula02/GameUnits/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace GameUnits
{
public class Program
{
private static void Main(string[] args)
{
Unit mu = new MilitaryUnit(2, 100, 50);
Unit su = new SettlerUnit(5, 25);

mu.Move(new Vector2(2, -7));
su.Move(new Vector2(-1, 4));

Console.WriteLine(mu);
Console.WriteLine(su);
}
}
}
17 changes: 17 additions & 0 deletions Aula02/GameUnits/SettlerUnit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace GameUnits
{
public class SettlerUnit : Unit
{
public override float Value => 5;

public SettlerUnit(
int movement, int health)
: base(movement, health)
{ }

public void Settle()
{
// Criar aldeia/cidade
}
}
}
Loading

0 comments on commit 7ed237d

Please sign in to comment.