11#include " ai.h"
22
3- #include < ctime>
43#include < vector>
54#include < iostream>
65
@@ -10,19 +9,12 @@ using namespace ks::models;
109using namespace ks ::commands;
1110
1211
13- AI::AI (World *world): TurnbasedAI <World*>(world)
12+ AI::AI (World *world): RealtimeAI <World*>(world)
1413{
15- srand (time (0 ));
1614}
1715
1816AI::~AI ()
1917{
20- if (board)
21- {
22- for (int i = 0 ; i < world->height (); i++)
23- delete[] board[i];
24- delete[] board;
25- }
2618}
2719
2820void AI::initialize ()
@@ -33,15 +25,108 @@ void AI::initialize()
3325void AI::decide ()
3426{
3527 cout << " decide" << endl;
28+
29+ if (this ->mySide == " Police" )
30+ {
31+ for (int i = 0 ; i < this ->world ->ref_polices ().size (); i++)
32+ {
33+ Police police = this ->world ->ref_polices ()[i];
34+ if (police.status () == EAgentStatus::Dead)
35+ continue ;
36+
37+ bool doingBombOperation = police.defusionRemainingTime () != -1 ;
38+ if (doingBombOperation)
39+ {
40+ cout << " Agent[" << police.id () << " ]: " << " Continue Bomb Operation" << endl;
41+ continue ;
42+ }
43+
44+ auto bombsiteDirection = findBombsiteDirection (police.position ());
45+ if (std::get<0 >(bombsiteDirection) == false )
46+ {
47+ cout << " Agent[" << police.id () << " ]: " << " Random Move" << endl;
48+ move (police.id (), ECommandDirection::Left);
49+ }
50+ else
51+ {
52+ cout << " Agent[" << police.id () << " ]: " << " Start Bomb Operation" << endl;
53+ defuse (police.id (), std::get<1 >(bombsiteDirection));
54+ }
55+ }
56+ }
57+ else
58+ {
59+ for (int i = 0 ; i < this ->world ->ref_terrorists ().size (); i++)
60+ {
61+ Terrorist terrorist = this ->world ->ref_terrorists ()[i];
62+ if (terrorist.status () == EAgentStatus::Dead)
63+ continue ;
64+
65+ bool doingBombOperation = terrorist.plantingRemainingTime () != -1 ;
66+ if (doingBombOperation)
67+ {
68+ cout << " Agent[" << terrorist.id () << " ]: " << " Continue Bomb Operation" << endl;
69+ continue ;
70+ }
71+
72+ auto bombsiteDirection = findBombsiteDirection (terrorist.position ());
73+ if (std::get<0 >(bombsiteDirection) == false )
74+ {
75+ cout << " Agent[" << terrorist.id () << " ]: " << " Random Move" << endl;
76+ move (terrorist.id (), ECommandDirection::Down);
77+ }
78+ else
79+ {
80+ cout << " Agent[" << terrorist.id () << " ]: " << " Start Bomb Operation" << endl;
81+ plant (terrorist.id (), std::get<1 >(bombsiteDirection));
82+ }
83+ }
84+ }
3685}
3786
38- int AI::getRandInt (int start, int end )
87+ void AI::move (int agentId, ECommandDirection moveDirection )
3988{
40- return (rand () % (end - start + 1 )) + start;
89+ Move cmd;
90+ cmd.id (agentId);
91+ cmd.direction (moveDirection);
92+ this ->sendCommand (&cmd);
4193}
4294
95+ void AI::plant (int agentId, ECommandDirection bombsiteDirection)
96+ {
97+ PlantBomb cmd;
98+ cmd.id (agentId);
99+ cmd.direction (bombsiteDirection);
100+ this ->sendCommand (&cmd);
101+ }
43102
44- void AI::sendCommand (ks::KSObject *command )
103+ void AI::defuse ( int agentId, ECommandDirection bombsiteDirection )
45104{
46- BaseAI::sendCommand (command);
105+ DefuseBomb cmd;
106+ cmd.id (agentId);
107+ cmd.direction (bombsiteDirection);
108+ this ->sendCommand (&cmd);
109+ }
110+
111+
112+
113+ std::tuple<bool , ECommandDirection> AI::findBombsiteDirection (Position position)
114+ {
115+ if ((this ->world ->board ()[position.y () - 1 ][position.x ()] >= ECell::SmallBombSite) &&
116+ (this ->world ->board ()[position.y () - 1 ][position.x ()] <= ECell::VastBombSite))
117+ return std::make_tuple (true , ECommandDirection::Up);
118+
119+ if ((this ->world ->board ()[position.y ()][position.x () + 1 ] >= ECell::SmallBombSite) &&
120+ (this ->world ->board ()[position.y ()][position.x () + 1 ] <= ECell::VastBombSite))
121+ return std::make_tuple (true , ECommandDirection::Right);
122+
123+ if ((this ->world ->board ()[position.y () + 1 ][position.x ()] >= ECell::SmallBombSite) &&
124+ (this ->world ->board ()[position.y () + 1 ][position.x ()] <= ECell::VastBombSite))
125+ return std::make_tuple (true , ECommandDirection::Down);
126+
127+ if ((this ->world ->board ()[position.y ()][position.x () - 1 ] >= ECell::SmallBombSite) &&
128+ (this ->world ->board ()[position.y ()][position.x () - 1 ] <= ECell::VastBombSite))
129+ return std::make_tuple (true , ECommandDirection::Left);
130+
131+ return std::make_tuple (false , ECommandDirection::Up);
47132}
0 commit comments