Browse Source

re init

master
Robin Thoni 7 years ago
commit
50d658fa40
33 changed files with 1505 additions and 0 deletions
  1. 76
    0
      .gitignore
  2. 6
    0
      .idea/.idea.uqac-ia-aspirobot/.idea/contentModel.xml
  3. 8
    0
      .idea/.idea.uqac-ia-aspirobot/.idea/modules.xml
  4. 12
    0
      .idea/.idea.uqac-ia-aspirobot/riderModule.iml
  5. 22
    0
      uqac-ia-aspirobot.sln
  6. 165
    0
      uqac-ia-aspirobot/Agent/AgAgent.cs
  7. 20
    0
      uqac-ia-aspirobot/Agent/AgConfig.cs
  8. 74
    0
      uqac-ia-aspirobot/Agent/AgEnvironment.cs
  9. 48
    0
      uqac-ia-aspirobot/Agent/AgRoom.cs
  10. 14
    0
      uqac-ia-aspirobot/Agent/AgState.cs
  11. 32
    0
      uqac-ia-aspirobot/Agent/Effectors/AgEngineEffector.cs
  12. 41
    0
      uqac-ia-aspirobot/Agent/Effectors/AgVaccumEffector.cs
  13. 15
    0
      uqac-ia-aspirobot/Agent/Sensors/AgBatterySensor.cs
  14. 19
    0
      uqac-ia-aspirobot/Agent/Sensors/AgDustSensor.cs
  15. 12
    0
      uqac-ia-aspirobot/Agent/Sensors/AgPickedSensor.cs
  16. 12
    0
      uqac-ia-aspirobot/Agent/Sensors/AgVaccumSensor.cs
  17. 55
    0
      uqac-ia-aspirobot/Common/ArClient.cs
  18. 18
    0
      uqac-ia-aspirobot/Common/ArConfig.cs
  19. 85
    0
      uqac-ia-aspirobot/Common/ArServer.cs
  20. 72
    0
      uqac-ia-aspirobot/Common/ArStreamString.cs
  21. 30
    0
      uqac-ia-aspirobot/Environment/EnvConfig.cs
  22. 56
    0
      uqac-ia-aspirobot/Environment/EnvEnvironment.cs
  23. 33
    0
      uqac-ia-aspirobot/Environment/EnvRoom.cs
  24. 127
    0
      uqac-ia-aspirobot/Environment/EnvThread.cs
  25. 153
    0
      uqac-ia-aspirobot/Extensions/Extensions.cs
  26. 24
    0
      uqac-ia-aspirobot/Interfaces/IEnvironment.cs
  27. 19
    0
      uqac-ia-aspirobot/Interfaces/IRoom.cs
  28. 7
    0
      uqac-ia-aspirobot/Interfaces/IUi.cs
  29. 46
    0
      uqac-ia-aspirobot/Program.cs
  30. 26
    0
      uqac-ia-aspirobot/Properties/AssemblyInfo.cs
  31. 132
    0
      uqac-ia-aspirobot/UI/UiConsole.cs
  32. 24
    0
      uqac-ia-aspirobot/project.json
  33. 22
    0
      uqac-ia-aspirobot/uqac-ia-aspirobot.xproj

+ 76
- 0
.gitignore View File

1
+*.swp
2
+*.*~
3
+project.lock.json
4
+.DS_Store
5
+*.pyc
6
+
7
+# Visual Studio Code
8
+.vscode
9
+
10
+# User-specific files
11
+*.suo
12
+*.user
13
+*.userosscache
14
+*.sln.docstates
15
+
16
+# Build results
17
+[Dd]ebug/
18
+[Dd]ebugPublic/
19
+[Rr]elease/
20
+[Rr]eleases/
21
+x64/
22
+x86/
23
+build/
24
+bld/
25
+[Bb]in/
26
+[Oo]bj/
27
+msbuild.log
28
+msbuild.err
29
+msbuild.wrn
30
+
31
+# Visual Studio 2015
32
+.vs/
33
+
34
+
35
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
36
+# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
37
+
38
+# User-specific stuff:
39
+.idea/**/workspace.xml
40
+.idea/**/tasks.xml
41
+
42
+# Sensitive or high-churn files:
43
+.idea/**/dataSources/
44
+.idea/**/dataSources.ids
45
+.idea/**/dataSources.xml
46
+.idea/**/dataSources.local.xml
47
+.idea/**/sqlDataSources.xml
48
+.idea/**/dynamic.xml
49
+.idea/**/uiDesigner.xml
50
+
51
+# Gradle:
52
+.idea/**/gradle.xml
53
+.idea/**/libraries
54
+
55
+# Mongo Explorer plugin:
56
+.idea/**/mongoSettings.xml
57
+
58
+## File-based project format:
59
+*.iws
60
+
61
+## Plugin-specific files:
62
+
63
+# IntelliJ
64
+/out/
65
+
66
+# mpeltonen/sbt-idea plugin
67
+.idea_modules/
68
+
69
+# JIRA plugin
70
+atlassian-ide-plugin.xml
71
+
72
+# Crashlytics plugin (for Android Studio and IntelliJ)
73
+com_crashlytics_export_strings.xml
74
+crashlytics.properties
75
+crashlytics-build.properties
76
+fabric.properties

+ 6
- 0
.idea/.idea.uqac-ia-aspirobot/.idea/contentModel.xml View File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="RiderRiderContentModelStore">
4
+    <excludedPaths />
5
+  </component>
6
+</project>

+ 8
- 0
.idea/.idea.uqac-ia-aspirobot/.idea/modules.xml View File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="ProjectModuleManager">
4
+    <modules>
5
+      <module fileurl="file://$PROJECT_DIR$/.idea/.idea.uqac-ia-aspirobot/riderModule.iml" filepath="$PROJECT_DIR$/.idea/.idea.uqac-ia-aspirobot/riderModule.iml" />
6
+    </modules>
7
+  </component>
8
+</project>

+ 12
- 0
.idea/.idea.uqac-ia-aspirobot/riderModule.iml View File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<module type="RIDER_MODULE" version="4">
3
+  <component name="NewModuleRootManager">
4
+    <content url="file://$MODULE_DIR$/../..">
5
+      <sourceFolder url="file://$MODULE_DIR$/../../uqac-ia-aspirobot" isTestSource="false" />
6
+      <excludeFolder url="file://$MODULE_DIR$/../../packages" />
7
+      <excludeFolder url="file://$MODULE_DIR$/../../uqac-ia-aspirobot/bin" />
8
+      <excludeFolder url="file://$MODULE_DIR$/../../uqac-ia-aspirobot/obj" />
9
+    </content>
10
+    <orderEntry type="sourceFolder" forTests="false" />
11
+  </component>
12
+</module>

+ 22
- 0
uqac-ia-aspirobot.sln View File

1
+
2
+Microsoft Visual Studio Solution File, Format Version 12.00
3
+# Visual Studio 2013
4
+VisualStudioVersion = 12.0.0.0
5
+MinimumVisualStudioVersion = 10.0.0.1
6
+Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "uqac_ia_aspirobot", "uqac-ia-aspirobot/uqac-ia-aspirobot.xproj", "{2A192A8A-B6F4-4752-A622-04C7A4AF0EE5}"
7
+EndProject
8
+Global
9
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
10
+		Debug|Any CPU = Debug|Any CPU
11
+		Release|Any CPU = Release|Any CPU
12
+	EndGlobalSection
13
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
14
+		{2A192A8A-B6F4-4752-A622-04C7A4AF0EE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15
+		{2A192A8A-B6F4-4752-A622-04C7A4AF0EE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16
+		{2A192A8A-B6F4-4752-A622-04C7A4AF0EE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17
+		{2A192A8A-B6F4-4752-A622-04C7A4AF0EE5}.Release|Any CPU.Build.0 = Release|Any CPU
18
+	EndGlobalSection
19
+	GlobalSection(SolutionProperties) = preSolution
20
+		HideSolutionNode = FALSE
21
+	EndGlobalSection
22
+EndGlobal

+ 165
- 0
uqac-ia-aspirobot/Agent/AgAgent.cs View File

1
+using System.Linq;
2
+using System.Threading;
3
+using Microsoft.Extensions.DependencyInjection;
4
+using Microsoft.Extensions.Options;
5
+using uqac_ia_aspirobot.Agent.Effectors;
6
+using uqac_ia_aspirobot.Common;
7
+using uqac_ia_aspirobot.Extensions;
8
+using uqac_ia_aspirobot.Interfaces;
9
+using uqac_ia_aspirobot.UI;
10
+
11
+namespace uqac_ia_aspirobot.Agent
12
+{
13
+    public class AgAgent
14
+    {
15
+        private readonly AgDustSensor _agDustSensor;
16
+
17
+        private readonly AgEngineEffector _engineEffector;
18
+
19
+        private readonly AgVaccumEffector _vaccumEffector;
20
+
21
+        private readonly IEnvironment _environment;
22
+
23
+        private readonly AgConfig _options;
24
+
25
+        private readonly AgState _state;
26
+        private readonly IUi _ui;
27
+
28
+        private static Thread _thread;
29
+
30
+        public static void Start(AgConfig agConfig, ArConfig arConfig)
31
+        {
32
+            var agServices = new ServiceCollection();
33
+            agServices.AddOptions();
34
+            agServices.AddSingleton<AgState>();
35
+            agServices.AddSingleton<AgAgent>();
36
+            agServices.AddSingleton<ArClient>();
37
+
38
+            agServices.AddSingleton<AgDustSensor>();
39
+            agServices.AddSingleton<AgBatterySensor>();
40
+            agServices.AddSingleton<AgVaccumSensor>();
41
+            agServices.AddSingleton<AgPickedSensor>();
42
+
43
+            agServices.AddSingleton<AgVaccumEffector>();
44
+            agServices.AddSingleton<AgEngineEffector>();
45
+
46
+            agServices.AddSingleton<IUi, UiConsole>();
47
+            agServices.AddSingleton<IEnvironment, AgEnvironment>();
48
+            agServices.AddTransient<IRoom, AgRoom>();
49
+            agServices.Configure<AgConfig>(agConfig.CopyTo);
50
+            agServices.Configure<ArConfig>(arConfig.CopyTo);
51
+            var agProvider = agServices.BuildServiceProvider();
52
+            var agThread = agProvider.GetService<AgAgent>();
53
+            _thread = new Thread(agThread.Run)
54
+            {
55
+                Name = nameof(AgAgent)
56
+            };
57
+            _thread.Start();
58
+        }
59
+
60
+        public static void Join()
61
+        {
62
+            _thread.Join();
63
+        }
64
+
65
+        public AgAgent(IEnvironment environment, IOptions<AgConfig> options, AgState state, IUi ui,
66
+            AgDustSensor agDustSensor,
67
+            AgEngineEffector engineEffector, AgVaccumEffector vaccumEffector)
68
+        {
69
+            _agDustSensor = agDustSensor;
70
+            _engineEffector = engineEffector;
71
+            _vaccumEffector = vaccumEffector;
72
+            _environment = environment;
73
+            _state = state;
74
+            _ui = ui;
75
+            _options = options.Value;
76
+            _state.SleepTime = _options.SleepTime;
77
+        }
78
+
79
+        private void Run()
80
+        {
81
+            var running = true;
82
+
83
+            _environment.Setup();
84
+            _state.DustyRooms = _environment.FindDustyRoomsWithoutJewel();
85
+
86
+            while (running)
87
+            {
88
+                if (_state.SleepTime > 0)
89
+                {
90
+                    Thread.Sleep(_state.SleepTime);
91
+                }
92
+                UpdateSensors();
93
+                _ui.Update();
94
+                UpdateState();
95
+                Think();
96
+                Work();
97
+            }
98
+        }
99
+
100
+        public void UpdateSensors()
101
+        {
102
+            _agDustSensor.Update();
103
+        }
104
+
105
+        public void UpdateState()
106
+        {
107
+            var dustyRooms = _environment.FindDustyRooms();//TODO Compute average on xx last values
108
+            if (_options.AutoAdjustSleepTime)
109
+            {
110
+                if (dustyRooms.Count > _state.DustyRooms.Count)
111
+                {
112
+                    _state.SleepTime += _state.SleepTime / 2;
113
+                }
114
+                else if (dustyRooms.Count < _state.DustyRooms.Count)
115
+                {
116
+                    _state.SleepTime -= _state.SleepTime / 2;
117
+                }
118
+            }
119
+            _state.DustyRooms = dustyRooms;
120
+        }
121
+
122
+        public void Think()
123
+        {
124
+            if (_state.Destination == null && _state.DustyRooms.Any())
125
+            {
126
+                _state.Destination = _state.DustyRooms.OrderBy(room => room.Distance(_engineEffector)).First();
127
+            }
128
+        }
129
+
130
+        public void Work()
131
+        {
132
+            if (_environment.GetRoomState(_engineEffector.X, _engineEffector.Y).HasFlag(RoomState.Jewel))
133
+            {
134
+                _vaccumEffector.Pick();
135
+            }
136
+            if (_environment.GetRoomState(_engineEffector.X, _engineEffector.Y).HasFlag(RoomState.Dust))
137
+            {
138
+                _vaccumEffector.Vaccum();
139
+            }
140
+            if (_state.Destination != null && _engineEffector.IsInRoom(_state.Destination))
141
+            {
142
+                _state.Destination = null;
143
+            }
144
+            if (_state.Destination != null)
145
+            {
146
+                if (_state.Destination.X < _engineEffector.X)
147
+                {
148
+                    _engineEffector.Move(-1, 0);
149
+                }
150
+                else if (_state.Destination.X > _engineEffector.X)
151
+                {
152
+                    _engineEffector.Move(1, 0);
153
+                }
154
+                else if (_state.Destination.Y < _engineEffector.Y)
155
+                {
156
+                    _engineEffector.Move(0, -1);
157
+                }
158
+                else if (_state.Destination.Y > _engineEffector.Y)
159
+                {
160
+                    _engineEffector.Move(0, 1);
161
+                }
162
+            }
163
+        }
164
+    }
165
+}

+ 20
- 0
uqac-ia-aspirobot/Agent/AgConfig.cs View File

1
+namespace uqac_ia_aspirobot.Agent
2
+{
3
+    public class AgConfig
4
+    {
5
+        public bool AutoAdjustSleepTime { get; set; }
6
+
7
+        public int SleepTime { get; set; }
8
+
9
+        public int StartX { get; set; }
10
+
11
+        public int StartY { get; set; }
12
+
13
+        public void CopyTo(AgConfig other)
14
+        {
15
+            other.SleepTime = SleepTime;
16
+            other.StartX = StartX;
17
+            other.StartY = StartY;
18
+        }
19
+    }
20
+}

+ 74
- 0
uqac-ia-aspirobot/Agent/AgEnvironment.cs View File

1
+using System;
2
+using System.Collections.Generic;
3
+using Microsoft.Extensions.DependencyInjection;
4
+using uqac_ia_aspirobot.Common;
5
+using uqac_ia_aspirobot.Extensions;
6
+using uqac_ia_aspirobot.Interfaces;
7
+
8
+namespace uqac_ia_aspirobot.Agent
9
+{
10
+    public class AgEnvironment : IEnvironment
11
+    {
12
+        private readonly IServiceProvider _serviceProvider;
13
+
14
+        private readonly ArClient _arClient;
15
+
16
+        protected int _width;
17
+
18
+        protected int _height;
19
+
20
+        protected readonly IDictionary<string, IRoom> _rooms = new Dictionary<string, IRoom>();
21
+
22
+        public AgEnvironment(IServiceProvider serviceProvider, ArClient arClient)
23
+        {
24
+            _serviceProvider = serviceProvider;
25
+            _arClient = arClient;
26
+        }
27
+
28
+        public void Setup()
29
+        {
30
+            _arClient.Setup();
31
+            _width = _arClient.GetEnvWidth();
32
+            _height = _arClient.GetEnvWidth();
33
+            for (var x = 0; x < _width; ++x)
34
+            {
35
+                for (var y = 0; y < _height; ++y)
36
+                {
37
+                    var room = _serviceProvider.GetService<IRoom>();
38
+                    room.X = x;
39
+                    room.Y = y;
40
+                    _rooms.Add(GetKey(x, y), room);
41
+                }
42
+            }
43
+        }
44
+
45
+        public void Update()
46
+        {
47
+            this.ForeachRoom<AgRoom>(room =>
48
+            {
49
+                room.Update();
50
+                return true;
51
+            });
52
+        }
53
+
54
+        protected string GetKey(int x, int y)
55
+        {
56
+            return $"{x},{y}";
57
+        }
58
+
59
+        public int GetWidth()
60
+        {
61
+            return _width;
62
+        }
63
+
64
+        public int GetHeight()
65
+        {
66
+            return _height;
67
+        }
68
+
69
+        public IRoom GetRoom(int x, int y)
70
+        {
71
+            return _rooms[GetKey(x, y)];
72
+        }
73
+    }
74
+}

+ 48
- 0
uqac-ia-aspirobot/Agent/AgRoom.cs View File

1
+using uqac_ia_aspirobot.Common;
2
+using uqac_ia_aspirobot.Extensions;
3
+using uqac_ia_aspirobot.Interfaces;
4
+
5
+namespace uqac_ia_aspirobot.Agent
6
+{
7
+    public class AgRoom : IRoom
8
+    {
9
+        private readonly ArClient _arClient;
10
+
11
+        public int X { get; set; }
12
+
13
+        public int Y { get; set; }
14
+
15
+        public RoomState State { get; protected set; }
16
+
17
+        public AgRoom(ArClient arClient)
18
+        {
19
+            _arClient = arClient;
20
+            State = RoomState.Unknown;
21
+        }
22
+
23
+        public void AddDust()
24
+        {
25
+            throw new System.NotImplementedException();
26
+        }
27
+
28
+        public void RemoveDust()
29
+        {
30
+            _arClient.RemoveDust(this);
31
+        }
32
+
33
+        public void AddJewel()
34
+        {
35
+            throw new System.NotImplementedException();
36
+        }
37
+
38
+        public void RemoveJewel()
39
+        {
40
+            _arClient.RemoveJewel(this);
41
+        }
42
+
43
+        public void Update()
44
+        {
45
+            State = _arClient.GetRoomState(this);
46
+        }
47
+    }
48
+}

+ 14
- 0
uqac-ia-aspirobot/Agent/AgState.cs View File

1
+using System.Collections.Generic;
2
+using uqac_ia_aspirobot.Interfaces;
3
+
4
+namespace uqac_ia_aspirobot.Agent
5
+{
6
+    public class AgState
7
+    {
8
+        public int SleepTime { get; set; }
9
+
10
+        public IList<IRoom> DustyRooms { get; set; }
11
+
12
+        public IRoom Destination { get; set; }
13
+    }
14
+}

+ 32
- 0
uqac-ia-aspirobot/Agent/Effectors/AgEngineEffector.cs View File

1
+using System;
2
+using Microsoft.Extensions.Options;
3
+using uqac_ia_aspirobot.Extensions;
4
+
5
+namespace uqac_ia_aspirobot.Agent.Effectors
6
+{
7
+    public class AgEngineEffector
8
+    {
9
+        private readonly AgBatterySensor _agBatterySensor;
10
+
11
+        private readonly AgConfig _options;
12
+
13
+        public int X { get; protected set; }
14
+
15
+        public int Y { get; protected set; }
16
+
17
+        public AgEngineEffector(IOptions<AgConfig> options, AgBatterySensor agBatterySensor)
18
+        {
19
+            _agBatterySensor = agBatterySensor;
20
+            _options = options.Value;
21
+            X = _options.StartX;
22
+            Y = _options.StartY;
23
+        }
24
+
25
+        public void MoveTo(int x, int y)
26
+        {
27
+            _agBatterySensor.Add(this.Distance(x, y));
28
+            X = x;
29
+            Y = y;
30
+        }
31
+    }
32
+}

+ 41
- 0
uqac-ia-aspirobot/Agent/Effectors/AgVaccumEffector.cs View File

1
+using uqac_ia_aspirobot.Common;
2
+using uqac_ia_aspirobot.Extensions;
3
+using uqac_ia_aspirobot.Interfaces;
4
+
5
+namespace uqac_ia_aspirobot.Agent.Effectors
6
+{
7
+    public class AgVaccumEffector
8
+    {
9
+        private readonly AgEngineEffector _engineEffector;
10
+        private readonly ArClient _arClient;
11
+        private readonly IEnvironment _environment;
12
+        private readonly AgBatterySensor _agBatterySensor;
13
+        private readonly AgVaccumSensor _agVaccumSensor;
14
+        private readonly AgPickedSensor _agPickedSensor;
15
+
16
+        public AgVaccumEffector(AgEngineEffector engineEffector, ArClient arClient, IEnvironment environment,
17
+            AgBatterySensor agBatterySensor, AgVaccumSensor agVaccumSensor, AgPickedSensor agPickedSensor)
18
+        {
19
+            _engineEffector = engineEffector;
20
+            _arClient = arClient;
21
+            _environment = environment;
22
+            _agBatterySensor = agBatterySensor;
23
+            _agVaccumSensor = agVaccumSensor;
24
+            _agPickedSensor = agPickedSensor;
25
+        }
26
+
27
+        public void Vaccum()
28
+        {
29
+            _agBatterySensor.Add(1);
30
+            _agVaccumSensor.Increase();
31
+            _environment.RemoveDust(_engineEffector.X, _engineEffector.Y);
32
+        }
33
+
34
+        public void Pick()
35
+        {
36
+            _agBatterySensor.Add(1);
37
+            _agPickedSensor.Increase();
38
+            _arClient.RemoveJewel(_engineEffector.X, _engineEffector.Y);
39
+        }
40
+    }
41
+}

+ 15
- 0
uqac-ia-aspirobot/Agent/Sensors/AgBatterySensor.cs View File

1
+namespace uqac_ia_aspirobot.Agent
2
+{
3
+    public class AgBatterySensor
4
+    {
5
+        public int Spent { get; protected set; }
6
+
7
+        public void Add(int v)
8
+        {
9
+            if (v > 0)
10
+            {
11
+                Spent += v;
12
+            }
13
+        }
14
+    }
15
+}

+ 19
- 0
uqac-ia-aspirobot/Agent/Sensors/AgDustSensor.cs View File

1
+using uqac_ia_aspirobot.Interfaces;
2
+
3
+namespace uqac_ia_aspirobot.Agent
4
+{
5
+    public class AgDustSensor
6
+    {
7
+        private readonly AgEnvironment _environment;
8
+
9
+        public AgDustSensor(IEnvironment environment)
10
+        {
11
+            _environment = environment as AgEnvironment;
12
+        }
13
+
14
+        public void Update()
15
+        {
16
+            _environment.Update();
17
+        }
18
+    }
19
+}

+ 12
- 0
uqac-ia-aspirobot/Agent/Sensors/AgPickedSensor.cs View File

1
+namespace uqac_ia_aspirobot.Agent
2
+{
3
+    public class AgPickedSensor
4
+    {
5
+        public int Picked { get; protected set; }
6
+
7
+        public void Increase()
8
+        {
9
+            ++Picked;
10
+        }
11
+    }
12
+}

+ 12
- 0
uqac-ia-aspirobot/Agent/Sensors/AgVaccumSensor.cs View File

1
+namespace uqac_ia_aspirobot.Agent
2
+{
3
+    public class AgVaccumSensor
4
+    {
5
+        public int Vaccumed { get; protected set; }
6
+
7
+        public void Increase()
8
+        {
9
+            ++Vaccumed;
10
+        }
11
+    }
12
+}

+ 55
- 0
uqac-ia-aspirobot/Common/ArClient.cs View File

1
+using System.IO.Pipes;
2
+using Microsoft.Extensions.Options;
3
+using uqac_ia_aspirobot.Interfaces;
4
+
5
+namespace uqac_ia_aspirobot.Common
6
+{
7
+    public class ArClient
8
+    {
9
+        private readonly ArConfig _options;
10
+
11
+        private NamedPipeClientStream _client;
12
+
13
+        private ArStreamString _stream;
14
+
15
+        public ArClient(IOptions<ArConfig> options)
16
+        {
17
+            _options = options.Value;
18
+        }
19
+
20
+        public void Setup()
21
+        {
22
+            _client = new NamedPipeClientStream(_options.PipeServer, _options.PipeName, PipeDirection.InOut);
23
+            _client.Connect();
24
+            _stream = new ArStreamString(_client);
25
+        }
26
+
27
+        public int GetEnvWidth()
28
+        {
29
+            _stream.Write(ArServer.ArCommands.EnvGetWidth);
30
+            return _stream.ReadInt();
31
+        }
32
+
33
+        public RoomState GetRoomState(int x, int y)
34
+        {
35
+            _stream.Write(ArServer.ArCommands.RoomGetState);
36
+            _stream.Write(x);
37
+            _stream.Write(y);
38
+            return _stream.ReadEnum<RoomState>();
39
+        }
40
+
41
+        public void RemoveDust(int x, int y)
42
+        {
43
+            _stream.Write(ArServer.ArCommands.RoomRemoveDust);
44
+            _stream.Write(x);
45
+            _stream.Write(y);
46
+        }
47
+
48
+        public void RemoveJewel(int x, int y)
49
+        {
50
+            _stream.Write(ArServer.ArCommands.RoomRemoveJewel);
51
+            _stream.Write(x);
52
+            _stream.Write(y);
53
+        }
54
+    }
55
+}

+ 18
- 0
uqac-ia-aspirobot/Common/ArConfig.cs View File

1
+namespace uqac_ia_aspirobot.Common
2
+{
3
+    public class ArConfig
4
+    {
5
+        public string PipeName { get; set; }
6
+
7
+        public string PipeServer { get; set; }
8
+
9
+        public int ServerThreadCount { get; set; }
10
+
11
+        public void CopyTo(ArConfig other)
12
+        {
13
+            other.PipeName = PipeName;
14
+            other.PipeServer = PipeServer;
15
+            other.ServerThreadCount = ServerThreadCount;
16
+        }
17
+    }
18
+}

+ 85
- 0
uqac-ia-aspirobot/Common/ArServer.cs View File

1
+using System;
2
+using Microsoft.Extensions.Options;
3
+using System.IO.Pipes;
4
+using System.Threading;
5
+using uqac_ia_aspirobot.Extensions;
6
+using uqac_ia_aspirobot.Interfaces;
7
+
8
+namespace uqac_ia_aspirobot.Common
9
+{
10
+    public class ArServer
11
+    {
12
+        private readonly IEnvironment _environment;
13
+
14
+        public enum ArCommands
15
+        {
16
+            EnvGetWidth,
17
+            EnvGetHeight,
18
+            RoomGetState,
19
+            RoomRemoveDust,
20
+            RoomRemoveJewel
21
+        }
22
+
23
+        private readonly ArConfig _options;
24
+
25
+        private Thread[] _servers;
26
+
27
+        public ArServer(IOptions<ArConfig> options, IEnvironment environment)
28
+        {
29
+            _environment = environment;
30
+            _options = options.Value;
31
+        }
32
+
33
+        public void Setup()
34
+        {
35
+            _servers = new Thread[_options.ServerThreadCount];
36
+            for (var i = 0; i < _servers.Length; i++)
37
+            {
38
+                _servers[i] = new Thread(ServerThread)
39
+                {
40
+                    Name = $"ArServer-{i}"
41
+                };
42
+                _servers[i].Start();
43
+            }
44
+        }
45
+
46
+        private void ServerThread()
47
+        {
48
+            var server = new NamedPipeServerStream(_options.PipeName, PipeDirection.InOut, _servers.Length);
49
+            server.WaitForConnection();
50
+
51
+            var stream = new ArStreamString(server);
52
+
53
+            while (true)
54
+            {
55
+                var command = stream.ReadEnum<ArCommands>();
56
+                if (command == ArCommands.EnvGetWidth)
57
+                {
58
+                    stream.Write(_environment.GetWidth());
59
+                }
60
+                else if (command == ArCommands.EnvGetHeight)
61
+                {
62
+                    stream.Write(_environment.GetHeight());
63
+                }
64
+                else if (command == ArCommands.RoomGetState)
65
+                {
66
+                    var x = stream.ReadInt();
67
+                    var y = stream.ReadInt();
68
+                    stream.Write(_environment.GetRoomState(x, y));
69
+                }
70
+                else if (command == ArCommands.RoomRemoveDust)
71
+                {
72
+                    var x = stream.ReadInt();
73
+                    var y = stream.ReadInt();
74
+                    _environment.GetRoom(x, y).RemoveDust();
75
+                }
76
+                else if (command == ArCommands.RoomRemoveJewel)
77
+                {
78
+                    var x = stream.ReadInt();
79
+                    var y = stream.ReadInt();
80
+                    _environment.GetRoom(x, y).RemoveJewel();
81
+                }
82
+            }
83
+        }
84
+    }
85
+}

+ 72
- 0
uqac-ia-aspirobot/Common/ArStreamString.cs View File

1
+using System;
2
+using System.Collections.Generic;
3
+using System.IO;
4
+using System.Text;
5
+
6
+namespace uqac_ia_aspirobot.Common
7
+{
8
+    public class ArStreamString
9
+    {
10
+        private readonly Stream ioStream;
11
+        private readonly UnicodeEncoding streamEncoding;
12
+        private readonly IList<string> _writtenStrings = new List<string>();
13
+        private readonly IList<string> _readStrings = new List<string>();
14
+
15
+        public ArStreamString(Stream ioStream)
16
+        {
17
+            this.ioStream = ioStream;
18
+            streamEncoding = new UnicodeEncoding();
19
+        }
20
+
21
+        public string ReadString()
22
+        {
23
+            int len = ioStream.ReadByte() * 256;
24
+            len += ioStream.ReadByte();
25
+            byte[] inBuffer = new byte[len];
26
+            ioStream.Read(inBuffer, 0, len);
27
+
28
+            var str = streamEncoding.GetString(inBuffer);
29
+            _readStrings.Add(str);
30
+            return str;
31
+        }
32
+
33
+        public int ReadInt()
34
+        {
35
+            var str = ReadString();
36
+            return int.Parse(str);
37
+        }
38
+
39
+        public T ReadEnum<T>()
40
+        {
41
+            var str = ReadString();
42
+            return (T)Enum.Parse(typeof(T), str);
43
+        }
44
+
45
+        public int Write(string outString)
46
+        {
47
+            _writtenStrings.Add(outString);
48
+            byte[] outBuffer = streamEncoding.GetBytes(outString);
49
+            int len = outBuffer.Length;
50
+            if (len > UInt16.MaxValue)
51
+            {
52
+                len = (int)UInt16.MaxValue;
53
+            }
54
+            ioStream.WriteByte((byte)(len / 256));
55
+            ioStream.WriteByte((byte)(len & 255));
56
+            ioStream.Write(outBuffer, 0, len);
57
+            ioStream.Flush();
58
+
59
+            return outBuffer.Length + 2;
60
+        }
61
+
62
+        public int Write(int outInt)
63
+        {
64
+            return Write(outInt.ToString());
65
+        }
66
+
67
+        public int Write(Enum outEnum)
68
+        {
69
+            return Write(outEnum.ToString());
70
+        }
71
+    }
72
+}

+ 30
- 0
uqac-ia-aspirobot/Environment/EnvConfig.cs View File

1
+namespace uqac_ia_aspirobot.Environment
2
+{
3
+    public class EnvConfig
4
+    {
5
+        public int Width { get; set; }
6
+
7
+        public int Height { get; set; }
8
+
9
+        public float ActionPropability { get; set; }
10
+
11
+        public float AddJewelProbability { get; set; }
12
+
13
+        public float RemoveJewelProbability { get; set; }
14
+
15
+        public float AddDustProbability { get; set; }
16
+
17
+        public int SleepTime { get; set; }
18
+
19
+        public void CopyTo(EnvConfig other)
20
+        {
21
+            other.Width = Width;
22
+            other.Height = Height;
23
+            other.ActionPropability = ActionPropability;
24
+            other.AddJewelProbability = AddJewelProbability;
25
+            other.RemoveJewelProbability = RemoveJewelProbability;
26
+            other.AddDustProbability = AddDustProbability;
27
+            other.SleepTime = SleepTime;
28
+        }
29
+    }
30
+}

+ 56
- 0
uqac-ia-aspirobot/Environment/EnvEnvironment.cs View File

1
+using System;
2
+using System.Collections.Generic;
3
+using Microsoft.Extensions.DependencyInjection;
4
+using Microsoft.Extensions.Options;
5
+using uqac_ia_aspirobot.Interfaces;
6
+
7
+namespace uqac_ia_aspirobot.Environment
8
+{
9
+    public class EnvEnvironment : IEnvironment
10
+    {
11
+        private readonly IServiceProvider _serviceProvider;
12
+        private readonly EnvConfig _options;
13
+
14
+        protected readonly IDictionary<string, IRoom> _rooms = new Dictionary<string, IRoom>();
15
+
16
+        protected string GetKey(int x, int y)
17
+        {
18
+            return $"{x},{y}";
19
+        }
20
+
21
+        public EnvEnvironment(IServiceProvider serviceProvider, IOptions<EnvConfig> options)
22
+        {
23
+            _serviceProvider = serviceProvider;
24
+            _options = options.Value;
25
+        }
26
+
27
+        public void Setup()
28
+        {
29
+            for (var x = 0; x < _options.Width; ++x)
30
+            {
31
+                for (var y = 0; y < _options.Height; ++y)
32
+                {
33
+                    var room = _serviceProvider.GetService<IRoom>();
34
+                    room.X = x;
35
+                    room.Y = y;
36
+                    _rooms.Add(GetKey(x, y), room);
37
+                }
38
+            }
39
+        }
40
+
41
+        public int GetWidth()
42
+        {
43
+            return _options.Width;
44
+        }
45
+
46
+        public int GetHeight()
47
+        {
48
+            return _options.Height;
49
+        }
50
+
51
+        public IRoom GetRoom(int x, int y)
52
+        {
53
+            return _rooms[GetKey(x, y)];
54
+        }
55
+    }
56
+}

+ 33
- 0
uqac-ia-aspirobot/Environment/EnvRoom.cs View File

1
+using uqac_ia_aspirobot.Interfaces;
2
+
3
+namespace uqac_ia_aspirobot.Environment
4
+{
5
+    public class EnvRoom : IRoom
6
+    {
7
+        public int X { get; set; }
8
+
9
+        public int Y { get; set; }
10
+
11
+        public RoomState State { get; protected set; }
12
+
13
+        public void AddDust()
14
+        {
15
+            State |= RoomState.Dust;
16
+        }
17
+
18
+        public void RemoveDust()
19
+        {
20
+            State &= ~RoomState.Dust;
21
+        }
22
+
23
+        public void AddJewel()
24
+        {
25
+            State |= RoomState.Jewel;
26
+        }
27
+
28
+        public void RemoveJewel()
29
+        {
30
+            State &= ~RoomState.Jewel;
31
+        }
32
+    }
33
+}

+ 127
- 0
uqac-ia-aspirobot/Environment/EnvThread.cs View File

1
+using System;
2
+using System.Threading;
3
+using Microsoft.Extensions.DependencyInjection;
4
+using Microsoft.Extensions.Options;
5
+using uqac_ia_aspirobot.Common;
6
+using uqac_ia_aspirobot.Extensions;
7
+using uqac_ia_aspirobot.Interfaces;
8
+
9
+namespace uqac_ia_aspirobot.Environment
10
+{
11
+    public class EnvThread
12
+    {
13
+        private readonly IEnvironment _environment;
14
+
15
+        private readonly ArServer _server;
16
+
17
+        private readonly EnvConfig _config;
18
+
19
+        private readonly Random _rand;
20
+
21
+        private enum Actions
22
+        {
23
+            NoAction = 0,
24
+            AddDust = 1,
25
+            AddJewel = 2,
26
+            RemoveJewel = 3
27
+        }
28
+
29
+        private static Thread _thread;
30
+
31
+        public static void Start(EnvConfig envConfig, ArConfig arConfig)
32
+        {
33
+            var envServices = new ServiceCollection();
34
+            envServices.AddOptions();
35
+            envServices.AddSingleton<EnvThread>();
36
+            envServices.AddSingleton<ArServer>();
37
+            envServices.AddSingleton<IEnvironment, EnvEnvironment>();
38
+            envServices.AddTransient<IRoom, EnvRoom>();
39
+            envServices.Configure<EnvConfig>(envConfig.CopyTo);
40
+            envServices.Configure<ArConfig>(arConfig.CopyTo);
41
+            var envProvider = envServices.BuildServiceProvider();
42
+            var envThread = envProvider.GetService<EnvThread>();
43
+            _thread = new Thread(envThread.Run)
44
+            {
45
+                Name = nameof(EnvThread)
46
+            };
47
+            _thread.Start();
48
+        }
49
+
50
+        public static void Join()
51
+        {
52
+            _thread.Join();
53
+        }
54
+
55
+        public EnvThread(IOptions<EnvConfig> config, IEnvironment environment, ArServer server)
56
+        {
57
+            _environment = environment;
58
+            _server = server;
59
+            _config = config.Value;
60
+            _rand = new Random();
61
+        }
62
+
63
+        private bool GenerateProba(float proba)
64
+        {
65
+            return _rand.NextDouble() * 100.0 <= proba;
66
+        }
67
+
68
+        private Actions GenerateAction()
69
+        {
70
+            if (GenerateProba(_config.ActionPropability))
71
+            {
72
+                if (GenerateProba(_config.AddDustProbability))
73
+                {
74
+                    return Actions.AddDust;
75
+                }
76
+                if (GenerateProba(_config.RemoveJewelProbability))
77
+                {
78
+                    return Actions.RemoveJewel;
79
+                }
80
+                if (GenerateProba(_config.AddJewelProbability))
81
+                {
82
+                    return Actions.AddJewel;
83
+                }
84
+            }
85
+            return Actions.NoAction;
86
+        }
87
+
88
+        private void Run()
89
+        {
90
+            var running = true;
91
+
92
+            _environment.Setup();
93
+            _server.Setup();
94
+
95
+            while (running)
96
+            {
97
+                Thread.Sleep(_config.SleepTime);
98
+                var action = GenerateAction();
99
+                if (action == Actions.AddDust || action == Actions.AddJewel)
100
+                {
101
+                    var x = _rand.Next(0, _environment.GetWidth());
102
+                    var y = _rand.Next(0, _environment.GetWidth());
103
+                    if (action == Actions.AddDust)
104
+                    {
105
+                        _environment.AddDust(x, y);
106
+                    }
107
+                    else if (action == Actions.AddJewel)
108
+                    {
109
+                        _environment.AddJewel(x, y);
110
+                    }
111
+                }
112
+                else if (action == Actions.RemoveJewel)
113
+                {
114
+                    _environment.ForeachRoom(room =>
115
+                    {
116
+                        if (room.State.HasFlag(RoomState.Jewel))
117
+                        {
118
+                            room.RemoveJewel();
119
+                            return false;
120
+                        }
121
+                        return true;
122
+                    });
123
+                }
124
+            }
125
+        }
126
+    }
127
+}

+ 153
- 0
uqac-ia-aspirobot/Extensions/Extensions.cs View File

1
+using System;
2
+using System.Collections.Generic;
3
+using System.Linq;
4
+using uqac_ia_aspirobot.Agent.Effectors;
5
+using uqac_ia_aspirobot.Common;
6
+using uqac_ia_aspirobot.Interfaces;
7
+
8
+namespace uqac_ia_aspirobot.Extensions
9
+{
10
+    public static class Extensions
11
+    {
12
+        public static void AddDust(this IEnvironment env, int x, int y)
13
+        {
14
+            env.GetRoom(x, y).AddDust();
15
+        }
16
+
17
+        public static void RemoveDust(this IEnvironment env, int x, int y)
18
+        {
19
+            env.GetRoom(x, y).RemoveDust();
20
+        }
21
+
22
+        public static void AddJewel(this IEnvironment env, int x, int y)
23
+        {
24
+            env.GetRoom(x, y).AddJewel();
25
+        }
26
+
27
+        public static void RemoveJewel(this IEnvironment env, int x, int y)
28
+        {
29
+            env.GetRoom(x, y).RemoveJewel();
30
+        }
31
+
32
+        public static RoomState GetRoomState(this IEnvironment env, int x, int y)
33
+        {
34
+            return env.GetRoom(x, y).State;
35
+        }
36
+
37
+        public static bool ForeachRoom<T>(this IEnvironment env, Func<T, bool> action)
38
+            where T : IRoom
39
+        {
40
+            for (var x = 0; x < env.GetWidth(); ++x)
41
+            {
42
+                for (var y = 0; y < env.GetHeight(); ++y)
43
+                {
44
+                    if (!action((T) env.GetRoom(x, y)))
45
+                    {
46
+                        return false;
47
+                    }
48
+                }
49
+            }
50
+            return true;
51
+        }
52
+
53
+        public static bool ForeachRoom(this IEnvironment env, Func<IRoom, bool> action)
54
+        {
55
+            return env.ForeachRoom<IRoom>(action);
56
+        }
57
+
58
+        public static IList<T> FindDustyRooms<T>(this IEnvironment env)
59
+            where T : IRoom
60
+        {
61
+            var rooms = new List<T>();
62
+            ForeachRoom<T>(env, room =>
63
+            {
64
+                if (room.State.HasFlag(RoomState.Dust))
65
+                {
66
+                    rooms.Add(room);
67
+                }
68
+                return true;
69
+            });
70
+            return rooms;
71
+        }
72
+
73
+        public static IList<IRoom> FindDustyRooms(this IEnvironment env)
74
+        {
75
+            return env.FindDustyRooms<IRoom>();
76
+        }
77
+
78
+        public static IList<T> FindDustyRoomsWithoutJewel<T>(this IEnvironment env)
79
+            where T : IRoom
80
+        {
81
+            var rooms = new List<T>();
82
+            ForeachRoom<T>(env, room =>
83
+            {
84
+                if (room.State.CanBeVaccumed())
85
+                {
86
+                    rooms.Add(room);
87
+                }
88
+                return true;
89
+            });
90
+            return rooms;
91
+        }
92
+
93
+        public static IList<IRoom> FindDustyRoomsWithoutJewel(this IEnvironment env)
94
+        {
95
+            return env.FindDustyRoomsWithoutJewel<IRoom>();
96
+        }
97
+
98
+        public static RoomState GetRoomState(this ArClient client, IRoom room)
99
+        {
100
+            return client.GetRoomState(room.X, room.Y);
101
+        }
102
+
103
+        public static void RemoveDust(this ArClient client, IRoom room)
104
+        {
105
+            client.RemoveDust(room.X, room.Y);
106
+        }
107
+
108
+        public static void RemoveJewel(this ArClient client, IRoom room)
109
+        {
110
+            client.RemoveJewel(room.X, room.Y);
111
+        }
112
+
113
+        public static int Distance(this IRoom room, int x, int y)
114
+        {
115
+            return Math.Abs(room.X - x) + Math.Abs(room.Y - y);
116
+        }
117
+
118
+        public static int Distance(this IRoom room, AgEngineEffector effector)
119
+        {
120
+            return room.Distance(effector.X, effector.Y);
121
+        }
122
+
123
+        public static int Distance(this AgEngineEffector effector, int x, int y)
124
+        {
125
+            return Math.Abs(effector.X - x) + Math.Abs(effector.Y - y);
126
+        }
127
+
128
+        public static int Distance(this AgEngineEffector effector, IRoom room)
129
+        {
130
+            return effector.Distance(room.X, room.Y);
131
+        }
132
+
133
+        public static bool CanBeVaccumed(this RoomState state)
134
+        {
135
+            return state.HasFlag(RoomState.Dust) && !state.HasFlag(RoomState.Jewel);
136
+        }
137
+
138
+        public static bool IsInPosition(this AgEngineEffector engine, int x, int y)
139
+        {
140
+            return engine.X == x && engine.Y == y;
141
+        }
142
+
143
+        public static bool IsInRoom(this AgEngineEffector engine, IRoom room)
144
+        {
145
+            return engine.IsInPosition(room.X, room.Y);
146
+        }
147
+
148
+        public static void Move(this AgEngineEffector engine, int dx, int dy)
149
+        {
150
+            engine.MoveTo(engine.X + dx, engine.Y + dy);
151
+        }
152
+    }
153
+}

+ 24
- 0
uqac-ia-aspirobot/Interfaces/IEnvironment.cs View File

1
+using System;
2
+
3
+namespace uqac_ia_aspirobot.Interfaces
4
+{
5
+    [Flags]
6
+    public enum RoomState
7
+    {
8
+        Unknown = -1,
9
+        Clean = 0,
10
+        Dust = 1,
11
+        Jewel = 2
12
+    }
13
+
14
+    public interface IEnvironment
15
+    {
16
+        void Setup();
17
+
18
+        int GetWidth();
19
+
20
+        int GetHeight();
21
+
22
+        IRoom GetRoom(int x, int y);
23
+    }
24
+}

+ 19
- 0
uqac-ia-aspirobot/Interfaces/IRoom.cs View File

1
+namespace uqac_ia_aspirobot.Interfaces
2
+{
3
+    public interface IRoom
4
+    {
5
+        int X { get; set; }
6
+
7
+        int Y { get; set; }
8
+
9
+        RoomState State { get; }
10
+
11
+        void AddDust();
12
+
13
+        void RemoveDust();
14
+
15
+        void AddJewel();
16
+
17
+        void RemoveJewel();
18
+    }
19
+}

+ 7
- 0
uqac-ia-aspirobot/Interfaces/IUi.cs View File

1
+namespace uqac_ia_aspirobot.Interfaces
2
+{
3
+    public interface IUi
4
+    {
5
+        void Update();
6
+    }
7
+}

+ 46
- 0
uqac-ia-aspirobot/Program.cs View File

1
+using System.Threading;
2
+using uqac_ia_aspirobot.Agent;
3
+using uqac_ia_aspirobot.Common;
4
+using uqac_ia_aspirobot.Environment;
5
+
6
+namespace uqac_ia_aspirobot
7
+{
8
+    internal class Program
9
+    {
10
+        public static void Main(string[] args)
11
+        {
12
+            Thread.CurrentThread.Name = "Main";
13
+            var arConfig = new ArConfig
14
+            {
15
+                PipeName = "aspirobot-1.0",
16
+                PipeServer = ".",
17
+                ServerThreadCount = 1
18
+            };
19
+
20
+            EnvThread.Start(new EnvConfig
21
+            {
22
+                Height = 5,
23
+                Width = 5,
24
+                ActionPropability = 30.0f,
25
+                AddDustProbability = 50.0f,
26
+                AddJewelProbability = 50.0f,
27
+//                RemoveJewelProbability = 50.0f,
28
+                RemoveJewelProbability = 0,
29
+                SleepTime = 1000
30
+            }, arConfig);
31
+
32
+            Thread.Sleep(2);
33
+
34
+            AgAgent.Start(new AgConfig
35
+            {
36
+                SleepTime = 1000,
37
+                StartX = 0,
38
+                StartY = 0,
39
+                AutoAdjustSleepTime = false
40
+            }, arConfig);
41
+
42
+            AgAgent.Join();
43
+            EnvThread.Join();
44
+        }
45
+    }
46
+}

+ 26
- 0
uqac-ia-aspirobot/Properties/AssemblyInfo.cs View File

1
+using System.Reflection;
2
+using System.Runtime.CompilerServices;
3
+using System.Runtime.InteropServices;
4
+
5
+// General Information about an assembly is controlled through the following
6
+// set of attributes. Change these attribute values to modify the information
7
+// associated with an assembly.
8
+
9
+[assembly: AssemblyTitle("uqac_ia_aspirobot")]
10
+[assembly: AssemblyDescription("")]
11
+[assembly: AssemblyConfiguration("")]
12
+[assembly: AssemblyCompany("")]
13
+[assembly: AssemblyProduct("uqac_ia_aspirobot")]
14
+[assembly: AssemblyCopyright("Copyright ©  2017")]
15
+[assembly: AssemblyTrademark("")]
16
+[assembly: AssemblyCulture("")]
17
+
18
+// Setting ComVisible to false makes the types in this assembly not visible
19
+// to COM components.  If you need to access a type in this assembly from
20
+// COM, set the ComVisible attribute to true on that type.
21
+
22
+[assembly: ComVisible(false)]
23
+
24
+// The following GUID is for the ID of the typelib if this project is exposed to COM
25
+
26
+[assembly: Guid("2A192A8A-B6F4-4752-A622-04C7A4AF0EE5")]

+ 132
- 0
uqac-ia-aspirobot/UI/UiConsole.cs View File

1
+using System;
2
+using System.Runtime.InteropServices;
3
+using uqac_ia_aspirobot.Agent;
4
+using uqac_ia_aspirobot.Agent.Effectors;
5
+using uqac_ia_aspirobot.Extensions;
6
+using uqac_ia_aspirobot.Interfaces;
7
+
8
+namespace uqac_ia_aspirobot.UI
9
+{
10
+    public class UiConsole : IUi
11
+    {
12
+        private readonly IEnvironment _environment;
13
+        private readonly AgEngineEffector _agEngineEffector;
14
+        private readonly AgPickedSensor _agPickedSensor;
15
+        private readonly AgBatterySensor _agBatterySensor;
16
+        private readonly AgVaccumSensor _agVaccumSensor;
17
+
18
+        public UiConsole(IEnvironment environment, AgEngineEffector agEngineEffector, AgPickedSensor agPickedSensor,
19
+            AgBatterySensor agBatterySensor, AgVaccumSensor agVaccumSensor)
20
+        {
21
+            _environment = environment;
22
+            _agEngineEffector = agEngineEffector;
23
+            _agPickedSensor = agPickedSensor;
24
+            _agBatterySensor = agBatterySensor;
25
+            _agVaccumSensor = agVaccumSensor;
26
+        }
27
+
28
+        public void SetBackgroundColor(ConsoleColor? color)
29
+        {
30
+            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
31
+            {
32
+                Console.BackgroundColor = color.GetValueOrDefault(ConsoleColor.Black);
33
+            }
34
+            else
35
+            {
36
+                var str = "";
37
+                if (color == null)
38
+                {
39
+                    str = $"{(char)27}[1;49m";
40
+                }
41
+                if (color == ConsoleColor.Black)
42
+                {
43
+                    str = $"{(char)27}[1;40m";
44
+                }
45
+                if (color == ConsoleColor.Red)
46
+                {
47
+                    str = $"{(char)27}[1;41m";
48
+                }
49
+                if (color == ConsoleColor.Green)
50
+                {
51
+                    str = $"{(char)27}[1;42m";
52
+                }
53
+                if (color == ConsoleColor.Yellow)
54
+                {
55
+                    str = $"{(char)27}[1;43m";
56
+                }
57
+                if (color == ConsoleColor.Blue)
58
+                {
59
+                    str = $"{(char)27}[1;44m";
60
+                }
61
+                if (color == ConsoleColor.Magenta)
62
+                {
63
+                    str = $"{(char)27}[1;45m";
64
+                }
65
+                if (color == ConsoleColor.Cyan)
66
+                {
67
+                    str = $"{(char)27}[1;46m";
68
+                }
69
+                if (color == ConsoleColor.Gray)
70
+                {
71
+                    str = $"{(char)27}[1;47m";
72
+                }
73
+                Console.Write(str);
74
+            }
75
+        }
76
+
77
+        public void Clear()
78
+        {
79
+            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
80
+            {
81
+                Console.Clear();
82
+            }
83
+            else
84
+            {
85
+                Console.WriteLine();
86
+            }
87
+        }
88
+
89
+        public void Update()
90
+        {
91
+            Clear();
92
+            var bgColor = Console.BackgroundColor;
93
+            for (var x = 0; x < _environment.GetWidth(); ++x)
94
+            {
95
+                Console.Write("|");
96
+                for (var y = 0; y < _environment.GetHeight(); ++y)
97
+                {
98
+                    var state = _environment.GetRoomState(x, y);
99
+                    ConsoleColor? color = null;
100
+                    if (state == RoomState.Clean)
101
+                    {
102
+                        color = null;
103
+                    }
104
+                    else if (state == RoomState.Dust)
105
+                    {
106
+                        color = ConsoleColor.Gray;
107
+                    }
108
+                    else if (state == RoomState.Jewel)
109
+                    {
110
+                        color = ConsoleColor.Cyan;
111
+                    }
112
+                    else if (state == RoomState.Unknown)
113
+                    {
114
+                        color = ConsoleColor.Yellow;
115
+                    }
116
+                    else if (state == (RoomState.Jewel | RoomState.Dust))
117
+                    {
118
+                        color = ConsoleColor.Red;
119
+                    }
120
+                    SetBackgroundColor(color);
121
+                    Console.Write(_agEngineEffector.IsInPosition(x, y) ? "X" : " ");
122
+                    SetBackgroundColor(null);
123
+                }
124
+                Console.WriteLine("|");
125
+            }
126
+            SetBackgroundColor(bgColor);
127
+            Console.WriteLine($"Battery: {_agBatterySensor.Spent}");
128
+            Console.WriteLine($"Vaccumed: {_agVaccumSensor.Vaccumed}");
129
+            Console.WriteLine($"Picked: {_agPickedSensor.Picked}");
130
+        }
131
+    }
132
+}

+ 24
- 0
uqac-ia-aspirobot/project.json View File

1
+{
2
+  "version": "1.0.0-*",
3
+  "buildOptions": {
4
+    "debugType": "portable",
5
+    "emitEntryPoint": true
6
+  },
7
+  "dependencies": {
8
+      "Microsoft.Extensions.DependencyInjection": "1.1.0",
9
+      "Microsoft.Extensions.Options": "1.1.0",
10
+      "System.IO.Pipes": "4.3.0"
11
+  },
12
+  "frameworks": {
13
+    "netcoreapp1.0": {
14
+      "dependencies": {
15
+        "Microsoft.NETCore.App": {
16
+          "type": "platform",
17
+          "version": "1.0.1"
18
+        }
19
+      },
20
+      "imports": "dnxcore50"
21
+
22
+    }
23
+  }
24
+}

+ 22
- 0
uqac-ia-aspirobot/uqac-ia-aspirobot.xproj View File

1
+<?xml version="1.0" encoding="utf-8"?>
2
+<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3
+  <PropertyGroup>
4
+    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5
+    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6
+  </PropertyGroup>
7
+
8
+  <Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''"  />
9
+  <PropertyGroup Label="Globals">
10
+    <ProjectGuid>{2A192A8A-B6F4-4752-A622-04C7A4AF0EE5}</ProjectGuid>
11
+    <ProjectTypeGuids>{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}</ProjectTypeGuids>
12
+    <RootNamespace>uqac_ia_aspirobot</RootNamespace>
13
+    <BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
14
+    <OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
15
+    <TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
16
+  </PropertyGroup>
17
+
18
+  <PropertyGroup>
19
+    <SchemaVersion>2.0</SchemaVersion>
20
+  </PropertyGroup>
21
+  <Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
22
+</Project>

Loading…
Cancel
Save