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;
- namespace LevelGenerator_02
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[,] levelArray = new int[20, 20]; //Setting size of level
- //Populating initial level array with numbers 0-399
- int index = -1;
- int increacing = 0;
- double counter = 0;
- for (index = 0; increacing < 20 * 20; index++)
- {
- levelArray[((int)counter), index] = increacing; //Loop to fill the array
- //Console.Write(levelArray[((int)counter), index] + "|");
- counter = counter + .05;
- increacing = increacing + 1;
- if (index == 19)
- {
- index = -1;
- }
- }
- //Console.ReadKey(); //Finish populating level array with initial numbers.
- //--------------------------------------------------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------------------------------------------------
- Random randomVariable = new Random(); //Begin the random level generation
- int startingPoint = randomVariable.Next(0, 19);
- int endingPoint = randomVariable.Next(0, 19);
- int blank = 2000;
- int wall = 3000;
- int start = 4000;
- int end = 5000;
- int currentPositionUpDown = 0;
- int currentPositionLeftRight = startingPoint;
- int randomDirection = randomVariable.Next(0,3);
- levelArray[0,startingPoint] = start;
- levelArray[19, endingPoint] = end;
- int errorControl = 0;
- int randomLevelSize = randomVariable.Next(500, 700);
- int currentLevelSize = 0;
- do{
- if (randomDirection == 0 && currentPositionLeftRight-2 <19 && currentPositionLeftRight-2 >0 && levelArray[(currentPositionUpDown), (currentPositionLeftRight - 2)] <= 400) //Move left 2
- {
- currentPositionLeftRight = currentPositionLeftRight - 2;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionLeftRight = currentPositionLeftRight + 1;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionLeftRight = currentPositionLeftRight - 1;
- errorControl = 0;
- }
- else if (randomDirection == 1 && currentPositionLeftRight+2 <19 && currentPositionLeftRight+2 >0 && levelArray[(currentPositionUpDown), (currentPositionLeftRight + 2)] <= 400) //Move right 2
- {
- currentPositionLeftRight = currentPositionLeftRight + 2;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionLeftRight = currentPositionLeftRight - 1;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionLeftRight = currentPositionLeftRight + 1;
- errorControl = 0;
- }
- else if (randomDirection == 2 && currentPositionUpDown+2 <19 && currentPositionUpDown+2 >0 && levelArray[(currentPositionUpDown +2), (currentPositionLeftRight)] <= 400) //Move down 2
- {
- currentPositionUpDown = currentPositionUpDown + 2;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionUpDown = currentPositionUpDown - 1;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionUpDown = currentPositionUpDown + 1;
- errorControl = 0;
- }
- else if (randomDirection == 2 && currentPositionUpDown-2 <19 && currentPositionUpDown-2 >0 && levelArray[(currentPositionUpDown -2), (currentPositionLeftRight)] <= 400) //Move up 2
- {
- currentPositionUpDown = currentPositionUpDown - 2;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionUpDown = currentPositionUpDown + 1;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionUpDown = currentPositionUpDown - 1;
- errorControl = 0;
- }
- errorControl = errorControl + 1;
- if (errorControl >= 5)
- {
- if (randomDirection == 0 && currentPositionLeftRight - 2 <19 && currentPositionLeftRight - 2 >0 && levelArray[(currentPositionUpDown), (currentPositionLeftRight - 2)] == blank) //Move left 2
- {
- currentPositionLeftRight = currentPositionLeftRight - 2;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionLeftRight = currentPositionLeftRight + 1;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionLeftRight = currentPositionLeftRight - 1;
- errorControl = 0;
- }
- else if (randomDirection == 1 && currentPositionLeftRight + 2 <19 && currentPositionLeftRight + 2 >0 && levelArray[(currentPositionUpDown), (currentPositionLeftRight + 2)] <= blank) //Move right 2
- {
- currentPositionLeftRight = currentPositionLeftRight + 2;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionLeftRight = currentPositionLeftRight - 1;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionLeftRight = currentPositionLeftRight + 1;
- errorControl = 0;
- }
- else if (randomDirection == 2 && currentPositionUpDown + 2 <19 && currentPositionUpDown + 2 >0 && levelArray[(currentPositionUpDown+2), (currentPositionLeftRight)] <= blank) //Move down 2
- {
- currentPositionUpDown = currentPositionUpDown + 2;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionUpDown = currentPositionUpDown - 1;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionUpDown = currentPositionUpDown + 1;
- errorControl = 0;
- }
- else if (randomDirection == 2 && currentPositionUpDown - 2 <19 && currentPositionUpDown - 2 >0 && levelArray[(currentPositionUpDown-2), (currentPositionLeftRight)] <= blank) //Move up 2
- {
- currentPositionUpDown = currentPositionUpDown - 2;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionUpDown = currentPositionUpDown + 1;
- levelArray[(currentPositionUpDown), (currentPositionLeftRight)] = blank;
- currentPositionUpDown = currentPositionUpDown - 1;
- errorControl = 0;
- }
- }
- randomDirection = randomVariable.Next(0, 3);
- currentLevelSize = currentLevelSize + 1;
- } while (levelArray[19, endingPoint] == end && currentLevelSize != randomLevelSize);
- //--------------------------------------------------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------------------------------------------------
- //--------------------------------------------------------------------------------------------------------------------------------
- increacing = 0; //Writing out the level to console.
- double printCounter = 0;
- int[,] finalArray = new int[20, 20];
- for (index = 0; increacing < 20 * 20; index++)
- {
- increacing = increacing + 1;
- if (levelArray[(int)printCounter, index] == blank)
- {
- Console.Write(" ");
- }
- else if (levelArray[(int)printCounter, index] == wall)
- {
- Console.Write("|");
- }
- else if (levelArray[(int)printCounter, index] == start)
- {
- Console.Write("S ");
- }
- else if (levelArray[(int)printCounter, index] == end)
- {
- Console.Write("E ");
- }
- else if (levelArray[(int)printCounter, index] <= 400)
- {
- Console.Write("__");
- }
- printCounter = printCounter + .05;
- if (index == 19)
- {
- index = -1;
- Console.WriteLine();
- }
- }
- Console.ReadKey();
- }
- }
- }
Add Comment
Please, Sign In to add comment