【写在前面】
之前帮助一个留学生朋友完成了一个利用Python的2维列表实现一个类似消消乐的小游戏,其中综合运用了python语言的列表、判断语句、循环以及函数等语法知识,很适合初学者用来练手并且检验自己的学习效果。
【要求说明】 2D-list说明文档
【代码】
import copyimport randomSIZE = 7 # define the global variable SIZE and assign a valueLEN = 5 # define the global variable LEN and assign a value''' Creates a 2D-List board of SIZE x SIZE.With probability ~33% each cell of board stores a symbol from the set ($, #, *).Finally returns the board. '''def create_board(SIZE): board = [] for _ in range(SIZE): row = [] for _ in range(SIZE): if random.randint(1,10) > 7: row.append("$") elif random.randint(1,10) > 3: row.append("#") else: row.append("*") board.append(row) return board## Add (and test) all required functions here ### ----------------------------------------------def print_rules(): # Print game rules print(" *** Match the Symbols *** ") print("Rules:") print("** Find a horizontal/vertical line of length at least {} consisting of the same symbols.".format(LEN)) print("-- Select 'M'/'m' to enter the position 'row/col' of the starting symbol of that line.") print("-- score 1 point for each symbol on that line.") print("-- Also score points for all horizontal/vertical neighbours (symbols IDentical to point at 'row/col').") print("** Can't find a matching line and want to change an existing symbol?") print("-- Select 'C'/'c' to enter the new symbol, but you will lose 2 points each time.") print("-- If you can finish the board you'll get 10 additional points.") print("** You can quit anytime by selecting 'Q'/'q'.") print("--------------------------------------------------------------------------------------------------------------\n")def print_board(board_create): # Print your board print("Your Board:\n") head = [str(i) for i in range(SIZE)] print(" " + " " + "".join(head) + " " + " ") print("-" * (SIZE + 4)) # Use the loop to print out the 2-Dimentional Lists in accordance with the format for i in range(SIZE): print(str(i) + "|" + "".join(board_create[i]) + "|" + str(i)) print("-" * (SIZE + 4)) print(" " + " " + "".join(head) + " " + " ")def take_input(): row = input("row: ") while True: # Determine if the row entered by the user is valID row = int(row) if ((row < 0) or (row > (SIZE-1))): print("InvalID row selected") row = input("row: ") else: break col = input("col: ") while True: # Determine if the column entered by the user is valID col = int(col) if ((col < 0) or (col > (SIZE-1))): print("InvalID column selected") col = input("col: ") else: break return (row, col)def check_matching(row, col, board): new_board = copy.deepcopy(board) # Make a deep copy of the original board rrow = row ccol = col original_rrow = copy.deepcopy(rrow) original_ccol = copy.deepcopy(ccol) string = board[original_rrow][original_ccol] # Gets the character specifIEd by the user count = 0 count_top = 1 count_under = 1 count_left = 1 count_right = 1 rrow = original_rrow - 1 while (rrow >= 0): # Looks to the top of the user-specifIEd character if new_board[rrow][original_ccol] == string: count_top = count_top + 1 rrow = rrow - 1 else: break rrow = original_rrow + 1 while (rrow <= (SIZE - 1)): # Looks to the under of the user-specifIEd character if new_board[rrow][original_ccol] == string: count_under = count_under + 1 rrow = rrow + 1 else: break ccol = original_ccol - 1 while (ccol >= 0): # Looks to the left of the user-specifIEd character if new_board[original_rrow][ccol] == string: count_left = count_left + 1 ccol = ccol - 1 else: break ccol = original_ccol + 1 while (ccol <= (SIZE - 1)): # Looks to the right of the user-specifIEd character if new_board[original_rrow][ccol] == string: count_right = count_right + 1 ccol = ccol + 1 else: break # Determine whether horizontal or vertical line of length LEN has started from that given location and replaces them if ((count_top >= LEN) or (count_under >= LEN) or (count_left >= LEN) or (count_right >= LEN)): count = count + 1 count_top = 0 count_under = 0 count_left = 0 count_right = 0 rrow = original_rrow - 1 while (rrow >= 0): if new_board[rrow][original_ccol] == string: new_board[rrow][original_ccol] = '.' count_top = count_top + 1 rrow = rrow - 1 else: break count = count + count_top rrow = original_rrow + 1 while (rrow <= (SIZE - 1)): if new_board[rrow][original_ccol] == string: new_board[rrow][original_ccol] = '.' count_under = count_under + 1 rrow = rrow + 1 else: break count = count + count_under ccol = original_ccol - 1 while (ccol >= 0): if new_board[original_rrow][ccol] == string: new_board[original_rrow][ccol] = '.' count_left = count_left + 1 ccol = ccol - 1 else: break count = count + count_left ccol = original_ccol + 1 while (ccol <= (SIZE - 1)): if new_board[original_rrow][ccol] == string: new_board[original_rrow][ccol] = '.' count_right = count_right + 1 ccol = ccol + 1 else: break count = count + count_right new_board[original_rrow][original_ccol] = "." print("You successfully matched {} symbols at this step!".format(count)) return (True, count, new_board) else: print("No {} matching symbols in any direction! You lost 2 points.".format(LEN)) count = count - 2 return (False, count, board)def is_board_finished(board): num = 0 for i in range(SIZE): for j in range(SIZE): if board[i][j] != ".": num = num + 1 # Determine if the player has completed the board if (num == (SIZE * SIZE)): print("Good job! You finished the board!") print("You got 10 more points!!") return True else: return False# ----------------------------------------------def main(): # add logics and call functions as required print_rules() board_create = create_board(SIZE) print_board(board_create) sum = 0 copy_board = copy.deepcopy(board_create) choose = input("[M]atch the symbols, [C]hange a symbol, or [Q]uit the game? ") while True: if ((choose == 'm') or (choose == 'M')): input_row, input_col = take_input() matching_score = check_matching(input_row, input_col, copy_board) if matching_score[0]: sum = sum + matching_score[1] print("Your new score: {}".format(sum)) print_board(matching_score[2]) copy_board = matching_score[2] if is_board_finished(copy_board): print("Your final score is {}".format(sum + 10)) break choose = input("[M]atch the symbols, [C]hange a symbol, or [Q]uit the game? ") else: sum = sum + matching_score[1] print("Your new score: {}".format(sum)) print_board(matching_score[2]) choose = input("[M]atch the symbols, [C]hange a symbol, or [Q]uit the game? ") elif ((choose == 'c') or (choose == 'C')): input_row, input_col = take_input() input_symbol = input("symbol? ") while True: if input_symbol == ".": print("InvalID symbol, try again!") input_symbol = input("symbol? ") else: print("You've successfully changed a symbol, but you lost 2 points.") sum = sum - 2 print("score: {}".format(sum)) break copy_board[input_row][input_col] = input_symbol print_board(copy_board) choose = input("[M]atch the symbols, [C]hange a symbol, or [Q]uit the game? ") elif ((choose == 'q') or (choose == 'Q')): print("Board is not finished yet!") print("Your final score is {}".format(sum)) break else: print("InvalID selection, try again!") choose = input("[M]atch the symbols, [C]hange a symbol, or [Q]uit the game? ")# -----------------------------------------------#----------------- Do not change --------------------#''' Ensures that the main() function is called when a3.py is executed as a stand-alone program'''# ----------------------------------------------------if __name__ == "__main__": main()
【效果演示】(注:长截屏过程中,有部分内容被折叠,读者可以将代码复制到IDE中自己运行,观察效果)
以上是内存溢出为你收集整理的利用Python的2维列表实现一个类似消消乐的小游戏全部内容,希望文章能够帮你解决利用Python的2维列表实现一个类似消消乐的小游戏所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)