Difference between revisions of "Programmering II Januar 2017"

From House of Technology
Jump to: navigation, search
(Middel hold)
Line 116: Line 116:
  
 
https://drive.google.com/file/d/0B5oDizcmFQ1YcER5ZF9HU21oQnM/view?usp=sharing
 
https://drive.google.com/file/d/0B5oDizcmFQ1YcER5ZF9HU21oQnM/view?usp=sharing
 +
 +
=Spiludvikling i WPF=
 +
Snapshot (klokken 13): https://drive.google.com/file/d/0B5oDizcmFQ1YRHhmTFE4WHFMQ1k/view?usp=sharing
  
 
=Middel hold=
 
=Middel hold=

Revision as of 14:05, 12 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/

Link til C# fundamentals (film materiale): https://drive.google.com/file/d/0B5oDizcmFQ1YalVYcGgzQWRrSUU/view?usp=sharing

Link til C# WPF (film materiale): https://drive.google.com/file/d/0B5oDizcmFQ1YNTZwNGZ4XzREWFU/view?usp=sharing

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:

Variabler
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

Spiludvikling i WPF

Snapshot (klokken 13): https://drive.google.com/file/d/0B5oDizcmFQ1YRHhmTFE4WHFMQ1k/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


Serializerings eksempel:

            //Opretter et GameData objekt:
            GameData gd = new GameData();
            gd.playerMoney = 100;

            //Hvad vi skal bruge til at serializere:
            XmlSerializer serializer = new XmlSerializer(typeof(GameData));
            StringWriter sWriter = new StringWriter();

            //Serializering af objekt:
            serializer.Serialize(sWriter, gd);

            //Opretter en string med alt XML:
            string myXml = sWriter.ToString();

            //Udskriver XML fil til skrivebordet. BEMÆRK at du skal ændre filstien for at det virker:
            //File.WriteAllText(@"C:\Users\MaKN\Desktop\save.xml", myXml);

            //Indlæsning af XMLfil fra skrivebordet:
            string readXML = File.ReadAllText(@"C:\Users\MaKN\Desktop\save.xml");
            Console.WriteLine(readXML);

            //Deserializering af xmlfil til objekt:
            StringReader sReader = new StringReader(readXML);
            GameData newGD = (GameData)serializer.Deserialize(sReader);

            //Udskriver den nye værdi fra det nye objekt:
            Console.WriteLine("New GameData money value is: "+ newGD.playerMoney);
            Console.ReadKey();

GameData klasse: (husk at lave den public)

    [Serializable]
    public class GameData
    {
        public int playerMoney = 0;
        public int playerUpgrades = 0;
    }

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.