forked from ivogeorg/ucd-csci2312-pa4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simple.cpp
72 lines (61 loc) · 2.26 KB
/
Simple.cpp
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
//
// Created by ryanm on 4/12/2016.
//
#include <chrono>
#include "Game.h"
#include "Gaming.h"
#include "Simple.h"
namespace Gaming {
const char Simple::SIMPLE_ID = 'S';
Simple::Simple(const Game &g, const Position &p, double energy) : Agent(g, p, energy) {
}
Simple::~Simple() {
}
void Simple::print(std::ostream &os) const {
os << SIMPLE_ID << __id;
}
ActionType Simple::takeTurn(const Surroundings &s) const {
std::vector<int> positions;
unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine rnd(seed);
for (int i = 0; i < 9; ++i) {
//std::cout << "Surr of " << __id << ": i: " << i << ": " << s.array[i] << std::endl;
if (s.array[i] == PieceType::ADVANTAGE || s.array[i] == PieceType::FOOD) {
//std::cout << "Pushing advantage" << std::endl;
positions.push_back(i);
//std::cout << "size of positions: " << positions.size() << std::endl;
}
}
//Check for Empty
if (positions.size() == 0) {
for (int i = 0; i < 9; ++i) {
//std::cout << "Surrounding[i]: " << s.array[i] << std::endl;
if (s.array[i] == PieceType::EMPTY) {
positions.push_back(i);
//std::cout << "Available position: " << i << std::endl;
}
}
}
if (positions.size() > 0) {
int posIndex = positions[rnd() % positions.size()];
if (positions.size() == 1) posIndex = positions[0];
//std::cout << "Chosen Position: " << posIndex << std::endl;
ActionType ac;
switch (posIndex) {
case 0: ac = NW; break;
case 1: ac = N; break;
case 2: ac = NE; break;
case 3: ac = W; break;
case 4: ac = STAY; break;
case 5: ac = E; break;
case 6: ac = SW; break;
case 7: ac = S; break;
case 8: ac = SE; break;
default: ac = STAY;
}
//std::cout << "Chosen Action: " << ac << std::endl;
return (ac);
}
return ActionType::STAY;
}
}