forked from holderlb/wumpus-world-simulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyAgent.cc
92 lines (83 loc) · 2.05 KB
/
PyAgent.cc
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
// PyAgent.cc
#include "PyAgent.h"
using namespace std;
PyObject * myPyObject_CallObject(PyObject *callable_object, PyObject *args) {
PyObject *pValue = PyObject_CallObject(callable_object, args);
if (pValue == NULL) {
PyErr_Print();
exit(1);
}
return pValue;
}
Agent::Agent (PyObject* module)
{
pModule = module;
PyObject *pFunc = PyObject_GetAttrString(pModule, "PyAgent_Constructor");
myPyObject_CallObject(pFunc, NULL);
Py_DECREF(pFunc);
}
Agent::~Agent ()
{
PyObject *pFunc = PyObject_GetAttrString(pModule, "PyAgent_Destructor");
myPyObject_CallObject(pFunc, NULL);
Py_DECREF(pFunc);
}
void Agent::Initialize ()
{
PyObject *pFunc = PyObject_GetAttrString(pModule, "PyAgent_Initialize");
myPyObject_CallObject(pFunc, NULL);
Py_DECREF(pFunc);
}
Action Agent::Process (Percept& percept)
{
PyObject *pValue;
PyObject *pArgs = PyTuple_New(5);
if (percept.Stench) {
pValue = PyInt_FromLong(1);
} else {
pValue = PyInt_FromLong(0);
}
PyTuple_SetItem(pArgs, 0, pValue);
if (percept.Breeze) {
pValue = PyInt_FromLong(1);
} else {
pValue = PyInt_FromLong(0);
}
PyTuple_SetItem(pArgs, 1, pValue);
if (percept.Glitter) {
pValue = PyInt_FromLong(1);
} else {
pValue = PyInt_FromLong(0);
}
PyTuple_SetItem(pArgs, 2, pValue);
if (percept.Bump) {
pValue = PyInt_FromLong(1);
} else {
pValue = PyInt_FromLong(0);
}
PyTuple_SetItem(pArgs, 3, pValue);
if (percept.Scream) {
pValue = PyInt_FromLong(1);
} else {
pValue = PyInt_FromLong(0);
}
PyTuple_SetItem(pArgs, 4, pValue);
PyObject *pFunc = PyObject_GetAttrString(pModule, "PyAgent_Process");
pValue = myPyObject_CallObject(pFunc, pArgs);
Action action = (Action) PyInt_AsLong(pValue);
Py_DECREF(pValue);
Py_DECREF(pArgs);
Py_DECREF(pFunc);
return action;
}
void Agent::GameOver (int score)
{
PyObject *pArgs = PyTuple_New(1);
PyObject *pValue = PyInt_FromLong(score);
PyTuple_SetItem(pArgs, 0, pValue);
PyObject *pFunc = PyObject_GetAttrString(pModule, "PyAgent_GameOver");
myPyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
//Py_DECREF(pValue);
Py_DECREF(pFunc);
}