source: src/stateAnalysis.py @ 501

Last change on this file since 501 was 501, checked in by ecoughlan, 3 years ago

minor changes to stateAnalysis, still needs to be cleaned up

File size: 1.8 KB
Line 
1'''
2Created on Mar 24, 2012
3
4@author: TechTrek
5'''
6
7import os
8
9from event import Event
10from heuristic import Heuristic, loadHeuristic
11from globals import *
12
13
14class StateAnalysis:
15    def __init__(self):
16        """
17            initializes the StateAnalysis class
18           
19            input type: none
20        """
21        self.events = []
22        self.heuristics = []
23        self.loadAllHeuristics()
24
25    def loadAllHeuristics(self):
26        """
27            loads all heuristics into self.heuristics for use in the game
28           
29            input type: none
30            output type: none
31        """
32        heurFiles = os.listdir(heurDir)
33        for heur in heurFiles:
34            fileExt = os.path.splitext(heur)[1]
35            if fileExt == heurExt:
36                heuristic = loadHeuristic(heur)
37                if heuristic is not False:
38                    self.heuristics.append(heuristic)
39
40    def addEvent(self, event):
41        self.events.append(event)
42
43    def addPriorityEvent(self, event):
44        self.events.insert(0, event)
45
46    def analyze(self, state):
47        heurIndex = -1
48        for i, heuristic in enumerate(self.heuristics):
49            if heuristic.trigger(state):
50                print "Heuristic %s triggered" % heuristic.name
51                if heuristic.priority:
52                    heurIndex = i
53                    self.addPriorityEvent(heuristic.event)
54                    break
55                else:
56                    if heurIndex < 0:
57                        heurIndex = i
58                    self.addEvent(heuristic.event)
59        if heurIndex >= 0 and not self.heuristics[heurIndex].repeat:
60            self.heuristics.pop(heurIndex)
61
62    def popAndFlushEvent(self):
63        if len(self.events) != 0:
64            priority = self.events[0]
65            self.events = []
66            return priority
67        else:
68            return None
Note: See TracBrowser for help on using the repository browser.