Skip to content

Commit 9f8fcd7

Browse files
committed
Merged PR 28: Merge branch Dev into Master
2 parents 05199ac + 1f8397a commit 9f8fcd7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+10036
-6914
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
.idea/
3+
.idea/*
4+
**/.vscode
5+
venv*/
6+
build-SearchAndDefuse*

CppClient/Game/ai.cpp

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "ai.h"
22

3-
#include <ctime>
43
#include <vector>
54
#include <iostream>
65

@@ -10,19 +9,12 @@ using namespace ks::models;
109
using 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

1816
AI::~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

2820
void AI::initialize()
@@ -33,15 +25,108 @@ void AI::initialize()
3325
void 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
}

CppClient/Game/ai.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@
77
#include "ks/commands.h"
88

99

10-
class AI : public koala::chillin::client::TurnbasedAI<ks::models::World*>
10+
class AI : public koala::chillin::client::RealtimeAI<ks::models::World*>
1111
{
12-
private:
13-
int **board;
14-
int getRandInt(int start, int end);
15-
1612
public:
1713
AI(ks::models::World *world);
1814
~AI();
1915

2016
void initialize();
2117
void decide();
22-
void sendCommand(ks::KSObject *command);
18+
19+
void move(int agentId, ks::commands::ECommandDirection moveDirection);
20+
void plant(int agentId, ks::commands::ECommandDirection bombsiteDirection);
21+
void defuse(int agentId, ks::commands::ECommandDirection bombsiteDirection);
22+
23+
private:
24+
std::tuple<bool, ks::commands::ECommandDirection> findBombsiteDirection(ks::models::Position position);
2325
};
2426

2527
#endif // AI_H

0 commit comments

Comments
 (0)