Sunday, August 25, 2013

30

/*In programming, every character space in a string is a position, but we refer to that position as index beginning at zero*/
static void Main(string[] args)
{
  string foo = "Hello World";
  int index = foo.IndexOf('e');
  Console.WriteLine(index);
}
/*This returns '1' because the index of 'e' is the 2nd position in the string*/
int index = foo.IndexOf('el');
/*would return the same index because C# will look for the beginning of the string 'el'*/
int index = foo.IndexOf('e ');
/*would return -1 because there is no 'e' followed immediately by ' '*/

/*Find the second L
string foo = "Hello World";
int index = foo.IndexOf('L' foo.IndexOf("l")+1);
Console.WriteLine(index);

/*Find the third L
string foo = "Hello World";
int index = foo.IndexOf('L' foo.IndexOf("l")+2);
Console.WriteLine(index);
/*Or
string foo = "Hello World";
int index = foo.LastIndexOf('L');
Console.WriteLine(index);

Substring
string foo = "Hello World";
string bar = foo.Substring(7, 3);

Console.WriteLine(bar);
//Will output "orl"

Substring
string foo = "Hello World";
string bar = foo.Remove(0, 3);
string bar1 = bar.Remove(2, 3);
string bar2 = bar1.Remove(3, 1);

Console.WriteLine(bar2);
//This will output lord


Substring
string foo = "Hello World";
string bar = foo.Replace("Hello ", string.Empty);

Console.WriteLine(bar);
/*Will output Hello

string foo = "Hello World";
string bar = foo.Replace("o", "a");
string bar1 = bar.Replace("arl", "eir");
Console.WriteLine(bar1);
/*Will output Hella Weird












Sunday, August 4, 2013

While/Do Loops

This is very very very important to understand if you want to make games.


int[] intArray = {3, 5, 2, 6};

int count = 0;

while (count < intArray.Length) 
       { 
       Console.WriteLine("index " + count + " = " + intArray[count]);//See how I passed that into the parameter?
       count++;
       }
            Console.WriteLine("End Program");

            Console.Read();

NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
            string action = "Quit";
            
            //Note that these are just options display or instructions and the actions are called in the switch statements
     Console.WriteLine("What would you like to do?");
     Console.WriteLine("1 - Attack?");
     Console.WriteLine("2 - Run?");
     Console.WriteLine("3 - Defend?");
     Console.WriteLine("4 - Quit?");


     do     
     {
         action = (Console.ReadLine());
         switch (action)
            { 
                case "attack":
                    Console.WriteLine("You Attacked!");
                    break;
                case "defend":
                    Console.WriteLine("You Defended!");
                    break;
                case "run":
                    Console.WriteLine("You Ran!");
                    break;
                case "quit":
                    Console.WriteLine("Good bye!");
                    break;
                default:
                    Console.WriteLine("Invalid input");
                    break;
            }
       } while (action != "Quit");
// do will check at this point to see if the program should continue or if it should end.

Saturday, August 3, 2013

Case Swiitch

******************With numbers ******************

int number;//You need this or nothing will work

            Console.WriteLine("What would you like to do?");
            Console.WriteLine("1 - Attack?");
            Console.WriteLine("2 - Run?");
            Console.WriteLine("3 - Defend?");


            number = Convert.ToInt32(Console.ReadLine());

            switch (number)
            { 
                case 1:
                    Console.WriteLine("You Attacked!");
                    break;//kicks you out of this case
                case 2:
                    Console.WriteLine("You Defended!");
                    break;
                case 3:
                    Console.WriteLine("You Ran!");
                    break;
                default:
                    Console.WriteLine("That is not a valid input");
                    break;
            }

*

Switch Statements are better to use if you know that there are going to be a limited number of answers

Nested

bool tralse false;
bool fruetrue;


if (tralse) {
    I would like this statement to run;
    // Too bad: the condition is false and so this statement wiln't run. 
    if (frue) {
        I would like this statement to run as well;
    // Too bad: the initial condition is false and so compiler immediately turns it's snout up at this nested if statement
}
}

When nesting for loops you have to use different names for the incrementer


for(int x = 1; x < 1000: x *= 2)
   {
   for(int y = 1; y < 1000: y *= 2)//Not the best example
      {
         if (y == 8 || y == 32)
         {
            continue;
         }  
       Console.WriteLine("etcétera bla bla bla"); 
      }
    if (y == 8 || y == 32)
    {
       continue;
    }  
    Console.WriteLine("etcétera bla bla bla"); 
   }  

Monday, July 29, 2013

The Convert Class

What's your name?
            string name;
     Console.WriteLine("What is your name");
     name = Console.ReadLine();// This will create the user input at enter
     Console.WriteLine("Hello " + name);
     Console.Read();

What's your number- ..hold up I gotta convert the string
            int celly;
     int homePhone;

     Console.WriteLine("What is your cell number?");
     celly = Convert.ToInt32(Console.ReadLine());// the readline is for text input, so any integers need to be converted.

     Console.WriteLine("What is your home number?");
     homePhone = Convert.ToInt32(Console.ReadLine());

     Console.WriteLine("Your cell number is " + celly + " and your home number is " + homePhone);
     Console.WriteLine("These numbers added together is " + (celly + homePhone));
// note "+ (celly + homePhone)" in parentheses  If this were done without parentheses, this would simply concatenate the variables to the writeline string instead of performing the math operation.
     Console.Read();

Double
     double myDoubleVar;
     myDoubleVar Convert.ToDouble(Console.ReadLine());

Floats
     double myFloatVar;
     myFloatVar (float)Convert.ToDouble(Console.ReadLine());

Math

Modulus is useful in that you can keep things from going over a certain threshold 

            const int maxDegrees = 360;
            int angle = 365; //Doesn't exist on a 2D scale
            int finalOutput;

            finalOutput = angle % maxDegrees;
//Instead of continuing past the set threshold, the value will loop clocklike

            Console.WriteLine("Angle is equal to " + finalOutput);
            Console.Read();

        
This will echo back 5 instead of 365

C# will truncate any decimals values. If you want to round off, use:


int x = 3; Math.Round //Simple Round
int x = 3; Math.Floor //Round down
int x = 3; Math.Ceiling  //Round Up

Typecasting:


int x = 10;
float y = 3;
______________________________________________________________
finalValue = x / y; //This doesn't work the way you'd want it to because C# will upgrade the integer to float.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

finalValue = x / (int)y; //This will temporarily unfloat the Y value

or 

finalValue = (int)(x / y); //This also "works" kind of, but the returned value will be a little off.

Friday, July 19, 2013

Arrays

Declare you datatype with a set of braces affixed  - int[] x;
Then allocate the array  - int[] x = new int[3];
Assign the values  - 
int[] x = new int[3];
x[0] = 1;
x[1] = 2;
x[2] = 3;
x[2] = 3;//Try to add this one & it will crash

or

int[] x = new int[3] {1, 2, 3};

or just

int[] x = {1, 2, 3};

Access the elements

System.Console.WriteLine(x[1] * x[2]); // will output 6
----------------------------------------------------------------------------------------------------

There are two kinds of multidimensional arrays:

Rektangular                                                                                                                        string[,] x = new string[2, 2];
x[0, 0] = "00"; x[0, 1] = "01"; 
x[1, 0] = "10"; x[1, 1] = "11"; 

or

string[,] x = { { "00", "01" },
                      { "10", "11" }  };


Jagged

string[][] a = new array[2][];
a[0] = new array[1]; a[0][0] = "00";
a[1] = new array[2]; a[1][0] = "10"; a[1][1] = "11";

or


string[][] a = { new string[]  { "00", };

                 new string[]  { "00" , "00", }  };

#################################################################