python之掃雷小遊戲(附程式碼)

LBOcean發表於2020-12-08

這是之前做的一個掃雷小遊戲.程式碼如下(有一些bug請多見諒!)。

#@Time :    2020-12-08
#@Author : LBOcean


"""
物件:
    棋盤:
        屬性:長, 寬, 雷, 盤面
        方法: 生成盤面, 設定雷的個數、長、寬, 翻牌, 判斷輸贏
"""

from random import randint
import sys

class Pan:
    def __init__(self, length=10, width=10, lei_c=10):
        self.__length = length
        self.__width = width
        self.__lei_c = lei_c
        self.__lei_positions = None
        self.__is_clicked = None
        self.__pan = None
        self.left_position = length * width - lei_c
        self.generate_pan()

    def __check(self):
        if self.__length <= 0 or self.__width <= 0:
            return False

        if self.__lei_c >= self.__length * self.__width or self.__lei_c <= 0:
            return False

        return True

    def __generate_lei(self):
        self.__lei_positions = set()
        while True:
            x = randint(0, self.__length - 1)
            y = randint(0, self.__width - 1)
            self.__lei_positions.add((x, y))
            if len(self.__lei_positions) == self.__lei_c:
                break
        self.__lei_positions = list(self.__lei_positions)


    def __cal_num_of_position(self, x, y):
        res = 0
        for i in range(x-1, x+2):
            for j in range(y-1, y+2):
                if i >= 0 and i < self.__length and j >= 0 and j < self.__width:
                    if self.__pan[i][j] == -1:
                        res += 1

        return res
        pass

    def __generate_num(self):
        self.__pan = [[0 for i in range(self.__width)] for j in range(self.__length)]
        for p in self.__lei_positions:
            self.__pan[p[0]][p[1]] = -1  # -1代表雷
        for i in range(self.__length):
            for j in range(self.__width):
                if self.__pan[i][j] != -1:
                    self.__pan[i][j] = self.__cal_num_of_position(i, j)

    def generate_pan(self):
        if self.__check():
            # self.left_position = self.length * self.width - self.lei_c
            self.__generate_lei()
            self.__generate_num()
            self.__is_clicked = [[False for i in range(self.__width)] for j in range(self.__length)]
        else:
            print("引數設定錯誤")

    def print_pan(self):
        print("當前的盤面:")
        print(end='\t')
        for i in range(self.__width):
            print(i, end='\t')
        print()
        for i in range(self.__length):
            print(i, end='\t')
            for j in range(self.__width):
                if self.__is_clicked[i][j]:
                    if self.__pan[i][j] == -1:
                        print('*', end='\t')
                    else:
                        print(self.__pan[i][j], end='\t')
                else:
                    print('#', end='\t')
            print()
        print()

    def continue_act(self):
        self.print_pan()
        self.click()

    def print_all_pan(self):
        self.__is_clicked = [[True for i in range(self.__width)] for j in range(self.__length)]
        self.print_pan()

    def click(self):
        print("請輸入下次翻開的座標:")
        x,y = input().split(' ')
        x = int(x)
        y = int(y)
        # 判斷合法性

        self.__is_clicked[x][y] = True
        if self.__pan[x][y] == -1:
            print("輸了")
            self.print_all_pan()
            print("是否再次嘗試")
        else:
            self.left_position -= 1
            if self.left_position == 0:
                print("贏了")
                self.print_all_pan()
            else:
                self.continue_act()
        self.re_run()
    def re_run(self):
        next =input('重新開始請輸入R,退出遊戲請輸入D:\n')
        if next == 'R':
            print('下一局開始請設定盤面和雷的個數:\n')
            self.nextGame()
        elif next == 'D':
            print('遊戲結束')
            sys.exit(0)
        else:
            print('玩我呢,滾!滾!滾!')
            print('遊戲結束!拜拜了您嘞~')
            sys.exit(0)


        # 下一局初始化資訊


    def nextGame(self):
        pan = Pan(int(input()),int(input()),int(input()))
        pan.start()

    def start(self):
        self.continue_act()

pan_1,pan_2,pan_3 = int(input('請分別輸入盤的長、寬、雷的數量,並用,分隔:\n').split(','))
pan = Pan(pan_1,pan_2,pan_3)
pan.start()



以下是執行的結果。

在這裡插入圖片描述

在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

如有什麼問題可以留言噢!

相關文章