Puzzle Game in Python with Source code. We can develop the puzzle game using depth first search in an Artificial Intelligence manner.In this code we can evaluate the steps of the user that in how much steps user reached to the desired destination and shows the output in how many steps he reached the final destination.
Tools & Technologies:
Use the following code and just paste it in the compile like idle and Anaconda.
Take care of the indentation of the code as python requires the indentation of its code.
# -*- coding: utf-8 -*-
Created on Wed Oct 10 10:30:35 2018
def id_dfs(puzzle, goal, get_moves):
def dfs(route, depth):
if depth == 0:
if route[-1] == goal:
for move in get_moves(route[-1]):
if move not in route:
next_route = dfs(route + [move], depth – 1)
for depth in itertools.count():
route = dfs([puzzle], depth)
def num_matrix(rows, cols, steps=25):
nums = list(range(1, rows * cols)) + 
goal = [ nums[i:i+rows] for i in range(0, len(nums), rows) ]
get_moves = num_moves(rows, cols)
puzzle = goal
for steps in range(steps):
puzzle = random.choice(get_moves(puzzle))