Last active
January 3, 2017 05:00
-
-
Save pratikone/48c9a50a07c302dd1f6ccf5f100a29ad to your computer and use it in GitHub Desktop.
Snake game question :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 3 | |
4 | |
0 1 | |
0 2 | |
1 2 | |
1 1 | |
8 | |
R | |
R | |
D | |
L | |
U | |
R | |
D | |
L |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
class Snake : | |
def __init__(self, x, y, d) : | |
self.x = x | |
self.y = y | |
self.dir = d # U D L R | |
def move_by_direction( x, y, d, new_snake = False ) : | |
if d == 'U' : | |
if new_snake is False : | |
y = y - 1 | |
else : | |
y = y+1 | |
elif d == 'D' : | |
if new_snake is False : | |
y = y + 1 | |
else : | |
y = y - 1 | |
elif d == 'R' : | |
if new_snake is False : | |
x = x + 1 | |
else: | |
x = x - 1 | |
elif d == 'L' : | |
if new_snake is False : | |
x = x - 1 | |
else: | |
x = x + 1 | |
# print x,y,d | |
return x,y | |
def check_for_food(foods, x, y) : | |
for (fx,fy) in foods : | |
# print fx,fy, x, y | |
if fx == x and fy == y : | |
return True | |
return False | |
def snake_move( foods, moves ) : | |
snake = [] | |
for count,move in enumerate(moves) : | |
if not snake : | |
snake.append( Snake(0, 0, move)) # initialise the snake object | |
# print snake[0].x,snake[0].y, snake[0].dir | |
#regular movement | |
#updating the directions from the previous one and moving | |
for i in reversed(xrange( len(snake))) : | |
if i == 0 : | |
snake[i].dir = move | |
else : | |
snake[i].dir = snake[i-1].dir | |
x = snake[i].x | |
y= snake[i].y | |
snake[i].x,snake[i].y = move_by_direction(x,y,snake[i].dir) | |
if check_for_food(foods, snake[0].x, snake[0].y) is True : | |
last_dir = snake[-1].dir | |
x = snake[-1].x | |
y= snake[-1].y | |
x,y = move_by_direction(x,y,last_dir, new_snake= True) | |
new_snake= Snake( x, y, last_dir) #adding new snake | |
snake.append(new_snake) | |
if (len(snake) > 1) : | |
for s in xrange(1, len(snake)) : | |
if snake[0].x == snake[s].x and snake[0].y == snake[s].y : #snake bites itself | |
return (count+1) | |
return -1 #snake never bites itself | |
if __name__ == '__main__' : | |
lines = sys.stdin.readlines() | |
food_count = int(lines[1]) | |
foods = [] | |
for i in xrange(food_count) : | |
y,x = lines[2 + i].split(' ') | |
foods.append( (int(x),int(y) ) ) | |
move_count = int( lines[2 + food_count] ) | |
moves = [] | |
for i in xrange( move_count ) : | |
move = lines[3 + food_count + i] | |
moves.append(move.strip()) | |
print snake_move(foods, moves) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first line will be the size of the sandbox R C in rows & columns separated by a space, where 1 < R ≤
100 and 1 < C ≤ 100. The next line will be the total number of food locations, 2 < M ≤ 400, followed by
that many lines of R i C i (coordinates here are zero-indexed). The next line will be the total number of
moves 3< N ≤ 600, followed by that many lines each having a single-character directional code. The
codes are:
U : Up (row-1)
R : Right (col+1)
L : Left (col-1)
D : Down (row+1)
Note that the directions provided will never cause the snake to go beyond the bounds of the sandbox.