AI 代写 Minesweeper AI Agent

I. Introduction

In this programming assignment, you will be tasked with implementing a Minesweeper AI Agent, which should be able to play and solve the Minesweeper game. You will have the choice of programming in C++, Java, or Python. Much of the code has already been written for you. Your agent should be able to take in percepts and act accordingly. Your grade will depend on your agent’s performance measure. At the end of the quarter, your agent will compete against your peers’ agents in a class-wide tournament.

II. Minesweeper Game Mechanics

This version of Minesweeper is based on the classic computer game. You can easily find out the details of the game from the web, for example: https:// en.wikipedia.org/wiki/Minesweeper_(video_game)

However, there are a few differences designed to better evaluate your agent which will become apparent as you play with and familiarize yourself with the game. In Minesweeper, you are given a board that is set up as a 2D grid of tiles. Each tile covers either: (1) a hint number ( that tells how many mines are around that tile ) or (2) a mine. Ultimately, your agent’s goal is to uncover all tiles which do not contain a mine. A more concrete definition of the game is given by the following PEAS description.

Performance Measure

● The performance measure of your agent will be a score calculated based on number of worlds your agent has completed. Points are awarded to your agent only if it successfully solves the entire world. Each difficulty has different weight. ● The game ends when your agent chooses to leave the game or if your agent uncovers a mine. In either of these cases you’ll get a zero.

Environment

● Each difficulty has a different dimension and number of mines: ○ Beginner: 8 row x 8 column with 10 mines
○ Intermediate: 16×16 with 40 mines
○ Expert: 16×30 with 99 mines

● The board begins with 1 random tile already uncovered and presumably safe ● Mines are randomly placed throughout the board.
● Your agent dies when it uncovers a mine.

3

Actuators

● Your agent has 4 moves:
○ (1) The action UNCOVER reveals a covered tile.
○ (2) The action FLAG places a flag on a tile.
○ (3) The action UNFLAG removes a flag from a tile if that tile has a flag. ○ (4) The action LEAVE ends the game immediately.

● The actions UNCOVER, FLAG, and UNFLAG are to be coupled with a pair of coordinates which allows the agent to act on a single tile.

Sensors

● Your agent will receive only one percept:
○ Following an UNCOVER action, your argent will perceive the hint

d.

number associated with the previous UNCOVER action. This number

represents how many mines are within that tile’s immediate neighbors. ○ Following a FLAG or UNFLAG action, your agent will perceive -1.

4

Program Your AI

Once you have your environment setup, you can start to program your agent. In
the ‘src’ folder of your shell you will find the source code of the project. You are only allowed to make changes to the MyAI class. You are not allowed to copy source codes from any source because it violates Academic Honesty, however, you are allowed to include standard libraries.

Compile Your AI

Compiling your program is easy as executing the command make from the shell’s root directory (the directory with the makefile in it).

Test Your AI

To run your program after you have compiled it, navigate to the bin folder. You should find the compiled program inside. Refer to the Shell Manual Appendix for help running it. To generate large amounts of worlds to use with the folder option, refer to the World Generator. If you are using the Python Shell make sure you are using Python 3.5.2. On openlabs, run the command “module load python/3.5.2” to load Python 3.5.2.

5

IV. WorldGenerator Manual

The Minesweeper World File

If you’d like to create your own custom Minesweeper World, you can create a text file using the following format:

[rowDimension][space][colDimension] [startingRow][space][startingColumn]
 [2D grid of board]

• “StartingRow” and “startingColumn” represent the coordinates of the first tile that the world uncovers for you. This feature is designed to guarantee your agent will be safe on its’ starting tile.

• The 2D grid should be a sequence of 0’s and 1’s. Columns are separated by a single space while rows are separated by a new line. 0’s represent safe tiles while 1’s represent tiles with mines. The bottom left-most tile should be interpreted as (1, 1). When selecting starting tile, the tiles around it will always be safe, namely 0.

• If your 2D grid does not contain at least a 3×3 square of 0’s ( because the starting tile must be a safe tile ), the world is considered invalid.

Using the Python script

● There is a Python script “WorldGenerator.py” that you can use to easily generate a set of worlds in the form of txt files.

● If you see “Error opening file” in your console when running the script, that means you haven’t create a folder called “Problems” in the directory you are running this script, hence the script fails to locate.

6

● To run the script, issue the command:

python3 WorldGenerator.py [numFiles] [filename] [rowDimension]
[colDimension] [numMines]

The arguments in square brackets are in that order and represent the following:

numFiles filename rowDimension colDimension numMines

– The – The – The – The – The

number of files to generate base name of the file number of rows
number of columns number of mines

certain restrictions, which are listed

Note that all arguments are required and have below:

(1) The minimum number of rows is 4.
(2) The minimum number of columns is
(3) The minimum number of mines is 1.
(4) The number of mines must also be less than or equal to

(rowDimension)*(colDimension) – 9

If any of these conditions are not met, the script will not generate any worlds. Another thing to note is that you can only generate a set worlds of the same dimensions. In order to generate worlds of different sizes, you need to rerun the script with different command-line arguments.

4.

7

Using the bash script

● Another way to generate worlds is to use the bash script. There are two bash scripts provided. You can easily change the script to generate a different number of worlds or worlds of different dimensions. You may also write your own bash script.

● To run the scripts, issue the command: ./generateTournament

This script will be used to generate the tournament set of worlds and is a good simulation for your agent’s actual performance in the tournament. This script creates a “Problems” folder and generate a random set of 1000 Beginner, 1000 Intermediate, and 1000 Expert worlds.

./generateSuperEasy

This script will be used to generate the set of worlds for the Minimal submission. It generates 1000 random Easy worlds.

● Run chmod +x generateTournament.sh or chmod +x generateSuperEasy.sh if you run into a “permission denied” message

● As a side note, these bash scripts are written to automatically delete any previous file/ folder called “Problems” and create a new one.

Example World

A txt file like this:

88
13
 00111010 00000000 00000000 00000000 00100000 00000001 00001010 00100010

will generate ( in console ):

8

Remember that arrays start with the index 0 while the minesweeper’s dimension starts with the index of 1.

9

VI. Appendix: Shell Manual

Synopsis

[Name] [Options] [InputFile] [OutputFile]

Name

The command line name used to invoke this program will change depending on the shells:

python3 Main.pyc
java -jar mine.jar
./Minesweeper

Options

if using python shell if using java Shell
if using cpp shell

-m Use the ManualAI instead of MyAI. If both –m and –r specified, ManualAI will be turned off.
-r Use the RandomAI instead of MyAI.
-d Debug mode, which displays the game board after every move.

-v Verbose mode, which displays name of world files as they are loaded.
-f Depending on the InputFile format supplied, this operand will trigger program 1) Treats the InputFile as a folder containing many worlds. The
program will then construct a world for every valid world file found. The program to display total score instead of a single score. The InputFile operand must be specified with this option 2) Threats the inputFile as a file. The program will then construct a world for a single valid world file found. The program to display a single score.

Operands

[InputFile]: A path to a valid Minesweeper World file, or folder with –f. This operand is optional unless used with –f or OutputFile.
[OutputFile]: A path to a file where the results will be written. This is optional.

13

Examples

You can change “python 3 Main.pyc” to other [name] depending on the language you use

python3 Main.pyc
python3 Main.pyc -m
python3 Main.pyc -d
python3 Main.pyc -r
python3 Main.pyc -rd

Constructs a random 8×8 with 10 mines world, runs the MyAI agent on the world, and prints output to console.

Constructs a random 8×8 with 10 mines world, runs the ManualAI agent on the world, and prints output to console.

Constructs a random 8×8 with 10 mines world, runs the MyAI agent on the world, and prints output to console. After every turn, the game pauses and prints the current game state to the console.

Constructs a random 8×8 with 10 mines world, runs the RandomAI on the world, and prints output to console.

Constructs a random 8×8 with 10 mines world, runs the RandomAI agent on the world using debug mode, and prints output to console. After every turn, the game pauses and prints the current game state to the console.

python3 Main.pyc -f /path/to/world/file.txt

Constructs the world specified in the file, runs the MyAI agent on the world, and prints output to console.

python3 Main.pyc -f /path/to/world/files/

Constructs all the worlds specified in the folder, runs the MyAI agent on all the worlds, and prints output to console.

14

python3 Main.pyc -f /path/to/world/files/ /path/to/outputfile/
yourscores.txt

Constructs all the worlds specified in the folder, runs the MyAI agent on all the worlds, and write output to txt file.

python3 Main.pyc -fv /path/to/world/files/

Constructs all worlds specified in the folder, runs the MyAI agent on all the worlds, and prints output to console. Before running every world, print a message indicating which specific world is running.

Notes

The Python shell uses Python version 3.5.2. When using debug mode or ManualAI, the board will printed to the console. Each tile is represented as a full stop potentially followed by a series of characters. Every board that gets displayed starts with 1- indexing and bottom left.

15