예제 #1
0
def main():
	print "What is the start location?"
	start_x_coor, start_y_coor = tools.get_input_coordinates(width, height)

	print "What is the end location?"
	unique_end_coor = False
	while not unique_end_coor:
		end_x_coor, end_y_coor = tools.get_input_coordinates(width, height)
		if (start_x_coor == end_x_coor) and (start_y_coor == end_y_coor):
			print "Same as start.  Enter new end location:"
			unique_end_coor =  False
		else:
			unique_end_coor =  True

	# initialize start/end
	board = [['.' for x in range(height)] for x in range(width)]
	board[start_x_coor][start_y_coor] = 'S'
	board[end_x_coor][end_y_coor] = 'E'

	# initialize board w/ moves
	board_with_moves = [['.' for x in range(height)] for x in range(width)]
	board_with_moves[start_x_coor][start_y_coor] = []

	board_with_moves_set = [['.' for x in range(height)] for x in range(width)]
	board_with_moves_set[start_x_coor][start_y_coor] = set([])

	continue_loop = True
	current_step = 0
	while continue_loop:
		continue_loop = False

		for y in range(len(board[0])):
			for x in range(len(board)):
				if board_with_moves[x][y] != '.' and len(board_with_moves[x][y]) == current_step:
					modified = draw_knight_move(board_with_moves, board_with_moves_set, x, y)
					if modified:
						continue_loop = True

		current_step += 1

		# print out percentage more to go through
		if current_step % 30 == 0:
			print 'About ', ( current_step / 10.24), '% done.'

	# shortest move found!
	move_to_end = board_with_moves[end_x_coor][end_y_coor]

	for num_move in range(1, len(move_to_end)):
		x, y = tools.string_to_coor(move_to_end[num_move])
		board[x][y] = num_move

	tools.print_board(board)

	print 'Sequence of moves: '
	for num_move in range(0, len(move_to_end)):
		x, y = tools.string_to_coor(move_to_end[num_move])
		print '(' , x, ', ', y, ')'
	print '(' , end_x_coor, ', ', end_y_coor, ')'
예제 #2
0
def main():
	print "What is the start location?"
	start_x_coor, start_y_coor = tools.get_input_coordinates(width, height)

	print "What is the end location?"
	unique_end_coor = False
	while not unique_end_coor:
		end_x_coor, end_y_coor = tools.get_input_coordinates(width, height)
		if (start_x_coor == end_x_coor) and (start_y_coor == end_y_coor):
			print "Same as start.  Enter new end location:"
			unique_end_coor =  False
		else:
			unique_end_coor =  True

	# initialize start/end points
	board = [['.' for x in range(height)] for x in range(width)]
	board[start_x_coor][start_y_coor] = 'S'
	board[end_x_coor][end_y_coor] = 'E'

	# initialize to memorize order of moves in each spot
	board_with_moves = [['.' for x in range(height)] for x in range(width)]
	board_with_moves[start_x_coor][start_y_coor] = []

	# continue loop until we hit step in which nothing changes.
	continue_loop = True
	current_step = 0
	while continue_loop:
		continue_loop = False

		for y in range(len(board[0])):
			for x in range(len(board)):
				if board_with_moves[x][y] != '.' and len(board_with_moves[x][y]) == current_step:
					modified = draw_knight_move(board_with_moves, x, y)
					if modified:
						continue_loop = True

		# shortest move found! We can now print results and exit.
		if board_with_moves[end_x_coor][end_y_coor] != '.':
			move_to_end = board_with_moves[end_x_coor][end_y_coor]

			# print the move number in each spot
			for num_move in range(1, len(move_to_end)):
				x, y = tools.string_to_coor(move_to_end[num_move])
				board[x][y] = num_move

			tools.print_board(board)

			# print sequence of move
			print 'Sequence of moves: '
			for num_move in range(0, len(move_to_end)):
				x, y = tools.string_to_coor(move_to_end[num_move])
				print '(' , x, ', ', y, ')'
			print '(' , end_x_coor, ', ', end_y_coor, ')'

			exit()

		current_step += 1
예제 #3
0
def main():
	special_board = import_special_board()

	width = len(special_board)
	height = len(special_board[0])
	tools.print_board(special_board)

	# get list of teleport spots
	teleport_list = []
	for y in range(len(special_board[0])):
		for x in range(len(special_board)):
			if special_board[x][y] == 'T':
				teleport_list.append(tools.coor_to_string(x, y))

	# get input for data.  We don't want to start on an element for simplicity.
	print "What is the start location?"
	valid_start_coor = False
	while not valid_start_coor:
		start_x_coor, start_y_coor = tools.get_input_coordinates(width, height)
		element = special_board[start_x_coor][start_y_coor]
		if element == 'W' or element == 'R' or element == 'B' or element == 'T' or element == 'L':
			print "Start has element.  Enter new start location:"
			valid_start_coor =  False
		else:
			valid_start_coor =  True

	print "What is the end location?"
	unique_end_coor = False
	while not unique_end_coor:
		end_x_coor, end_y_coor = tools.get_input_coordinates(width, height)
		element = special_board[end_x_coor][end_y_coor]
		if (start_x_coor == end_x_coor) and (start_y_coor == end_y_coor):
			print "Same as start.  Enter new end location:"
			unique_end_coor =  False
		elif element == 'W' or element == 'R' or element == 'B' or element == 'T' or element == 'L':
			print "End has element.  Enter new end location:"
			unique_end_coor =  False
		else:
			unique_end_coor =  True

	# mark start/end location
	board = [['.' for x in range(height)] for x in range(width)]
	board[start_x_coor][start_y_coor] = 'S'
	board[end_x_coor][end_y_coor] = 'E'

	# initialize board storing moves
	board_with_moves = [['.' for x in range(height)] for x in range(width)]
	board_with_moves[start_x_coor][start_y_coor] = []

	# store number of moves.  Important because of our elements
	board_with_num_moves = [['.' for x in range(height)] for x in range(width)]
	board_with_num_moves[start_x_coor][start_y_coor] = 0

	continue_loop = True
	current_step = 0
	teleport_done = False
	while continue_loop:
		continue_loop = False

		for y in range(len(board[0])):
			for x in range(len(board)):
				if board_with_moves[x][y] != '.' and board_with_num_moves[x][y] == current_step:
					modified = draw_knight_move(board_with_moves, board_with_num_moves, special_board, x, y, current_step + 1, width, height)
					if modified:
						continue_loop = True

		# shortest move found!
		if board_with_moves[end_x_coor][end_y_coor] != '.':
			move_to_end = board_with_moves[end_x_coor][end_y_coor]

			special_board_to_print = [row[:] for row in special_board]
			for num_move in range(1, len(move_to_end)):
				x, y = tools.string_to_coor(move_to_end[num_move])
				special_board_to_print[start_x_coor][start_y_coor] = 'S'
				special_board_to_print[end_x_coor][end_y_coor] = 'E'
				special_board_to_print[x][y] = board_with_num_moves[x][y]

			tools.print_board(special_board_to_print)

			print 'Sequence of moves: '
			for num_move in range(0, len(move_to_end)):
				x, y = tools.string_to_coor(move_to_end[num_move])
				print '(' , x, ', ', y, ')'
			print '(' , end_x_coor, ', ', end_y_coor, ')'

			# we want to check if sequence of moves are all valid
			valid_knight_moves = True
			for idx in range(1, len(move_to_end)):
				prev_x, prev_y = tools.string_to_coor(move_to_end[idx - 1])
				x, y = tools.string_to_coor(move_to_end[idx])

				if not tools.is_valid_knight_move(prev_x, prev_y, x, y) and move_to_end[idx] not in teleport_list:
					print 'Invalid'
					valid_knight_moves = False
					break

			if valid_knight_moves:
				print '--- All valid knight moves'
			else:
				print '--- Invalid knight moves'

			exit()

		current_step += 1

		# check if anything has landed on teleport locations.
		# because we care about shortest moves, if we land on one, then move all other moves to teleport.
		if not teleport_done:
			for teleport_str in teleport_list:
				t_x, t_y = tools.string_to_coor(teleport_str)
				if board_with_moves[t_x][t_y] != '.':
					for teleport_str in teleport_list:
						t_x_, t_y_ = tools.string_to_coor(teleport_str)
						if board_with_moves[t_x_][t_y_] == '.':
							board_with_moves[t_x_][t_y_] = board_with_moves[t_x][t_y][:]
							board_with_num_moves[t_x_][t_y_] = board_with_num_moves[t_x][t_y]
					break