def testTableSize(self): gameEngine = GameEngine(1, 7, 8) table = gameEngine.getTable() self.assertEqual(table.height, 8) self.assertEqual(table.width, 7) self.assertEqual(table.emptyCells, table.height * table.width) self.assertFalse(table.isFull())
def main(): logging.basicConfig(level=logging.INFO, format='%(asctime)s: %(message)s', datefmt="%I:%M:%S %p") engine = GameEngine(ADDRESS, PORT) print('Game is paused.') print('Controls:') print(' r - restart') print(' p - (un)pause') print(' q - quit') engine.run()
def testPlayerMove(self): gameEngine = GameEngine(1, 7, 7) table = gameEngine.getTable() result = gameEngine.playerMove(1, 1) self.assertRaises(EmptyError, gameEngine.playerMove, 0, 0) self.assertRaises(EmptyError, gameEngine.playerMove, 0, 1) self.assertRaises(EmptyError, gameEngine.playerMove, 0, 2) self.assertRaises(EmptyError, gameEngine.playerMove, 2, 0) self.assertRaises(EmptyError, gameEngine.playerMove, 1, 1) self.assertEqual(table.emptyCells, 40) self.assertIsNone(result)
def run(self): if self.print_stuff: print("Starting " + self.name) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) game = GameEngine(ADDRESS, PORT, weight_set=self.weight_set, run_on_clock=self.run_on_clock, using_visualizer=USING_VISUALIZER) if self.run_on_clock: game.run() if self.print_stuff: print(game.final_state) self.scores[self.counter] = game.final_state if self.print_stuff: print("Exiting " + self.name) return game.final_state
def testComputerMove(self): gameEngine = GameEngine(1, 7, 7) table = gameEngine.getTable() result = gameEngine.computerMove() self.assertIsNone(result) temp = table.emptyCells self.assertLess(temp, 49) self.assertGreaterEqual(temp, 40) result = gameEngine.computerMove() self.assertIsNone(result) self.assertLess(table.emptyCells, temp) self.assertGreaterEqual(table.emptyCells, temp - 9)
def testMoveToWin2(self): """ x|_|_ _|_|_ _|_|_ """ gameEngine = GameEngine(1, 3, 3) table = gameEngine.getTable() table.setValue(0, 0, OCCUPIED_SYMBOL) result = gameEngine.computerMove() self.assertEqual(result, COMPUTER_SYMBOL) self.assertTrue(table.isFull()) self.assertEqual(table.emptyCells, 0) self.assertEqual(table.getValue(1, 1), COMPUTER_SYMBOL)
def testMoveToWin4(self): """ the state of the board before the computer move - in this case, the computer cannot move to win _|_|_ _|x|_ _|_|_ """ gameEngine = GameEngine(1, 3, 3) table = gameEngine.getTable() table.setValue(1, 1, OCCUPIED_SYMBOL) result = gameEngine.computerMove() self.assertIsNone(result) self.assertFalse(table.isFull()) self.assertLessEqual(table.emptyCells, 5)
def testMoveToWin1(self): """ the state of the board before the computer move _|_|_ _|_|_ _|_|_ """ gameEngine = GameEngine(1, 3, 3) table = gameEngine.getTable() result = gameEngine.computerMove() self.assertEqual(result, COMPUTER_SYMBOL) self.assertTrue(table.isFull()) self.assertEqual(table.emptyCells, 0) self.assertEqual(table.getValue(1, 1), COMPUTER_SYMBOL)
def start(self): player = self.__getPlayer() (tableWidth, tableHeight) = self.__getTableSize() gameEngine = GameEngine(player, tableWidth, tableHeight) while True: self.__play(player, gameEngine)
def start(self): #choose player playerChoice = self.__getPlayerChoice() #choose table size (boardWidth, boardHeight) = self.__getBoardSize() gameEngine = GameEngine(playerChoice, boardWidth, boardHeight) #self.__printTable(boardHeight, boardWidth, [[" "] * boardWidth] * boardHeight) while True: #the player moves first if playerChoice == 1: (xCoord, yCoord) = self.__getPlayerCoords() try: result = gameEngine.playerMove(xCoord, yCoord) if result != None: self.__printResult(result) return except Exception as e: print(str(e)) continue self.__printTable(boardHeight, boardWidth, gameEngine.toPrint()) result = gameEngine.computerMove() if result != None: self.__printResult(result) return #the computer moves first elif playerChoice == 2: result = gameEngine.computerMove() if result != None: self.__printResult(result) return self.__printTable(boardHeight, boardWidth, gameEngine.toPrint()) (xCoord, yCoord) = self.__getPlayerCoords() try: result = gameEngine.playerMove(xCoord, yCoord) if result != None: self.__printResult(result) return except Exception as e: print(str(e)) continue self.__printTable(boardHeight, boardWidth, gameEngine.toPrint())
def testGetValue(self): gameEngine = GameEngine(1, 7, 8) table = gameEngine.getTable() self.assertEqual(table.getValue(1, 1), EMPTY_SYMBOL) gameEngine.playerMove(1, 1) self.assertEqual(table.getValue(1, 1), PLAYER_SYMBOL) self.assertEqual(table.getValue(0, 0), OCCUPIED_SYMBOL) self.assertEqual(table.getValue(0, 1), OCCUPIED_SYMBOL) self.assertEqual(table.getValue(0, 2), OCCUPIED_SYMBOL) self.assertEqual(table.getValue(1, 0), OCCUPIED_SYMBOL) self.assertEqual(table.getValue(1, 2), OCCUPIED_SYMBOL) self.assertEqual(table.getValue(2, 0), OCCUPIED_SYMBOL) self.assertEqual(table.getValue(2, 1), OCCUPIED_SYMBOL) self.assertEqual(table.getValue(2, 2), OCCUPIED_SYMBOL) self.assertEqual(table.getValue(3, 3), EMPTY_SYMBOL)
def into_regular_engine(self, index, ui): lvl = list(zip(self.level_heights[index], self.level_bads[index])) ge = GameEngine(ui, lvl) ge.player.x = self.players_x[index] ge.player.y = self.players_y[index] ge.player.vx = self.players_vx[index] ge.player.vy = self.players_vy[index] ge.player.width = PLAYER_WIDTH return ge
def testGetAllEmpty(self): gameEngine = GameEngine(1, 7, 7) table = gameEngine.getTable() posList = table.getAllEmpty() self.assertEqual(len(posList), 49) self.assertEqual(len(posList), table.emptyCells) gameEngine.playerMove(3, 3) posList = table.getAllEmpty() self.assertEqual(len(posList), 40) self.assertEqual(posList[0], (0, 0)) self.assertEqual(posList[1], (0, 1)) self.assertEqual(posList[2], (0, 2)) self.assertEqual(posList[3], (0, 3)) self.assertEqual(posList[4], (0, 4)) self.assertEqual(posList[35], (6, 2)) self.assertEqual(posList[36], (6, 3)) self.assertEqual(posList[37], (6, 4)) self.assertEqual(posList[38], (6, 5)) self.assertEqual(posList[39], (6, 6))
def get_data(): all_results = [] for fear in tqdm(np.linspace(0, 15, num=25)): weight_set = WEIGHT_SET weight_set['FEAR'] = fear results = [] for i in range(50): game = GameEngine(ADDRESS, PORT, weight_set=weight_set, run_on_clock=USE_CLOCK, using_visualizer=USING_VISUALIZER) if PRINT_ALL: print(game.final_state) results.append(game.final_state['score']) all_results.append((fear, results)) with open('data.npy', 'wb') as f: np.save(f, all_results)
"word": game.getBlueWord(), "quantity": game.getBlueQuantity() }) attributes.update({"attributes": newAttributes}) response = {"data": {"type": "status", "attributes": attributes}} return json.dumps(response) elif request_type == "images": response = { "data": { "type": "images", "attributes": { "state": game.getState(), "images": game.returnImageMap() } } } return json.dumps(response) # type=images&gid=<GAME_ID>&uid=<USER_ID> # return request.args.get("user") game = GameEngine() app.run() game.setState("LOADING")
def testEmptyCells(self): gameEngine = GameEngine(1, 7, 7) table = gameEngine.getTable() self.assertEqual(table.emptyCells, 49) gameEngine.playerMove(1, 1) self.assertEqual(table.emptyCells, 40) gameEngine.playerMove(4, 4) self.assertEqual(table.emptyCells, 31) gameEngine.playerMove(1, 4) self.assertEqual(table.emptyCells, 22) gameEngine.playerMove(4, 1) self.assertEqual(table.emptyCells, 13) gameEngine.playerMove(6, 0) self.assertEqual(table.emptyCells, 11) gameEngine.playerMove(0, 6) self.assertEqual(table.emptyCells, 9) gameEngine.playerMove(6, 3) self.assertEqual(table.emptyCells, 6) gameEngine.playerMove(3, 6) self.assertEqual(table.emptyCells, 3) gameEngine.computerMove() self.assertEqual(table.emptyCells, 0) self.assertTrue(table.isFull())
def testGetFirstEmpty(self): gameEngine = GameEngine(1, 7, 7) table = gameEngine.getTable() self.assertEqual(table.getFirstEmpty(), (0, 0)) gameEngine.playerMove(1, 1) self.assertEqual(table.getFirstEmpty(), (0, 3)) gameEngine.playerMove(1, 4) self.assertEqual(table.getFirstEmpty(), (0, 6)) gameEngine.playerMove(1, 6) self.assertEqual(table.getFirstEmpty(), (3, 0)) gameEngine.playerMove(4, 1) self.assertEqual(table.getFirstEmpty(), (3, 3)) gameEngine.playerMove(4, 4) self.assertEqual(table.getFirstEmpty(), (3, 6)) gameEngine.playerMove(4, 6) self.assertEqual(table.getFirstEmpty(), (6, 0)) gameEngine.playerMove(6, 1) self.assertEqual(table.getFirstEmpty(), (6, 3)) gameEngine.playerMove(6, 4) self.assertEqual(table.getFirstEmpty(), (6, 6)) gameEngine.computerMove() self.assertTrue(table.isFull())
def test_hyperparams(*args): """ Evaluates the algorithm with the given hyperparameters. Returns the average score. """ weight_set = dict(zip(HYPERPARAM_RANGES.keys(), args)) if PRINT_ALL: print(weight_set) start = time.time() if USE_CLOCK: threads = [] scores = [] numThreads = 1 notifyInterval = 100 # Create new threads for i in range(1, numThreads+1): scores.append(None) threads.append(MyThread(i, "Thread-" + str(i), scores, i-1, print_stuff=False, weight_set=weight_set, run_on_clock=USE_CLOCK)) # Start new Threads for i in range(numThreads): threads[i].start() if PRINT_ALL and i % notifyInterval == 0: print('Starting thread ' + str(i) + '...') # Join new Threads for i in range(numThreads): threads[i].join() end = time.time() if PRINT_ALL: print('done in ' + str(end - start) + ' s') sum = 0 for i in range(numThreads): # print(scores[i]) sum += scores[i]['score'] if scores[i]['score'] > 11000 and PRINT_ALL: print('super score: ' + str(weight_set)) # print(str(scores[i])) avg_score = sum / numThreads if PRINT_ALL: print('average score: ' + str(avg_score)) return avg_score else: results = [] numGamesFinished = 0 totalNumGames = 70 for i in range(totalNumGames): game = GameEngine(ADDRESS, PORT, weight_set=weight_set, run_on_clock=USE_CLOCK, using_visualizer=USING_VISUALIZER) if PRINT_ALL: print(game.final_state) results.append(game.final_state['score']) if True: numGamesFinished += 1 all_results.append(results) with open('data.npy', 'wb') as f: np.save(f, all_results) median_score = np.median(results) print(f'median score: {median_score}') print(f'number of games finished: {numGamesFinished} / {totalNumGames}') return median_score
def start(self): if (not self.game): self.game = GameEngine()
import timeit from multiGameEngine import MultiGameEngine from gameEngine import GameEngine from graphics import UI from util import Actions from levelGenerator import NenaGenerator TIME_TIMES = 1000 N_MULTI = 1000 ui = UI(False, 0) lvl = NenaGenerator(1).generate(100) ge = GameEngine(ui, lvl) mge = MultiGameEngine([lvl] * N_MULTI) t_ge = 1 / TIME_TIMES * timeit.timeit("ge.performTick(Actions.RIGHT)", globals=globals(), number=TIME_TIMES) print("One normal gameEngine:", t_ge) t_ges = 1 / (TIME_TIMES * N_MULTI) * timeit.timeit("mge.performTick([Actions.RIGHT] * N_MULTI)", globals=globals(), number=TIME_TIMES) print("One of", N_MULTI, " parallel gameEngines:", t_ges) print(t_ge / t_ges, "x faster")
import json import math from pprint import pprint from graphicalEngine import GraphicalEngine from gameEngine import GameEngine # Init and configure graphical engine graphical_engine = GraphicalEngine() game_engine = GameEngine() game_engine.loadGraphicalEngine(graphical_engine) game_engine.graphicalEngine.loadLibrary('lib.pygletLib', 'PygletLib') game_engine.graphicalEngine.addResourcePath('./assets/sprites') game_engine.graphicalEngine.loadResourcePath() game_engine.graphicalEngine.addImage('wall', 'wall.png') game_engine.graphicalEngine.initWindow(500, 500) data = game_engine.readJsonFile('testMap.json') # Get map width = 500 // data['width'] height = 500 // data['height'] game_map = data['map'] for index, case in enumerate(game_map): if case == 1: x = ((index % data['width']) * width) y = ((int(math.floor(index // data['width']))) * height) print 'x: ' + str(x) + ' y: ' + str(y) opts = {
def main(): ge = GameEngine()