def test_Runstep_no_food(self): nn = NeuralNetwork() s = SnakeRL( nn=nn, sizeX=globe.GRID_X, sizeY=globe.GRID_Y ) x = s.X y = s.Y l = s.length s.runStep(1) #move right self.assertEqual(s.X, x+1) self.assertEqual(s.Y, y) self.assertEqual(s.length, l) x = s.X y = s.Y l = s.length s.runStep(3) #move left self.assertEqual(s.X, x-1) self.assertEqual(s.Y, y) self.assertEqual(s.length, l) x = s.X y = s.Y l = s.length s.runStep(2) #move up (up on grid, down if looking at it) self.assertEqual(s.X, x) self.assertEqual(s.Y, y+1) self.assertEqual(s.length, l) x = s.X y = s.Y l = s.length s.runStep(0) #move down (down on grid, up i flooking at it) self.assertEqual(s.X, x) self.assertEqual(s.Y, y-1) self.assertEqual(s.length, l)
def test_play(self): nn = NeuralNetwork() s = SnakeRL(nn=nn, sizeX = globe.GRID_X, sizeY = globe.GRID_Y) g = PlayGames(nn) s.play(g.gamestate_to_nn) self.assertTrue(s.gameover,False) self.assertGreater(len(s.moveList),0) print(s.moveList) print(s.grid)
def test_evaluateNext(self): nn = NeuralNetwork() s = SnakeRL(nn=nn, sizeX = globe.GRID_X, sizeY = globe.GRID_Y) g = PlayGames(nn) direction, move_array, head = s.evaluateNextStep(g.gamestate_to_nn) print(direction) self.assertGreaterEqual(np.argmax(direction),0,'invalid direction output') self.assertLessEqual(np.argmax(move_array),3, 'invalid direction output') self.assertEqual(np.argmax(move_array),direction, 'direction doesn\'t match move array') self.assertEqual(head[0], 1, 'up is free') self.assertEqual(head[1], 1, 'right is free') self.assertEqual(head[2], 0, 'down is free') self.assertEqual(head[3], 0, 'left is free')
def test_convertHead_right(self): #no food grid = np.zeros((4,4))-1 #empty grid[3,1] = 0 #head isFree = SnakeRL.convertHead( 3, 1, 4, 4, grid) truth = [1,0,1,1] #up not free for i,j in zip(isFree, truth): self.assertEqual(i, j) # with food grid[2,1] = -2 #food isFree = SnakeRL.convertHead( 3, 1, 4, 4, grid) for i,j in zip(isFree, truth): self.assertEqual(i, j)
def test_convertHead_start(self): #no food grid = np.zeros((4,4))-1 #empty grid[0,0] = 0 #head isFree = SnakeRL.convertHead( 0, 0, 4, 4, grid) truth = [1,1,0,0] #up and right are free for i,j in zip(isFree, truth): self.assertEqual(i, j) # with food grid[0,1] = -2 #food isFree = SnakeRL.convertHead( 0, 0, 4, 4, grid) for i,j in zip(isFree, truth): self.assertEqual(i, j)
def test_Runstep_with_food(self): nn = NeuralNetwork() s = SnakeRL( nn=nn, sizeX=globe.GRID_X, sizeY=globe.GRID_Y ) x = s.X y = s.Y l = s.length score = s.score s.foodX = x+1 s.foodY = y; s.grid[s.foodX][s.foodY] = -2 s.runStep(1) #move right self.assertEqual(s.grid[x][y], l+1) self.assertGreater(s.score, score) self.assertEqual(s.length, l+1)
def test_init(self): nn = NeuralNetwork() s = SnakeRL(nn=nn, sizeX = 8, sizeY = 8) self.assertTrue(hasattr(s,'nn'))