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"); 
   }