diff options
Diffstat (limited to 'Snake')
| -rw-r--r-- | Snake/Snake_CSharp/Snake/BodyPart.cs | 52 | ||||
| -rw-r--r-- | Snake/Snake_CSharp/Snake/Food.cs | 26 | ||||
| -rw-r--r-- | Snake/Snake_CSharp/Snake/Map.cs | 30 | ||||
| -rw-r--r-- | Snake/Snake_CSharp/Snake/Program.cs | 56 | ||||
| -rw-r--r-- | Snake/Snake_CSharp/Snake/Properties/AssemblyInfo.cs | 26 | ||||
| -rw-r--r-- | Snake/Snake_CSharp/Snake/Snake.cs | 69 | ||||
| -rw-r--r-- | Snake/Snake_CSharp/Snake/Snake.csproj | 43 | ||||
| -rw-r--r-- | Snake/Snake_CSharp/Snake_CSharp.sln | 39 |
8 files changed, 341 insertions, 0 deletions
diff --git a/Snake/Snake_CSharp/Snake/BodyPart.cs b/Snake/Snake_CSharp/Snake/BodyPart.cs new file mode 100644 index 0000000..8969f6f --- /dev/null +++ b/Snake/Snake_CSharp/Snake/BodyPart.cs @@ -0,0 +1,52 @@ +using System; +namespace Snake { + public class BodyPart { + public int XPos { get; private set; } + public int YPos { get; private set; } + public string LastDirection { get; private set; } + + public char Symbol { get; set; } + + public BodyPart(int XPos, int YPos, char Symbol) { + this.XPos = XPos; + this.YPos = YPos; + this.Symbol = Symbol; + } + + public bool IsOnPosition(int XPos, int YPos) { + return XPos == this.XPos && YPos == this.YPos; + } + + public void Move(string direction) { + switch (direction) { + case "up": + if (YPos == 1) YPos = Map.Height; + else YPos--; + break; + case "down": + if (YPos == Map.Height) YPos = 1; + else YPos++; + break; + case "left": + if (XPos == 1) XPos = Map.Width; + else XPos--; + break; + case "right": + if (XPos == Map.Width) XPos = 1; + else XPos++; + break; + } + UpdateLastDirection(direction); + } + + public void UpdatePosition(int newXPos, int newYPos) { + this.XPos = newXPos; + this.YPos = newYPos; + } + + //this exists because of Snake.SetInitialHeadDirection + public void UpdateLastDirection(string direction) { + LastDirection = direction; + } + } +} diff --git a/Snake/Snake_CSharp/Snake/Food.cs b/Snake/Snake_CSharp/Snake/Food.cs new file mode 100644 index 0000000..b49cbb2 --- /dev/null +++ b/Snake/Snake_CSharp/Snake/Food.cs @@ -0,0 +1,26 @@ +using System; +namespace Snake { + public static class Food { + public static int XPos { get; private set; } + public static int YPos { get; private set; } + public static char Symbol = 'O'; + + private static Random rnd = new Random(); + + public static void IfEaten() { + if (Snake.AnyBodyPartIsAtPosition(XPos, YPos )) { + GenerateRandomPosition(); + Snake.AppendBodyPart(); + } + } + + public static void GenerateRandomPosition() { + XPos = rnd.Next(1, Map.Width + 1); + YPos = rnd.Next(1, Map.Height + 1); + } + + public static bool IsAtPosition(int XPos, int YPos) { + return Food.XPos == XPos && Food.YPos == YPos; + } + } +} diff --git a/Snake/Snake_CSharp/Snake/Map.cs b/Snake/Snake_CSharp/Snake/Map.cs new file mode 100644 index 0000000..b6259db --- /dev/null +++ b/Snake/Snake_CSharp/Snake/Map.cs @@ -0,0 +1,30 @@ +using System; +using System.Text; + +namespace Snake { + public static class Map { + //Height and width are the playable area and there is a frame around + public static int Height { get; private set; } + public static int Width { get; private set; } + + public static string GetLine(int line) { + if (line == 0 || line == Height + 1) return new string('-', Width + 2); + + StringBuilder toReturn = new StringBuilder("|"); + + char toAppend = ' '; + for(int x = 1; x <= Width; x++) { + if (Snake.AnyBodyPartIsAtPosition( x, line )) toAppend = Snake.GetBodySymbol( x, line ); + else if (Food.IsAtPosition( x, line )) toAppend = Food.Symbol; + toReturn.Append(toAppend); + toAppend = ' '; + } + return toReturn.Append("|").ToString(); + } + + public static void ChangeSize(int height, int width) { + Map.Height = height; + Map.Width = width; + } + } +} diff --git a/Snake/Snake_CSharp/Snake/Program.cs b/Snake/Snake_CSharp/Snake/Program.cs new file mode 100644 index 0000000..6652d00 --- /dev/null +++ b/Snake/Snake_CSharp/Snake/Program.cs @@ -0,0 +1,56 @@ +using System; +using System.Linq; +using System.Threading; + +namespace Snake { + class MainClass { + private const int DEFAULT_MAP_HEIGHT = 20; + private const int DEFAULT_MAP_WIDTH = 20; + + private const string FIRST_DIRECTION = "right"; + + private static ConsoleKey moveUp = ConsoleKey.W; + private static ConsoleKey moveDown = ConsoleKey.S; + private static ConsoleKey moveRight = ConsoleKey.D; + private static ConsoleKey moveLeft = ConsoleKey.A; + + public static void Main(string[] args) { + InitialSetup(); + bool playing = true; + + string direction = FIRST_DIRECTION; + while (playing) { + if (Console.KeyAvailable) { + ConsoleKey pressedKey = Console.ReadKey().Key; + + if (pressedKey == moveUp) direction = "up"; + else if (pressedKey == moveDown) direction = "down"; + else if (pressedKey == moveRight) direction = "right"; + else if (pressedKey == moveLeft) direction = "left"; + } + Snake.MoveHead(direction); + Food.IfEaten(); + PrintMap(); + if (Snake.HasBittenItself) playing = false; + else { + Thread.Sleep(200); + Console.Clear(); + } + } + Console.WriteLine("YOU LOST!"); + } + + public static void PrintMap() { + for(int row = 0; row <= Map.Height + 1; row++) { + Console.WriteLine(Map.GetLine(row)); + } + } + + public static void InitialSetup() { + Map.ChangeSize(DEFAULT_MAP_HEIGHT, DEFAULT_MAP_WIDTH); + Snake.AddBodyPart(2, 1, '+'); + Snake.SetInitialHeadDirection(FIRST_DIRECTION); + Food.GenerateRandomPosition(); + } + } +} diff --git a/Snake/Snake_CSharp/Snake/Properties/AssemblyInfo.cs b/Snake/Snake_CSharp/Snake/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d33d8d0 --- /dev/null +++ b/Snake/Snake_CSharp/Snake/Properties/AssemblyInfo.cs @@ -0,0 +1,26 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle("Snake")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("")] +[assembly: AssemblyCopyright("")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] diff --git a/Snake/Snake_CSharp/Snake/Snake.cs b/Snake/Snake_CSharp/Snake/Snake.cs new file mode 100644 index 0000000..1b6b0dc --- /dev/null +++ b/Snake/Snake_CSharp/Snake/Snake.cs @@ -0,0 +1,69 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Snake { + public static class Snake { + private const char DEFAULT_BODY_SYMBOL = '*'; + private static List<BodyPart> body = new List<BodyPart>(); + + public static void AddBodyPart(int XPos, int YPos) { + AddBodyPart(XPos, YPos, DEFAULT_BODY_SYMBOL); + } + + public static void AddBodyPart(int XPos, int YPos, char Symbol) { + Body.Add(new BodyPart(XPos, YPos, Symbol)); + } + + public static void MoveHead(string direction) { + for(int i = Body.Count() - 1; i > 0; i--) { + Body[i].Move(Body[i - 1].LastDirection); + } + Body[0].Move(direction); + } + + public static void AppendBodyPart() { + int currXPos = Body.Last().XPos, currYPos = Body.Last().YPos; + + switch (Body.Last().LastDirection) { + case "up": + if (currYPos == Map.Height) currYPos = 1; + else currYPos++; + break; + case "down": + if (currYPos == 1) currYPos = Map.Height; + else currYPos--; + break; + case "left": + if (currXPos == Map.Width) currXPos = 1; + else currXPos++; + break; + case "right": + if (currXPos == 1) currXPos = Map.Width; + else currXPos--; + break; + } + Snake.Body.Add(new BodyPart(currXPos, currYPos, '*')); + } + + public static bool HasBittenItself { + get => Body.Count(bp => bp.XPos == Body.First().XPos && bp.YPos == Body.First().YPos) > 1; + } + + public static bool AnyBodyPartIsAtPosition(int XPos, int YPos) { + return Body.Any(bp => bp.IsOnPosition(XPos, YPos)); + } + + public static char GetBodySymbol(int XPos, int YPos) { + return Body.First( bp => bp.IsOnPosition( XPos, YPos ) ).Symbol; + } + + public static void SetInitialHeadDirection(string direction) { + Body.First().UpdateLastDirection(direction); + } + + public static List<BodyPart> Body { + get => Snake.body; + } + } +} diff --git a/Snake/Snake_CSharp/Snake/Snake.csproj b/Snake/Snake_CSharp/Snake/Snake.csproj new file mode 100644 index 0000000..2f3f609 --- /dev/null +++ b/Snake/Snake_CSharp/Snake/Snake.csproj @@ -0,0 +1,43 @@ +<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
+ <ProjectGuid>{8D624E64-608A-47B7-B3CF-5BBB196C9DC1}</ProjectGuid>
+ <OutputType>Exe</OutputType>
+ <RootNamespace>Snake</RootNamespace>
+ <AssemblyName>Snake</AssemblyName>
+ <TargetFrameworkVersion>v4.7</TargetFrameworkVersion>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG;</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ExternalConsole>true</ExternalConsole>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ <ExternalConsole>true</ExternalConsole>
+ <PlatformTarget>x86</PlatformTarget>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Program.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Map.cs" />
+ <Compile Include="Snake.cs" />
+ <Compile Include="BodyPart.cs" />
+ <Compile Include="Food.cs" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+</Project>
\ No newline at end of file diff --git a/Snake/Snake_CSharp/Snake_CSharp.sln b/Snake/Snake_CSharp/Snake_CSharp.sln new file mode 100644 index 0000000..d52ffb8 --- /dev/null +++ b/Snake/Snake_CSharp/Snake_CSharp.sln @@ -0,0 +1,39 @@ +
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio 15
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Snake", "Snake\Snake.csproj", "{8D624E64-608A-47B7-B3CF-5BBB196C9DC1}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|x86 = Debug|x86
+ Release|x86 = Release|x86
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {8D624E64-608A-47B7-B3CF-5BBB196C9DC1}.Debug|x86.ActiveCfg = Debug|x86
+ {8D624E64-608A-47B7-B3CF-5BBB196C9DC1}.Debug|x86.Build.0 = Debug|x86
+ {8D624E64-608A-47B7-B3CF-5BBB196C9DC1}.Release|x86.ActiveCfg = Release|x86
+ {8D624E64-608A-47B7-B3CF-5BBB196C9DC1}.Release|x86.Build.0 = Release|x86
+ EndGlobalSection
+ GlobalSection(MonoDevelopProperties) = preSolution
+ Policies = $0
+ $0.DotNetNamingPolicy = $1
+ $1.DirectoryNamespaceAssociation = PrefixedHierarchical
+ $0.TextStylePolicy = $2
+ $2.inheritsSet = null
+ $2.scope = text/x-csharp
+ $0.CSharpFormattingPolicy = $3
+ $3.NewLinesForBracesInTypes = False
+ $3.NewLinesForBracesInMethods = False
+ $3.NewLinesForBracesInProperties = False
+ $3.NewLinesForBracesInAccessors = False
+ $3.NewLinesForBracesInAnonymousMethods = False
+ $3.NewLinesForBracesInControlBlocks = False
+ $3.NewLinesForBracesInAnonymousTypes = False
+ $3.NewLinesForBracesInObjectCollectionArrayInitializers = False
+ $3.NewLinesForBracesInLambdaExpressionBody = False
+ $3.NewLineForMembersInObjectInit = False
+ $3.NewLineForMembersInAnonymousTypes = False
+ $3.SpaceWithinMethodCallParentheses = True
+ $3.scope = text/x-csharp
+ EndGlobalSection
+EndGlobal
|
