This repository has been archived by the owner on Oct 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathminesweeper_test.py
62 lines (44 loc) · 1.52 KB
/
minesweeper_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Acceptance tests for minesweeper.py
Make sure that this file is in the same directory as minesweeper.py!
'Why do we fall sir? So that we can learn to pick ourselves up.'
- Batman Begins (2005)
"""
import pytest as pt
from minesweeper import Minesweeper, MinesweeperAI
# Feel free to change these numbers for different expectation
HEIGHT = 12
WIDTH = 12
MINES = 8
expectedWinPercent = 85
# Run the AI test for 10 times. Each test consists of letting the AI play
# minesweeper for 1000 rounds. If the inference function is implemented correctly,
# each test should have a very high win rate (≈90%) in most of the iterations.
# Use `pytest -s` to see win rate.
@pt.mark.parametrize("execution_number", range(10))
def test(execution_number):
return play1000()
# Helper functions
def play1000():
totalWon = 0
for _ in range(1000):
totalWon += play()
print(f"\nWin rate:{totalWon // 10}%")
assert totalWon >= expectedWinPercent * 10
def play():
game = Minesweeper(height=HEIGHT, width=WIDTH, mines=MINES)
ai = MinesweeperAI(height=HEIGHT, width=WIDTH)
won = lost = False
while not (won or lost):
# AI choose a move
move = ai.make_safe_move() or ai.make_random_move()
if move is None:
won = True
break
# Make move and update AI
if game.is_mine(move):
lost = True
else:
nearby = game.nearby_mines(move)
ai.add_knowledge(move, nearby)
return won