#!/usr/bin/python
# jumpsaround - jumper game
# (c) 2011 Alexander Kulbartsch
SIZEX = 6
SIZEY = 5
jump = ((-1,-2), (1,-2), (-2,-1), (2,-1), (-2,1), (2,1), (-1,2), (1,2))
# init board
board = []
for i in range(SIZEX):
col = []
for i in range(SIZEY):
col.append(0)
board.append(col)
solutions = 0
roundtrips = 0
def jumpto(x, y, iter):
global solutions
global roundtrips
global board
board[x][y] = iter
# finished?
if iter >= SIZEX * SIZEY:
# check for roundtrips
round = 0
for np in jump:
nx = x + np[0]
ny = y + np[1]
# check if position is on the board
if nx < 0 or nx > (SIZEX-1) or ny < 0 or ny > (SIZEY-1):
continue
# detect roundtrip
if board[nx][ny] == 1:
round = True
roundtrips += 1
break
solutions += 1
printboard(round)
board[x][y] = 0
return
# try next jump
for np in jump:
nx = x + np[0]
ny = y + np[1]
# check if position is on the board
if nx < 0 or nx > (SIZEX-1) or ny < 0 or ny > (SIZEY-1):
continue
# check if new field position is still empty
if board[nx][ny] != 0:
continue
jumpto(nx, ny, iter + 1)
board[x][y] = 0
def printboard(round):
# The syntax in the following line is not accepted by PyPy
# print(f"===== Solution #{solutions} - Roundtripps:{roundtrips} ======")
print('===== Solution #{} - Roundtripps:{} ======'.format(solutions, roundtrips))
if round:
print("*** Roundtrip ***")
for i in range(SIZEY):
for j in range(SIZEX):
print(f"{board[j][i]:2d}", end=":")
# print("{}".format(board[j][i]), end=":")
print("")
print()
print("Hello, Jumper!")
jumpto(0, 0, 1)
print("I am done.")
# EOF