Difference between revisions of "Programmering II Januar 2017"
(→Generalt) |
|||
Line 40: | Line 40: | ||
{ | { | ||
Console.WriteLine("Shooting pistol. 10 damage."); | Console.WriteLine("Shooting pistol. 10 damage."); | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | '''enumerations:''' | ||
+ | |||
+ | <source lang="Csharp"> | ||
+ | enum weapons | ||
+ | { | ||
+ | rifle, | ||
+ | pistol, | ||
+ | knife | ||
+ | } | ||
+ | |||
+ | static weapons currentWeapon; | ||
+ | |||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | currentWeapon = weapons.rifle; | ||
+ | Shoot(); | ||
+ | Console.WriteLine("Switching weapon"); | ||
+ | currentWeapon = weapons.pistol; | ||
+ | Shoot(); | ||
+ | |||
+ | |||
+ | Console.ReadKey(); | ||
+ | } | ||
+ | |||
+ | static void Shoot() | ||
+ | { | ||
+ | if(currentWeapon == weapons.rifle) | ||
+ | { | ||
+ | Console.WriteLine("Shooting with rifle. 50 damage"); | ||
+ | } | ||
+ | else if (currentWeapon == weapons.pistol) | ||
+ | { | ||
+ | Console.WriteLine("Shooting with pistol. 10 damage"); | ||
+ | } | ||
+ | |||
} | } | ||
</source> | </source> |
Revision as of 12:41, 11 January 2017
Generalt
Link til C# bog: https://drive.google.com/file/d/0B5oDizcmFQ1YSHUyQ0UyalNoMEk/view?usp=sharing
Link til Bob Tabor: https://mva.microsoft.com/en-us/training-courses/c-fundamentals-for-absolute-beginners-16169
Link til danske videoer: https://www.nemprogrammering.dk/Tutorials/c-sharp/1-introduktion.php
God WPF-Tutorial: http://www.wpf-tutorial.com/
Bold spil: https://drive.google.com/file/d/0B5oDizcmFQ1YRUdIdG1lMEtWbTA/view?usp=sharing
Nedarvning: https://drive.google.com/file/d/0B5oDizcmFQ1YRGVzQ1NQdVhpdVk/view?usp=sharing
delegates:
delegate void ShootWeapon();
static void Main(string[] args)
{
ShootWeapon shoot = ShootRifle;
shoot();
Console.WriteLine("Changing weapon");
shoot = ShootPistol;
shoot();
Console.ReadKey();
}
static void ShootRifle()
{
Console.WriteLine("Shooting rifle. 50 damage.");
}
static void ShootPistol()
{
Console.WriteLine("Shooting pistol. 10 damage.");
}
enumerations:
enum weapons
{
rifle,
pistol,
knife
}
static weapons currentWeapon;
static void Main(string[] args)
{
currentWeapon = weapons.rifle;
Shoot();
Console.WriteLine("Switching weapon");
currentWeapon = weapons.pistol;
Shoot();
Console.ReadKey();
}
static void Shoot()
{
if(currentWeapon == weapons.rifle)
{
Console.WriteLine("Shooting with rifle. 50 damage");
}
else if (currentWeapon == weapons.pistol)
{
Console.WriteLine("Shooting with pistol. 10 damage");
}
}
Begynder hold
Variabler
En variabel er noget som indeholder en værdi. Det kunne eksempelvis være brugerens navn der bliver indtastet i programmet, og gemt i en værdi.
Værdityper er som følgende:
Type | værdi |
---|---|
string | "Dette er tekst" |
int | 9001 |
float | 123.45 |
bool | true/false |
Færdig gør denne lommeregner:
https://drive.google.com/file/d/0B5oDizcmFQ1YZTV4Vm96Z282cTQ/view?usp=sharing
Færdiggør dette spil (Tic Tac Toe):
https://drive.google.com/file/d/0B5oDizcmFQ1YcER5ZF9HU21oQnM/view?usp=sharing
Middel hold
Opgave 1:
Lav en BMI beregner som denne:
http://www.beregnbmi.dk/Default.aspx
Opgave 2:
Lav en lommeregner lignende den som findes i Windows.
For at konvertere en string til en decimal bruges følgende kode:
string input = textBox.Text;
decimal convertedInput = decimal.Parse(input);
Lommeregner eksempel:
https://drive.google.com/file/d/0B5oDizcmFQ1YMzBVMUk3ZUZxbzg/view?usp=sharing
Expert hold
Opgave 1:
Lav et program der kan sende data til en applikation som kan lytte. Benyt gerne UDP.
Opgave 2:
Udvid programmet så programmerne kan både sende og modtage data til hinanden. (et simpelt chat program.)
Listener
UdpClient client = new UdpClient(11000);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 11000);
byte[] recivedArray = client.Receive(ref endPoint);
string message = Encoding.ASCII.GetString(recivedArray);
Console.WriteLine("The message was:");
Console.WriteLine(message);
Console.ReadKey();
Sender
string message = "Hello!";
UdpClient client = new UdpClient();
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
byte[] bArray = Encoding.ASCII.GetBytes(message);
client.Send(bArray, bArray.Length, endpoint);
Console.WriteLine("Message has been sent!");
Console.ReadKey();
Serializering:
Human joshua = new Human();
joshua.name = "Joshua";
joshua.money = 20000;
XmlSerializer serializer = new XmlSerializer(typeof(Human));
StringWriter sWriter = new StringWriter();
serializer.Serialize(sWriter, joshua);
string myXml = sWriter.ToString();
Console.WriteLine(myXml);
Console.ReadKey();
StringReader sReader = new StringReader(myXml);
Human newJoshua = (Human)serializer.Deserialize(sReader);
Console.WriteLine("New Human was created!");
Console.WriteLine(newJoshua.name);
Console.WriteLine(newJoshua.money);
Console.ReadKey();
Opgave:
Send et objekt fra et program til et andet program igennem UDP eller TCP.