@BrysonHaman/

project 3 : Oregon Trail

Python

No description

fork
loading
Files
  • main.py
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#import unittest

class Tests(unittest.TestCase):
  
  def setUp(self):
    """ This method runs before every test."""
    global HEALTH
    HEALTH = 5
    global FOOD
    FOOD = 500
    global MILES_TO_GO
    MILES_TO_GO = TOTAL_MILES
    global CURRENT_DAY
    CURRENT_DAY = 1
    global CURRENT_MONTH
    CURRENT_MONTH = 3
    global TIMES_HEALTH_DECREASED_THIS_MONTH
    TIMES_HEALTH_DECREASED_THIS_MONTH = 0
    global USER_QUIT
    USER_QUIT = False

  #"""      
  def test_maybe_decrease_health(self):
    """Tests that the percentage of days that you loose health is close to 10%.

    The test calculates this by running maybe_decrease_health 10 000 times and
    counting how many of those it actualy decreases health.

    It then divides how many times it decreased health by 10 000 to get the percentage
    and checks that it is something between 9 and 11.

    IMPORTANT: You will need to comment any print statement in maybe_decrease_health() 
    or Repl.it will die.
    """
    global TIMES_HEALTH_DECREASED_THIS_MONTH, HEALTH
    days = 10000
    decreased = 0
    for i in range(days):
      maybe_decrease_health()
      if (TIMES_HEALTH_DECREASED_THIS_MONTH == 1):
        TIMES_HEALTH_DECREASED_THIS_MONTH = 0
        decreased += 1
        self.assertTrue(HEALTH == 4)
        HEALTH += 1
    percent = decreased/(days * 1.0) * 100
    print(percent)
    self.assertTrue(9 < percent and percent < 11)
  #"""


  def test_maybe_decrease_health_limit(self):
    """Tests that maybe_decrease_health will not increase health when it is already 5,
    even if it is called 100 times.
    """
    global TIMES_HEALTH_DECREASED_THIS_MONTH
    TIMES_HEALTH_DECREASED_THIS_MONTH = 2
    days = 100
    for i in range(days):
      maybe_decrease_health()
      self.assertTrue(HEALTH == 5)
      
  def test_get_days_in_month(self):
    """Tests that get_days_in_month returns:
    31 days for january, 30 for April.
    """
    self.assertTrue(get_days_in_month(1) == 31)
    self.assertTrue(get_days_in_month(4) == 30)
    
  def test_add_day_decrease_food(self):
    """Test that add_day reduces food by 5 lbs."""
    add_day()
    self.assertTrue(FOOD == 495)
  
  def test_add_day_change_month(self):
    """Tests that when the month changes:
    - current_day is 1.
    - current_month is the next month. in this test, 4.
    -  TIMES_HEALTH_DECREASED_THIS_MONTH is 0.
    """
    global CURRENT_DAY, TIMES_HEALTH_DECREASED_THIS_MONTH
    CURRENT_DAY = 31
    TIMES_HEALTH_DECREASED_THIS_MONTH = 2
    add_day()
    self.assertTrue(CURRENT_MONTH == 4)
    self.assertTrue(TIMES_HEALTH_DECREASED_THIS_MONTH == 0)
    self.assertTrue(CURRENT_DAY == 1)
  
  def test_add_day_decreases_health(self):
    """Test that add_day eventually reduces health.
    We call add_day 100 times. In one of those calls, health must be reduced.
    """
    global CURRENT_DAY
    for i in range(100):
      add_day()
      CURRENT_DAY -= 1
      if (HEALTH == 4):
        break
    self.assertTrue(HEALTH == 4)
    
  def test_has_user_lost_no_food(self):
    """ Verifies that the user loses if it has no food."""
    global FOOD
    FOOD = 0
    self.assertTrue(has_user_lost())
  

  def test_has_user_lost_no_time(self):
    """ Verifies that the user loses if it is December 31st and ther are still miles to go."""
    global CURRENT_DAY, CURRENT_MONTH
    CURRENT_MONTH = 12
    CURRENT_DAY = 31
    self.assertTrue(has_user_lost())


  def test_has_user_lost_no_health(self):
    """ Verifies that the user loses if it has no health."""
    global HEALTH
    HEALTH = 0
    self.assertTrue(has_user_lost())
    
  def test_has_user_lost_false(self):
    """ Verifies that the user has not lost with default 
    values (when the game starts)."""
    self.assertFalse(has_user_lost())
    
  
  def test_has_user_lost_tricky(self):
    """ Verifies that the user does not lose if it reached the goal, even 
    if it did on December 31st."""
    global MILES_TO_GO, CURRENT_DAY, CURRENT_MONTH
    MILES_TO_GO = -10
    CURRENT_MONTH = 12
    CURRENT_DAY = 31
    self.assertFalse(has_user_lost())
    
  def test_has_user_won_true(self):
    """ Verifies that the user wins if it has no more miles to go."""
    global MILES_TO_GO
    MILES_TO_GO = 0
    self.assertTrue(has_user_won())
    
  def test_has_user_won_false(self):
    """ Verifies that the user does not win when the game starts."""
    self.assertFalse(has_user_won())
    
  def test_has_user_won_no_health(self):
    """ Verifies that the user wins if it reached the goal, even 
    if it has no health on the day he/she wins."""
    global MILES_TO_GO, HEALTH
    MILES_TO_GO = 0
    HEALTH = 0
    self.assertTrue(has_user_won())
  
  # Do you lose if you have no food left when you reach the end?
  def test_has_user_won_no_food(self):
    """ Verifies that the user wins if it reached the goal, even 
    if it has no food on the day he/she wins."""
    global MILES_TO_GO, FOOD
    MILES_TO_GO = 0
    FOOD = 0
    self.assertTrue(has_user_won())
    
  def test_travel(self):
    """ Verifies travel reduces the distance between 30 and 60 miles
    and it takes between 3 and 7 days."""
    travel()
    self.assertTrue(TOTAL_MILES - 60 <= MILES_TO_GO and MILES_TO_GO <= TOTAL_MILES - 30)
    self.assertTrue(8 >= CURRENT_DAY and CURRENT_DAY >= 4)
    
  def test_rest_increase_health(self):
    """ Verifies rest increases health.
    
    We set TIMES_HEALTH_DECREASED_THIS_MONTH so we do not lose health while we rest, which is possible.
    """
    global TIMES_HEALTH_DECREASED_THIS_MONTH, HEALTH
    HEALTH = 4
    TIMES_HEALTH_DECREASED_THIS_MONTH = 2
    rest()
    self.assertTrue(HEALTH == 5)
    self.assertTrue(6 >= CURRENT_DAY and CURRENT_DAY >= 3)
    
  def test_rest_max_health(self):
    """ Verifies rest does not increase health to more than 5.
    
    We set TIMES_HEALTH_DECREASED_THIS_MONTH so we do not lose health while we rest, which is possible.
    """
    global TIMES_HEALTH_DECREASED_THIS_MONTH
    TIMES_HEALTH_DECREASED_THIS_MONTH = 2
    rest()
    self.assertTrue(HEALTH == 5)
    self.assertTrue(6 >= CURRENT_DAY and CURRENT_DAY >= 3)
  
  def test_hunt(self):
    """Tests that hunt increases food supply.

    Since hunting takes days, and each day we lose food, we don't really get 100 lbs, so we make sure food is at least 70 more."""
    hunt()
    self.assertTrue(FOOD > 570)
    self.assertTrue(6 >= CURRENT_DAY and CURRENT_DAY >= 3)
  
  def test_quit(self):
    """Tests that quit set USER_QUIT to true."""
    quit()
    self.assertTrue(USER_QUIT)
  
# unittest.main()
if __name__ == '__main__':
    unittest.main()