利用Python的2维列表实现一个类似消消乐的小游戏
2021/4/20 12:25:08
本文主要是介绍利用Python的2维列表实现一个类似消消乐的小游戏,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
【写在前面】
之前帮助一个留学生朋友完成了一个利用Python的2维列表实现一个类似消消乐的小游戏,其中综合运用了python语言的列表、判断语句、循环以及函数等语法知识,很适合初学者用来练手并且检验自己的学习效果。
【要求说明】 2D-list说明文档
【代码】
import copy import random SIZE = 7 # Define the global variable SIZE and assign a value LEN = 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维列表实现一个类似消消乐的小游戏的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型
- 2024-12-23使用python部署一个usdt合约,部署自己的usdt稳定币
- 2024-12-20Python编程入门指南