Question
I have to make a "Sliding Puzzle" program In Python
So I'm stuck on these subprograms...
You have to use the 2 Dimensional array (List of list) to simulate this game. The 2 Dim array will be used to store the names the of pictures files. Again you are using a 2 Dim array with size 4 * 4. 4. To fill the 2 Dim array, you have to read a text file that contains the names of the pictures file (all on one line separated by a comma). You have to do that through calling a function.
(1)Write a function called LoadPictureNames that receive a string parameter that represents a file name (which contains the names of all pictures files names) and it will return back a list of List 4* 4 that contains these names. The passed file contains 16 filenames separated by a comma.
(2) You have to write a function called DrawPictures that accept four parameters; the first one is a string that represents the name of a picture file. The second and the third parameters are integers which represent the location where you need to center the picture, and the last one represents the GraphWin object which was previously defined in the main function. The function’s purpose is to load the picture at the specified location.
Thanks in advance for your help!
Explanation / Answer
from graphics import * import random # ALWAYS initialize all globals at the top of your program! win = None winning = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, " "] # We'll use 3 one-dimensional lists # buttons - keep track of the board # rects - the rectangles that represent the buttons # symbols - the characters that are drawn on the squares buttons = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, " "] rects = [] symbols = [] # Function: make_buttons # Parameter: None # Returns: None # Description: modifies the three global lists that represent the board def make_buttons(): global rects, symbols, buttons indx = 0 # create 16 board square graphics objects for y in range(0, 350, 100): for x in range(0, 400, 100): # make a rectangle for the square rect = Rectangle(Point(x, y), Point(x+100, y+100)) rect.setFill("grey") rect.draw(win) rects.append(rect) # make a symbol for the square symbol = Text(Point(x + 50, y + 50), buttons[indx]) symbol.setSize(36) symbol.draw(win) symbols.append(symbol) # an index to keep track of our board position indx = indx + 1 # Function: is_legal # Parameter: the index of the blank, the index of a possible move # Returns: Boolean # Description: determines if the move is legal based on the blank position # and considering the edges of the board def is_legal(move, blank): # move is to the left of the blank, unless we're on the left column (0) if move == blank - 1 and blank % 4 != 0: return True # move is to the right of the blank, unless we're on the right column (3) if move == blank + 1 and blank % 4 != 3: return True # if neither above is True, then check if move is above or below the blank return move == blank - 4 or move == blank + 4 # Function: make_move # Parameter: the index of the blank # Returns: the index of a legal move # Description: receives user clicks until a legal move is selected def make_move(blank): global board move = -1 while move == -1: # determine the move where = win.getMouse() col = where.x // 100 row = where.y // 100 move = row * 4 + col # if the move is on the board and legal, make it if 0