-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartificialIntelligence.py
More file actions
69 lines (61 loc) · 1.9 KB
/
artificialIntelligence.py
File metadata and controls
69 lines (61 loc) · 1.9 KB
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
from tank import Tank
from bullet import Bullet
from obstacle import Obstacle
from base import Base
from flag import Flag
import random
import math
SIGHT_ENEMY_RANGE = 600
FIRE_ENEMY_RANGE = 300
# This AI is probably be too difficult for human player to beat
class AI():
def __init__(self, color, gameObjects):
self.color = color
self.myTanks = []
self.enemyTanks = []
self.myFlag = None
self.enemyFlags = []
self.myBase = None
self.enemyBases = []
for go in gameObjects:
if (isinstance(go, Tank)):
if go.color == self.color:
self.myTanks.append(go)
else:
self.enemyTanks.append(go)
elif (isinstance(go, Flag)):
if go.color == self.color:
self.myFlag = go
else:
self.enemyFlags.append(go)
elif (isinstance(go, Base)):
if go.color == self.color:
self.myBase = go
else:
self.enemyBases.append(go)
def control(self):
for t in self.myTanks:
if (t.respawn):
continue
if (isinstance(t.flag, Flag)):
t.setDestination(self.myBase.position)
continue
# find closest tank
tankTarget = {'dist': math.inf, 'enemyTank': None}
for e in self.enemyTanks:
dist = math.hypot(e.position[0] - t.position[0], e.position[1] - t.position[1])
if (not e.respawn and dist < tankTarget['dist']):
tankTarget['enemyTank'] = e
tankTarget['dist'] = dist
random.shuffle(self.enemyFlags)
attackMode = True
for f in self.enemyFlags:
if(f.pickedUpBy is None):
t.setDestination(f.position)
attackMode = False
continue
if((tankTarget['enemyTank'] is not None and (dist < SIGHT_ENEMY_RANGE or attackMode))):
t.setDestination(tankTarget['enemyTank'].position)
if(tankTarget['dist'] < FIRE_ENEMY_RANGE):
t.fire()
continue