Advertisement
ekostadinov

FallingRocks

Nov 30th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.86 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace FallingRocksGame
  9. {
  10.     struct Position
  11.     {
  12.         public int row;
  13.         public int col;
  14.  
  15.         public Position(int row, int col)
  16.         {
  17.             this.row = row;
  18.             this.col = col;
  19.         }        
  20.     }
  21.  
  22.     class FallingRocks
  23.     {
  24.         static void Main()
  25.         {
  26.             byte right = 0; // the numbers are == index of the array directions
  27.             byte left = 1;
  28.             byte down = 0;
  29.             int userPoints = 0;
  30.  
  31.      
  32.             Position[] directions = new Position[]
  33.             {
  34.                 new Position(0, 1),  // right == [1] array element
  35.                 new Position(0, -1), // left  == [2] array element
  36.             };
  37.  
  38.             Position[] goldDirections = new Position[]
  39.             {
  40.                 new Position(1, 0),  // down == [1] array element
  41.                
  42.             };
  43.  
  44.             Position[] rocksDirections = new Position[]
  45.             {
  46.                 new Position(1, 0),  // down == [1] array element                
  47.             };
  48.  
  49.             int direction = right; // current dwarf direction
  50.             int goldDirection = down;
  51.             int rocksDirection = down;
  52.  
  53.             Random randomNumbersGenerator = new Random();
  54.             Console.BufferHeight = Console.WindowHeight;
  55.             Console.BufferWidth = Console.WindowWidth;
  56.  
  57.             //1.
  58.             Queue<Position> dwarfBody = new Queue<Position>();
  59.             for (int i = 0; i <= 1; i++)
  60.             {
  61.                 dwarfBody.Enqueue(new Position(Console.WindowHeight - 2, i));
  62.             }
  63.  
  64.             foreach (Position position in dwarfBody)
  65.             {
  66.                 Console.SetCursorPosition(position.col, position.row);
  67.                 Console.ForegroundColor = ConsoleColor.Green;
  68.                 Console.Write("0");
  69.             }
  70.             Queue<Position> goldBrick = new Queue<Position>();
  71.             for (int i = 0; i < 1; i++)
  72.             {
  73.                 goldBrick.Enqueue(new Position(0, randomNumbersGenerator.Next(0, Console.WindowWidth)));
  74.             }
  75.  
  76.             foreach (Position position in goldBrick)
  77.             {
  78.                 Console.SetCursorPosition(position.col, position.row);
  79.                 Console.ForegroundColor = ConsoleColor.Yellow;
  80.                 Console.Write("$");
  81.             }
  82.  
  83.             Queue<Position> goldBrick1 = new Queue<Position>();
  84.             for (int i = 0; i < 1; i++)
  85.             {
  86.                 goldBrick1.Enqueue(new Position(0, randomNumbersGenerator.Next(0, Console.WindowWidth)));
  87.             }
  88.  
  89.             foreach (Position position1 in goldBrick1)
  90.             {
  91.                 Console.SetCursorPosition(position1.col, position1.row);
  92.                 Console.ForegroundColor = ConsoleColor.Yellow;
  93.                 Console.Write("$");
  94.             }
  95.  
  96.             Queue<Position> goldBrick2 = new Queue<Position>();
  97.             for (int i = 0; i < 1; i++)
  98.             {
  99.                 goldBrick2.Enqueue(new Position(0, randomNumbersGenerator.Next(0, Console.WindowWidth)));
  100.             }
  101.  
  102.             foreach (Position position2 in goldBrick2)
  103.             {
  104.                 Console.SetCursorPosition(position2.col, position2.row);
  105.                 Console.ForegroundColor = ConsoleColor.Yellow;
  106.                 Console.Write("$");
  107.             }
  108.  
  109.             Queue<Position> rockBody = new Queue<Position>();
  110.             for (int i = 0; i < 2; i++)
  111.             {
  112.                 rockBody.Enqueue(new Position(0, randomNumbersGenerator.Next(0, Console.WindowWidth)));
  113.             }
  114.  
  115.             foreach (Position position in rockBody)
  116.             {
  117.                 Console.SetCursorPosition(position.col, position.row);
  118.                 Console.ForegroundColor = ConsoleColor.Cyan;
  119.                 Console.Write("+");
  120.             }
  121.  
  122.             Queue<Position> rockBody1 = new Queue<Position>();
  123.             for (int i = 0; i < 3; i++)
  124.             {
  125.                 rockBody1.Enqueue(new Position(0, randomNumbersGenerator.Next(0, Console.WindowWidth)));
  126.             }
  127.  
  128.             foreach (Position position1 in rockBody1)
  129.             {
  130.                 Console.SetCursorPosition(position1.col, position1.row);
  131.                 Console.ForegroundColor = ConsoleColor.Magenta;
  132.                 Console.Write("#");
  133.             }
  134.  
  135.             Queue<Position> rockBody2 = new Queue<Position>();
  136.             for (int i = 0; i <= 2; i++)
  137.             {
  138.                 rockBody2.Enqueue(new Position(0, randomNumbersGenerator.Next(0, Console.WindowWidth)));
  139.             }
  140.  
  141.             foreach (Position position2 in rockBody2)
  142.             {
  143.                 Console.SetCursorPosition(position2.col, position2.row);
  144.                 Console.ForegroundColor = ConsoleColor.DarkGreen;
  145.                 Console.Write("^");
  146.             }
  147.  
  148.             Queue<Position> rockBody3 = new Queue<Position>();
  149.             for (int i = 0; i <= 1; i++)
  150.             {
  151.                 rockBody3.Enqueue(new Position(0, randomNumbersGenerator.Next(0, Console.WindowWidth)));
  152.             }
  153.  
  154.             foreach (Position position3 in rockBody3)
  155.             {
  156.                 Console.SetCursorPosition(position3.col, position3.row);
  157.                 Console.ForegroundColor = ConsoleColor.Blue;
  158.                 Console.Write("%");
  159.             }
  160.  
  161.             Queue<Position> rockBody4 = new Queue<Position>();
  162.             for (int i = 0; i < 1; i++)
  163.             {
  164.                 rockBody4.Enqueue(new Position(0, randomNumbersGenerator.Next(0, Console.WindowWidth)));
  165.             }
  166.  
  167.             foreach (Position position4 in rockBody4)
  168.             {
  169.                 Console.SetCursorPosition(position4.col, position4.row);
  170.                 Console.ForegroundColor = ConsoleColor.Red;
  171.                 Console.Write("&");
  172.             }
  173.  
  174.             // loop
  175.             while (true)
  176.             {
  177.                 if (Console.KeyAvailable)
  178.                 {
  179.                     ConsoleKeyInfo userInput = Console.ReadKey();
  180.  
  181.                     if (userInput.Key == ConsoleKey.LeftArrow)
  182.                     {
  183.                         direction = left; // first element of our array - directions
  184.                     }
  185.                     if (userInput.Key == ConsoleKey.RightArrow)
  186.                     {
  187.                         direction = right;
  188.                     }
  189.                 }
  190.  
  191.                 goldDirection = down;
  192.                 rocksDirection = down;
  193.  
  194.                 //2.
  195.                 Position dwarfHead = dwarfBody.Last(); // old dwarf head
  196.                 Position nextDirection = directions[direction];
  197.                 Position dwarfNewHead = new Position(dwarfHead.row + nextDirection.row,
  198.                     dwarfHead.col + nextDirection.col);
  199.  
  200.                 Position goldEnd = goldBrick.Last(); // old glod end
  201.                 Position goldNextDirection = goldDirections[goldDirection];
  202.                 Position goldNewEnd = new Position(goldEnd.row + goldNextDirection.row,
  203.                     goldEnd.col + goldNextDirection.col);
  204.  
  205.                 Position goldEnd2 = goldBrick2.Last(); // old glod end
  206.                 Position goldNextDirection2 = goldDirections[goldDirection];
  207.                 Position goldNewEnd2 = new Position(goldEnd2.row + goldNextDirection2.row,
  208.                     goldEnd2.col + goldNextDirection2.col);
  209.  
  210.                 Position goldEnd1 = goldBrick1.Last(); // old gold end
  211.                 Position goldNextDirection1 = goldDirections[goldDirection];
  212.                 Position goldNewEnd1 = new Position(goldEnd1.row + goldNextDirection1.row,
  213.                     goldEnd1.col + goldNextDirection1.col);
  214.  
  215.                 Position rocksEnd = rockBody.Last(); // old rock end
  216.                 Position rocksNextDirection = rocksDirections[rocksDirection];
  217.                 Position rocksNewEnd = new Position(rocksEnd.row + rocksNextDirection.row,
  218.                     rocksEnd.col + rocksNextDirection.col);
  219.  
  220.                 Position rocksEnd1 = rockBody1.Last();
  221.                 Position rocksNextDirection1 = rocksDirections[rocksDirection];
  222.                 Position rocksNewEnd1 = new Position(rocksEnd1.row + rocksNextDirection1.row,
  223.                     rocksEnd1.col + rocksNextDirection1.col);
  224.  
  225.                 Position rocksEnd2 = rockBody2.Last();
  226.                 Position rocksNextDirection2 = rocksDirections[rocksDirection];
  227.                 Position rocksNewEnd2 = new Position(rocksEnd2.row + rocksNextDirection2.row,
  228.                     rocksEnd2.col + rocksNextDirection2.col);
  229.  
  230.                 Position rocksEnd3 = rockBody3.Last();
  231.                 Position rocksNextDirection3 = rocksDirections[rocksDirection];
  232.                 Position rocksNewEnd3 = new Position(rocksEnd3.row + rocksNextDirection3.row,
  233.                     rocksEnd3.col + rocksNextDirection3.col);
  234.  
  235.                 Position rocksEnd4 = rockBody4.Last();
  236.                 Position rocksNextDirection4 = rocksDirections[rocksDirection];
  237.                 Position rocksNewEnd4 = new Position(rocksEnd4.row + rocksNextDirection4.row,
  238.                     rocksEnd4.col + rocksNextDirection4.col);
  239.  
  240.  
  241.                 //3. lets the dwarf pass the window's borders
  242.                 if (dwarfNewHead.col >= Console.WindowWidth)
  243.                 {
  244.                     dwarfNewHead.row = Console.WindowHeight - 2; // bounce the dwarf backwards
  245.                     dwarfNewHead.col = Console.WindowWidth - 1;
  246.                     direction = left;
  247.                 }
  248.                 else if (dwarfNewHead.col <= Console.WindowWidth - Console.WindowWidth)
  249.                 {
  250.                     dwarfNewHead.row = Console.WindowHeight - 2;
  251.                     dwarfNewHead.col = 0;
  252.                     direction = right;
  253.                 }
  254.  
  255.                 if (goldNewEnd.row >= Console.WindowHeight)
  256.                 {
  257.                     goldNewEnd.row = randomNumbersGenerator.Next(0, Console.WindowHeight / 10);  // lets the gold to pass
  258.                     goldNewEnd.col = randomNumbersGenerator.Next(0, Console.WindowWidth);  // the window borders
  259.                        
  260.                 }
  261.                 if (goldNewEnd1.row >= Console.WindowHeight)
  262.                 {
  263.                     goldNewEnd1.row = randomNumbersGenerator.Next(0, Console.WindowHeight / 10);
  264.                     goldNewEnd1.col = randomNumbersGenerator.Next(0, Console.WindowWidth);
  265.                 }
  266.  
  267.                 if (goldNewEnd2.row >= Console.WindowHeight)
  268.                 {
  269.                     goldNewEnd2.row = randomNumbersGenerator.Next(0, Console.WindowHeight / 10);
  270.                     goldNewEnd2.col = randomNumbersGenerator.Next(0, Console.WindowWidth);
  271.                 }
  272.  
  273.                 if (rocksNewEnd.row >= Console.WindowHeight)
  274.                 {
  275.                     rocksNewEnd.row = randomNumbersGenerator.Next(0, Console.WindowHeight / 10);  // borders error top and left
  276.                     rocksNewEnd.col = randomNumbersGenerator.Next(0, Console.WindowWidth);    // if removed it bounce*    
  277.                 }
  278.                 if (rocksNewEnd1.row >= Console.WindowHeight)
  279.                 {
  280.                     rocksNewEnd1.row = randomNumbersGenerator.Next(0, Console.WindowHeight / 10);                                                    // use random num to change position!
  281.                     rocksNewEnd1.col = randomNumbersGenerator.Next(0, Console.WindowWidth);   //if used buffer width, should generate different row
  282.                 }
  283.                 if (rocksNewEnd2.row >= Console.WindowHeight)
  284.                 {
  285.                     rocksNewEnd2.row = randomNumbersGenerator.Next(0, Console.WindowHeight / 10);
  286.                     rocksNewEnd2.col = randomNumbersGenerator.Next(0, Console.WindowWidth);
  287.                 }
  288.                 if (rocksNewEnd3.row >= Console.WindowHeight)
  289.                 {
  290.                     rocksNewEnd3.row = randomNumbersGenerator.Next(0, Console.WindowHeight / 10);
  291.                     rocksNewEnd3.col = randomNumbersGenerator.Next(0, Console.WindowWidth);
  292.                 }
  293.                 if (rocksNewEnd4.row >= Console.WindowHeight)
  294.                 {
  295.                     rocksNewEnd4.row = randomNumbersGenerator.Next(0, Console.WindowHeight / 10);
  296.                     rocksNewEnd4.col = randomNumbersGenerator.Next(0, Console.WindowWidth);
  297.                 }
  298.  
  299.                
  300.                 //4.
  301.                 Console.SetCursorPosition(dwarfHead.col, dwarfHead.row);
  302.                 Console.ForegroundColor = ConsoleColor.Green;
  303.                 Console.Write("0");
  304.  
  305.                 Console.SetCursorPosition(goldEnd.col, goldEnd.row);
  306.                 Console.ForegroundColor = ConsoleColor.Yellow;
  307.                 Console.Write("$");
  308.  
  309.                 Console.SetCursorPosition(goldEnd1.col, goldEnd1.row);
  310.                 Console.ForegroundColor = ConsoleColor.Yellow;
  311.                 Console.Write("$");
  312.  
  313.                 Console.SetCursorPosition(goldEnd2.col, goldEnd2.row);
  314.                 Console.ForegroundColor = ConsoleColor.Yellow;
  315.                 Console.Write("$");
  316.  
  317.                 Console.SetCursorPosition(rocksEnd.col, rocksEnd.row);
  318.                 Console.ForegroundColor = ConsoleColor.Cyan;
  319.                 Console.Write("+");
  320.  
  321.                 Console.SetCursorPosition(rocksEnd1.col, rocksEnd1.row);
  322.                 Console.ForegroundColor = ConsoleColor.Magenta;
  323.                 Console.Write("#");
  324.  
  325.                 Console.SetCursorPosition(rocksEnd2.col, rocksEnd2.row);
  326.                 Console.ForegroundColor = ConsoleColor.DarkGreen;
  327.                 Console.Write("^");
  328.  
  329.                 Console.SetCursorPosition(rocksEnd3.col, rocksEnd3.row);
  330.                 Console.ForegroundColor = ConsoleColor.Blue;
  331.                 Console.Write("%");
  332.  
  333.                 Console.SetCursorPosition(rocksEnd4.col, rocksEnd4.row);
  334.                 Console.ForegroundColor = ConsoleColor.Red;
  335.                 Console.Write("&");
  336.  
  337.                 //5.
  338.                 dwarfBody.Enqueue(dwarfNewHead);
  339.                 Console.SetCursorPosition(dwarfNewHead.col, dwarfNewHead.row);
  340.                 Console.ForegroundColor = ConsoleColor.Red;
  341.                 if (direction == right) Console.Write(">"); //the head of the dwarf  
  342.                 if (direction == left) Console.Write("<");
  343.  
  344.                 goldBrick.Enqueue(goldNewEnd);
  345.                 Console.SetCursorPosition(goldNewEnd.col, goldNewEnd.row);
  346.                 Console.ForegroundColor = ConsoleColor.Yellow;
  347.                 if (goldDirection == down) Console.Write("$"); //the head of the snake
  348.  
  349.                 goldBrick1.Enqueue(goldNewEnd1);
  350.                 Console.SetCursorPosition(goldNewEnd1.col, goldNewEnd1.row);
  351.                 Console.ForegroundColor = ConsoleColor.Yellow;
  352.                 if (goldDirection == down) Console.Write("$"); //the head of the snake
  353.  
  354.                 goldBrick2.Enqueue(goldNewEnd2);
  355.                 Console.SetCursorPosition(goldNewEnd2.col, goldNewEnd2.row);
  356.                 Console.ForegroundColor = ConsoleColor.Yellow;
  357.                 if (goldDirection == down) Console.Write("$"); //the head of the snake
  358.  
  359.                 rockBody.Enqueue(rocksNewEnd);
  360.                 Console.SetCursorPosition(rocksNewEnd.col, rocksNewEnd.row);
  361.                 Console.ForegroundColor = ConsoleColor.Cyan;
  362.                 if (rocksDirection == down) Console.Write("+"); //the head of the rock
  363.  
  364.                 rockBody1.Enqueue(rocksNewEnd1);
  365.                 Console.SetCursorPosition(rocksNewEnd1.col, rocksNewEnd1.row);
  366.                 Console.ForegroundColor = ConsoleColor.Magenta;
  367.                 if (rocksDirection == down) Console.Write("#");
  368.  
  369.                 rockBody2.Enqueue(rocksNewEnd2);
  370.                 Console.SetCursorPosition(rocksNewEnd2.col, rocksNewEnd2.row);
  371.                 Console.ForegroundColor = ConsoleColor.DarkGreen;
  372.                 if (rocksDirection == down) Console.Write("^");
  373.  
  374.                 rockBody3.Enqueue(rocksNewEnd3);
  375.                 Console.SetCursorPosition(rocksNewEnd3.col, rocksNewEnd3.row);
  376.                 Console.ForegroundColor = ConsoleColor.Blue;
  377.                 if (rocksDirection == down) Console.Write("%");
  378.  
  379.                 rockBody4.Enqueue(rocksNewEnd4);
  380.                 Console.SetCursorPosition(rocksNewEnd4.col, rocksNewEnd4.row);
  381.                 Console.ForegroundColor = ConsoleColor.Red;
  382.                 if (rocksDirection == down) Console.Write("&");
  383.  
  384.  
  385.                 //6. moving...
  386.                 Position last = dwarfBody.Dequeue();
  387.                 Console.SetCursorPosition(last.col, last.row);
  388.                 Console.Write(" ");
  389.  
  390.                 Position goldlast = goldBrick.Dequeue();
  391.                 Console.SetCursorPosition(goldlast.col, goldlast.row);
  392.                 Console.Write(" ");
  393.  
  394.                 Position goldlast1 = goldBrick1.Dequeue();
  395.                 Console.SetCursorPosition(goldlast1.col, goldlast1.row);
  396.                 Console.Write(" ");
  397.  
  398.                 Position goldlast2 = goldBrick2.Dequeue();
  399.                 Console.SetCursorPosition(goldlast2.col, goldlast2.row);
  400.                 Console.Write(" ");
  401.  
  402.                 Position rockLast = rockBody.Dequeue();
  403.                 Console.SetCursorPosition(rockLast.col, rockLast.row);
  404.                 Console.Write(" ");
  405.  
  406.                 Position rockLast1 = rockBody1.Dequeue();
  407.                 Console.SetCursorPosition(rockLast1.col, rockLast1.row);
  408.                 Console.Write(" ");
  409.  
  410.                 Position rockLast2 = rockBody2.Dequeue();
  411.                 Console.SetCursorPosition(rockLast2.col, rockLast2.row);
  412.                 Console.Write(" ");
  413.  
  414.                 Position rockLast3 = rockBody3.Dequeue();
  415.                 Console.SetCursorPosition(rockLast3.col, rockLast3.row);
  416.                 Console.Write(" ");
  417.  
  418.                 Position rockLast4 = rockBody4.Dequeue();
  419.                 Console.SetCursorPosition(rockLast4.col, rockLast4.row);
  420.                 Console.Write(" ");
  421.  
  422.  
  423.                
  424.  
  425.                 if (dwarfBody.Contains(goldNewEnd) || dwarfBody.Contains(goldNewEnd1) || dwarfBody.Contains(goldNewEnd2))
  426.                 {
  427.                     userPoints++;
  428.                 }
  429.                 if (dwarfBody.Contains(rocksNewEnd) || dwarfBody.Contains(rocksNewEnd1) || dwarfBody.Contains(rocksNewEnd2) || dwarfBody.Contains(rocksNewEnd3) || dwarfBody.Contains(rocksNewEnd4))
  430.                 {
  431.                     Console.SetCursorPosition(0, 0);
  432.                     Console.ForegroundColor = ConsoleColor.Red;
  433.                     Console.WriteLine("Game over!");
  434.                     Console.WriteLine("Your points are: {0}", userPoints * 50);
  435.                     return;
  436.                 }
  437.                                
  438.                
  439.                 Thread.Sleep(150);
  440.                 Console.Clear();                
  441.             }
  442.  
  443.         }
  444.        
  445.  
  446.     }
  447. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement