Skip to content

Commit 4aac677

Browse files
committed
Merged PR 26: Final Refactors
Related work items: #206
2 parents 460ff14 + 0e2cc26 commit 4aac677

File tree

25 files changed

+436
-122
lines changed

25 files changed

+436
-122
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/.idea
22
**/.vscode
3+
build-Pacman*

CppClient/Game/Game.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ DISTFILES += \
4949
HEADERS += \
5050
ks/commands.h \
5151
ks/models.h \
52-
ai.h
52+
ai.h \
53+
simple_ai.h

CppClient/Game/ai.cpp

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

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

6+
#include "simple_ai.h"
7+
78
using namespace std;
89
using namespace koala::chillin::client;
910
using namespace ks::models;
@@ -12,35 +13,55 @@ using namespace ks::commands;
1213

1314
AI::AI(World *world): RealtimeAI<World*>(world)
1415
{
15-
srand(time(0));
16+
simple_ai::ai = this;
1617
}
1718

1819
AI::~AI()
1920
{
20-
if (board)
21-
{
22-
for (int i = 0; i < world->height(); i++)
23-
delete[] board[i];
24-
delete[] board;
25-
}
2621
}
2722

2823
void AI::initialize()
2924
{
3025
cout << "initialize" << endl;
26+
27+
simple_ai::initialize(
28+
world->width(),
29+
world->height(),
30+
world->ref_scores()[mySide],
31+
world->ref_scores()[otherSide],
32+
world->ref_board(),
33+
world->ref_pacman(),
34+
&world->ref_ghosts()[0],
35+
world->ref_ghosts().size(),
36+
world->ref_constants(),
37+
mySide,
38+
otherSide,
39+
currentCycle,
40+
cycleDuration
41+
);
3142
}
3243

3344
void AI::decide()
3445
{
3546
cout << "decide" << endl;
36-
}
3747

38-
int AI::getRandInt(int start, int end)
39-
{
40-
return (rand() % (end - start + 1)) + start;
48+
simple_ai::decide(
49+
world->width(),
50+
world->height(),
51+
world->ref_scores()[mySide],
52+
world->ref_scores()[otherSide],
53+
world->ref_board(),
54+
world->ref_pacman(),
55+
&world->ref_ghosts()[0],
56+
world->ref_ghosts().size(),
57+
world->ref_constants(),
58+
mySide,
59+
otherSide,
60+
currentCycle,
61+
cycleDuration
62+
);
4163
}
4264

43-
4465
void AI::sendCommand(ks::KSObject *command)
4566
{
4667
BaseAI::sendCommand(command);

CppClient/Game/ai.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
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();

CppClient/Game/gamecfg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"": "creating a new decision thread each time a snapshot of game is received from server",
1616
"create_new_thread": true,
1717
"agent_name": "0",
18-
"team_nickname": "BabyKnight",
18+
"team_nickname": "Team1",
1919
"token": "team_id1-xx"
2020
}
2121
}

CppClient/Game/ks/commands.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class KSObject
2828
namespace commands
2929
{
3030

31-
enum class ECommandDirection
31+
enum ECommandDirection
3232
{
3333
Up = 0,
3434
Right = 1,

CppClient/Game/ks/models.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class KSObject
2828
namespace models
2929
{
3030

31-
enum class ECell
31+
enum ECell
3232
{
3333
Empty = 0,
3434
Food = 1,
@@ -37,7 +37,7 @@ enum class ECell
3737
};
3838

3939

40-
enum class EDirection
40+
enum EDirection
4141
{
4242
Up = 0,
4343
Right = 1,

CppClient/Game/simple_ai.h

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,63 @@ namespace simple_ai {
1919
AI *ai;
2020

2121

22-
void move(int bananaId, int dir)
22+
const int CELL_EMPTY = ECell::Empty;
23+
const int CELL_FOOD = ECell::Food;
24+
const int CELL_SUPER_FOOD = ECell::SuperFood;
25+
const int CELL_WALL = ECell::Wall;
26+
27+
const int DIR_UP = EDirection::Up;
28+
const int DIR_RIGHT = EDirection::Right;
29+
const int DIR_DOWN = EDirection::Down;
30+
const int DIR_LEFT = EDirection::Left;
31+
32+
33+
void changePacmanDirection(int dir)
2334
{
24-
ai->sendCommand(&move);
35+
ChangePacmanDirection changeDir;
36+
changeDir.direction((ECommandDirection) dir);
37+
ai->sendCommand(&changeDir);
2538
}
2639

40+
void changeGhostDirection(int id, int dir)
41+
{
42+
ChangeGhostDirection changeDir;
43+
changeDir.id(id);
44+
changeDir.direction((ECommandDirection) dir);
45+
ai->sendCommand(&changeDir);
46+
}
47+
48+
2749
void initialize(
28-
int width, int height, int myScore, int otherScore, int **board,
29-
Banana myBananas[], int myBananasCount, Banana otherBananas[], int otherBananasCount,
30-
PowerUp powerups[], int powerupsCount, int enter_score,
50+
int width, int height, int myScore, int otherScore, std::vector<std::vector<ECell>>& board,
51+
Pacman& Pacman, Ghost ghosts[], int ghostsCount,
52+
Constants& constants,
3153
string mySide, string otherSide, int currentCycle, float cycleDuration)
3254
{
3355
}
3456

3557

3658
void decide(
37-
int width, int height, int myScore, int otherScore, int **board,
38-
Banana myBananas[], int myBananasCount, Banana otherBananas[], int otherBananasCount,
39-
PowerUp powerups[], int powerupsCount, int enter_score,
59+
int width, int height, int myScore, int otherScore, std::vector<std::vector<ECell>>& board,
60+
Pacman& Pacman, Ghost ghosts[], int ghostsCount,
61+
Constants& constants,
4062
string mySide, string otherSide, int currentCycle, float cycleDuration)
4163
{
42-
64+
// Write your own AI code here
65+
if (mySide == "Pacman")
66+
{
67+
changePacmanDirection(DIR_UP);
68+
}
69+
else if (mySide == "Ghost")
70+
{
71+
for (int i = 0; i < ghostsCount; i++)
72+
{
73+
Ghost ghost = ghosts[i];
74+
changeGhostDirection(ghost.id(), DIR_UP);
75+
}
76+
}
4377
}
4478

4579
}
4680

47-
#endif // SIMPLE_AI_H
81+
#endif // SIMPLE_AI_H

PythonClient/ai.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,33 @@
11
# -*- coding: utf-8 -*-
22

3-
# python imports
4-
import random
5-
63
# chillin imports
74
from chillin_client import RealtimeAI
85

96
# project imports
10-
from ks.models import World, Pacman, Ghost, Constants, ECell, EDirection
11-
from ks.commands import ChangePacmanDirection, ChangeGhostDirection, ECommandDirection
7+
import simple_ai
8+
from ks.models import World
129

1310

1411
class AI(RealtimeAI):
1512

1613
def __init__(self, world):
1714
super(AI, self).__init__(world)
15+
simple_ai.ai = self
1816

1917

2018
def initialize(self):
2119
print('initialize')
2220

21+
world = self.world
22+
simple_ai.initialize(world.width, world.height, world.scores[self.my_side], world.scores[self.other_side],
23+
world.board, world.pacman, world.ghosts, world.constants,
24+
self.my_side, self.other_side, self.current_cycle, self.cycle_duration)
25+
2326

2427
def decide(self):
2528
print('decide')
26-
27-
if self.my_side == 'Pacman':
28-
direction = random.choice([
29-
ECommandDirection.Up,
30-
ECommandDirection.Right,
31-
ECommandDirection.Down,
32-
ECommandDirection.Left
33-
])
34-
self.send_command(ChangePacmanDirection(direction=direction))
35-
36-
if self.my_side == 'Ghost':
37-
for ghost in self.world.ghosts:
38-
direction = random.choice([
39-
ECommandDirection.Up,
40-
ECommandDirection.Right,
41-
ECommandDirection.Down,
42-
ECommandDirection.Left
43-
])
44-
self.send_command(ChangeGhostDirection(direction=direction, id=ghost.id))
29+
30+
world = self.world
31+
simple_ai.decide(world.width, world.height, world.scores[self.my_side], world.scores[self.other_side],
32+
world.board, world.pacman, world.ghosts, world.constants,
33+
self.my_side, self.other_side, self.current_cycle, self.cycle_duration)

PythonClient/gamecfg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"": "creating a new decision thread each time a snapshot of game is received from server",
1919
"create_new_thread": true,
2020
"agent_name": "0",
21-
"team_nickname": "BabyKnight",
21+
"team_nickname": "Team1",
2222
"token": "team_id1-xx"
2323
}
2424
}

0 commit comments

Comments
 (0)