birdwatchers.py
import random
def addBirdSighting(birdSighting, ledger):
'''
Add a bird sighting to the current day in the ledger
birdSighting - the sighting to add
ledger - the ledger to update
'''
ledger.append(birdSighting)
def startNewDay(ledger):
'''
Advance the ledger to start a new day of entries
ledger - the ledger to advance
'''
if len(ledger) == 0:
ledger.append("StartDay")
else:
ledger.append("NextDay")
def birdsSeenEachDay(ledger):
'''
Compute the number of birds within the ledger for each day
ledger - the ledger to analyze
returns a list of counts, one for each day
'''
counts = []
i=0
ledger.append("LastDay")
for counts_list in ledger:
if "Day" in counts_list:
if(counts_list == "NextDay" or counts_list == "LastDay"):
counts.append(i)
i=0;
else:
i=i+1
ledger.remove("LastDay")
return counts
def printLedger(ledger):
'''
Print the sighting within the ledger, separated by the day
'''
i=1
for s in ledger:
if "Day" in s:
print("Day ", i ,"..........................")
i=i+1
else:
print(s)
#===================================================================
# Test code below DO NOT MODIFY
BIRDS = ["Finch", "Robin", "Hummingbird", "Crow", "Eagle", "Hawk", "Cardinal", "Vulture", "Phoenix", "Sparrow"]
LOCATIONS = ["Park", "Backyard", "Skyscraper", "Forest", "Jungle", "Roadside", "Cage", "Sky", "Desert"]
TIME_OF_DAY = ["6-10AM", "10AM-2PM", "2-6PM", "6-10PM", "10PM-2AM", "2-6AM"]
def generateSighting():
return BIRDS[random.randrange(len(BIRDS))], \
LOCATIONS[random.randrange(len(LOCATIONS))], \
TIME_OF_DAY[random.randrange(len(TIME_OF_DAY))]
def testLedger():
ledger = []
dayCount = []
numberOfDays = random.randint(4, 6)
print("The test cases will generate", numberOfDays, "days")
for day in range(numberOfDays):
startNewDay(ledger)
numberOfSightings = random.randint(0, 5)
dayCount.append(numberOfSightings)
print("Day", day + 1, "will contain", numberOfSightings, "sightings")
for count in range(numberOfSightings):
sighting = generateSighting()
addBirdSighting(sighting, ledger)
counts = birdsSeenEachDay(ledger)
if counts != dayCount:
print("birdsSeenEachDay() Failed: Your code returned", counts, "but I expected", dayCount)
else:
print("birdsSeenEachDay() Passed!")
printLedger(ledger)
# Only run this code below if this is called as the main, not imported
if __name__ == '__main__':
testLedger()
checkers.py
debugMove = False # Flip to True to see print statements to help in debugging makeMove()
runValidationTests = True # Flip to True if you are completing the validGameMove() function
debugValidate = False # Flip to True to see print statements to help in debugging validGameMove()
playAfterTest = True # Flip to True to use the User Interface to make test moves
moveLimit = 10 # Set for the maximum number of moves in a game (Hit Control-C to quit any game)
# These are the success and error messages returned by validGameMove()
OK_MOVE = "OK"
WRONG_PIECE = "This space does not contain your piece"
ONLY_2 = "You can only move a piece one space diagonally, or two when legally jumping"
PLAYER_1_UP = "Player 1 must move up"
PLAYER_2_DOWN = "Player 2 must move down"
SPACE_TAKEN = "The destination space is already taken"
JUMP_SELF = "You cannot jump your own piece"
JUMP_EMPTY = "You cannot jump an empty space"
UNKNOWN_INVALID = "Your move is not valid for an unknown reason"
#.................... 1 ........................................
def initializeBoard():
'''
Setup the board for a new game with 2 for player 2 pieces, 1 for player 1 pieces
and 0 for blank spaces.
returns a list with the players pieces setup for the start of a game
'''
# board = []
board = [[0 for i in range(8)] for j in range(8)]
for i in range(8):
if i ==0 or i == 2:
board[i] = [0, 2, 0, 2, 0, 2, 0, 2]
if i == 1:
board[i] = [2, 0, 2, 0, 2, 0, 2, 0]
if i == 5 or i == 7:
board[i] = [1, 0, 1, 0, 1, 0, 1, 0]
if i == 6:
board[i] = [0, 1, 0, 1, 0, 1, 0, 1]
for i in range(8):
print(board[i])
return board
#.................... 2 ........................................
def makeMove(player, move, board):
'''
Update the board with the requested move
move - the validated move
board - the game board
returns the updated board
'''
old_y = move[0]
old_x = move[1]
new_y = move[2]
new_x = move[3]
val1 = board[old_x][old_y]
val2 = board[new_x][new_y]
board[new_x][new_y] = val1
board[old_x][old_y] = val2
if debugMove: print("makeMove(", player, move, "board)")
return board
#.................... 3 ........................................
def validGameMove(player, move, board):
'''
Determine if the validated move request is a valid game move
move - a list of integers representing the moves in the order
[from column, from row, to column, to row]
player - the player number
board - the board currently in play
returns True if the move is valid else False
'''
old_y = move[0]
old_x = move[1]
new_y = move[2]
new_x = move[3]
val1 = board[old_x][old_y]
val2 = board[new_x][new_y]
print("player=",player,val1)
print("player=",player,val2)
if player==1:
if val1!=1:
return WRONG_PIECE
if val2==2:
return SPACE_TAKEN
if(new_x
return PLAYER_1_UP
if(new_x>(old_x-2)):
return ONLY_2
if(new_x>(old_x-2)):
if((board[old_x-1][old_y])==1):
return JUMP_SELF
elif((board[old_x][old_y+1])==1):
return JUMP_SELF
elif((board[old_x-1][old_y])==0):
return JUMP_EMPTY
elif((board[old_x][old_y-1])==0):
return JUMP_EMPTY
else:
return OK_MOVE
elif player==2:
if val2!=2:
return...