/
Launcher.cs
97 lines (82 loc) · 3.18 KB
/
Launcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
namespace VegetableNinja
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Controls;
using Controls.IO;
using Core;
using Core.Grid;
using Core.Grid.Ninja;
using GameObjects.Players;
using Interfaces;
internal class Launcher
{
private static IInput input;
private static IOutput output;
private static IDictionary<char, IPlayer> players;
private static void Main()
{
input = new StandardInput();
IKeyReader keyReader = new InputArrowKeyReader(input);
output = new StandardOutput();
players = new Dictionary<char, IPlayer>();
string p1name = input.ReadLine();
players.Add(p1name[0], CreatePlayer(p1name));
string p2name = input.ReadLine();
players.Add(p2name[0], CreatePlayer(p2name));
int[] dimensions = input.ReadLine().Split().Select(int.Parse).ToArray();
int rows = dimensions[0];
int cols = dimensions[1];
IGrid<IInteractable, IPlayer> grid = new NinjaGrid<IInteractable, IPlayer>(rows, cols);
PopulateGrid(rows, cols, grid);
IGame game = new NinjaGame(players[p1name[0]], players[p2name[0]], grid, keyReader);
game.Start();
PrintWinner(game.Winner);
}
private static IPlayer CreatePlayer(string playerName)
{
return new Player(playerName);
}
private static void PopulateGrid(int rows, int cols, IGrid<IInteractable, IPlayer> grid)
{
for (int row = 0; row < rows; row++)
{
string currentRow = input.ReadLine();
for (int col = 0; col < cols; col++)
{
char currentElement = currentRow[col];
if (currentElement == '-')
{
grid.Add(default(IInteractable), row, col);
}
else if (players.ContainsKey(currentElement))
{
grid.Add(players[currentElement], row, col);
}
else if (currentElement == '*')
{
// TO DO
}
else
{
Type vegetableType = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => !t.IsAbstract)
.Where(t => !t.IsInterface)
.Where(t => typeof(IVegetable).IsAssignableFrom(t))
.FirstOrDefault(t => t.Name[0] == currentElement);
IInteractable vegetable = (IInteractable)Activator.CreateInstance(vegetableType);
grid.Add(vegetable, row, col);
}
}
}
}
private static void PrintWinner(IPlayer winner)
{
output.WriteLine("Winner: {0}", winner.Name);
output.WriteLine("Power: {0}", winner.Power);
output.WriteLine("Stamina: {0}", winner.Stamina);
}
}
}