Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace anna_kuutti_su215_uppgifter_3
- {
- class Box
- {
- public int x;
- public int y;
- public int width;
- public int height;
- int randomColor;
- public ConsoleColor[] colors = { ConsoleColor.Yellow, ConsoleColor.Red, ConsoleColor.Magenta, ConsoleColor.Green, ConsoleColor.Gray, ConsoleColor.DarkYellow, ConsoleColor.DarkRed, ConsoleColor.DarkMagenta, ConsoleColor.DarkGreen, ConsoleColor.DarkGray, ConsoleColor.DarkCyan, ConsoleColor.DarkBlue, ConsoleColor.Blue };
- Random rndColor = new Random();
- public void Draw()
- {
- randomColor = rndColor.Next(0, colors.Length);
- Console.BackgroundColor = colors[randomColor];
- Console.ForegroundColor = colors[randomColor];
- for (int i = 0; i < height; i++)
- {
- System.Console.SetCursorPosition(x, y);
- for (int o = 0; o < width; o++)
- {
- Console.Write("x");
- //System.Threading.Thread.Sleep(1);
- }
- y += 1;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- ChooseWhatToDo();
- Console.ReadLine();
- }
- static void ChooseWhatToDo()
- {
- Console.WriteLine("Please choose witch program you want to play");
- Console.WriteLine("1. rock paper scissor");
- Console.WriteLine("2. stop time at 10");
- Console.WriteLine("3. 1 to 100 000");
- Console.WriteLine("4. Fibonaccis");
- Console.WriteLine("5. Login in with many users");
- Console.WriteLine("6. Create boxes");
- switch (EnterAnumber())
- {
- case 1:
- RockScissorPaper();
- break;
- case 2:
- ComeSoCloseAsYoucanTo30();
- break;
- case 3:
- CountTo100000();
- break;
- case 4:
- Fibonaccis();
- break;
- case 5:
- LogInManyUsers();
- break;
- case 6:
- CreateBoxes();
- break;
- }
- }
- static void LogInManyUsers()
- {
- string[] user = { "egon", "kalle", "per", "nils" }; //array its many strings in this case stored in one place
- string[] password = { "banan", "1234", "password", "nisse" };
- Console.WriteLine("Please write your username first, then press enter and write your password.");//writes out on the console
- string userName = Console.ReadLine(); //reads what the user writes on the console and put it in the string
- string userPassword = Console.ReadLine();
- try
- {
- int pos = Array.IndexOf(user, userName); //array.IndexOf looks at the array you choose and look at the position this string i this case has
- if (userPassword == password[pos])
- Console.WriteLine("Welcome to my program");
- }
- catch
- {
- Console.WriteLine("either your username or password is wrong");
- }
- }
- static void Fibonaccis()
- {
- long number1 = 0; // a bigger int
- long number2 = 1;
- long number3;
- long evenNumber = 0;
- for (int i = 0; i < 51; i++) // as long as i is smaller than 51
- {
- if (number1 == 0) //if 1 is 0 then it enter this if-statement
- Console.WriteLine(number2);
- number3 = number1 + number2;
- number1 = number2;
- number2 = number3;
- if (IsEven(number3) && number3 < 4000001)
- {
- evenNumber = number3 + evenNumber;
- }
- Console.WriteLine(number3);
- }
- Console.WriteLine("All even numbers under 4000 000 is " + evenNumber);
- }
- static bool IsEven(long value) // bool is true or false
- {
- return value % 2 == 0; // % gives backthe things that is left after it has been divided by 2 in this case
- }
- static void CountTo100000()
- {
- DateTime startTime = DateTime.Now; //dateTime is a funktion that creates a variabel that can store time. DateTime.now takes the time that is right now
- for (int i = 0; i < 100001; i++)
- {
- Console.WriteLine(i);
- }
- TimeSpan span = DateTime.Now - startTime;
- DateTime secondStartTime = DateTime.Now;
- for (int i = 0; i < 100001; i++)
- {
- Console.Write(i);
- }
- TimeSpan secondSpan = DateTime.Now - secondStartTime; //timespan creates a variabel that can count the difference between two times
- Console.WriteLine("Det tog writeLine {0} seconds och write {1} seconds ", span.TotalSeconds, secondSpan.TotalSeconds);
- }
- static void ComeSoCloseAsYoucanTo30()
- {
- Console.WriteLine("The goal is to come as close to 30 seconds as you can");
- Console.WriteLine("Press enter to start the timer");
- DateTime startTime = DateTime.Now;
- Console.ReadLine();
- Console.WriteLine("Press enter when you want to stop the timer");
- Console.ReadLine();
- TimeSpan span = DateTime.Now - startTime;
- int yourTime = 10 - (int)span.TotalSeconds;//(int) makes the timespan time to an int
- Console.WriteLine("You were {0} seconds away", yourTime);
- }
- static void RockScissorPaper()
- {
- int soManyWinsNeedsTowin;
- int computerWins = 0;
- int playerWins = 0;
- string[] options = { "spock", "scissor", "paper", "rock", "lizard", "spock", "scissor", "paper", "rock", "lizard" };
- Random rnd = new Random();
- Console.WriteLine("We are going to play rock paper scissor.");
- while (true)
- {
- Console.WriteLine("Please choose how many points you want to play to.");
- Console.WriteLine("Press either 1, 3, 5 or 7 for how many times you want to play");
- soManyWinsNeedsTowin = EnterAnumber();
- if (soManyWinsNeedsTowin != 1 || soManyWinsNeedsTowin != 3 || soManyWinsNeedsTowin != 5 || soManyWinsNeedsTowin != 7)
- Console.WriteLine("Please write a correct number");
- else
- break;
- }
- while (true)//countinues as long as its true
- {
- Console.WriteLine("Please write your choose");
- Console.WriteLine("Press 1 for paper");
- Console.WriteLine("Press 2 for rock");
- Console.WriteLine("Press 3 for lizard");
- Console.WriteLine("Press 4 for spock");
- Console.WriteLine("Press 5 for scissor");
- Console.WriteLine("Press 6 for quit");
- int yourNumber = EnterAnumber();
- string yourChoose = options[yourNumber + 1]; //makes yourchoose to the string which has this place
- int computerNumber = rnd.Next(2, 7);
- string computerChoice = options[computerNumber];
- Console.WriteLine(yourChoose);
- Console.WriteLine(computerChoice);
- if (yourChoose == computerChoice)
- Console.WriteLine("You choose {0} and computer choose {1} so nobody won", yourChoose, computerChoice);
- else if (yourChoose == options[computerNumber + 1] || yourChoose == options[computerNumber - 2])
- {
- Console.WriteLine("You choose {0} and computer choose {1} so computer won", yourChoose, computerChoice);
- computerWins++;//adds 1 to computerWins
- }
- else if (yourChoose == options[computerNumber - 1] || yourChoose == options[computerNumber + 2])
- {
- Console.WriteLine("You choose {0} and computer choose {1} so you won", yourChoose, computerChoice);
- playerWins++;
- }
- else if (yourNumber == 6)
- break;
- if (computerWins == soManyWinsNeedsTowin)
- {
- Console.WriteLine("Computer won with {0} to {1}", computerWins, playerWins);
- break;
- }
- else if (playerWins == soManyWinsNeedsTowin)
- {
- Console.WriteLine("You won over the computer with {0} over {1}", playerWins, computerWins);
- break;
- }
- Console.ReadLine();
- Console.Clear(); //clears the console blank
- }
- }
- static int EnterAnumber()
- {
- while (true)
- {
- try//tries to do this
- {
- return int.Parse(Console.ReadLine());//gives back this value
- }
- catch//if try fail then it does this
- {
- Console.WriteLine("You must only use numnbers");
- }
- }
- }
- static void CreateBoxes()
- {
- Console.Clear();
- Random rnd = new Random();
- while (true)
- {
- Box box1 = new Box(); //creates one box
- box1.x = rnd.Next(0, 107); ; // x value is 2
- box1.y = rnd.Next(0, 26);
- box1.width = rnd.Next(10, 80);
- box1.height = rnd.Next(10, 50);
- if (box1.height + box1.y < 30 && box1.x + box1.width < 120)
- {
- box1.Draw();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement