-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathagentSampleImpl.cs
More file actions
executable file
·172 lines (152 loc) · 7.33 KB
/
agentSampleImpl.cs
File metadata and controls
executable file
·172 lines (152 loc) · 7.33 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
using System;
using Data.Realm;
using Data;
namespace CsClient
{
public class Program
{
static AgentAPI agentTomek; //nasz agent, instancja klasy AgentAPI
static int energy; //tu zapisujemy aktualną energię naszego agenta
static WorldParameters cennikSwiata; //tu zapisujemy informacje o świecie
// Nasza metoda nasłuchująca
static void Listen(String krzyczacyAgent, String komunikat) {
Console.WriteLine(krzyczacyAgent + " krzyczy " + komunikat);
}
static void Main(string[] args)
{
//powtarzamy czynnosci az nam się uda
while (true)
{
agentTomek = new AgentAPI(Listen); //tworzymy nowe AgentAPI, podając w parametrze naszą metodę nasłuchującą
// pobieramy parametry połączenia i agenta z klawiatury
Console.Write("Podaj IP serwera: ");
String ip = Console.ReadLine();
Console.Write("Podaj nazwe druzyny: ");
String groupname = Console.ReadLine();
Console.Write("Podaj haslo: ");
String grouppass = Console.ReadLine();
Console.Write("Podaj nazwe swiata: ");
String worldname = Console.ReadLine();
Console.Write("Podaj imie: ");
String imie = Console.ReadLine();
try
{
//łączymy się z serwerem. Odbieramy parametry świata i wyświetlamy je
cennikSwiata = agentTomek.Connect(ip, 6008, groupname, grouppass, worldname, imie);
Console.WriteLine(cennikSwiata.initialEnergy + " - Maksymalna energia");
Console.WriteLine(cennikSwiata.maxRecharge + " - Maksymalne doładowanie");
Console.WriteLine(cennikSwiata.sightScope + " - Zasięg widzenia");
Console.WriteLine(cennikSwiata.hearScope + " - Zasięg słyszenia");
Console.WriteLine(cennikSwiata.moveCost + " - Koszt chodzenia");
Console.WriteLine(cennikSwiata.rotateCost + " - Koszt obrotu");
Console.WriteLine(cennikSwiata.speakCost + " - Koszt mówienia");
//ustawiamy nasza energie na poczatkowa energie kazdego agenta w danym swiecie
energy = cennikSwiata.initialEnergy;
//przechodzimy do obslugi zdarzen z klawiatury. Zamiast tej funkcji wstaw logikę poruszania się twojego agenta.
KeyReader();
//na koncu rozlaczamy naszego agenta
agentTomek.Disconnect();
Console.ReadKey();
break;
}
//w przypadku mało poważnego błędu, jak podanie złego hasła, rzucany jest wyjątek NonCriticalException; zaczynamy od nowa
catch (NonCriticalException ex)
{
Console.WriteLine(ex.Message);
}
// w przypadku każdego innego wyjątku niż NonCriticalException powinniśmy zakończyć program; taki wyjątek nie powinien się zdarzyć
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Console.ReadKey();
}
}
}
//funkcja wykonywująca określone akcję w zależności od naciśniętego przycisku
static void KeyReader() {
bool loop = true;
while(loop) {
Console.WriteLine("Moja energia: " + energy);
switch(Console.ReadKey().Key) {
case ConsoleKey.Spacebar: Look();
break;
case ConsoleKey.R: Recharge();
break;
case ConsoleKey.UpArrow: StepForward();
break;
case ConsoleKey.LeftArrow: RotateLeft();
break;
case ConsoleKey.RightArrow: RotateRight();
break;
case ConsoleKey.Enter: Speak();
break;
case ConsoleKey.Q: loop = false;
break;
case ConsoleKey.D: agentTomek.Disconnect();
break;
default: Console.Beep();
break;
}
}
}
// ładujemy się
private static void Recharge()
{
int added = agentTomek.Recharge();
energy += added;
Console.WriteLine("Otrzymano " + added + " energii");
}
//wysyłamy komunikat
private static void Speak()
{
if (!agentTomek.Speak(Console.ReadLine(), 1))
Console.WriteLine("Mowienie nie powiodlo sie - brak energii");
else
energy -= cennikSwiata.speakCost;
}
//obracamy się w lewo
private static void RotateLeft()
{
if (!agentTomek.RotateLeft())
Console.WriteLine("Obrot nie powiodl sie - brak energii");
else
energy -= cennikSwiata.rotateCost;
}
//obracamy się w prawo
private static void RotateRight()
{
if (!agentTomek.RotateRight())
Console.WriteLine("Obrot nie powiodl sie - brak energii");
else
energy -= cennikSwiata.rotateCost; //musimy sami zadbać o aktualizację naszej bieżącej energii - serwer nie dostarczy nam tej informacji
}
//idziemy do przodu
private static void StepForward()
{
if (!agentTomek.StepForward())
Console.WriteLine("Wykonanie kroku nie powiodlo sie");
if (energy >= cennikSwiata.moveCost)
energy -= cennikSwiata.moveCost;//musimy sami zadbać o aktualizację naszej bieżącej energii - serwer nie dostarczy nam tej informacji
// w tym wypadku zużytą energię policzyliśmy błędnie - nie uwzględniliśmy różnicy wysokości terenu (patrz Dokumentacja)
}
private static void Look()
{
OrientedField[] pola = agentTomek.Look(); //dostajemy listę pól które widzi nasz agent
//wyświetlamy informacje o wszystkich widzianych polach
foreach (OrientedField pole in pola)
{
Console.WriteLine("-----------------------------");
Console.WriteLine("POLE " + pole.x + "," + pole.y);
Console.WriteLine("Wysokosc: " + pole.height);
if (pole.energy != 0)
Console.WriteLine("Energia: " + pole.energy);
if (pole.obstacle)
Console.WriteLine("Przeszkoda");
if (pole.agent != null)
Console.WriteLine("Agent" + pole.agent.agentname + " i jest obrocony na " + pole.agent.direction.ToString());
Console.WriteLine("-----------------------------");
}
}
}
}