python之扫雷小游戏(附代码)

这是之前做的一个扫雷小游戏.代码如下(有一些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()



以下是运行的结果。

python之扫雷小游戏(附代码)_第1张图片

python之扫雷小游戏(附代码)_第2张图片
python之扫雷小游戏(附代码)_第3张图片
python之扫雷小游戏(附代码)_第4张图片

如有什么问题可以留言噢!

你可能感兴趣的:(pyhton,python,算法)