Python implementation, command line puzzle game

Source: Internet
Author: User

Python implementation, command line puzzle game
Requirements:

Now we want to make such a jigsaw puzzle game. There is a nine-square cell with eight blocks and a vacant space. Click one of the blocks and it will move to the vacant space... game clearance is performed when eight squares are arranged in sequence

Structure planning:

Game Data Structure

Player operations

Logic

Customs clearance Detection

Output part

Main Program

Concept:
  1. Use the letters A, B, C,... H to represent eight squares. The letter I represents 9th cells, that is, space, which is essentially A square.

    The data structure of the puzzle is composed of a series of dictionary elements: {1: [...], 2: []..., 9: []}, where KEY number indicates the correct location information

    A dictionary element is a list containing: [tile symbols, real-time location information, status signs (empty/non-empty)]

    When you click a square in one of the positions, if the surrounding square has KEY x or y [n + 1] [2] = 0, modify the current location information and status flag of the square and space, that is, exchange the current location information and status flag with KEY x or y. (In turn, it can be detected that the logo is 0 and the surrounding movable square is also detected)

    Until all elements are verified by correct location information, the KEY9 State remains 0 and it is an empty square.

    Implemented in Python. Operations:Enter A block (A), move the block (A) of the program, and print the result. The source code is as follows:

    #! /Usr/bin/python #-*-coding: UTF-8-*-importrandomimportsysfromcopyimportdeepcopy # generate random puzzles jigsaw = [chr (I) foriinrange ()] random. shuffle (jigsaw) palace ={} foriinrange (9): ifjigsaw [I] = 'I': palace [I + 1] = (['', I + 1, 0]) else: palace [I + 1] = ([jigsaw [I], I +]) # Test Data # palace = {1: ['A',], 2: ['B',], 3: ['C',], 4: ['D',], 5: ['E',], 6: ['F', 6, 1], 7: ['G', 7, 7], 8: ['', 8, 0], 9: ['h ', 9, 1]} # Move the square defmove_square (square, pledic): in_s Witch = bool () zero_switch = bool () fork, vinpledic. items (): pledic [k] [1] = k # player operation, move the square (input character) ifv [0] = square: in_key = kin_val = vin_switch = True # locate the character and Mark ifv [2] = 0: zero = v [1] zero_k = kzero_v = vzero_switch = Trueifzero_switch = in_switch: # swap position with empty squares ifzero % 3 = in_val [1] % 3 andabs (zero-in_val [1]) <= 3: pledic [in_key], pledic [zero_k] = zero_v, in_valifabs (zero-in_val [1]) = 1and (zero + in_val [1])-1) % 3! = 0: pledic [in_key], pledic [zero_k] = zero_v, in_valreturnpledic # Game detection defcheck_palace (dic): right_count = 0 ifdic [9] [2] = 0: fork, vindic. items (): ifk = ord (v [0])-64: right_count + = 1ifright_count = 8: returnTruereturnFalse # output defprint_palace (dic ): printdic [1] [0], dic [2] [0], dic [3] [0] printdic [4] [0], dic [5] [0], dic [6] [0] printdic [7] [0], dic [8] [0], dic [9] [0] # defplay_palace (palace ): ple = deepcopy (palace) print_palace (ple) play_dic ={} WhileTrue: try: square = raw_input ('inputa-H: ') iford (square. upper ()> = 65 andord (square. upper () <= 74: play_dic = move_square (square. upper (), ple) print_palace (play_dic) ifcheck_palace (play_dic): breakelse: print 'pleaseagainput' into ttypeerror: print 'pleaseagainput' continueprint "AllRight! YouWin !! "Sys. exit () if _ name __= = '_ main _': play_palace (palace)

     

    The final execution result is as follows:

    $python9palace.pyDFGCHEABinputA-H:fDFGCHEABinputA-H:cDCFGHEABinputA-H:

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.