Lab2 8 Puzzle Single Player Game (BFS)
Lab2 8 Puzzle Single Player Game (BFS)
an empty space. The tiles are numbers from the set 1 , .. , n2 −1. For any such board, the empty
space may be legally swapped with any tile horizontally or vertically adjacent to it. In this
assignment, the blank space is going to be represented with the number 0. Given an initial state
of the board, the combinatorial search problem is to find a sequence of moves that transitions
this state to the goal state; that is, the configuration with all tiles arranged in ascending order
2
0 , 1 ,. . , n − 1 .
The search space is the set of all possible states reachable from the initial state. The blank space
may be swapped with a component in one of the four directions {‘Up’, ‘Down’, ‘Left’, ‘Right’}, one
move at a time.
In this 8 puzzle problem is discussed. \ Given a 3×3 board with 8 tiles (every tile has one number
from 1 to 8) and one empty space. The objective is to place the numbers on tiles to match final
configuration using the empty space. We can slide four adjacent (left, right, above and below)
tiles into the empty space. \ For example: \
Breadth First Search (BFS): We can perform a breadth-first search on state space (Set of all
configurations of a given problem i.e. all states that can be reached from the initial state) tree. \
The searches begin by visiting the root node of the search tree, given by the initial state. Among
other book-keeping details, three major things happen in sequence in order to visit a node:
The nodes expanded by BFS (also the nodes that are in the fringe / frontier of the queue) are
shown in the following figure:
Dear Students, in the code below there are few TODO task that you have to complete in this
lab session.
#Import the necessary libraries
from time import time
from queue import Queue
#static method to find the legal action based on the current board
position
@staticmethod
def find_legal_actions(i,j):
legal_action = ['U', 'D', 'L', 'R']
if i == 0:
# if row is 0 in board then up is disable
legal_action.remove('U')
elif i == 2:
#TODO: down is disable
if j == 0:
#TODO: Left is disable
elif j == 2:
#TODO: Right is disable
return legal_action
#method to generate the child of the current state of the board
def generate_child(self):
#TODO: create an empty list
children=None
x = self.state.index(0)
i = int(x / 3)
j = int(x % 3)
#TODO: call the method to find the legal actions based on i
and j values
legal_actions=None
[2, 8, 1,
0, 4, 3,
7, 6, 5],
[2, 8, 1,
4, 6, 3,
0, 7, 5]]
#Iterate over number of initial_state
for i in range(0,3):
#TODO: Initialize the num_of_instances to zero
Puzzle.num_of_instances=None
#Set t0 to current time
t0=time()
bfs=breadth_first_search(state[i])
#Get the time t1 after executing the breadth_first_search method
t1=time()-t0
print('BFS:', bfs)
print('space:',Puzzle.num_of_instances)
print('time:',t1)
print()
print('------------------------------------------')
BONUS TASK: Now, perform the following task before submitting the assignment:
Show the path in traversing the BFS algorithm of each state from intial_state to goal state.