lilsnyderio lilsnyderio - 18 days ago 7
Python Question

Python Text-Based Baseball Game - Flipping Booleans

My friend and I are making a Text-Based Baseball game in Python. We are trying to figure out how to manage the runners on base. We were thinking if the best way would be to have a list of booleans and flip them based on if the play was a single, double, etc. Any help would be appreciated!
Ex.

bases = [False,False,False]
if hit == single:
bases = [True,False,False]

We are finding it difficult considering all the possible combinations of runners on base

Answer
from collections import deque
bases = [False, False, True]
playing_field = deque(bases)
print(playing_field) #deque([False, False, True])

playing_field.rotate(-1) 

print(playing_field) #deque([False, True, False])

#can use it like a list:
print(playing_field[0]) #False

deque from collections is perfect for what you want to do. You can basically shift the list and add a member to first base.

rotate() takes an integer, positive or negative, and shifts the list that many times left or right.

If I were you, make it include a home plate, so you can pop it off and score or what not. IF you need deque has popleft() and popright() methods to literally take the left or right most item off the list.

Comments