-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.py
145 lines (122 loc) · 4.13 KB
/
AI.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#Behold the AI for CS 1.6
import math
strikerLine_y = 900;
pocket1_x=0;
pocket1_y=0;
pocket2_x=1000;
pocket2_y=0;
def isOnLine(x):
if x>100 and x <900:
return True;
if x>80 and x<85:
return True;
if x>915 and x<920:
return True;
return False;
class Coin:
#Class that takes care of coins
radius =10;
def __init__(self,xcord,ycord):
self.x = xcord
self.y = ycord
self.slope1 = (self.y-pocket1_y)/float((self.x-pocket1_x))
self.slope2 = (self.y-pocket2_y)/float((self.x-pocket2_x))
self.intercept1= self.y - self.slope1*self.x
self.intercept2= self.y - self.slope2*self.x
def getx(self):
return self.x
def gety(self):
return self.y
def getCord(self):
return (self.x,self.y);
def setCord(self,xcord,ycord):
self.x=xcord;
self.y=ycord;
self.slope1 = (self.y-pocket1_y)/float((self.x-pocket1_x))
self.slope2 = (self.y-pocket2_y)/float((self.x-pocket2_x))
self.intercept1= self.y - self.slope1*self.x
self.intercept2= self.y - self.slope2*self.x
class WhiteCoin(Coin):
def __init__(self,xcord,ycord):
Coin.__init__(self,xcord,ycord)
def printCoin(self):
print("I am a " + "White" + " Coin at: " + str(Coin.getx(self)) +" , " + str(Coin.gety(self)))
class BlackCoin(Coin):
def __init__(self,xcord,ycord):
Coin.__init__(self,xcord,ycord)
def printCoin(self):
print("I am a " + "Black" + " Coin at: " + str(Coin.getx(self)) +" , " + str(Coin.gety(self)))
class RedCoin(Coin):
def __init__(self,xcord,ycord):
Coin.__init__(self,xcord,ycord)
def printCoin(self):
print("I am a " + "Queen" + " Coin at: " + str(Coin.getx(self)) +" , " + str(Coin.gety(self)))
#Creating my beloved coins at ORIGIN
listOfWhiteCoins = [WhiteCoin(500,500) for i in range (9)];
listOfBlackCoins = [BlackCoin(500,500) for i in range (9)];
listOfRedCoins = [RedCoin(500,500) for i in range (1)];
#Printing my coins
def printAllCoins():
for i in range (9):
listOfWhiteCoins[i].printCoin()
listOfBlackCoins[i].printCoin()
listOfRedCoins[0].printCoin()
#Testing out some coordinates
from random import randint
for j in range (9):
i=j+1
listOfWhiteCoins[j].setCord(randint(1,999),randint(1,999))
listOfBlackCoins[j].setCord(randint(1,999),randint(1,999))
listOfRedCoins[0].setCord(randint(1,999),randint(1,999))
printAllCoins();
def isCoinInWay(coinToPot,striker_x,pocketnumber):
if pocketnumber==1:
slope=coinToPot.slope1;
intercept=coinToPot.intercept1;
if pocketnumber==2:
slope=coinToPot.slope2;
intercept=coinToPot.intercept2;
intercept1=intercept-(2*coinToPot.radius)*math.sqrt(1 + (slope**2));
intercept2=intercept+(2*coinToPot.radius)*math.sqrt(1 + (slope**2));
for coin in listOfWhiteCoins:
if(coin!=coinToPot and coin.gety()<coinToPot.radius +strikerLine_y):
if ((coin.gety()-slope* coin.getx()-intercept1)*(coin.gety()-slope* coin.getx()-intercept2)) <= 0:
return False
for coin in listOfBlackCoins:
if(coin!=coinToPot and coin.gety()<coinToPot.radius +strikerLine_y):
if ((coin.gety()-slope* coin.getx()-intercept1)*(coin.gety()-slope* coin.getx()-intercept2)) <= 0:
return False
for coin in listOfRedCoins:
if(coin!=coinToPot and coin.gety()<coinToPot.radius +strikerLine_y):
if ((coin.gety()-slope* coin.getx()-intercept1)*(coin.gety()-slope* coin.getx()-intercept2)) <= 0:
return False
return True;
def directShot(coinToPot):
strikerLine_x1 = (strikerLine_y - coinToPot.gety())/coinToPot.slope1 + coinToPot.getx();
if(isOnLine(strikerLine_x1)):
if(isCoinInWay(coinToPot,strikerLine_x1,1)):
return True,strikerLine_x1;
strikerLine_x2 = (strikerLine_y - coinToPot.gety())/coinToPot.slope2 + coinToPot.getx();
if(isOnLine(strikerLine_x2)):
if(isCoinInWay(coinToPot,strikerLine_x2,2)):
return True,strikerLine_x2;
return False,0;
#Testing
for coin in listOfWhiteCoins:
boolv,x = directShot(coin)
if(boolv):
print("Shoot from position "+ str(x));
else :
print("Not possible to shoot");
for coin in listOfBlackCoins:
boolv,x = directShot(coin)
if(boolv):
print("Shoot from position "+ str(x));
else :
print("Not possible to shoot");
for coin in listOfRedCoins:
boolv,x = directShot(coin)
if(boolv):
print("Shoot from position "+ str(x));
else :
print("Not possible to shoot");