├── requirement.txt ├── images ├── pyfootball.gif ├── others │ ├── ball.jpeg │ ├── ball.png │ └── footballpitch.png └── players │ ├── TeamAk.png │ ├── TeamBk.png │ ├── TeamCk.png │ ├── teamA1.png │ ├── teamA10.png │ ├── teamA11.png │ ├── teamA2.png │ ├── teamA3.png │ ├── teamA4.png │ ├── teamA5.png │ ├── teamA6.png │ ├── teamA7.png │ ├── teamA8.png │ ├── teamA9.png │ ├── teamB1.png │ ├── teamB10.png │ ├── teamB11.png │ ├── teamB2.png │ ├── teamB3.png │ ├── teamB4.png │ ├── teamB5.png │ ├── teamB6.png │ ├── teamB7.png │ ├── teamB8.png │ ├── teamB9.png │ ├── teamC1.png │ ├── teamC10.png │ ├── teamC11.png │ ├── teamC2.png │ ├── teamC3.png │ ├── teamC4.png │ ├── teamC5.png │ ├── teamC6.png │ ├── teamC7.png │ ├── teamC8.png │ ├── teamC9.png │ ├── teamD1.png │ ├── teamD10.png │ ├── teamD11.png │ ├── teamD2.png │ ├── teamD3.png │ ├── teamD4.png │ ├── teamD5.png │ ├── teamD6.png │ ├── teamD7.png │ ├── teamD8.png │ ├── teamD9.png │ └── teamDk.png ├── Scripts ├── footballProperties.py ├── PlayersProperties.py ├── regression.py └── net.py ├── README.md └── pyfootball.py /requirement.txt: -------------------------------------------------------------------------------- 1 | pygame==1.9.6 2 | numpy==1.16.2 3 | -------------------------------------------------------------------------------- /images/pyfootball.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/pyfootball.gif -------------------------------------------------------------------------------- /images/others/ball.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/others/ball.jpeg -------------------------------------------------------------------------------- /images/others/ball.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/others/ball.png -------------------------------------------------------------------------------- /images/players/TeamAk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/TeamAk.png -------------------------------------------------------------------------------- /images/players/TeamBk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/TeamBk.png -------------------------------------------------------------------------------- /images/players/TeamCk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/TeamCk.png -------------------------------------------------------------------------------- /images/players/teamA1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA1.png -------------------------------------------------------------------------------- /images/players/teamA10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA10.png -------------------------------------------------------------------------------- /images/players/teamA11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA11.png -------------------------------------------------------------------------------- /images/players/teamA2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA2.png -------------------------------------------------------------------------------- /images/players/teamA3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA3.png -------------------------------------------------------------------------------- /images/players/teamA4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA4.png -------------------------------------------------------------------------------- /images/players/teamA5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA5.png -------------------------------------------------------------------------------- /images/players/teamA6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA6.png -------------------------------------------------------------------------------- /images/players/teamA7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA7.png -------------------------------------------------------------------------------- /images/players/teamA8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA8.png -------------------------------------------------------------------------------- /images/players/teamA9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamA9.png -------------------------------------------------------------------------------- /images/players/teamB1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB1.png -------------------------------------------------------------------------------- /images/players/teamB10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB10.png -------------------------------------------------------------------------------- /images/players/teamB11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB11.png -------------------------------------------------------------------------------- /images/players/teamB2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB2.png -------------------------------------------------------------------------------- /images/players/teamB3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB3.png -------------------------------------------------------------------------------- /images/players/teamB4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB4.png -------------------------------------------------------------------------------- /images/players/teamB5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB5.png -------------------------------------------------------------------------------- /images/players/teamB6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB6.png -------------------------------------------------------------------------------- /images/players/teamB7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB7.png -------------------------------------------------------------------------------- /images/players/teamB8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB8.png -------------------------------------------------------------------------------- /images/players/teamB9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamB9.png -------------------------------------------------------------------------------- /images/players/teamC1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC1.png -------------------------------------------------------------------------------- /images/players/teamC10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC10.png -------------------------------------------------------------------------------- /images/players/teamC11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC11.png -------------------------------------------------------------------------------- /images/players/teamC2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC2.png -------------------------------------------------------------------------------- /images/players/teamC3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC3.png -------------------------------------------------------------------------------- /images/players/teamC4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC4.png -------------------------------------------------------------------------------- /images/players/teamC5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC5.png -------------------------------------------------------------------------------- /images/players/teamC6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC6.png -------------------------------------------------------------------------------- /images/players/teamC7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC7.png -------------------------------------------------------------------------------- /images/players/teamC8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC8.png -------------------------------------------------------------------------------- /images/players/teamC9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamC9.png -------------------------------------------------------------------------------- /images/players/teamD1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD1.png -------------------------------------------------------------------------------- /images/players/teamD10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD10.png -------------------------------------------------------------------------------- /images/players/teamD11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD11.png -------------------------------------------------------------------------------- /images/players/teamD2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD2.png -------------------------------------------------------------------------------- /images/players/teamD3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD3.png -------------------------------------------------------------------------------- /images/players/teamD4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD4.png -------------------------------------------------------------------------------- /images/players/teamD5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD5.png -------------------------------------------------------------------------------- /images/players/teamD6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD6.png -------------------------------------------------------------------------------- /images/players/teamD7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD7.png -------------------------------------------------------------------------------- /images/players/teamD8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD8.png -------------------------------------------------------------------------------- /images/players/teamD9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamD9.png -------------------------------------------------------------------------------- /images/players/teamDk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/players/teamDk.png -------------------------------------------------------------------------------- /images/others/footballpitch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elishatofunmi/pyfootball/HEAD/images/others/footballpitch.png -------------------------------------------------------------------------------- /Scripts/footballProperties.py: -------------------------------------------------------------------------------- 1 | from Scripts.PlayersProperties import * 2 | from Scripts.net import network 3 | 4 | 5 | class Rules: 6 | def __init__(self): 7 | return 8 | 9 | def cornerKick(self): 10 | return 11 | 12 | def penalty(self): 13 | return 14 | 15 | def freekick(self): 16 | return 17 | 18 | def restartProgram(self): 19 | return 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pyfootball - A reinforcement learning environment for football game using model-free markov process 2 | A Reinforcement Learning environment built using pygame. 3 | 4 | Game formation for each Team: 4:3:3 5 | 6 | 7 | ![gif](images/pyfootball.gif) 8 | 9 | Experiment by following this commands. 10 | 11 | ``` 12 | git clone https://github.com/elishatofunmi/pyfootball.git 13 | 14 | cd "pyfootball" 15 | 16 | pip install -r requirements.txt 17 | 18 | python pyfootball.py 19 | 20 | ``` 21 | 22 | 23 | -------------------------------------------------------------------------------- /Scripts/PlayersProperties.py: -------------------------------------------------------------------------------- 1 | # General player properties 2 | class defaultProperties: 3 | def __init__(self): 4 | self.positionx = 0 5 | self.positiony = 0 6 | self.listX = [] 7 | self.listY = [] 8 | 9 | return 10 | 11 | def setCoordinates(self, x, y): 12 | self.positionx = x 13 | self.positiony = y 14 | self.listX = [self.positionx] 15 | self.listY = [self.positiony] 16 | return 17 | 18 | def getCoordinates(self): 19 | return self.positionx, self.positiony 20 | 21 | def update(self, count): 22 | self.positionx = self.listX[count] 23 | self.positiony = self.listY[count] 24 | return 25 | 26 | def GetlastLocation(self): 27 | 28 | return self.positionx, self.positiony 29 | -------------------------------------------------------------------------------- /Scripts/regression.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import sys 4 | import time 5 | 6 | 7 | class Regression: 8 | def __init__(self): 9 | return 10 | 11 | def network(self, xsource, ysource, Xnew, Ynew, divisor=50): 12 | slope = 0 13 | intercept = 0 14 | 15 | #Slope and intercept 16 | while True: 17 | 18 | try: 19 | slope = (ysource - Ynew)/(xsource - Xnew) 20 | intercept = ysource - (slope*xsource) 21 | except ZeroDivisionError: 22 | slope = 0 23 | pass 24 | 25 | if (slope != np.inf) and (intercept != np.inf): 26 | break 27 | else: 28 | slope = 0 29 | break 30 | 31 | # randomly select 50 new values along the slope between xsource and xnew (monotonically decreasing/increasing) 32 | XNewList = [xsource] 33 | if slope != 0 and slope != np.nan and intercept != 0 and intercept != np.nan: 34 | if xsource < Xnew: 35 | differences = Xnew - xsource 36 | increment = differences / divisor 37 | newXval = xsource 38 | for i in range(divisor): 39 | 40 | newXval += increment 41 | XNewList.append(int(newXval)) 42 | else: 43 | differences = xsource - Xnew 44 | decrement = differences / divisor 45 | newXval = xsource 46 | for i in range(divisor-1): 47 | 48 | newXval -= decrement 49 | XNewList.append(int(newXval)) 50 | 51 | # determine the values of y, from the new values of x, using y= mx + c 52 | yNewList = [] 53 | for i in XNewList: 54 | findy = (slope * i) + intercept # y = mx + c 55 | yNewList.append(int(findy)) 56 | 57 | else: 58 | XNewList = [xsource]*50 59 | yNewList = [ysource]*50 60 | return XNewList[:50], yNewList[:50] 61 | -------------------------------------------------------------------------------- /pyfootball.py: -------------------------------------------------------------------------------- 1 | import time 2 | import os 3 | import sys 4 | from Scripts.PlayersProperties import * 5 | from Scripts.footballProperties import * 6 | from Scripts.net import network 7 | import numpy as np 8 | 9 | import pygame 10 | import sys 11 | from pygame.locals import * 12 | pygame.init() 13 | 14 | 15 | class Environment: 16 | def __init__(self, scoreA, scoreB): 17 | 18 | self.FPS = 20 19 | self.fpsClock = pygame.time.Clock() 20 | 21 | # initialize the network 22 | self.net = network() 23 | self.net.TeamAScore = scoreA 24 | self.net.TeamBScore = scoreB 25 | 26 | # set up the window 27 | self.DISPLAYSURF = pygame.display.set_mode((1200, 780), 0, 32) 28 | pygame.display.set_caption( 29 | 'REINFORCEMENT LEARNING (Discrete Mathematics) - Football Analytics (footRein)') 30 | # set up the colors 31 | self.BLACK = (0, 0, 0) 32 | self.WHITE = (255, 255, 255) 33 | self.RED = (255, 0, 0) 34 | self.GREEN = (0, 255, 0) 35 | self.BLUE = (0, 0, 255) 36 | self.col = (0, 50, 150) 37 | 38 | # set game status 39 | self.myFontA = pygame.font.SysFont("Times New Roman", 35) 40 | self.myFontB = pygame.font.SysFont("Times New Roman", 20) 41 | 42 | # load Team and their positions 43 | MapTeam = { 44 | 'ball': 'others/ball.jpeg', 45 | 'PlayerAKeeper': 'players/TeamAk.png', 46 | 'PlayerA1': 'players/teamA1.png', 47 | 'PlayerA2': 'players/teamA2.png', 48 | 'PlayerA3': 'players/teamA3.png', 49 | 'PlayerA4': 'players/teamA4.png', 50 | 'PlayerA5': 'players/teamA5.png', 51 | 'PlayerA6': 'players/teamA6.png', 52 | 'PlayerA7': 'players/teamA7.png', 53 | 'PlayerA8': 'players/teamA8.png', 54 | 'PlayerA9': 'players/teamA9.png', 55 | 'PlayerA10': 'players/teamA10.png', 56 | 'PlayerBKeeper': 'players/TeamBk.png', 57 | 'PlayerB1': 'players/teamB1.png', 58 | 'PlayerB2': 'players/teamB2.png', 59 | 'PlayerB3': 'players/teamB3.png', 60 | 'PlayerB4': 'players/teamB4.png', 61 | 'PlayerB5': 'players/teamB5.png', 62 | 'PlayerB6': 'players/teamB6.png', 63 | 'PlayerB7': 'players/teamB7.png', 64 | 'PlayerB8': 'players/teamB8.png', 65 | 'PlayerB9': 'players/teamB9.png', 66 | 'PlayerB10': 'players/teamB10.png' 67 | } 68 | self.LoadTeamPositions = {} 69 | for m in MapTeam.keys(): 70 | image_path = 'images/'+str(MapTeam[m]) 71 | self.LoadTeamPositions[m] = pygame.image.load(image_path) 72 | self.LoadTeamPositions[m] = pygame.transform.scale( 73 | self.LoadTeamPositions[m], (20, 20)) 74 | 75 | self.nextTeam = 'Team A' 76 | 77 | # randomly decide start team 78 | self.Team = np.random.choice(['A', 'B']) 79 | 80 | if self.Team == 'A': 81 | # Randomly decide start strikers 82 | self.startingChoicePlayer = np.random.choice( 83 | ['PlayerA9', 'PlayerA10']) 84 | self.nextPlayer = np.random.choice(['PlayerA9', 'PlayerA10']) 85 | 86 | else: 87 | self.startingChoicePlayer = np.random.choice( 88 | ['PlayerB9', 'PlayerB10']) 89 | self.nextPlayer = np.random.choice(['PlayerA9', 'PlayerA10']) 90 | 91 | self.excludedPlayer = self.startingChoicePlayer 92 | return 93 | 94 | # draw on the surface object 95 | 96 | def boarddisplay(self, count, nextplayer): 97 | 98 | self.DISPLAYSURF.fill(self.WHITE) 99 | pygame.draw.rect(self.DISPLAYSURF, self.GREEN, (20, 20, 1160, 660)) 100 | # box 18 101 | pygame.draw.rect(self.DISPLAYSURF, self.WHITE, (20, 200, 200, 300)) 102 | pygame.draw.rect(self.DISPLAYSURF, self.WHITE, (1000, 200, 200, 300)) 103 | # insert the goal posts 104 | pygame.draw.rect(self.DISPLAYSURF, self.BLUE, (20, 250, 20, 200)) 105 | pygame.draw.rect(self.DISPLAYSURF, self.BLUE, (1160, 250, 20, 200)) 106 | 107 | pygame.draw.rect(self.DISPLAYSURF, self.WHITE, (598, 0, 4, 700)) 108 | pygame.draw.ellipse(self.DISPLAYSURF, self.WHITE, (500, 200, 200, 300)) 109 | pygame.draw.rect(self.DISPLAYSURF, self.WHITE, (20, 20, 20, 20)) 110 | pygame.draw.rect(self.DISPLAYSURF, self.WHITE, (1160, 20, 20, 20)) 111 | pygame.draw.rect(self.DISPLAYSURF, self.WHITE, (20, 660, 20, 20)) 112 | pygame.draw.rect(self.DISPLAYSURF, self.WHITE, (1160, 660, 20, 20)) 113 | 114 | # Object Detection 115 | pygame.draw.rect(self.DISPLAYSURF, self.RED, 116 | (self.net.superDict['ball'].listX[count]-3, self.net.superDict['ball'].listY[count]-3, 25, 25)) 117 | 118 | # Boundary Box around nextPlayer 119 | pygame.draw.rect(self.DISPLAYSURF, self.RED, 120 | (self.net.superDict['ball'].listX[-1]-3, self.net.superDict['ball'].listY[-1]-3, 25, 25)) 121 | 122 | return 123 | 124 | def displayBall(self, count): 125 | # display ball 126 | self.DISPLAYSURF.blit( 127 | self.LoadTeamPositions['ball'], (self.net.superDict['ball'].listX[count], self.net.superDict['ball'].listY[count])) 128 | return 129 | 130 | def displayDefaultA(self, count=0): 131 | 132 | for k in self.net.playersListA: 133 | self.DISPLAYSURF.blit(self.LoadTeamPositions[str(k)], (self.net.superDict[str( 134 | k)].listX[count], self.net.superDict[str(k)].listY[count])) 135 | 136 | return 137 | 138 | def displayDefaultB(self, count=0): 139 | # randomly display players A within enclosed position x, y, xwidth, ywidth 140 | for k in self.net.playersListB: 141 | self.DISPLAYSURF.blit(self.LoadTeamPositions[str(k)], (self.net.superDict[str( 142 | k)].listX[count], self.net.superDict[str(k)].listY[count])) 143 | 144 | return 145 | 146 | def updateLastPosition(self, count): 147 | 148 | for k in self.net.playersListA: 149 | self.net.superDict[str( 150 | k)].positionx = self.net.superDict[str(k)].listX[count] 151 | self.net.superDict[str( 152 | k)].positiony = self.net.superDict[str(k)].listY[count] 153 | 154 | for k in self.net.playersListB: 155 | self.net.superDict[str( 156 | k)].positionx = self.net.superDict[str(k)].listX[count] 157 | self.net.superDict[str( 158 | k)].positiony = self.net.superDict[str(k)].listY[count] 159 | 160 | # ball 161 | self.net.superDict['ball'].positionx = self.net.superDict['ball'].listX[count] 162 | self.net.superDict['ball'].positiony = self.net.superDict['ball'].listY[count] 163 | 164 | return 165 | 166 | # Always get sourced 167 | 168 | def sourcedTargetDict(self): 169 | player = self.startingChoicePlayer 170 | 171 | sourceTeam = {} 172 | OtherTeam = {} 173 | AList = ['PlayerAKeeper', 'PlayerA1', 'PlayerA2', 'PlayerA3', 'PlayerA4', 'PlayerA5', 'PlayerA6', 174 | 'PlayerA7', 'PlayerA8', 'PlayerA9', 'PlayerA10'] 175 | 176 | BList = ['PlayerBKeeper', 'PlayerB1', 'PlayerB2', 'PlayerB3', 'PlayerB4', 'PlayerB5', 'PlayerB6', 177 | 'PlayerB7', 'PlayerB8', 'PlayerB9', 'PlayerB10'] 178 | 179 | # Create Source Data of Source players 180 | for m in AList: 181 | sourceTeam[m] = self.net.GetplayersLocation(defaultPlayer=m) 182 | 183 | # Create Target Data of Target players 184 | for m in BList: 185 | OtherTeam[m] = self.net.GetplayersLocation(defaultPlayer=m) 186 | 187 | if player in AList: 188 | 189 | output = sourceTeam, OtherTeam 190 | 191 | else: 192 | 193 | output = OtherTeam, sourceTeam 194 | 195 | return output 196 | 197 | def FindLongDistance(self, x1, x2, y1, y2): 198 | x = np.abs(x1 - x2) 199 | y = np.abs(y1 - y2) 200 | 201 | return int(np.sqrt(pow(x, 2) + pow(y, 2))) 202 | 203 | def evaluateTeam(self, xTeam, yTeam): 204 | # get ball coordinate 205 | ballcoordinate = self.net.GetplayersLocation(defaultPlayer='ball') 206 | minimumDistance = {} 207 | 208 | for keys in list(xTeam.keys()): 209 | a, b = xTeam[keys] 210 | dist = self.FindLongDistance( 211 | ballcoordinate[0], a, ballcoordinate[1], b) 212 | minimumDistance[keys] = dist 213 | 214 | for keys in list(yTeam.keys()): 215 | a, b = yTeam[keys] 216 | dist = self.FindLongDistance( 217 | ballcoordinate[0], a, ballcoordinate[1], b) 218 | minimumDistance[keys] = dist 219 | 220 | self.nextplayer = self.net.sortdata(minimumDistance)[0] 221 | if self.nextplayer in list(xTeam.keys()): 222 | Team = 'A' 223 | else: 224 | Team = 'B' 225 | return self.nextplayer, Team 226 | 227 | # overwrite current player by making opposing players to attack thereby changing next player and Team 228 | 229 | def runProgram(self, startGame=True, FreeKick=(False, 'PlayerAKeeper')): 230 | count = 0 231 | Status = False 232 | StatusCheck = True 233 | outVal = False 234 | subsequentCount = True 235 | nextPlayer = self.nextPlayer 236 | freeKick = False 237 | 238 | if FreeKick[0] == True: 239 | self.net.game_status = 'This is a Free Kick!!!' 240 | self.startingChoicePlayer = FreeKick[1] 241 | xcor, ycor = self.net.GetplayersLocation(FreeKick[1]) 242 | self.net.superDict['ball'].positionx = xcor 243 | self.net.superDict['ball'].positiony = ycor 244 | 245 | if FreeKick[1] in self.net.playersListA: 246 | nextPlayer = np.random.choice(self.net.playersListA[1:]) 247 | else: 248 | nextPlayer = np.random.choice(self.net.playersListB[1:]) 249 | else: 250 | pass 251 | 252 | # display board 253 | self.boarddisplay(count, nextPlayer) 254 | 255 | # display ball 256 | self.displayBall(count) 257 | 258 | # display player A 259 | self.displayDefaultA(count) 260 | 261 | # display player B 262 | self.displayDefaultB(count) 263 | 264 | while True: 265 | # show game status 266 | self.randNumLabelA = self.myFontA.render( 267 | 'Team A: '+str(self.net.TeamAScore) + ', Team B: '+str(self.net.TeamBScore), 1, self.BLACK) 268 | self.randNumLabelB = self.myFontB.render( 269 | 'Game Status: ' + str(self.net.game_status), 1, self.BLACK) 270 | 271 | # make choice of player 272 | ChoicePlayerCoordinate = self.net.GetplayersLocation( 273 | defaultPlayer=self.startingChoicePlayer) 274 | 275 | XDict, YDict = self.sourcedTargetDict() 276 | # set Count in network to be current count 277 | self.net.count = count 278 | 279 | if count == 0: 280 | YDictZero = YDict 281 | if subsequentCount == True: 282 | playersChoice = 'B' 283 | else: 284 | playersChoice = np.random.choice(['A', 'B'], p=[0.3, 0.7]) 285 | 286 | if playersChoice == 'A': 287 | self.net.AttemptGoal(self.startingChoicePlayer, sourcePlayerCoordinate=ChoicePlayerCoordinate, 288 | SourcedictDataX=XDict, SourcedictDataY=YDict) 289 | 290 | else: 291 | # keep deciding the next player 292 | self.startingChoicePlayer, self.excludedPlayer = self.net.updateposition(self.startingChoicePlayer, sourcePlayerCoordinate=ChoicePlayerCoordinate, 293 | SourcedictDataX=XDict, SourcedictDataY=YDict) 294 | 295 | self.nextPlayer = self.startingChoicePlayer 296 | self.net.game_status = "Next Player: " + \ 297 | str(self.startingChoicePlayer) 298 | 299 | # Let the opposing team Attack 300 | if StatusCheck: 301 | OpposingPlayee, Status = self.net.Attack(YDict) 302 | else: 303 | pass 304 | 305 | pygame.display.update() 306 | self.fpsClock.tick(self.FPS) 307 | 308 | # display board 309 | self.boarddisplay(count, nextPlayer) 310 | 311 | # display ball 312 | self.displayBall(count) 313 | 314 | # display player A 315 | self.displayDefaultA(count) 316 | 317 | # display player B 318 | self.displayDefaultB(count) 319 | 320 | self.DISPLAYSURF.blit(self.randNumLabelA, (450, 700)) 321 | self.DISPLAYSURF.blit(self.randNumLabelB, (450, 740)) 322 | 323 | for event in pygame.event.get(): 324 | 325 | if event.type == QUIT: 326 | pygame.quit() 327 | sys.exit() 328 | 329 | if Status == True: 330 | self.startingChoicePlayer = OpposingPlayee 331 | # update count 332 | self.updateLastPosition(count=count) 333 | 334 | # increment count 335 | count += 1 336 | 337 | if count == 50: 338 | self.updateLastPosition(count=49) 339 | 340 | count = 0 341 | goal, goalStatus = self.net.ComputeGoal() 342 | 343 | if goal == False: 344 | if goalStatus in ['PlayerAKeeper', 'PlayerBKeeper']: 345 | freeKick = np.random.choice( 346 | [True, False], p=[0.3, 0.7]) 347 | outVal = True 348 | if freeKick == True: 349 | break 350 | else: 351 | pass 352 | else: 353 | outVal = False 354 | else: 355 | self.net.game_status = 'Goal!!!!' 356 | outVal = True 357 | break 358 | else: 359 | pass 360 | 361 | if startGame == True: 362 | time.sleep(2) 363 | startGame = False 364 | 365 | subsequentCount = False 366 | 367 | return outVal, freeKick, goalStatus 368 | 369 | 370 | # Score of the match 371 | ScoreA, ScoreB = 0, 0 372 | freeKick = False 373 | 374 | 375 | def main(ScoreA, ScoreB, freeKick=freeKick): 376 | env = Environment(ScoreA, ScoreB) 377 | 378 | outVal, FreeKick, goalStatus = env.runProgram( 379 | startGame=True, FreeKick=freeKick) 380 | if outVal == True: 381 | # get the score of the match 382 | ScoreA, ScoreB = env.net.TeamAScore, env.net.TeamBScore 383 | main(ScoreA, ScoreB, freeKick=(FreeKick, goalStatus)) 384 | return 385 | 386 | 387 | if __name__ == "__main__": 388 | main(ScoreA, ScoreB, freeKick=(False, "PlayerAKeeper")) 389 | -------------------------------------------------------------------------------- /Scripts/net.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from Scripts.PlayersProperties import defaultProperties 3 | from Scripts.regression import Regression 4 | import os 5 | import sys 6 | import time 7 | from copy import deepcopy 8 | 9 | 10 | class network: 11 | 12 | def __init__(self): 13 | self.count = 0 14 | self.updateScore = False 15 | self.TeamAScore = 0 16 | self.TeamBScore = 0 17 | self.game_status = '' 18 | 19 | self.playersListA = ['PlayerAKeeper', 'PlayerA1', 'PlayerA2', 'PlayerA3', 'PlayerA4', 'PlayerA5', 20 | 'PlayerA6', 'PlayerA7', 'PlayerA8', 'PlayerA9', 'PlayerA10'] 21 | self.playersListB = ['PlayerBKeeper', 'PlayerB1', 'PlayerB2', 'PlayerB3', 'PlayerB4', 'PlayerB5', 22 | 'PlayerB6', 'PlayerB7', 'PlayerB8', 'PlayerB9', 'PlayerB10'] 23 | 24 | DefaultCoordinates = { 25 | 'ball': (600, 350), 'PlayerAKeeper': (40, 330), 'PlayerA1': (140, 100), 26 | 'PlayerA2': (140, 250), 'PlayerA3': (140, 400), 'PlayerA4': (140, 550), 27 | 'PlayerA5': (340, 150), 'PlayerA6': (340, 300), 'PlayerA7': (340, 450), 28 | 'PlayerA8': (500, 350), 'PlayerA9': (550, 300), 'PlayerA10': (550, 400), 29 | 'PlayerB1': (1030, 100), 'PlayerB2': (1030, 250), 'PlayerB3': (1030, 400), 30 | 'PlayerB4': (1030, 550), 'PlayerB5': (830, 150), 'PlayerB6': (830, 300), 31 | 'PlayerB7': (830, 450), 'PlayerB8': (750, 350), 'PlayerB9': (650, 300), 32 | 'PlayerB10': (650, 400), 'PlayerBKeeper': (1130, 330) 33 | } 34 | 35 | self.superDict = {} 36 | # initialize ball 37 | self.superDict['ball'] = defaultProperties() 38 | self.superDict['ball'].setCoordinates(*DefaultCoordinates['ball']) 39 | 40 | # initialize playersA 41 | for k in self.playersListA: 42 | self.superDict[str(k)] = defaultProperties() 43 | self.superDict[str(k)].setCoordinates(*DefaultCoordinates[str(k)]) 44 | 45 | # initialize playersB 46 | for k in self.playersListB: 47 | self.superDict[str(k)] = deepcopy(defaultProperties()) 48 | self.superDict[str(k)].setCoordinates(*DefaultCoordinates[str(k)]) 49 | 50 | x, y = 0, 0 51 | return 52 | 53 | def sortdata(self, dictData): 54 | vals = list(dictData.values()) 55 | keys = list(dictData.keys()) 56 | vals.sort() 57 | sortedKeys = [] 58 | for i in vals: 59 | for j in keys: 60 | if dictData[j] == i: 61 | sortedKeys.append(j) 62 | 63 | return sortedKeys 64 | 65 | def StraightLineDistance(self, x1, x2, y1, y2): 66 | # returns the straight line distance between coordinate (x1, y1) and coordinate (x2, y2) 67 | x = pow(np.abs(x1 - x2), 2) 68 | y = pow(np.abs(y1 - y2), 2) 69 | 70 | return np.sqrt(x+y) 71 | 72 | def ComputeGoal(self): 73 | # get KeeperA's location 74 | x1, y1 = self.GetplayersLocation(defaultPlayer='PlayerAKeeper') 75 | x2, y2 = self.GetplayersLocation(defaultPlayer='PlayerBKeeper') 76 | ballx, bally = self.superDict['ball'].GetlastLocation() 77 | GoalStatus = 'Nil' 78 | if ballx >= 20 and ballx <= 40: 79 | if (bally > y1+20 or bally < y1-20) and (bally > 250 and bally < 450): 80 | self.game_status = 'Goal!!!!' 81 | # increment team score 82 | self.TeamBScore += 1 83 | out = True 84 | GoalStatus = 'Nil' 85 | 86 | else: 87 | GoalStatus = 'PlayerAKeeper' 88 | out = False 89 | 90 | elif ballx >= 1130 and ballx <= 1180: 91 | if (bally > y2+20 or bally < y2-20) and (bally > 250 and bally < 450): 92 | self.game_status = 'Goal!!!!' 93 | # increment team score 94 | self.TeamAScore += 1 95 | out = True 96 | else: 97 | GoalStatus = 'PlayerBKeeper' 98 | out = False 99 | else: 100 | GoalStatus = 'Nil' 101 | out = False 102 | return out, GoalStatus 103 | 104 | def GetPostBoundary(self, keeper='PlayerBKeeper'): 105 | if keeper == 'PlayerBKeeper': 106 | ytarget = np.random.choice([i for i in range(250, 450)]) 107 | xtarget = 1160 108 | 109 | else: 110 | ytarget = np.random.choice([i for i in range(250, 450)]) 111 | xtarget = 20 112 | return xtarget, ytarget 113 | 114 | def AttemptScore(self, player, keeper, XDict, YDict): 115 | xtarget, ytarget = self.GetPostBoundary(keeper=keeper) 116 | xsource, ysource = self.GetplayersLocation(defaultPlayer=player) 117 | xmoves, ymoves = self.linearRegression( 118 | xsource, ysource, xtarget, ytarget) 119 | 120 | # set balls trajectory 121 | self.setplayersLocation(coordinates=( 122 | xmoves, ymoves), defaultPlayer='ball') 123 | 124 | # decide if the keeper should get the ball or loose it 125 | keeperStatus = np.random.choice([True, False], p=[0.7, 0.3]) 126 | if keeperStatus == True: 127 | # set Keeper's movement to follow the ball 128 | 129 | if keeper in self.playersListA: 130 | self.superDict['PlayerAKeeper'].listY = [ymoves[-1]-10, ymoves[-1]-5, 131 | ymoves[-1]+10, ymoves[-1]+5, ymoves[-1]]*10 132 | self.superDict['PlayerAKeeper'].listX = [xmoves[-1]]*50 133 | self.game_status = "Awesome!!! Goalkeeper caught the ball." 134 | 135 | else: 136 | self.superDict['PlayerBKeeper'].listY = [ymoves[-1]-10, ymoves[-1]-5, 137 | ymoves[-1]+10, ymoves[-1]+5, ymoves[-1]]*10 138 | self.superDict['PlayerBKeeper'].listX = [xmoves[-1]]*50 139 | self.game_status = "Awesome!!! Goalkeeper caught the ball." 140 | 141 | else: 142 | currentPositionX, currentPositionY = self.GetplayersLocation( 143 | defaultPlayer=keeper) 144 | xbound, ybound = self.GetplayersBoundary(keeper) 145 | 146 | xmoves, ymoves = self.linearRegression(currentPositionX, currentPositionY, 147 | xbound, ybound) 148 | self.setplayersLocation((xmoves, ymoves), defaultPlayer=keeper) 149 | 150 | if keeper in list(XDict.keys()): 151 | XDict.pop(keeper) 152 | else: 153 | YDict.pop(keeper) 154 | 155 | # Re-position other players 156 | for playee in XDict.keys(): 157 | currentPositionX, currentPositionY = XDict[playee] 158 | xbound, ybound = self.GetplayersBoundary(playee) 159 | 160 | xmoves, ymoves = self.linearRegression(currentPositionX, currentPositionY, 161 | xbound, ybound) 162 | self.setplayersLocation((xmoves, ymoves), defaultPlayer=playee) 163 | 164 | for playee in YDict.keys(): 165 | currentPositionX, currentPositionY = YDict[playee] 166 | xbound, ybound = self.GetplayersBoundary(playee) 167 | xmoves, ymoves = self.linearRegression(currentPositionX, currentPositionY, 168 | xbound, ybound) 169 | self.setplayersLocation((xmoves, ymoves), defaultPlayer=playee) 170 | 171 | return 172 | 173 | def Attack(self, OpposingPlayers): 174 | """ 175 | Return the next player in the opposing team close enough to get the ball from the 176 | current team. 177 | """ 178 | playee = 'A' 179 | Status = False 180 | # Get the current ball location 181 | count = self.count 182 | currentX, currentY = self.superDict['ball'].listX[count], self.superDict['ball'].listY[count] 183 | 184 | EstimatedDistance = {} 185 | for player in OpposingPlayers.keys(): 186 | # find Euclidean Distance from ball 187 | playeeCoordinate = OpposingPlayers[player] 188 | EstimatedDistance[player] = self.StraightLineDistance(playeeCoordinate[0], 189 | currentX, playeeCoordinate[1], currentY) 190 | 191 | sortedKey = self.sortdata(EstimatedDistance)[0] 192 | if EstimatedDistance[sortedKey] <= 50: 193 | Status = True 194 | playee = sortedKey 195 | else: 196 | Status = False 197 | playee = 'A' 198 | return playee, Status 199 | 200 | def TagPlayers(self, XDict, YDict): 201 | """ 202 | YDict in this case represents the source players 203 | XDict in this case represents the target players 204 | That is, YDict are tagging each XDict Player (excluding goal keepers) 205 | """ 206 | SourcePlayers = list(YDict.keys()) 207 | TargetPlayers = list(XDict.keys()) 208 | 209 | # remove goal keepers 210 | # goalkeepers are always first on the list 211 | TargetPlayers.remove(TargetPlayers[0]) 212 | # goalkeepers are always first on the list 213 | SourcePlayers.remove(SourcePlayers[0]) 214 | 215 | # Tag players to players based on nearest neighbors 216 | DictTags = {} 217 | 218 | for k in SourcePlayers: 219 | EstimatedDistance = {} 220 | for j in TargetPlayers: 221 | # get source location 222 | xsource, ysource = self.GetplayersLocation(k) 223 | # get target location 224 | xtarget, ytarget = self.GetplayersLocation(j) 225 | 226 | EstimatedDistance[j] = self.StraightLineDistance( 227 | xsource, xtarget, ysource, ytarget) 228 | FirstKey = self.sortdata(EstimatedDistance)[0] 229 | DictTags[k] = FirstKey 230 | # remove the chosen key from this data. 231 | TargetPlayers.remove(FirstKey) 232 | 233 | # reset movement of source players to target players 234 | for keys in DictTags.keys(): 235 | value = DictTags[keys] 236 | # get source location 237 | xsource, ysource = self.GetplayersLocation(keys) 238 | xtarget, ytarget = self.GetplayersLocation(value) 239 | 240 | xmoves, ymoves = self.linearRegression( 241 | xsource, ysource, xtarget, ytarget) 242 | 243 | # reposition the ball in front of the player 244 | 245 | self.setplayersLocation(coordinates=( 246 | xmoves, ymoves), defaultPlayer=keys) 247 | 248 | return 249 | 250 | def LikePairsNearestNeighbor(self, playername, sourcePlayerCoordinate, SourcedictDataX, SourcedictDataY, 251 | milestone='short', conditionedValue=30): 252 | """ 253 | SourcedictDataX: details of all source Players and their respective coordinate. 254 | SourcedictDataY: details of all target Players and their respecitve coordinate. 255 | playername: name of the current player 256 | sourcePlayerCoordinate: current coordinate of the player 257 | milstone: either long pass, short pass or across. 258 | conditionedValue: give the game to player who is free from an opponent at a distance less than 30 259 | """ 260 | 261 | EstimatedDistance = {} 262 | for playee in SourcedictDataX.keys(): 263 | playeeCoordinate = SourcedictDataX[playee] 264 | EstimatedDistance[playee] = self.StraightLineDistance( 265 | sourcePlayerCoordinate[0], playeeCoordinate[0], sourcePlayerCoordinate[1], playeeCoordinate[1]) 266 | 267 | sortedKeys = self.sortdata(EstimatedDistance) 268 | sortedKeys.remove(playername) 269 | nextplayer = np.random.choice(sortedKeys) 270 | return nextplayer 271 | 272 | def repositionBall(self, xmove, ymove): 273 | i = 0 274 | x = [i+1 for i in xmove] 275 | y = [i+1 for j in ymove] 276 | return x, y 277 | 278 | def AttemptGoal(self, playername, sourcePlayerCoordinate, SourcedictDataX, SourcedictDataY): 279 | # Attempt Shot 280 | # get opposing keeper, target Team. 281 | if playername in self.playersListA: 282 | goalKeeper = 'PlayerBKeeper' 283 | else: 284 | goalKeeper = 'PlayerAKeeper' 285 | 286 | self.game_status = playername + \ 287 | ' is Attempting to make Shot, goalkeeper is ' + goalKeeper 288 | self.AttemptScore(playername, goalKeeper, 289 | SourcedictDataX, SourcedictDataY) 290 | return 291 | 292 | def updateposition(self, playername, sourcePlayerCoordinate, SourcedictDataX, SourcedictDataY): 293 | 294 | # get the next player 295 | outplayer = self.LikePairsNearestNeighbor( 296 | playername, sourcePlayerCoordinate, SourcedictDataX, SourcedictDataY) 297 | 298 | # update straight line movement of the ball from source to nextplayer 299 | 300 | xbound, ybound = SourcedictDataX[outplayer] 301 | xsource, ysource = self.superDict['ball'].GetlastLocation() 302 | 303 | xmoves, ymoves = self.linearRegression( 304 | xsource, ysource, xbound, ybound) 305 | 306 | # reposition the ball in front of the player 307 | 308 | self.setplayersLocation(coordinates=( 309 | xmoves, ymoves), defaultPlayer='ball') 310 | self.setplayersLocation(coordinates=( 311 | [xbound]*50, [ybound]*50), defaultPlayer=outplayer) 312 | 313 | # remove playername, and update new player 314 | SourcedDictDataX_copy = SourcedictDataX.copy() 315 | SourcedDictDataX_copy.pop(outplayer) 316 | 317 | # update the positions of all players including playername 318 | """ 319 | 1. get their current position from the sourcedictDataX and SourcedDictDataY 320 | 2. estimate a straight line move to their new position (within the specified boundary) 321 | """ 322 | for playee in SourcedDictDataX_copy.keys(): 323 | currentPositionX, currentPositionY = SourcedDictDataX_copy[playee] 324 | xbound, ybound = self.GetplayersBoundary(playee) 325 | 326 | xmoves, ymoves = self.linearRegression(currentPositionX, currentPositionY, 327 | xbound, ybound) 328 | self.setplayersLocation((xmoves, ymoves), defaultPlayer=playee) 329 | 330 | # reset oppossing keepers position 331 | currentPositionX, currentPositionY = SourcedictDataY[list( 332 | SourcedictDataY.keys())[0]] 333 | xbound, ybound = self.GetplayersBoundary( 334 | list(SourcedictDataY.keys())[0]) 335 | xmoves, ymoves = self.linearRegression(currentPositionX, currentPositionY, 336 | xbound, ybound) 337 | self.setplayersLocation( 338 | (xmoves, ymoves), defaultPlayer=list(SourcedictDataY.keys())[0]) 339 | 340 | # for the opposing players, reset their positions to attack. 341 | self.TagPlayers(SourcedictDataX, SourcedictDataY) 342 | 343 | return outplayer, playername 344 | 345 | def linearRegression(self, x1, y1, x2, y2): 346 | reg = Regression() 347 | xmove, ymove = reg.network(x1, y1, x2, y2) 348 | 349 | return xmove, ymove 350 | 351 | def setplayersLocation(self, coordinates, defaultPlayer='ball'): 352 | """ 353 | x and y are lists of 50 different positions for the specific 'player' 354 | updates a new location for the player passed in as defaultPlayer 355 | """ 356 | x = coordinates[0] 357 | y = coordinates[1] 358 | 359 | if defaultPlayer in self.superDict.keys(): 360 | self.superDict[defaultPlayer].listX = x 361 | self.superDict[defaultPlayer].listY = y 362 | else: 363 | pass 364 | 365 | return 366 | 367 | def GetplayersLocation(self, defaultPlayer='ball'): 368 | """ 369 | updates a new location for the player passed in as defaultPlayer 370 | """ 371 | x = self.superDict[defaultPlayer].positionx 372 | y = self.superDict[defaultPlayer].positiony 373 | 374 | return x, y 375 | 376 | def GetplayersBoundary(self, defaultPlayer='ball'): 377 | """ 378 | Get the boundary's of keepers, ball, defenders, strikers and mid-fielders 379 | """ 380 | # Determine factor, range of factor is 0 - 1 381 | factor = 1 382 | factor2 = 1 383 | stat = False 384 | # get ball's coordinate 385 | xball, yball = self.GetplayersLocation('ball') 386 | if yball < 330: 387 | stat = False 388 | # within player's A post 389 | factor = 0.2 390 | factor2 = 1 391 | 392 | elif yball > 330 and yball < 660: 393 | stat = True 394 | factor = 1 395 | factor2 = 1 396 | 397 | else: 398 | stat = False 399 | factor = 0.7 400 | factor2 = 1/factor 401 | 402 | if defaultPlayer in ['PlayerA1', 'PlayerA2', 'PlayerB9']: 403 | x1, x2, y1, y2 = 150*factor, 600*factor*factor2, 330, 660 404 | 405 | elif defaultPlayer in ['PlayerA3', 'PlayerA4', 'PlayerB10']: 406 | x1, x2, y1, y2 = 150*factor, 600*factor*factor2, 20, 330 407 | 408 | elif defaultPlayer == 'PlayerB8': 409 | x1, x2, y1, y2 = 150*factor, 600*factor*factor2, 275, 345 410 | 411 | elif defaultPlayer in ['PlayerA5', 'PlayerB5']: 412 | x1, x2, y1, y2 = 400*factor, 900*factor*factor2, 330, 660 413 | 414 | elif defaultPlayer in ['PlayerA6', 'PlayerB6']: 415 | x1, x2, y1, y2 = 400*factor, 900*factor*factor2, 275, 345 416 | 417 | elif defaultPlayer in ['PlayerA7', 'PlayerB7']: 418 | x1, x2, y1, y2 = 400*factor, 900*factor*factor2, 20, 330 419 | 420 | elif defaultPlayer == 'PlayerA8': 421 | x1, x2, y1, y2 = 600*factor, 1000*factor*factor2, 275, 345 422 | 423 | elif defaultPlayer == 'PlayerA9': 424 | x1, x2, y1, y2 = 600*factor, 1000*factor*factor2, 330, 660 425 | 426 | elif defaultPlayer == 'PlayerA10': 427 | x1, x2, y1, y2 = 600*factor, 1000*factor*factor2, 20, 330 428 | 429 | elif defaultPlayer in ['PlayerB1', 'PlayerB2']: 430 | x1, x2, y1, y2 = 600*factor, 1000*factor*factor2, 330, 660 431 | 432 | elif defaultPlayer in ['PlayerB3', 'PlayerB4']: 433 | x1, x2, y1, y2 = 600*factor, 1000*factor*factor2, 20, 330 434 | 435 | elif defaultPlayer == 'PlayerAKeeper': 436 | x1, x2, y1, y2 = 20, 220, 200, 500 437 | elif defaultPlayer == 'PlayerBKeeper': 438 | x1, x2, y1, y2 = 1000, 1100, 200, 500 439 | else: 440 | x1, x2, y1, y2 = 20, 1160, 20, 660 441 | 442 | # Get player's location to minimize movement around the field 443 | xloc, yloc = self.GetplayersLocation(defaultPlayer) 444 | xchoice = np.random.choice([i for i in range(int(x1), int(x2))]) 445 | ychoice = np.random.choice([i for i in range(int(y1), int(y2))]) 446 | 447 | # ensure xsource isn't too far from Xnew 448 | # if stat == True: 449 | if xloc > xchoice: 450 | if np.abs(xloc-xchoice) > 100: 451 | xchoice = xloc - 100 452 | 453 | else: 454 | if np.abs(xloc-xchoice) > 100: 455 | xchoice = xloc + 100 456 | # else: 457 | # pass 458 | 459 | # return a random coordinate between x1 and x2, likewise, y1 and y2. 460 | 461 | return xchoice, ychoice 462 | --------------------------------------------------------------------------------