-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_SnakeGame.py
90 lines (82 loc) · 3.6 KB
/
test_SnakeGame.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import unittest
import numpy as np
from SnakeGame import SnakeGame
class TestSnakeGame(unittest.TestCase):
def test_make_grid_input(self):
sg = SnakeGame(test=True)
# input must be an int
self.assertRaises(Exception, sg.make_grid, 6.3)
self.assertRaises(Exception, sg.make_grid, 'Snake')
# input must be 5 or greater
self.assertRaises(Exception, sg.make_grid, 2)
def test_make_grid(self):
# 6 -> [[-1,-1,-1,-1,-1,-1],
# [-1, 1, 2, 3, 0,-1],
# [-1, 0, 0 ,0, 0, 4],
# [-1, 0, 0, 0, 0,-1],
# [-1, 0, 0, 0, 0,-1],
# [-1,-1,-1,-1,-1,-1]]
sg = SnakeGame()
expected_grid = np.array([[-1, -1, -1, -1, -1, -1], [-1, 1, 2, 3, 0, -1], [-1, 0, 0, 0, 0, -1],
[-1, 0, 0, 0, 0, -1], [-1, 0, 0, 0, 0, -1], [-1, -1, -1, -1, -1, -1]])
np.testing.assert_equal(sg.make_grid(6), expected_grid)
def test_find_head_loc(self):
# find location of snake head
# [[-1,-1,-1,-1,-1,-1],
# [-1, 1, 2, 3, 0,-1],
# [-1, 0, 0 ,0, 0, 4],
# [-1, 0, 0, 0, 0,-1],
# [-1, 0, 0, 0, 0,-1],
# [-1,-1,-1,-1,-1,-1]]
# -> (1,3)
sg = SnakeGame(test=True)
head_loc = sg.find_head_loc(sg.grid, 3)
np.testing.assert_equal(head_loc, np.array([1, 3]))
def test_new_end_loc(self):
sg = SnakeGame(test=True)
head_loc = np.array([1, 3])
np.testing.assert_equal(sg.new_end_loc(head_loc, 'down'), np.array([2, 3]))
np.testing.assert_equal(sg.new_end_loc(head_loc, 'up'), np.array([0, 3]))
np.testing.assert_equal(sg.new_end_loc(head_loc, 'left'), np.array([1, 2]))
np.testing.assert_equal(sg.new_end_loc(head_loc, 'right'), np.array([1, 4]))
self.assertRaises(ValueError, sg.new_end_loc, head_loc, 'error')
def test_fatal_move(self):
# [[-1,-1,-1,-1,-1,-1],
# [-1, 1, 2, 3, 0,-1],
# [-1, 0, 0 ,0, 0, 4],
# [-1, 0, 0, 0, 0,-1],
# [-1, 0, 0, 0, 0,-1],
# [-1,-1,-1,-1,-1,-1]]
# up and left -> True, down and right -> False
sg = SnakeGame(test=True)
grid = sg.grid
head_loc_down = np.array([2, 3])
head_loc_right = np.array([1, 4])
head_loc_up = np.array([0, 3])
head_loc_left = np.array([1, 2])
self.assertEqual(sg.fatal_move(grid, head_loc_down), False)
self.assertEqual(sg.fatal_move(grid, head_loc_right), False)
self.assertEqual(sg.fatal_move(grid, head_loc_up), True)
self.assertEqual(sg.fatal_move(grid, head_loc_left), True)
def test_update_grid(self):
# grid= [[-1,-1,-1,-1,-1,-1],
# [-1, 1, 2, 3, 0,-1],
# [-1, 0, 0 ,0, 0, 4],
# [-1, 0, 0, 0, 0,-1],
# [-1, 0, 0, 0, 0,-1],
# [-1,-1,-1,-1,-1,-1]]
# and new_head_loc np.array([2, 3]) ->
# [[-1,-1,-1,-1,-1,-1],
# [-1, 0, 1, 2, 0,-1],
# [-1, 0, 0 ,3, 0, 4],
# [-1, 0, 0, 0, 0,-1],
# [-1, 0, 0, 0, 0,-1],
# [-1,-1,-1,-1,-1,-1]]
sg = SnakeGame(test=True)
grid = sg.grid
head_loc_down = np.array([2, 3])
expected_updated_grid = np.array(
[[-1, -1, -1, -1, -1, -1], [-1, 0, 1, 2, 0, -1], [-1, 0, 0, 3, 0, -1], [-1, 0, 0, 0, 0, -1],
[-1, 0, 0, 0, 0, -1],
[-1, -1, -1, -1, -1, -1]])
np.testing.assert_equal(sg.update_grid(grid, head_loc_down), expected_updated_grid)