-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
195 lines (156 loc) · 6.09 KB
/
Program.cs
File metadata and controls
195 lines (156 loc) · 6.09 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace CocktailSort
{
// Не пишите всё в одном namespace! (.cs файле)
// Этот код просто пример реализации темы!
internal class Program
{
static void Main(string[] args)
{
RenderSystem renderSystem = new RenderSystem();
renderSystem.AddObject(new GameObject("Дерево", 1.2f, GameObjectType.Tree));
renderSystem.AddObject(new GameObject("Дерево", 3.7f, GameObjectType.Tree));
renderSystem.AddObject(new GameObject("Камень", 5.1f, GameObjectType.Stone));
renderSystem.AddObject(new GameObject("Камень", 7.3f, GameObjectType.Stone));
renderSystem.AddObject(new GameObject("Камень", 2.8f, GameObjectType.Stone));
renderSystem.AddObject(new GameObject("Игрок", 4.5f, GameObjectType.Player));
renderSystem.AddObject(new GameObject("NPC", 6.2f, GameObjectType.NPC));
renderSystem.AddObject(new GameObject("NPC", 8.0f, GameObjectType.NPC));
renderSystem.AddObject(new GameObject("Предмет", 9.1f, GameObjectType.Item));
renderSystem.AddObject(new GameObject("Предмет", 0.5f, GameObjectType.Item));
renderSystem.AddObject(new GameObject("Предмет", 5.9f, GameObjectType.Item));
renderSystem.Shuffle();
renderSystem.DisplayOrder("Неотсортированное состояние");
renderSystem.CocktailSort();
renderSystem.DisplayOrder("Отсортированное состояние");
renderSystem.SimulateMovement();
renderSystem.DisplayOrder("Почти отсортированное состояние (до сортировки)");
renderSystem.CocktailSort();
renderSystem.DisplayOrder("После повторной сортировки");
}
}
public enum GameObjectType
{
Player,
Tree,
Stone,
NPC,
Item
}
public class GameObject
{
public int Id { get; private set; }
public string Name { get; set; }
public float Y { get; set; }
public GameObjectType Type { get; set; }
private static int _nextId = 0;
public GameObject(string name, float y, GameObjectType type)
{
Id = _nextId++;
Name = name ?? "Unknown";
Y = y;
Type = type;
}
public override string ToString()
{
return $"[ID:{Id}] {Type}:{Name} (Y={Y:F2})";
}
}
public class RenderSystem
{
private List<GameObject> _objects;
public RenderSystem()
{
_objects = new List<GameObject>();
}
public void AddObject(GameObject obj)
{
_objects.Add(obj);
}
public void CocktailSort()
{
int left = 0;
int right = _objects.Count - 1;
int swapsCount = 0;
int comparisonCount = 0;
Stopwatch sortTimer = new Stopwatch();
sortTimer.Start();
while (left < right)
{
bool swapped = false;
for (int i = left; i < right; i++)
{
if (_objects[i].Y > _objects[i + 1].Y)
{
Swap(i, i + 1);
swapsCount++;
swapped = true;
}
comparisonCount++;
}
right--;
if (!swapped)
break;
swapped = false;
for (int i = right; i > left; i--)
{
if (_objects[i].Y < _objects[i - 1].Y)
{
Swap(i, i - 1);
swapsCount++;
swapped = true;
}
comparisonCount++;
}
left++;
if (!swapped)
break;
}
sortTimer.Stop();
Console.WriteLine($"\nCocktailSort: кол-во сравнений - {comparisonCount}, обменов - {swapsCount}. Время сортировки: {sortTimer.ElapsedTicks} тиков.");
}
public void Swap(int i, int j)
{
var temp = _objects[i];
_objects[i] = _objects[j];
_objects[j] = temp;
}
public void DisplayOrder(string title)
{
Console.WriteLine($"\n{title}");
Console.WriteLine(new string('-', 50));
for (int i = 0; i < _objects.Count; i++)
{
Console.WriteLine($"{i,2}. {_objects[i]}");
}
Console.WriteLine(new string('-', 50));
}
public void Shuffle()
{
Random rnd = new Random();
for (int i = 0; i < _objects.Count; i++)
{
int j = rnd.Next(i, _objects.Count);
var temp = _objects[i];
_objects[i] = _objects[j];
_objects[j] = temp;
}
}
public void SimulateMovement()
{
Random rnd = new Random();
for (int i = 0; i < _objects.Count; i++)
{
if (_objects[i].Type == GameObjectType.Player || _objects[i].Type == GameObjectType.NPC)
{
float oldY = _objects[i].Y;
float delta = (float)(rnd.NextDouble() * 2 - 1) * 0.5f;
_objects[i].Y = Math.Max(0, Math.Min(10, _objects[i].Y + delta));
Console.WriteLine($"{_objects[i].Type} переместился: Y {oldY:F2} -> {_objects[i].Y:F2}");
}
}
}
}
}