def test_count_mines(self): g = minesweeper.Game(2, 2, 1) g.board = [ [et(), et()], [et(), mt()], ] self.assertEqual(g.get_num_mines(0, 0), 1) self.assertEqual(g.get_num_mines(1, 0), 1) self.assertEqual(g.get_num_mines(0, 1), 1) self.assertEqual(g.get_num_mines(1, 1), 0) g.board = [ [mt(), et()], [et(), mt()], ] self.assertEqual(g.get_num_mines(0, 0), 1) self.assertEqual(g.get_num_mines(1, 0), 2) self.assertEqual(g.get_num_mines(0, 1), 2) self.assertEqual(g.get_num_mines(1, 1), 1) g = minesweeper.Game(3, 3, 1) g.board = [ [et(), et(), et()], [et(), mt(), et()], [et(), et(), et()], ] self.assertEqual(g.get_num_mines(0, 0), 1) self.assertEqual(g.get_num_mines(1, 1), 0) self.assertEqual(g.get_num_mines(2, 2), 1) self.assertEqual(g.get_num_mines(0, 2), 1) self.assertEqual(g.get_num_mines(1, 2), 1)
def test_find_neighboring_mines(): ms = minesweeper.Game(x_max=3, y_max=3, num_mines=3) ms.mine_locations = TEST_BOARD assert ms._find_neighboring_mines(0, 0) == 0 assert ms._find_neighboring_mines(1, 2) == 0 assert ms._find_neighboring_mines(1, 1) == 1 assert ms._find_neighboring_mines(0, 2) == 1 assert ms._find_neighboring_mines(2, 1) == 1
def test_flood_click(self): g = minesweeper.Game(2, 2, 1) g.board = [ [et(), et()], [et(), mt()], ] g.flood_click(0, 0) self.assertTrue(g.get_tile(0, 0).clicked) self.assertFalse(g.get_tile(0, 1).clicked) self.assertFalse(g.get_tile(1, 0).clicked) self.assertFalse(g.get_tile(1, 1).clicked) g = minesweeper.Game(3, 3, 1) g.board = [ [et(), et(), et()], [et(), mt(), et()], [et(), et(), et()], ] clicked_mask = [ [True, False, False], [False, False, False], [False, False, False], ] g.flood_click(0, 0) for brow, crow in zip(g.board, clicked_mask): for bcol, ccol in zip(brow, crow): self.assertEqual(bcol.clicked, ccol) g = minesweeper.Game(3, 3, 1) g.board = [ [et(), et(), et()], [et(), et(), et()], [et(), et(), mt()], ] clicked_mask = [ [True, True, True], [True, True, True], [True, True, False], ] g.flood_click(0, 0) for brow, crow in zip(g.board, clicked_mask): for bcol, ccol in zip(brow, crow): self.assertEqual(bcol.clicked, ccol)
def reset_game(): """ Create new game with specified game parameters Headers (Content-Type, application/json) Body (all optional) {'x_max':number_of_rows (default 10), 'y_max':number_of_cols (default 10), 'num_mines':number_of_miens (default 10)} Returns game_state (dict): state of game """ game_inputs = json.loads(flask.request.data) game_id = str(uuid.uuid4()) global GAMES GAMES[game_id] = minesweeper.Game(**game_inputs) return find_game_state(game_id)
def policy_backward(eph, epdlogp): """ backward pass. (eph is array of intermediate hidden states) """ # dW2 = np.dot(eph.T, epdlogp).ravel() # dh = np.outer(epdlogp, model['W2']) # dh[eph <= 0] = 0 # backpro prelu # dW1 = np.dot(dh.T, epx) dW2 = eph.T.dot(epdlogp) dh = epdlogp.dot(model['W2'].T) dh[eph <= 0] = 0 # backpro prelu dW1 = epx.T.dot(dh) return {'W1': dW1, 'W2': dW2} game = ms.Game(10, 10, 0.1) x = np.asarray(game.output_ml()) xs, hs, dlogps, drs = [], [], [], [] episode_number = 0 batch_reward = 0 while True: x = np.asarray(game.output_ml()) # forward the policy network and sample an action from the returned probability aprob, h = policy_forward(x) u = np.random.uniform() aprob_cum = np.cumsum(aprob) action = np.where(u <= aprob_cum)[0][0] # record various intermediates (needed later for backprop) xs.append(x) # observation
import pygame import minesweeper as ms pygame.init() pygame.font.init() screen = pygame.display.set_mode((450, 450)) pygame.display.set_caption('Minesweeper') game = ms.Game('EASY') dif = 50 font1 = pygame.font.SysFont("comicsans", 40) LEFT = 1 RIGHT = 3 def draw_field(): screen.fill((0, 0, 0)) for i in range(9): for j in range(9): pygame.draw.rect(screen, (0, 153, 153), (i * dif, j * dif, dif - 1, dif - 1)) def reveal_cell(pos): pos_x = pos[0] // 50 pos_y = pos[1] // 50 game.dig(pos_x, pos_y)
from flask import Flask, jsonify, send_from_directory from flask_cors import CORS import json import numpy as np import flask import uuid import minesweeper GAMES = {"TEST": minesweeper.Game()} app = Flask(__name__, static_folder="../build") CORS(app) class NumpyEncoder(json.JSONEncoder): """ Extends json dumps to recursively convert numpy arrays to lists """ def default(self, obj): if isinstance(obj, np.ndarray): return obj.tolist() return json.JSONEncoder.default(self, obj) def find_game_state(game_id): """ Returns json blob with current state of game with specific id Args: game (minesweeper.Game)
import minesweeper as ms game = ms.Game(20, 20, 0.2) play = True while play: game.print_board() x = int(input("X> ")) y = int(input("Y> ")) play = not game.move(x, y)
def test_click_on_non_mine_with_neighbour(): ms = minesweeper.Game(x_max=3, y_max=3, num_mines=3) ms.mine_locations = TEST_BOARD ms.click(1, 1) assert ms.revealed_state[1][1] == 1
def test_click_on_mine(): ms = minesweeper.Game(x_max=3, y_max=3, num_mines=3) ms.mine_locations = TEST_BOARD ms.click(1, 2) assert ms.has_lost
def game1(): mines = flip([[True, False, False, False, False], [False, False, False, True, False], [False, False, False, True, False], [False, False, True, False, False]]) return ms.Game(ms.GameConfig(5, 4, 4), mines)
def run(self): game = minesweeper.Game(self.screen, self.database, self.user, self.dimensions, self.size, self.mines) returned = game.run() return returned
def test_game_init_for_total_mine_count(): game = ms.Game(ms.GameConfig(100, 100, 800)) assert 800 == sum(row.count(True) for row in game.mines)
def game4(): mines = flip([[False, False, True], [False, False, False], [True, False, True]]) return ms.Game(ms.GameConfig(3, 3, 3), mines)