assertboard[sr][sc]==O,"starting position is not pawn"
assertboard[er][ec]==E,"ending position is not empty"
ifec==sc:
ifer-sr==2:
assertboard[er-1][ec]==O,"No pawn in between (ec==sc, er-sr)"
board[er-1][ec]=E
elifsr-er==2:
assertboard[er+1][ec]==O,"No pawn in between (ec==sc, sr-er)"
board[er+1][ec]=E
else:
raiseException("There must be a pawn in between.")
elifer==sr:
ifec-sc==2:
assertboard[er][ec-1]==O,"No pawn in between (er==sr, ec-sc)"
board[er][ec-1]=E
elifsc-ec==2:
assertboard[er][ec+1]==O,"No pawn in between (er==sr, sc-ec)"
board[er][ec+1]=E
else:
raiseException("There must be a pawn in between.")
else:
raiseException("You can only jump 1.")
board[sr][sc]=E
board[er][ec]=O
returnboard
defundo_move(move,board):
move=move.upper()
start_pos,end_pos=move[2:4],move[0:2]
sr,sc=translate_pos_rc(start_pos)
er,ec=translate_pos_rc(end_pos)
assertboard[sr][sc]==O,"starting position is not pawn"
assertboard[er][ec]==E,"ending position is not empty"
ifec==sc:
ifer-sr==2:
assertboard[er-1][ec]==E,"pawn in between (ec==sc, er-sr)"
board[er-1][ec]=O
elifsr-er==2:
assertboard[er+1][ec]==E,"pawn in between (ec==sc, sr-er)"
board[er+1][ec]=O
else:
raiseException("There must NOT be a pawn in between.")
elifer==sr:
ifec-sc==2:
assertboard[er][ec-1]==E,"pawn in between (er==sr, ec-sc)"
board[er][ec-1]=O
elifsc-ec==2:
assertboard[er][ec+1]==E,"pawn in between (er==sr, sc-ec)"
board[er][ec+1]=O
else:
raiseException("There must NOT be a pawn in between.")
else:
raiseException("You can only jump 1.")
board[sr][sc]=E
board[er][ec]=O
returnboard
defprint_board(board):
print("",end="")
forrinCOLS:
print("",r,"",end="")
print()
forc,rowinzip(ROWS,board):
print(c,row,end="")
print()
defcount_pawns(board):
count=0
forrowinboard:
forpawninrow:
ifpawn==O:
count+=1
returncount
defplay():
moves_this_game=[]
stat=dict(
moves=defaultdict(int),
num_moves=0,
)
board=deepcopy(STARTING_BOARD)
i=0
whileTrue:
print("MOVE: {}".format(i))
print_board(board)
moves=gen_moves(board)
stat["moves"][len(moves)]+=1
print("MOVES: {}".format(moves))
iflen(moves)==0:
print("No valid moves left")
break
# this_move = random.choice(moves)\
# print("Chose Move: {}".format(this_move))
this_move=input("> ")
moves_this_game.append(this_move)
try:
board=play_move(this_move,board)
exceptExceptionase:
print("invalid move")
print(e)
continue
i+=1
stat["num_moves"]=i
returnboard,moves_this_game,stat
defmain_play():
game_no=1
gauss=defaultdict(int)
whileTrue:
board,moves_this_game,stat=play()
count=count_pawns(board)
# print("{} Left pawns: {} | {} | {}".format(game_no, count, stat["num_moves"], sorted([(a,b) for a, b in stat['moves'].items()], key=lambda x: x[1], reverse=True)))
print("{} Left pawns: {}".format(game_no,count))
gauss[count]+=1
game_no+=1
ifcount==1:
print("Moves this game: {}".format(moves_this_game))