简单扫雷游戏中的编程方法学 - python实现

5 篇文章 0 订阅
3 篇文章 0 订阅

一、说明

该题来自国外大学的一个课堂作业,需求文档中要求采用一种自底向上(bottom-up)编程方法,先实现最基本的函数,然后编写高层一点的函数来调用这些基本函数,先细节再慢慢抽象。为了引导学生,文档把程序细化为了六个level(每一个level,均有该level的功能说明,非常详细),先是基础功能,然后逐渐增加功能,逐步求精,直到整个功能完全实现。同时另一份文档还给出了简单的示例(如输入、输出等)。基本上,凭借文档,即使你以前没有玩过扫雷游戏(向我一样),你也可以把这个程序很有条理的写出来,因为有详细的需求规则说明书。然后你将深深地体会到“自底向上”编程方法以及文档化所带来的好处。故,不在于游戏本身(图形界面都没有!就是一玩具游戏),本文的目的就是:学习一种自底向上的编程方法,本文给出的仅是一个例子,希望通过分享,读者可以从中学习、体会该编程方法的思想(建议自己编程实现一遍)。

:完整的文档(.txt文件)已上传:在这里 可以下载,如图:



二、扫雷游戏规则

The game consists of a board with the board divided into a grid. Throughout the grid, mines are scattered. 
At the beginning of the game, all squares of the grid are covered, obscuring the location of the mines. 
The object of the game is to uncover all the squares on the board that do not hold mines, avoiding uncovering those squares that do. Squares are uncovered one at a time.

When a square is uncovered, three things can happen. If the square holds a mine, the mine blows up and the game ends. If the square does not hold a mine, but is neighbors with a square that does, then the number of neighbors that hold mines is shown in the uncovered square. If there are no adjacent mines, then all neighboring squares are also uncovered. If any of the newly uncovered squares are not adjacent to mines, then their neighbors are uncovered, and so on. Each square has eight neighbors, unless it is on the edge of the board. In that case, it will have fewer neighbors.


三、示例

The number of rows in grid, the number of columns in the grid, and the number of mines are passed as command line arguments. The program then randomly (or not) places the mines and prints out a representation of the board with all squares covered. It then goes into a loop, prompting for a row and column of the first/next square to be uncovered. For each square location entered by the user, the program displays the updated board if the square did not hold a mine or an impressive pyrotechnic display if it did.

Compliance Instructions

Your program should run like this, but look better:

    $ python3 minesweeper.py 5 9 5

    Here is the minefield:

       0  1  2  3  4  5  6  7  8
    0 [-] [-] [-] [-] [-] [-] [-] [-] [-]
    1 [-] [-] [-] [-] [-] [-] [-] [-] [-]
    2 [-] [-] [-] [-] [-] [-] [-] [-] [-]
    3 [-] [-] [-] [-] [-] [-] [-] [-] [-]
    4 [-] [-] [-] [-] [-] [-] [-] [-] [-]

    Which square do you wish to uncover?
    Enter a row and column: 1 7

       0  1  2  3  4  5  6  7   8
    0 [-] [-] [-] [1] [ ] [ ] [ ] [ ] [ ]
    1 [-] [-] [-] [1] [ ] [ ] [ ] [ ] [ ]
    2 [-] [-] [-] [1] [2] [2] [1] [ ][ ] 
    3 [-] [-] [-] [-] [-] [-] [2] [1][1]
    4 [-] [-] [-] [-] [-] [-] [-] [-] [-]

    Which square do you wish to uncover?
    Enter a row and column:

四、程序的完整代码(编程环境python3.3):在这里可以下载(即上图中的.py文件)


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值