Please ask your parents to fill out this form
Solution to problem on slide 2
Same solution as before, have a list of possible solutions and recursively iterate over them.
Solution to problem on slide 3:
A Pretty straightforward solution, implement the game's rules and apply them sequentially.
import math
def isPrime(n):
prime = True
if n == 2:
return prime
else:
for z in range(2, n):
if n % z == 0:
prime = False
break
return prime
def endTurn(name):
if name == "P1NOTMOVED":
return "P1MOVED"
elif name == "P2NOTMOVED":
return "P2MOVED"
else:
return name
f = open("4int_sampledata.txt")
for z in range(5):
data = list(map(int, (f.readline()).split()))
board = []
directions = ["", "V", "V", "H"]
for x in range(53):
board.append("")
player1 = data[0:3]
player2 = data[3:6]
numRolls = data[6]
rolls = data[7:]
playersLeft = 0
for x in player1:
board[x] = "P1NOTMOVED"
for x in player2:
board[x] = "P2NOTMOVED"
for rollVal in rolls:
everyoneGone = False
if not everyoneGone:
everyoneGone = True
for player in ['P2']:
for x in range(52):
if player in board[x]:
everyoneGone = False
if x + rollVal > 52:
board[x] = endTurn(board[x])
elif x + rollVal == 52:
board[x] = ""
elif board[x + rollVal] != "":
board[x] = endTurn(board[x])
elif isPrime(x + rollVal):
futureLoc = x + rollVal
for extraForward in range(0, 7, 1):
if futureLoc + extraForward < 52 and board[
futureLoc + extraForward + 1] != "":
board[futureLoc + extraForward] = endTurn(
board[x])
break
board[x] = ""
elif int(math.sqrt(x + rollVal)) == math.sqrt(
x + rollVal) and math.sqrt(x + rollVal) > 4:
futureLoc = x + rollVal
for extraForward in range(0, -7, -1):
if futureLoc + extraForward < 52 and board[
futureLoc + extraForward - 1] != "":
board[futureLoc + extraForward] = endTurn(
board[x])
break
board[x] = ""
else:
path = [i for i in range(x, x + rollVal + 1)]
hasDirChange = False
path_ = []
for i in [8, 13, 18, 23, 36, 41, 46, 51]:
if (i in path) and ((i - 2) in path):
hasDirChange = True
for i in path:
if ((i % rollVal == 0) and (board[i] == "")):
path_.append(i)
if hasDirChange:
path = path_
path.append(x)
loc = max(path)
board[loc] = endTurn(board[x])
board[x] = ""
else:
if (board[x + rollVal] == ""):
board[x + rollVal] = endTurn(board[x])
board[x] = ''
pass
break
pos = []
for i, x in enumerate(board):
if 'P2' in x:
pos.append(i)
print(pos)