1Jan

Slide Puzzle Python Codes

1 Jan 2000admin

I've implemented the sliding blocks puzzle in Python to solve it using different algorithms. I'd like to know if the class 'Sliding_blocks' is nicely designed or if I am missing concepts of OOP. I think that the indices of the blocks are a bit obfuscated, because I mix tuples, lists and arrays (arrays are better to add but then I have to convert them to tuples at some point). I use the A* algorithm with the number of misplaced blocks as heuristic function. It's quite slow and I think's it's because I have to create many copies of objects. I'm more concerned with best practices than speed.

Drama taiwan fated to love you subtitle indonesia goblin full. Whether the code is well designed or not -- is hard to tell without using it. But I have several points: • It's good that you have small functions/methods which do one thing and do well. It's good that you wrote docstrings and comments. • shortest=[i for i,l in enumerate(lengths) if l have two different lines. Np.arange(1,self.size**2) use one space after commas.

Guessing Game written in Python Guessing Game This script is an interactive guessing game, which will ask the user to guess a number between 1 and 99. We are using the random module with the randint function to get a random number. 15 Puzzle Game You are encouraged to solve this task according to the task description, using any language you may know.

Game = Sliding_blocks() -- only classes should named CamelCased -> game = SlidingBlocks(). Use pylint to see all warnings. • Use enumerate: for i in range(len(possible)): actions.append(deepcopy(obj)) actions[i].move(possible[i]) Will become: for i, _possible in enumerate(possible): action = deepcopy(obj) actions.append(action) action.move(_possible) • Move the imports to the top of the file def rand_move(self): from random import choice • Import module instead of objects from it.

From itertools import product. From random import choice Will become: import copy import itertools import random • Using constants? Name them uppercase and make global.

Is it a setting? Pass it as an argument with a default value or store as a class attribute. Def shuffle(self): steps = 30 for i in xrange(steps): self.rand_move() • Fail early.

If list(piece) in self.find_moves(): self.block[tuple( self.find_free() )] = self.block[tuple(piece)] self.block[tuple(piece)] = 0 return 'success' else: return 'error' This can be rewritten as if list(piece) not in self.find_moves(): return 'error'. Return 'success' This will also reduce indentation. Return 'error' -- this is a hardcoded string. Use a constant. • Indeed, using tuples and lists is hard to read and is slow if list(piece) in self.find_moves(): self.block[tuple( self.find_free() )] = self.block[tuple(piece)] self.block[tuple(piece)] = 0 You should rethink this.