工具来模拟一个扫雷游戏并以编程方式与之交互。

minesweeper-model的Python项目详细描述


扫雷艇模型

PyPI Version

此包是没有前端的Minesweeper game。换句话说,它只提供了一个扫雷模型和一个接口来访问和修改雷区或“玩”游戏编程。因此,这个软件包并不是一个最终用户可以玩的扫雷游戏。在

这套方案主要是为开发扫雷解决方案的人准备的。任何开发这样一个求解器的人都可以使用这个软件包跳过开发扫雷舰模型的步骤来测试他们的解算器,并立即开始对解算器本身进行工作。在

安装

包托管在PyPI。您可以使用pip安装它:

pip install minesweeper-model

使用

这个包提供了一个Field类,该类为雷区游戏中的雷区建模。它存储了一个领域的所有必要状态:开放的瓷砖、有标志的瓷砖、下面有地雷的瓷砖等等。根据游戏规则,玩家不允许知道地雷藏在哪里。在

Field类具有允许作为玩家与之交互的方法(即,只向您提供提示,即磁贴周围的地雷数量,而不是地雷位置)。当然,如果需要,也可以直接进入矿区。在

初始化

^{pr2}$

存储在Field

中的数据
# List of tuples (x, y) of mine coordinates.field.mine_coords# List of tuples (x, y) of coordinates which have been opened by the player.field.open_coords# List of tuples (x, y) of coordinates which have been flagged as having a mine.field.flag_coords# Dictionary populated with the "hint" for all tiles in the Field given# during initialization of the `PlayerField` class. This is the number # of mines in the surrounding 8 tiles for each tile. Dictionary is in the # format `{(x, y): int}` where int is the hint with possible values # being 0 to 8 or -1 (for tiles which have mines underneath)field.hints

Field

交互

使用tilenine_tiles方法分别获取单个字段和九个字段组的信息。你只会收到玩家会得到的信息(即除了地雷的位置以外的任何信息)

# Return a dictionary {"hint": int, "flag": bool} where# "hint" is the hint revealed by openning the tile, or# the value None if the tile is closed. "flag" is whether# the tile is flagged.field.tile(x,y)# Returns the same data as `tile` method for the given# tile as well as the 8 tiles surrounding it.# Data is returned in format: {(x, y): {"hint": int, "flag": bool}, ...}field.nine_tiles(middle_x,middle_y)

打开瓷砖并在字段中放置标志:

# This will open the tile in given coordinates. Will return# True if tile does not hide a mine, False otherwise.# If tile has no mine, given coordinates will be added to open_coords.field.open_tile(x,y)# Pass True to the third coordinate so that in addition to the given tile# being opened, all the adjacent tiles with a hint of 0 will also be opened,# similar to the way it does in some versions of the Minesweeper game. field.open_tile(x,y,True)# Add or remove flag on tile.field.toggle_flag(x,y)

场的视觉表现

绘制显示矿井位置的字段:

print(field.render_mines())# x...# ...x# ....# .x..# You can use custom strings when drawingprint(field.render_mines(tile_str="o",mine_str="@"))# @ooo# ooo@# oooo# o@oo

绘制玩家看到的区域,只显示提示和旗帜,但不显示地雷:

print(field.render_player_field())# !...# 22..# xx..# ....# You can use custom strings when drawingprint(player_field.render_player_field(flag_str="?",closed_str="o"))# ?ooo# 22oo# xxoo# oooo

发电机公用设施

该软件包还包含帮助您生成雷区的实用程序。在

对于给定的N个矿场大小,随机生成多个矿场:

fromminesweeper_modelimportgenerator# generate 4 mine coordinates # for a field of size (5, 10)generator.random_mine_coords(5,10,4)# => [(1, 5), (3, 2), (0, 0), (1, 6)]

为给定矿坐标列表的单个平铺生成提示:

mines=[(4,1),(6,4)]# Generate hint for a tile at (1, 1)generator.hint_for_tile(1,1,mines):# => 0

为整个字段生成提示:

mines=[(4,1),(6,4)]# Generate hints for a field of size (10, 10)generator.hints_for_field(10,10,mines)# => {(0, 0): 0, (0, 1): 0, ... , (9, 9): 0}

通用设施

获取周围瓷砖的坐标:

fromminesweeper_modelimportutilityutility.surrounding_tiles(1,1)# => [(0, 0), (0, 1), (0, 2), (1, 0), #     (1, 2), (2, 0), (2, 1), (2, 2)]# If the tile is on the edge, method will# return -ve coordinates as well.utility.surrounding_tiles(0,0)# => [(-1, -1), (-1, 0), (-1, 1), (0, -1), #     (0, 1), (1, -1), (1, 0), (1, 1)]# You can choose to remove -ve coordinates:utility.surrounding_tiles(0,0,True)# => [(0, 1), (1, 0), (1, 1)]

将字段的文本表示转换为可以传递给Field对象的参数,这是创建字段的一种更简单的方法(感谢@27Anurag的贡献):

str_field="""..x...x...........x........xx......"""width,height,mine_coords=utility.str_input_to_mine_coords(str_field)# => width: 7, height: 5, mine_coords: [(2, 0), (6, 0), (4, 2), (6, 3), (0, 4)]f=field.Field(width,height,mine_coords)

发展

  • 该包目前仅正式支持python3。在
  • 测试是用unittest编写的,位于“Tests”目录下。您可以使用python3 -m unittest在命令行上运行所有测试。在
  • ^{}用于检查代码是否遵循一些Python最佳实践和样式标准。您可以使用flake8在命令行上运行检查(需要先安装flake8)。在
  • tox用于自动测试。您可以使用tox在命令行上运行所有测试和检查(例如flake8)(需要先安装tox)。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java StoredProcedureCall 1x Varchar输出1x游标输出   java StackOverflower运行时错误   算法Java基准测试:确保对象在超出范围后不被重用   java在SpringDataNeo4j中使用RelationshipEntity保存节点的正确方法是什么?   命令行参数设置登录java的属性   Java泛型AnyType,如何允许调用任何方法?   java如何检查Html字符串和字符串   sql如何插入一个日期,然后从java执行的存储过程中向oracle数据库中插入的日期添加小时、分钟和秒   java 安卓 3布局相同的活动   安卓应用程序中的java毕达哥拉斯   使用javaw在批处理文件中运行JAR的服务会在process explorer XYNTService中产生多个java进程   java需要在Derby中编写正确的查询   多线程Java在缓存中为多个线程保存变量   持久化java实体引用问题   java在SpringMVC应用程序中使用本地线程安全吗?   JavaSwing,100个文本字段都有类似的任务,所以我想写一个函数来完成这个任务   java我们在新字符串(“literal”)中放置的字符串文字发生了什么变化;   java注入需要在GUI中使用枚举的对象   在Spark SQL中加载JDBC表时java数据不正确