Question

Create the slider puzzle game in python 3, it should be a 3x3 grid and have...

Create the slider puzzle game in python 3, it should be a 3x3 grid and have each square be a number 1 through 8 with one blank square. Only Tkinter and random may be imported. I've attached what I have so far below.

class eight_puzzel:
def _init__(self):
self.moves = dict()
self.moves[0] = [1, 3]
self.moves[1] = [0, 2, 4]
self.moves[2] = [1, 5]
self.moves[3] = [0, 4, 6]
self.moves[4] = [3, 5, 7]
self.moves[5] = [2, 4, 8]
self.moves[6] = [3, 7]
self.moves[7] = [4, 6, 8]
self.moves[8] = [5, 7]

self.config = ['1', '2', '3', '4', '5', '6', '7', '8', ' ']
shuffle(self.config)

def display(self):
if self.window != None:
self.window = Tk()

def gui_move(self, event):
self.move(self.button_locs(event.widget)

def move(self, tile):
tile_loc = self.config.index(str(tile))
blank_loc = self.config.index(' ')
if tile_loc in self.moves[blank_loc]:
self.config[tile_loc] = ' '
self.config[blank_loc] = str(tile)
self.display()

0 0
Add a comment Improve this question Transcribed image text
Answer #1
01 # tiles.py
02 #
03 import sys
04
05 class TileGame :
06     def __init__ (self, sampleBoard="", dim=None) :
07         if len(sampleBoard) == 9 or dim==3 :
08              self.dim = 3;
09              self.legalMoves = legalMoves3; self.goal="12345678_"
10         elif len(sampleBoard) == 16 or dim==4 :
11              self.dim = 4;
12              self.legalMoves = legalMoves4; self.goal="123456789abcdef_"
13         else : scream("Invalid dimension or sample board")
14
15         self.makeManhatten()
16         if sampleBoard and sorted(sampleBoard) != sorted(self.goal) :
17             scream ("Invalid sample board")

    

18
19     def getMoves(self, board) :
20         empty = board.find('_')
21         return empty, self.legalMoves[empty]
22
23     def makeMove(self, board, empty, mov) :
24         lbrd = list(board)
25         lbrd[empty],lbrd[mov] = lbrd[mov],lbrd[empty]
26         return "".join(lbrd)
27
28     def futureCost(self, board) :
29         # estimate future cost by sum of tile displacements
30         future = 0
31         for sq in range(self.dim*self.dim):
32             occupant = board[sq]
33             if occupant != '_' :
34                 shouldbe = self.goal.find(occupant)
35                 future  += self.manTable[(sq,shouldbe)]
36         return future
37
38     def makeManhatten(self) :
39         self.manTable = {}; Dim = self.dim
40         for aa in range(Dim*Dim) :
41             for bb in range(Dim*Dim) :
42                 arow = aa/Dim; acol=aa%Dim
43                 brow = bb/Dim; bcol=bb%Dim
44                 self.manTable[(aa,bb)] = abs(arow-brow)+abs(acol-bcol)
46     def printBoard(self, board, mesg="", sep="\f") :
47         if sep  : print sep
48         if mesg : print mesg
49         expand = ["%02s"%x for x in board]
50         rows = [0]*self.dim
51         for i in range(self.dim) :
52             rows[i] = "".join(expand[self.dim*i:self.dim*(i+1)])
53         print "\n".join(rows)
54
55 def scream(error) :
56     sys.stderr.write("Error: %s\n" % error)
57     sys.stdout.write("Error: %s\n" % error)
58     sys.exit(1)
59
60 legalMoves3 = ( # for a 3x3 board
61    (1,3     ),  # these can slide into square 0
62    (0,4,2),     # these can slide into square 1
63    (1,5),       # these can slide into square 2
64    (0,4,6),     # these can slide into square 3
65    (1,3,5,7),   # these can slide into square 4
66    (2,4,8),     # these can slide into square 5
67    (3,7),       # these can slide into square 6
68    (4,6,8),     # these can slide into square 7
69    (5,7))       # these can slide into square 8
70
71 legalMoves4 = ( # for a 4x4 board
72    (1,4     ),  # these can slide into square  0
73    (0,5,2),     # these can slide into square  1
  ...
85    (9,12,14),   # these can slide into square 13
86    (10,13,15),  # these can slide into square 14
87    (11,14))     # these can slide into square 15
88
89
Add a comment
Know the answer?
Add Answer to:
Create the slider puzzle game in python 3, it should be a 3x3 grid and have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create the slider puzzle game in python 3, it should be a 3x3 grid and have...

    Create the slider puzzle game in python 3, it should be a 3x3 grid and have each square be a number 1 through 8 with one blank square. Each square should be a button in Tkinter. Only Tkinter and Math may be imported. I've attached what I have so far below. class eight_puzzel: def _init__(self): self.moves = dict() self.moves[0] = [1, 3] self.moves[1] = [0, 2, 4] self.moves[2] = [1, 5] self.moves[3] = [0, 4, 6] self.moves[4] = [3, 5,...

  • you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is...

    you will implement the A* algorithm to solve the sliding tile puzzle game. Your goal is to return the instructions for solving the puzzle and show the configuration after each move. A majority of the code is written, I need help computing 3 functions in the PuzzleState class from the source code I provided below (see where comments ""TODO"" are). Also is this for Artificial Intelligence Requirements You are to create a program in Python 3 that performs the following:...

  • Help with Python code. Right now I'm creating a code in Python to create a GUI....

    Help with Python code. Right now I'm creating a code in Python to create a GUI. Here's my code: #All modules are built into Python so there is no need for any installation import tkinter from tkinter import Label from tkinter import Entry def calculatewages(): hours=float(nhours.get()) nsal=float(nwage.get()) wage=nsal*hours labelresult=Label(myGUI,text="Weekly Pay: $ %.2f" % wage).grid(row=7,column=2) return Tk = tkinter.Tk()    myGUI=Tk myGUI.geometry('400x200+100+200') myGUI.title('Pay Calculator') nwage=float() nhours=float() label1=Label(myGUI,text='Enter the number of hours worked for the week').grid(row=1, column=0) label2=Label(myGUI,text='Enter the pay rate').grid(row=2, column=0)...

  • I have this py program and I need assistance using it. I am using Visual Studio...

    I have this py program and I need assistance using it. I am using Visual Studio and I need to know how to: Add a new vehicle Delete a vehicle Update vehicle attributes Print out inventory Here is the code: 1 2 import uuid from datetime import datetime class automobile: 3 4 5 6 7 8 9 def _init__(self,make: str, model: str,color: str,year: int,mileage: int): self._id = uuid uuid1().hex self. make = make self. model = model self. color =...

  • Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste...

    Need help with Intro to Comp Sci 2 (Python) problem: This is what is provided: (Copy/Paste version): from tkinter import Tk, Label, Entry, Button from random import * class Craps(Tk): #set up the main window def __init__(self, parent=None): Tk.__init__(self, parent) self.title('Play Craps') self.new_game() self.make_widgets() #when a new game is started, the firstRoll will start at 0 def new_game(self): self.firstRoll = 0    #create and place the widgets in the window def make_widgets(self): Label(self, text="Die 1").grid(row=0, column=0, columnspan=1) Label(self, text="Die 2").grid(row=0,...

  • ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value):...

    ill thumb up do your best python3 import random class CardDeck: class Card: def __init__(self, value): self.value = value self.next = None def __repr__(self): return "{}".format(self.value) def __init__(self): self.top = None def shuffle(self): card_list = 4 * [x for x in range(2, 12)] + 12 * [10] random.shuffle(card_list) self.top = None for card in card_list: new_card = self.Card(card) new_card.next = self.top self.top = new_card def __repr__(self): curr = self.top out = "" card_list = [] while curr is not None:...

  • Python 3 Code does not save the data into excel properly ​​​​​​​ # required library import...

    Python 3 Code does not save the data into excel properly ​​​​​​​ # required library import tkinter as tk from tkcalendar import DateEntry import xlsxwriter # frame window = tk.Tk() window.title("daily logs") #window.resizable(0,0) # labels tk.Label(window, text="Bar code").grid(row=0, sticky="W", pady=20, padx=20) tk.Label(window, text="Products failed").grid(row=1, sticky="W", pady=20, padx=20) tk.Label(window, text="Money Lost").grid(row=2, sticky="W", pady=20, padx=20) tk.Label(window, text="Failed date").grid(row=3, sticky="W", pady=20, padx=20) # entries barcode = tk.Entry(window) product = tk.Entry(window) money = tk.Entry(window) # arraging barcode.grid(row=0, column=1) product.grid(row=1, column=1) money.grid(row=2, column=1) cal =...

  • Page 3 of 7 (Python) For each substring in the input string t that matches the...

    Page 3 of 7 (Python) For each substring in the input string t that matches the regular expression r, the method call re. aub (r, o, t) substitutes the matching substring with the string a. Wwhat is the output? import re print (re.aub (r"l+\d+ " , "$, "be+345jk3726-45+9xyz")) a. "be$jk3726-455xyz" c. "bejkxyz" 9. b. "be$jks-$$xyz" d. $$$" A object a- A (2) 10. (Python) What is the output? # Source code file: A.py class A init (self, the x): self.x-...

  • Please program this in Visual Basic 6. You have chosen to create an electronic version of the sliding tile puzzle game....

    Please program this in Visual Basic 6. You have chosen to create an electronic version of the sliding tile puzzle game. The object of the game is to slide the tiles so that they end up in the required order. The images shown below are examples of the two different versions of this puzzle (numeric and graphical) Puzzle Board-Numeric Puzzle Board-Graphical File Options Help Elapsed Time File Options Help Elapsed Time 00:02:12 00:04:20 5 6 7 8 9 10 11...

  • PRBE Errors in Python Class and Test Filles. Correct the errors in source code in the...

    PRBE Errors in Python Class and Test Filles. Correct the errors in source code in the O1ympicMeda traditional and unit rt c. Correct class and in the modules olympicmedal. code- do not recopy (5 Py, testi.py, and teat2.py. Mark your corrections directly on the source point penalty for recopying). Adding, deleting, or correcting a pair of parentheses braces ( { }), single quotes (·. double quotes (- .), or triple quotesぃ.. test scripts on pages 6 and 7. There are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT