Last active
December 14, 2015 23:09
-
-
Save jasonsperske/5163529 to your computer and use it in GitHub Desktop.
A slightly cleaner implementation of this http://stackoverflow.com/questions/15415825/c-sharp-is-there-a-way-of-shortening-my-code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ ][ ][ ][ ][ ][ ][ ][T][ ][ ][ ][ ][ ] 5 | |
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] 4 | |
[ ][ ][ ][ ][T][ ][ ][ ][ ][T][ ][ ][ ] 3 | |
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] 2 | |
[ ][ ][T][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ] 1 | |
[T][ ][ ][ ][ ][ ][ ][T][ ][ ][ ][T][ ] 0 | |
[ ][ ][ ][ ][T][ ][ ][ ][ ][ ][ ][ ][ ]-1 | |
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]-2 | |
[ ][ ][T][ ][ ][ ][T][ ][ ][ ][T][X][ ]-3 | |
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]-4 | |
-7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 | |
Press enter... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace so15415825 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var badguys = new List<Badguy>() | |
{ | |
new Badguy(0, 5), | |
new Badguy(-3, 3), | |
new Badguy(2, 3), | |
new Badguy(-5, 1), | |
new Badguy(-7, 0), | |
new Badguy(0, 0), | |
new Badguy(4, 0), | |
new Badguy(-3, -1), | |
new Badguy(-5, -3), | |
new Badguy(-1, -3), | |
new Badguy(3, -3) | |
}; | |
var player = new Player(4, -3); | |
drawMap(player, badguys); | |
Console.WriteLine("Press enter..."); | |
Console.Read(); | |
} | |
private static void drawMap(Player player, List<Badguy> badguys) | |
{ | |
for (var y = 5; y > -5; y--) | |
{ | |
for (var x = -7; x < 6; x++) | |
{ | |
var badguy = FetchBadguyAt(x, y, badguys); | |
if(player.IsStandingAt(x, y)) { | |
Console.Write(player.ToString()); | |
} | |
else if (badguy != null) | |
{ | |
Console.Write(badguy.ToString()); | |
} | |
else | |
{ | |
Console.Write("[ ]"); | |
} | |
} | |
Console.WriteLine(rightAlign(y)); | |
} | |
for (var x = -7; x < 6; x++) | |
{ | |
Console.Write(rightAlign(x) + " "); | |
} | |
Console.WriteLine(); | |
} | |
private static string rightAlign(int n) { | |
return (n >= 0) ? " " + n : "" + n; | |
} | |
private static Badguy FetchBadguyAt(int x, int y, List<Badguy> badguys) | |
{ | |
foreach (var badguy in badguys) | |
{ | |
if (badguy.IsStandingAt(x, y)) | |
{ | |
return badguy; | |
} | |
} | |
return null; | |
} | |
} | |
class Player | |
{ | |
public int X { get; set; } | |
public int Y { get; set; } | |
public Player(int x, int y) | |
{ | |
this.X = x; | |
this.Y = y; | |
} | |
public string ToString() | |
{ | |
return "[X]"; | |
} | |
public bool IsStandingAt(int x, int y) | |
{ | |
return this.X == x && this.Y == y; | |
} | |
} | |
class Badguy | |
{ | |
public int X { get; set; } | |
public int Y { get; set; } | |
public Badguy(int x, int y) | |
{ | |
this.X = x; | |
this.Y = y; | |
} | |
public string ToString() | |
{ | |
return "[T]"; | |
} | |
public bool IsStandingAt(int x, int y) | |
{ | |
return this.X == x && this.Y == y; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment