[Guide] Assignments to help learn C#

06/22/2010 21:22 pro4never#1
Ok so due to a request in the Co2 programing section, I decided to post some really simple programs people can try to code as ways to teach themselves C# and test out new concepts. Note: I'll try to rank them by difficulty but A: it's been forever since I saw the writeup for these programs and B: I everyone finds different things more/less complicated. I'll also link off to some good resources for people to look at.

C# Resources!

GREAT video tutorials in C# that hybrid made a while back
[Only registered and activated users can see links. Click Here To Register...]

Some ones I wrote up explaining how to relate basic programing principles to Conquer pserver development. Not as useful to most people as it's not as generic.
[Only registered and activated users can see links. Click Here To Register...]

Some really nice C# tuts (Note: I haven't read most of them but I saw a few and they looked very useful)
[Only registered and activated users can see links. Click Here To Register...]

C# For dummies PDF file.
[Only registered and activated users can see links. Click Here To Register...]


Ok so now you know some basics of C# and are thinking "what now!". Chances are you aren't at a level where you can code something super difficult but it's always good to have some goals set for yourself. Here's some simple ideas for programs you can make to test out your new skills.

Sample Program Suggestions:

Make a program to ask the user some questions. Store these variables and return them later

EG: Ask the user for their first name, last name, then return a string along the lines of "Hi: First Name, Last Name. How are you today?"


Make a program that asks the users to enter one number at a time. Take these numbers and store them and when the user is finished entering numbers, take the average of them.

Hint: I'd suggest an array to store numbers. For added complexity, check user input for validity, handle any exceptions with nice user friendly comments and such.


Make a program that accepts user input and then saves it to a file (name of their choosing)

For added challenge, check if the file name is already existing, then ask if the user wants to overwrite it, add to it or use a different file name.


Make a program that asks the user to enter a username and password. Check to see if it already exists in a text file. If so check if the user/pass match and print out a msg. If not ask if they wish to create the new account (if so, add to the accounts text file)


Make a program that does a similar thing using sql databases!


Mini Projects:

Game of Nim:
Simple marble game where you can take up to half the current stack of marbles and no more. You play vs a computer and the person who leaves 1 marble left loses.

Rules for program:


User enters number of marbles for round (between 10-100) (check for validity)
50/50 chance that computer will play "smart" or dumb.
50/50 chance that player goes first
Player and computer will alternate between turns (player entering a number of marbles to take. Check for validity and that it is < 1/2 current marbles). Check win conditions.

If computer is playing smart then you will want to follow a few rules (I think it was something like square root of current marbles -1 or something like that... look it up on google). If computer is not playing smart then have it chose a random valid number of marbles to take. Printing out the choice it made and the remaining marbles obviously.

On round end, print out who won and ask if the user wishes to play another round. If not, close the program.


Population simulator

Ok... I'm very forgetful on the details for this program... it was the first thing I ever had to code for some damn class (which I dropped after seeing the second assignment lol!) so bear with me..


On program start, the user will be prompted for a number of things.

Initial population:
Maximum population:
Years to simulate:
Death rate (0-100):
Birth Rate (0-100):

You will now run a simulation through the number of years entered printing out the result for every year.

Note: All initial population is considered to be Adults.

Each year the following things should happen in your program.

Adults give birth (to babies) X the birth rate
Adults die X the death rate

Babies grow into youth
Youth grow into adults

Population is checked VS max population. If this happens, the population is cut in half (no decimals) and a msg is printed out during the yearly report.

Population is checked to ensure that it is > 0.

Deaths are rounded UP (you can't have a partial death)
Births are truncated DOWN (you can't have a partial birth)


When the years are up you should print out the final results and ask the user if they wish to start a new simulation at which point the program starts from the beginning.

(yes... this was the first program they ever tried to get me to write when I'd never even used the language before... I was less than impressed)


Image Manipulator

Ok so this is the point where I switched classes so I'm again, rough on the details...

Basically you want to write a program that will manipulate a png photograph to find outlines of objects.

To do this you will want to load in the data from the photograph using a binary reader and examine each pixel (they are stored as values in the photo, try opening a png with notepad and you will see what I mean).

You want to use the header information to grab the width/height of the photograph and then the remaining information to create a proper database storing information in your program. You then want to compare each pixel in the photograph to each one around it (checking for edges of course). You will then round each pixel either to 0 or 255 (full black or full white) [note... this was intended to be run on black and white photographs...]. Once finished, save the new file to a new .png and examine results.

This is a basic version of the program, there was alot more needing to be done in the actual version but you can use this type of thing for all sorts of image manipulation/checking *cough* Dmaps in conquer converted to png are a great way to run checks to see if the target coords are valid.



So yah... I'll try to think of some more sample projects people may want to try to teach themselves coding and I'll also try to add in a few more helpful links to fill this out some. If anyone can think of any, just lemme know and I'll add them to the list (credit given of course). Hopefully this will help a few people who are actually trying to learn some coding but if not, try to enjoy ^^

Pro4Never
06/23/2010 06:00 Arcо#2
Very nice little thing you got going here chris.
Blunt and right to the point.
06/24/2010 01:38 hunterman01#3
Thanks buddy this is really nice. Gives me something to do.
06/26/2010 20:53 _DreadNought_#4
The first ex I made in 10 minutes...

Make 2 labels
- First name
- Last name
2 buttons
- Submit
- Exit
2 textboxes

add this code
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // vars
        public string FirstName = "";
        public string LastName = "";


        private void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Please fill out the form", "TestProg");
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != (""))
            {
                if (textBox2.Text != (""))
                {
                    FirstName = (textBox1.Text);
                    LastName = (textBox2.Text);
                    MessageBox.Show("Hello " + FirstName + ". Your last name is " + LastName + ". What a lovley day it is!", "Hello " + FirstName);
                    Reset();
                }
                else
                {
                    MessageBox.Show("Error: You cannot leave your last name empty", "Error!");
                }
            }
            else
            {
                MessageBox.Show("Error: You cannot leave your first name empty", "Error!");
            }
        }
        public void Reset()
        {
            FirstName = "";
            LastName = "";
            textBox1.Text = ("");
            textBox2.Text = ("");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
so damn simple you could also do
MessageBox.Show("Hello {0} Your last name is {1}",FirstName,LastName); <-- along those lines
06/27/2010 04:43 pro4never#5
Oohhh windows forms.


I'm a huge fan of consoles personally but nice example for people.
06/27/2010 18:27 _DreadNought_#6
Not to keen on console's but I made this is under 5 minutes without reciving an error once ( type or mistake ) so I think I did pretty good
Code:
//vars
            string Firstname = "";
            string Lastname = "";

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Hello sir, what is your first name ?");
            Console.ForegroundColor = ConsoleColor.Blue;
            Firstname = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("What is your last name then {0}?", Firstname);
            Console.ForegroundColor = ConsoleColor.Blue;
            Lastname = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Nice to meet you {0} {1} what a nice day it is", Firstname, Lastname);
            Console.ReadLine();
edit: spend another 10 minutes made it more better / advanced here the script
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        //vars
        public static string Firstname = "";
        public static string Lastname = "";
        public static string ConsoleFName = "";
        public static string ConsoleLName = "";
        public static string NwhyR = "";

        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Hello sir, what is your first name ?");
            Console.ForegroundColor = ConsoleColor.Blue;
            Firstname = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("What is your last name then {0}?", Firstname);
            Console.ForegroundColor = ConsoleColor.Blue;
            Lastname = Console.ReadLine();
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Nice to meet you {0} {1} would you like to know my name?", Firstname, Lastname);
            Console.WriteLine("************ Y = Yes, N = No ************");
            Commands(Console.ReadLine());
        }
        public static void Commands(string command)
        {
            try
            {
                string[] data = command.Split(' ');
                switch (data[0])
                {
                    case "Y":
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("Well, Guess what!, your going to name");
                            Console.WriteLine("Please type in my first name {0}", Firstname);
                            Console.ForegroundColor = ConsoleColor.Blue;
                            ConsoleFName = Console.ReadLine();
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine("Please type in my last name {0}", Firstname);
                            Console.ForegroundColor = ConsoleColor.Blue;
                            ConsoleLName = Console.ReadLine();
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.WriteLine("My name is now {0} {1} thanks for the awesome name {2}", ConsoleFName, ConsoleLName, Firstname);
                            Console.WriteLine("Press enter to exit.");
                            Console.ReadLine();
                        }
                        break;

                    case "N":
                        {
                            Console.WriteLine("Please tell my why ?");
                            NwhyR = Console.ReadLine();
                            Console.ForegroundColor = ConsoleColor.Magenta;
                            Console.WriteLine("What! and you thing *{0}* is a good enough reason??, Screw you {1} {2}", NwhyR, Firstname, Lastname);
                            Console.WriteLine("Press enter to exit.");
                            Console.ReadLine();
                        }
                        break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
Thanks would be nice -_-
07/04/2010 23:12 ~Yuki~#7
why ?
07/10/2010 08:01 Jay Niize#8
#Added to List

Very nice tut!!!
08/03/2010 16:39 _Emme_#9
@Elim


Quote:
class Program
{
static void Main(string[] args)
{
Console.Clear();
SetColor(ConsoleColor.White);
Console.WriteLine("Hello, can I please have your full name?");
Console.Write("Name: ");
string[] fullname = Console.ReadLine().Split(' ');
if (fullname.Length < 2)
Main(new string[2]);
goto AskForName;
AskForName:
Console.WriteLine("Mr/Mrs. {0}, would you like to know my name? Y/N", fullname[fullname.Length - 1]);

ConsoleKeyInfo keyInfo = Console.ReadKey();
Console.WriteLine();
if (keyInfo.Key == ConsoleKey.Y)
Console.WriteLine("My name is 'test'. Goodbye {0}", fullname[0]);
else if (keyInfo.Key == ConsoleKey.N)
Console.WriteLine("Alright {0}, see ya later!", fullname[0]);
else
goto AskForName;

Console.ReadLine();


}
static void SetColor(ConsoleColor color)
{ Console.ForegroundColor = color; }
}
09/01/2010 16:48 _DreadNought_#10
Didn't think of that ^ +k
09/04/2010 01:26 ShapyShape#11
Fine.. ;) - very nice tutorial..
09/21/2010 06:36 rhinostopper#12
thanks, helped alot and will be my main guide to this for now. =)