Snake
1) Procedures
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snake
{
class Program
{
static void Main( string[] args )
{
int x1 = 1;
int y1 = 3;
char sym1 = '*';
Draw( x1, y1, sym1 );
int x2 = 4;
int y2 = 5;
char sym2 = '#';
Draw( x2, y2, sym2 );
Console.ReadLine();
}
static void Draw(int x, int y, char sym)
{
Console.SetCursorPosition( x, y );
Console.Write( sym );
}
}
}
2) Classes
Classes – the definitions for the data format and available procedures for a given type or class of object; may also contain data and procedures (known as class methods) themselves, i.e. classes contain the data members and member functions
Objects – instances of classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
int x1 = 1;
int y1 = 3;
char sym1 = '*';
Draw(x1, y1, sym1);
int x2 = 4;
int y2 = 5;
char sym2 = '*';
Draw(x2, y2, sym2);
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Point
{
public int x;
public int y;
public char sym;
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write(sym);
}
}
}
3) Constructors
Method in object-oriented programming is a procedure associated with a message and an object. An object consists of data and behavior; these comprise an interface, which specifies how the object may be utilized by any of its various consumers.
Constructor – a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.
Properties are as in everyday language and technically are fields of objects/classes with dedicated getter/setter routines.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Point
{
public int x;
public int y;
public char sym;
public Point()
{
}
public Point(int _x,int _y,char _sym)
{
x = _x;
y = _y;
sym = _sym;
}
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write(sym);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Point p1 = new Point(1,3,'*');
p1.Draw();
Point p2 = new Point(4,5,'*');
p2.Draw();
Console.ReadLine();
}
}
}
4) Lists
Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Point p1 = new Point(1,3,'*');
p1.Draw();
Point p2 = new Point(4,5,'*');
p2.Draw();
List<int> numlist = new List<int>;
numlist.Add(0);
numlist.Add(1);
numlist.Add(2);
int x = numlist[0];
int y = numlist[1];
int z = numlist[2];
foreach (int i in numlist)
{
Console.WriteLine(i);
}
numlist.RemoveAt(0);
List<Point> plist = new List<Point>();
plist.Add(p1);
plist.Add(p2);
Console.ReadLine();
}
}
}
5) Horizontal Line
Abstraction – one of the key concepts of object-oriented programming languages. Its main goal is to handle complexity by hiding unnecessary details from the user. That enables the user to implement more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Point p1 = new Point(1,3,'*');
p1.Draw();
Point p2 = new Point(4,5,'*');
p2.Draw();
HorizontalLine line = new HorizontalLine(5,10,8,'+');
line.Drow();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class HorizontalLine
{
List<Point> plist;
public HorizontalLine(int xleft,int xright,int y, char sym)
{
plist = new List<Point>();
for (int x = xleft; x < xright; x++)
{
Point p = new Point(x, y, sym);
plist.Add(p);
}
}
public void Drow()
{
foreach (Point p in plist)
{
p.Draw();
}
}
}
}
6) Inheritance
Inheritance – a system property that allows you to describe a new class based on an existing one with partially or completely borrowed functionality. The class from which inheritance is made is called base, parent, or superclass. The new class is a descendant, inheritor, child, or derived class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Figure
{
protected List<Point> plist;
public void Drow()
{
foreach (Point p in plist)
{
p.Draw();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class HorizontalLine:Figure
{
public HorizontalLine(int xleft,int xright,int y, char sym)
{
plist = new List<Point>();
for (int x = xleft; x < xright; x++)
{
Point p = new Point(x, y, sym);
plist.Add(p);
}
}
public void Drow()
{
foreach (Point p in plist)
{
p.Draw();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class VerticalLine:Figure
{
public VerticalLine(int yUp, int yDown, int x, char sym)
{
plist = new List<Point>();
for (int y = yUp; y < yDown; y++)
{
Point p = new Point(x, y, sym);
plist.Add(p);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(80, 25);
HorizontalLine upline = new HorizontalLine(0,78,0,'+');
HorizontalLine downline = new HorizontalLine(0, 78, 24, '+');
VerticalLine leftline = new VerticalLine(0,24,0,'+');
VerticalLine rightline = new VerticalLine(0,24,78,'+');
upline.Drow();
downline.Drow();
leftline.Drow();
rightline.Drow();
Point p = new Point(4, 5, '*');
p.Draw();
Console.ReadLine();
}
}
}
7)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(80, 25);
HorizontalLine upline = new HorizontalLine(0,78,0,'+');
HorizontalLine downline = new HorizontalLine(0, 78, 24, '+');
VerticalLine leftline = new VerticalLine(0,24,0,'+');
VerticalLine rightline = new VerticalLine(0,24,78,'+');
upline.Drow();
downline.Drow();
leftline.Drow();
rightline.Drow();
Point p = new Point(4, 5, '*');
Snake snake=new Snake(p,4,Directions.RIGHT);
snake.Drow();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Snake:Figure
{
public Snake(Point tail, int length, Directions direction)
{
plist = new List<Point>();
for (int i = 0; i < length; i++)
{
Point p = new Point(tail);
p.Move(i, direction);
plist.Add(p);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
enum Directions
{
LEFT,
RIGHT,
UP,
DOWN
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Point
{
public int x;
public int y;
public char sym;
public Point()
{
}
public Point(int _x,int _y,char _sym)
{
x = _x;
y = _y;
sym = _sym;
}
public Point (Point p)
{
x = p.x;
y = p.y;
sym = p.sym;
}
public void Move(int offset,Directions direction)
{
if (direction==Directions.RIGHT)
{
x = x + offset;
}
else if (direction==Directions.LEFT)
{
x = x - offset;
}
else if (direction==Directions.UP)
{
y = y + offset;
}
else if (direction==Directions.DOWN)
{
y = y - offset; }
}
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write(sym);
}
}
}
8)Movement
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Point
{
public int x;
public int y;
public char sym;
public Point()
{
}
public Point(int _x,int _y,char _sym)
{
x = _x;
y = _y;
sym = _sym;
}
public Point (Point p)
{
x = p.x;
y = p.y;
sym = p.sym;
}
public void Move(int offset,Directions direction)
{
if (direction==Directions.RIGHT)
{
x = x + offset;
}
else if (direction==Directions.LEFT)
{
x = x - offset;
}
else if (direction==Directions.UP)
{
y = y + offset;
}
else if (direction==Directions.DOWN)
{
y = y - offset; }
}
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write(sym);
}
public void Clear()
{
sym = ' ';
Draw();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(80, 25);
HorizontalLine upline = new HorizontalLine(0,78,0,'+');
HorizontalLine downline = new HorizontalLine(0, 78, 24, '+');
VerticalLine leftline = new VerticalLine(0,24,0,'+');
VerticalLine rightline = new VerticalLine(0,24,78,'+');
upline.Drow();
downline.Drow();
leftline.Drow();
rightline.Drow();
Point p = new Point(4, 5, '*');
Snake snake=new Snake(p,4,Directions.RIGHT);
snake.Drow();
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
Thread.Sleep(300);
snake.Move();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Snake:Figure
{
Directions direction;
public Snake(Point tail, int length, Directions _direction)
{
direction = _direction;
plist = new List<Point>();
for (int i = 0; i < length; i++)
{
Point p = new Point(tail);
p.Move(i, direction);
plist.Add(p);
}
}
internal void Move()
{
Point tail = plist.First();
plist.Remove(tail);
Point head = GetNextPoint();
plist.Add(head);
tail.Clear();
head.Draw();
}
public Point GetNextPoint()
{
Point head = plist.Last();
Point nextPoint = new Point(head);
nextPoint.Move(1, direction);
return nextPoint;
}
}
}
9) Controls
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Point
{
public int x;
public int y;
public char sym;
public Point()
{
}
public Point(int _x,int _y,char _sym)
{
x = _x;
y = _y;
sym = _sym;
}
public Point (Point p)
{
x = p.x;
y = p.y;
sym = p.sym;
}
public void Move(int offset,Directions direction)
{
if (direction==Directions.RIGHT)
{
x = x + offset;
}
else if (direction==Directions.LEFT)
{
x = x - offset;
}
else if (direction==Directions.UP)
{
y = y + offset;
}
else if (direction==Directions.DOWN)
{
y = y - offset; }
}
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write(sym);
}
public void Clear()
{
sym = ' ';
Draw();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Snake:Figure
{
public Directions direction;
public Snake(Point tail, int length, Directions _direction)
{
direction = _direction;
plist = new List<Point>();
for (int i = 0; i < length; i++)
{
Point p = new Point(tail);
p.Move(i, direction);
plist.Add(p);
}
}
internal void Move()
{
Point tail = plist.First();
plist.Remove(tail);
Point head = GetNextPoint();
plist.Add(head);
tail.Clear();
head.Draw();
}
public Point GetNextPoint()
{
Point head = plist.Last();
Point nextPoint = new Point(head);
nextPoint.Move(1, direction);
return nextPoint;
}
public void HandleKey(ConsoleKey key)
{
if (key == ConsoleKey.LeftArrow)
direction = Directions.LEFT;
else if (key == ConsoleKey.RightArrow)
direction = Directions.RIGHT;
else if (key == ConsoleKey.DownArrow)
direction = Directions.DOWN;
else if (key == ConsoleKey.UpArrow)
direction = Directions.UP;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(80, 25);
HorizontalLine upline = new HorizontalLine(0,78,0,'+');
HorizontalLine downline = new HorizontalLine(0, 78, 24, '+');
VerticalLine leftline = new VerticalLine(0,24,0,'+');
VerticalLine rightline = new VerticalLine(0,24,78,'+');
upline.Drow();
downline.Drow();
leftline.Drow();
rightline.Drow();
Point p = new Point(4, 5, '*');
Snake snake=new Snake(p,4,Directions.RIGHT);
snake.Drow();
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey();
snake.HandleKey(key.Key);
}
Thread.Sleep(100);
snake.Move();
}
}
}
}
10)Food
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(80, 25);
HorizontalLine upline = new HorizontalLine(0,78,0,'+');
HorizontalLine downline = new HorizontalLine(0, 78, 24, '+');
VerticalLine leftline = new VerticalLine(0,24,0,'+');
VerticalLine rightline = new VerticalLine(0,24,78,'+');
upline.Drow();
downline.Drow();
leftline.Drow();
rightline.Drow();
Point p = new Point(4, 5, '*');
Snake snake=new Snake(p,4,Directions.RIGHT);
snake.Drow();
FoodCreator foodCreator = new FoodCreator(80, 25, '$');
Point food = foodCreator.CreateFood();
food.Draw();
while (true)
{
if (snake.Eat(food))
{
food = foodCreator.CreateFood();
food.Draw();
}
else
{
snake.Move();
}
Thread.Sleep(100);
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey();
snake.HandleKey(key.Key);
}
Thread.Sleep(50);
snake.Move();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class FoodCreator
{
int mapWidht;
int mapHeight;
char sym;
Random random = new Random();
public FoodCreator(int mapWidht,int mapHeight,char sym)
{
this.mapWidht = mapWidht;
this.mapHeight = mapHeight;
this.sym = sym;
}
public Point CreateFood()
{
int x = random.Next(2, mapWidht - 2);
int y = random.Next(2, mapHeight - 2);
return new Point(x, y, sym);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Point
{
public int x;
public int y;
public char sym;
public Point()
{
}
public Point(int _x,int _y,char _sym)
{
x = _x;
y = _y;
sym = _sym;
}
public Point (Point p)
{
x = p.x;
y = p.y;
sym = p.sym;
}
public void Move(int offset,Directions direction)
{
if (direction==Directions.RIGHT)
{
x = x + offset;
}
else if (direction==Directions.LEFT)
{
x = x - offset;
}
else if (direction==Directions.UP)
{
y = y - offset;
}
else if (direction==Directions.DOWN)
{
y = y + offset; }
}
public bool IsHit(Point p)
{
return p.x == this.x && p.y == this.y;
}
public void Draw()
{
Console.SetCursorPosition(x, y);
Console.Write(sym);
}
public void Clear()
{
sym = ' ';
Draw();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Snake:Figure
{
public Directions direction;
public Snake(Point tail, int length, Directions _direction)
{
direction = _direction;
plist = new List<Point>();
for (int i = 0; i < length; i++)
{
Point p = new Point(tail);
p.Move(i, direction);
plist.Add(p);
}
}
internal void Move()
{
Point tail = plist.First();
plist.Remove(tail);
Point head = GetNextPoint();
plist.Add(head);
tail.Clear();
head.Draw();
}
public Point GetNextPoint()
{
Point head = plist.Last();
Point nextPoint = new Point(head);
nextPoint.Move(1, direction);
return nextPoint;
}
public void HandleKey(ConsoleKey key)
{
if (key == ConsoleKey.LeftArrow)
direction = Directions.LEFT;
else if (key == ConsoleKey.RightArrow)
direction = Directions.RIGHT;
else if (key == ConsoleKey.DownArrow)
direction = Directions.DOWN;
else if (key == ConsoleKey.UpArrow)
direction = Directions.UP;
}
internal bool Eat(Point food)
{
Point head = GetNextPoint();
if (head.IsHit(food))
{
food.sym = head.sym;
plist.Add(food);
return true;
}
else
{
return false;
}
}
}
}
10)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Walls
{
List<Figure> wallList;
public Walls(int mapWidth, int mapHeight)
{
wallList = new List<Figure>();
HorizontalLine upline = new HorizontalLine(0, mapWidth-2, 0, '+');
HorizontalLine downline = new HorizontalLine(0, mapWidth-2, 24, '+');
VerticalLine leftline = new VerticalLine(0, mapHeight-1, 0, '+');
VerticalLine rightline = new VerticalLine(0, mapHeight-1, 78, '+');
wallList.Add(upline);
wallList.Add(downline);
wallList.Add(leftline);
wallList.Add(rightline);
}
internal bool IsHit(Figure figure)
{
foreach (var wall in wallList)
{
if (wall.IsHit(figure))
{
return true;
}
}
return false;
}
public void Draw()
{
foreach (var wall in wallList)
{
wall.Draw();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Snake:Figure
{
public Directions direction;
public Snake(Point tail, int length, Directions _direction)
{
direction = _direction;
plist = new List<Point>();
for (int i = 0; i < length; i++)
{
Point p = new Point(tail);
p.Move(i, direction);
plist.Add(p);
}
}
internal void Move()
{
Point tail = plist.First();
plist.Remove(tail);
Point head = GetNextPoint();
plist.Add(head);
tail.Clear();
head.Draw();
}
public Point GetNextPoint()
{
Point head = plist.Last();
Point nextPoint = new Point(head);
nextPoint.Move(1, direction);
return nextPoint;
}
internal bool IsHitTail()
{
var head = plist.Last();
for (int i = 0; i < plist.Count-2; i++)
{
if (head.IsHit(plist[i]))
return true;
}
return false;
}
public void HandleKey(ConsoleKey key)
{
if (key == ConsoleKey.LeftArrow)
direction = Directions.LEFT;
else if (key == ConsoleKey.RightArrow)
direction = Directions.RIGHT;
else if (key == ConsoleKey.DownArrow)
direction = Directions.DOWN;
else if (key == ConsoleKey.UpArrow)
direction = Directions.UP;
}
internal bool Eat(Point food)
{
Point head = GetNextPoint();
if (head.IsHit(food))
{
food.sym = head.sym;
plist.Add(food);
return true;
}
else
return false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(80, 25);
Walls walls = new Walls(80, 25);
walls.Draw();
Point p = new Point(4, 5, '*');
Snake snake=new Snake(p,4,Directions.RIGHT);
snake.Draw();
FoodCreator foodCreator = new FoodCreator(80, 25, '$');
Point food = foodCreator.CreateFood();
food.Draw();
while (true)
{
if (walls.IsHit(snake) || snake.IsHitTail())
{
}
if (snake.Eat(food))
{
food = foodCreator.CreateFood();
food.Draw();
}
else
{
snake.Move();
}
Thread.Sleep(100);
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey();
snake.HandleKey(key.Key);
}
Thread.Sleep(50);
snake.Move();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Snek_ssssssss
{
class Figure
{
protected List<Point> plist;
public void Draw()
{
foreach (Point p in plist)
{
p.Draw();
}
}
internal bool IsHit(Figure figure)
{
foreach (var p in plist)
{
if (figure.IsHit(p))
return true;
}
return false;
}
private bool IsHit(Point point)
{
foreach (var p in plist)
{
if (p.IsHit(point))
return true;
}
return false;
}
}
}
Final changes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Snek_ssssssss
{
class Program
{
static void Main(string[] args)
{
Console.SetWindowSize(80, 25);
Walls walls = new Walls(80, 25);
walls.Draw();
Point p = new Point(4, 5, '*');
Snake snake = new Snake(p, 4, Directions.RIGHT);
snake.Draw();
FoodCreator foodCreator = new FoodCreator(80, 25, '$');
Point food = foodCreator.CreateFood();
food.Draw();
while (true)
{
if (walls.IsHit(snake) || snake.IsHitTail())
{
break;
}
if (snake.Eat(food))
{
food = foodCreator.CreateFood();
food.Draw();
}
else
{
snake.Move();
}
Thread.Sleep(100);
if (Console.KeyAvailable)
{
ConsoleKeyInfo key = Console.ReadKey();
snake.HandleKey(key.Key);
}
}
WriteGameOver();
Console.ReadLine();
}
static void WriteGameOver()
{
int xOffset = 25;
int yOffset = 8;
Console.ForegroundColor = ConsoleColor.Red;
Console.SetCursorPosition(xOffset, yOffset++);
WriteText("============================", xOffset, yOffset++);
WriteText("G A M E O V E R", xOffset + 1, yOffset++);
yOffset++;
WriteText("Autor: Evgenij Kartavec", xOffset + 2, yOffset++);
WriteText("Made for GeekBrains", xOffset + 1, yOffset++);
WriteText("============================", xOffset, yOffset++);
}
static void WriteText(String text, int xOffset, int yOffset)
{
Console.SetCursorPosition(xOffset, yOffset);
Console.WriteLine(text);
}
}
}
Additional info
Polymorphism – the method in an object-oriented programming language that performs different things as per the object’s class, which calls it. With Polymorphism, a message is sent to multiple class objects, and every object responds appropriately according to the properties of the class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Snake
{
class Program
{
static void Main( string[] args )
{
VerticalLine vl = new VerticalLine( 0, 10, 5, '%' );
Draw( vl );
Point p = new Point( 4, 5, '*' );
Figure fSnake = new Snake( p, 4, Direction.RIGHT );
Draw( fSnake );
Snake snake = (Snake) fSnake;
HorizontalLine hl = new HorizontalLine( 0, 5, 6, '&' );
List<Figure> figures = new List<Figure>();
figures.Add( fSnake );
figures.Add( vl );
figures.Add( hl );
foreach(var f in figures)
{
f.Draw();
}
}
static void Draw( Figure figure )
{
figure.Draw();
}
}
}