I decided to put my theory behind how an actual possession would work to the test, and put together a quick proof of concept for a possession. The theory would be that each play would be a loop. A variable would be used to progress through the play, with each step pointing to the next step that would be called... In the following example I use the 'o' variable to keep the play alive, and the 'i' variable to point the play at the next step each time the loop runs.
This is a simple play that sees the C set a high pick for the PG who, if the screen is a success, will either shoot, pass, or drive:
Code:
import random
o = 0
i = ""
while o != 1:
if i == "":
#first step of the play
#offensive player's positions on the court set.
print "5 man moves to set a high screen for the 1"
#subtract time from game clock and shot clocks.
i = 1
elif i == 1:
#second step of the play
#offensive player's positions on the court set.
#calculate the success of the screen and/or any fouls.
screensuccess = random.randrange(0,2)
#subtract time from game clock and shot clocks.
if screensuccess == 0:
print "5 man sets a bad screen."
i = 99
#set a game-state variable that would mean the next possession
#is a 'broken play' with the ball in the PG's hands.
else:
print "1 runs off the pick by 5 man"
i = 2
elif i == 2:
#third step of the play
#offensive player's positions on the court set.
#check for turnover by ballhandler.
#determine if ball handler goes to the basket, shoots, or passes.
#subtract time from game clock and shot clocks.
pgdecision = random.randrange(0,3)
if pgdecision == 0:
#final step of play.
#offensive player's positions on the court set.
#check for turnover by ballhandler.
#subtract time from game clock and shot clocks.
print "1 drives to the basket."
i = 99
#set a game-state variable that would mean the next possession
#is a drive by the current ballhandler.
elif pgdecision == 1:
#final step of play.
#offensive player's positions on the court set.
#check for turnover by ballhandler.
#subtract time from game clock and shot clocks.
print "1 pulls up for the long jumper"
i = 99
#set a game-state variable that would mean the next possession
#is a jumpshot by the ballhandler.
elif pgdecision == 2:
#final step of play.
#offensive player's positions on the court set.
#check for turnover by ballhandler.
#subtract time from game clock and shot clocks.
print "1 passes to a teammate"
i = 99
#set a game-state variable that would mean the next possession
#is a 'broken play'.
elif i == 99:
o = 1
i = ""
The end result:
Code:
5 man moves to set a high screen for the 1
1 runs off the pick by 5 man
1 pulls up for the long jumper
I run it a bunch of times to make sure the results are random, and it looks good! Obviously there would be a lot more complexity involved in the final version - ie. not straight random decisions, but based on tactics and attributes. But it's a start.