-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_-_while_loops.py
238 lines (222 loc) · 7.83 KB
/
1_-_while_loops.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# This is a challenge to build a simple version of the snake game using only while loops, strings and fuctions.
# The challenge consists in printing the table with the head and body of the snake. When the player chooses a direction,
# the head moves and the body must grow in the direction of the head. The snake can collide with the walls and itself,
# that's the other part of the callenge, keep the coordinates of the body without using matrix, vectors, dictionaries, etc.
# I put a win condition just for the sake of the game. The codition for the original snake game is different from this.
# I'm not using this in an academic level or for commercial purposes. This code is only to demonstrate programming technics.
# This code was all written by me (@diguitarrista) and was inspired by a project from my class about introduction to computer science.
def play():
print("Welcome to the Snake Game! The challenge is to complete the game without hitting itself or the walls.")
print("The head is represented by the letter H and the body with *.")
print()
# Game loop.
game()
play_again = True
while play_again:
play_again_input = str(input("Do you want to play again? Type Y for yes or N for No: "))
print()
play_again_input = play_again_input.capitalize() # Make sure that the user's input is a capital letter.
if play_again_input == 'Y':
game()
play_again = True
elif play_again_input == 'N':
print("Thanks for playing! Bye!")
play_again = False
else:
print("You must type the letters Y or N. Try again!")
def game():
# Check the user's input
check_positive = True
while check_positive:
inputs = check_inputs()
n_col = inputs[0]
n_row = inputs[1]
xo = inputs[2]
yo = inputs[3]
if xo <= 0 or yo <= 0 or n_row <= 0 or n_col <= 0:
print()
print("You must choose the numbers > 0.")
print()
elif xo >= n_col or yo >= n_row:
print()
print("The xo and yo must be lower than the number of columns and rows")
print()
else:
check_positive = False
# The snake starts in the position xo - 1, yo - 1.
yo -= 1
body_snake = "" # It's the string that will keep the snake body positions
direction = 3
# Game loop.
play = True
while play:
print()
# Add the direction to the string body_snake (directions) to calculate the body path from xo, yo.
body_snake += str(direction)
hx, hy = head_snake(body_snake, xo, yo)
hit = collision(body_snake, xo, yo, hx, hy, n_col, n_row)
# Check for collisions.
if hit:
play = False
print("Finished!")
print("You made points:", len(body_snake) - 1, "points.")
print()
# Check if the player completed the game.
elif (len(body_snake) - 1) == ((n_row*n_col) - 1):
play = False
print("Finished!")
print("You won!")
print("You made points:", len(body_snake) - 1, "points.")
print()
# Game loop. Receive the input from the player.
else:
draw_board(body_snake, xo, yo, hx, hy, n_col, n_row)
print()
print("Points:", str(len(body_snake) - 1) + ".")
print()
direction = check_direction()
if direction == "5":
play = False
print("Finished")
print("You made points:", len(body_snake) - 1, "points.")
print()
def check_inputs():
check_inputs = True
while check_inputs:
try:
n_col = int(input("Type the number of columns: "))
n_row = int(input("Type the number of rows: "))
xo = int(input("Type the x position where the snake starts: "))
yo = int(input("Type the y position where the snake starts: "))
check_inputs = False
except ValueError:
print()
print("Invalid input. Try again from the beginning.")
print()
return [n_col, n_row, xo, yo]
def check_direction():
check_direction = True
# Check the user's input
while check_direction:
direction = input("Type a number to move the snake: 1- right, 2- up, 3- left, 4- down, 5- finish: ")
try:
test = int(direction)
if test > 0 and test < 6:
check_direction = False
else:
print("Invalid input. Please enter a number between 1 and 5.")
except ValueError:
print("Invalid input. Please enter a number between 1 and 5.")
return direction
def draw_board(body_snake, xo, yo, x, y, n_col, n_row):
col = 0
row = 0
# top
top = n_col + 2
while top > 0:
print("#", end="")
top -= 1
print()
# middle
while row < n_row:
print("#", end="")
while col < n_col:
# body
body = body_snake_coordinates(body_snake, xo, yo, col, row)
if body:
col += 1
# head
elif x == col and y == row:
print("H", end="")
col += 1
# rest of the table
else:
print(".", end="")
col += 1
print("#")
row += 1
col = 0
# bottom
bot = n_col + 2
while bot > 0:
print("#", end="")
bot -= 1
print()
def reverse(number):
count_digits = 1
reversed = ""
if len(number) > 1:
# Count the number of digits of direction.
x = int(number)
y = x // 10 # The last digit is the snake's head
while y > 0:
count_digits += 1
y = y // 10
# Divide by the number of digits.
while count_digits > 0:
r = x%10
# Invert the position of the digits.
reversed += str(r)
x = x // 10
count_digits -= 1
return int(reversed)
else:
return int(number)
def body_snake_coordinates(body_snake, xo, yo, col, row):
n = reverse(body_snake)
# Return True if any part of the snake is in (col, row) and print the body.
while n > 0:
r = n%10
if r == 1:
xo += 1
elif r == 3:
xo -= 1
elif r == 2:
yo -= 1
elif r == 4:
yo += 1
if n > 10 and xo == col and yo == row:
print("*", end="")
return True
n = n // 10
def head_snake(body_snake, xo, yo):
n = reverse(body_snake)
# Return the head's position.
while n > 0:
r = n%10
if r == 1:
xo += 1
elif r == 3:
xo -= 1
elif r == 2:
yo -= 1
elif r == 4:
yo += 1
if n < 10:
head_snake_coordinates = xo, yo
n = n//10
return head_snake_coordinates
def collision(body_snake, xo, yo, x, y, n_col, n_row):
n = reverse(body_snake)
# Check if the head collides with the body.
while n > 0:
r = n%10
if r == 1:
xo += 1
elif r == 3:
xo -= 1
elif r == 2:
yo -= 1
elif r == 4:
yo += 1
if n > 10 and xo == x and yo == y and r < 5:
print("Collision with itself.")
return True
n = n // 10
# Check if the head collides with the borders.
if x == n_col or y == n_row or x == -1 or y == -1:
print("Collision with wall.")
return True
else:
return False
play()