Advertisement
Guest User

Untitled

a guest
Dec 1st, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5.  
  6. namespace ConsoleSnake
  7. {
  8.     public static class ConsoleHelper
  9.     {
  10.         public static void SetCursor(Point p)
  11.         {
  12.             Console.SetCursorPosition(p.X, p.Y);
  13.         }
  14.     }
  15.  
  16.     public sealed class Point
  17.     {
  18.         public int X { get; set; }
  19.         public int Y { get; set; }
  20.  
  21.         public Point() { }
  22.  
  23.         public Point(int x, int y)
  24.         {
  25.             X = x;
  26.             Y = y;
  27.         }
  28.  
  29.         public static Point operator +(Point p1, Point p2)
  30.         {
  31.             return new Point(p1.X + p2.X, p1.Y + p2.Y);
  32.         }
  33.  
  34.         public override bool Equals(object obj)
  35.         {
  36.             Point p = (Point)obj;
  37.             if (p == null)
  38.                 return false;
  39.             return X == p.X && Y == p.Y;
  40.         }
  41.  
  42.         public override int GetHashCode()
  43.         {
  44.             return X ^ Y;
  45.         }
  46.     }
  47.  
  48.     public sealed class SnakeGame
  49.     {
  50.         private enum Direction
  51.         {
  52.             Up = 1,
  53.             Right = 2,
  54.             Down = 3,
  55.             Left = 4,
  56.         }
  57.  
  58.         private Dictionary<Direction, Point> DirectionToPoint = new Dictionary<Direction, Point>
  59.         {
  60.             { Direction.Down, new Point(0,1)},
  61.             { Direction.Up, new Point(0,-1)},
  62.             { Direction.Right, new Point(1,0)},
  63.             { Direction.Left, new Point(-1,0)},
  64.         };
  65.  
  66.         public int Width { get; set; }
  67.         public int Height { get; set; }
  68.         private Direction direction;
  69.         private IList<Point> Body { get; set; }
  70.         private Point food;
  71.         private Random random;
  72.         private bool isDead = false;
  73.         public bool IsDead
  74.         {
  75.             get { return isDead; }
  76.         }
  77.         public bool Walls { get; set; }
  78.  
  79.         public SnakeGame(int width, int height)
  80.         {
  81.             Width = width;
  82.             Height = height;
  83.             Body = new List<Point>() { new Point() };
  84.             random = new Random();
  85.             food = new Point();
  86.             GenerateFood();
  87.             direction = Direction.Right;
  88.         }
  89.  
  90.         private void GenerateFood()
  91.         {
  92.             do
  93.             {
  94.                 food.X = random.Next(Width);
  95.                 food.Y = random.Next(Height);
  96.             } while (Body.Contains(food));
  97.         }
  98.  
  99.         public void ChangeDirection(ConsoleKey key)
  100.         {
  101.             if (key == ConsoleKey.A || key == ConsoleKey.LeftArrow)
  102.                 direction = Direction.Left;
  103.             else if (key == ConsoleKey.D || key == ConsoleKey.RightArrow)
  104.                 direction = Direction.Right;
  105.             else if (key == ConsoleKey.W || key == ConsoleKey.UpArrow)
  106.                 direction = Direction.Up;
  107.             else if (key == ConsoleKey.S || key == ConsoleKey.DownArrow)
  108.                 direction = Direction.Down;
  109.         }
  110.  
  111.         bool ValidateNextStep(Point p)
  112.         {
  113.             return p.X >= 0 && p.X < Width && p.Y >= 0 && p.Y <= Height;
  114.         }
  115.  
  116.         void ChangeNextStep(Point p)
  117.         {
  118.             if (p.X < 0)
  119.                 p.X += Width;
  120.             else if (p.X >= Width)
  121.                 p.X -= Width;
  122.             else if (p.Y < 0)
  123.                 p.Y += Height;
  124.             else if (p.Y >= Height)
  125.                 p.Y -= Height;
  126.         }
  127.  
  128.         public bool Move()
  129.         {
  130.             Point nextStep = Body.First() + DirectionToPoint[direction];
  131.  
  132.             if (!Walls)
  133.                 ChangeNextStep(nextStep);
  134.  
  135.             // fuck this wall || hit his own body
  136.             // validatenextstep dont want to be called if
  137.             if (!ValidateNextStep(nextStep) || Body.Contains(nextStep))
  138.             {
  139.                 isDead = true;
  140.                 return false;
  141.             }
  142.  
  143.             // fucking bitch eated my cake
  144.             if (nextStep.Equals(food))
  145.             {
  146.                 Body.Insert(0, nextStep);
  147.                 GenerateFood();
  148.             }
  149.             // lol noob
  150.             else
  151.             {
  152.                 Body.RemoveAt(Body.Count - 1);
  153.                 Body.Insert(0, nextStep);
  154.             }
  155.  
  156.             return true;
  157.         }
  158.  
  159.         public void Draw()
  160.         {
  161.             Console.Clear();
  162.             // head
  163.             Console.ForegroundColor = ConsoleColor.Cyan;
  164.             ConsoleHelper.SetCursor(Body.First());
  165.             Console.Write("▲►▼◄"[(int)direction - 1]);
  166.             // body
  167.             Console.BackgroundColor = ConsoleColor.Cyan;
  168.             foreach (var p in Body.Skip(1))
  169.             {
  170.                 ConsoleHelper.SetCursor(p);
  171.                 Console.Write(" ");
  172.             }
  173.             Console.ResetColor();
  174.             // food
  175.             Console.ForegroundColor = ConsoleColor.Red;
  176.             ConsoleHelper.SetCursor(food);
  177.             Console.Write("♥");
  178.         }
  179.     }
  180.  
  181.     class Program
  182.     {
  183.         static void Main(string[] args)
  184.         {
  185.             SnakeGame game = new SnakeGame(Console.WindowWidth, Console.WindowHeight);
  186.             Console.WriteLine("Walls 0/1 [0 = off, 1 = on]?");
  187.             game.Walls = Console.ReadLine() == "1";
  188.             Console.WriteLine("Interval between move in miliseconds? (recommended interval is 100ms)");
  189.             int interval = int.Parse(Console.ReadLine());
  190.  
  191.             new Thread(new ThreadStart(() =>
  192.                 {
  193.                     while (game.Move())
  194.                     {
  195.                         game.Draw();
  196.                         Thread.Sleep(interval);
  197.                     }
  198.                     Console.Clear();
  199.                     Console.WriteLine("YOU LOST BITCH!");
  200.                 })).Start();
  201.  
  202.             do
  203.                 game.ChangeDirection(Console.ReadKey(true).Key);
  204.             while (!game.IsDead);
  205.  
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement