My first CodeInGame experience was running Thor across a grassy field. We had quite the adventure, Thor and I. If it were not for me, he would have died. I'd say he owes me one!
Instructions from CodeInGame :
Thor moves on a map which is 40 wide by 18 high. Note that the coordinates (X and Y) start at the top left! This means the most top left cell has the coordinates "X=0,Y=0" and the most bottom right one has the coordinates "X=39,Y=17".
Once the program starts you are given:
the variable lightX: the X position of the light of power that Thor must reach.
the variable lightY: the Y position of the light of power that Thor must reach.
the variable initialTX: the starting X position of Thor.
At the end of the game turn, you must output the direction in which you want Thor to go among:
N (North)
NE (North-East)
E (East)
SE (South-East)
S (South)
SW (South-West)
W (West)
NW (North-West)
Each movement makes Thor move by 1 cell in the chosen direction.
Here's the code I submitted:
import sys
import math
# light_x: the X position of the light of power
# light_y: the Y position of the light of power
# initial_tx: Thor's starting X position
# initial_ty: Thor's starting Y position
light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()]
current_x = initial_tx
current_y = initial_ty
# game loop
while True:
remaining_turns = int(input()) # The remaining amount of turns Thor can move.
first = ""
second = ""
final = ""
EAST = (light_x - current_x)
WEST = (current_x - light_x)
NORTH = (current_y - light_y)
SOUTH = (light_y - current_y)
if NORTH > SOUTH:
first = "N"
current_y -=1
if ( EAST > NORTH or WEST > NORTH ):
first = ""
current_y +=1
elif NORTH < SOUTH:
first = "S"
current_y +=1
if ( EAST > SOUTH or WEST > SOUTH ):
first = ""
current_y -=1
if EAST > WEST:
second = "E"
current_x +=1
if ( NORTH > EAST or SOUTH > EAST ):
second = ""
current_x -=1
elif EAST < WEST:
second = "W"
current_x -=1
if ( NORTH > WEST or SOUTH > WEST ):
second = ""
current_x +=1
final = first + second
print(final)
#print("Debug messages...", file=sys.stderr)
# Write an action using print
# To debug: print("Debug messages...", file=sys.stderr)
# A single line providing the move to be made: N NE E SE S SW W or NW
Drop me a line if you wanna talk programming.
~ Friar Greg
Original: https://friargreg.blogspot.com/2017/10/my-codeingame-adventures.html
By: Greg Denyes
Posted: October 14, 2017, 8:35 pm
The Landing is a social site for Athabasca University staff, students and invited guests. It is a space where they can share, communicate and connect with anyone or everyone.
Unless you are logged in, you will only be able to see the fraction of posts on the site that have been made public. Right now you are not logged in.
If you have an Athabasca University login ID, use your standard username and password to access this site.
We welcome comments on public posts from members of the public. Please note, however, that all comments made on public posts must be moderated by their owners before they become visible on the site. The owner of the post (and no one else) has to do that.
If you want the full range of features and you have a login ID, log in using the links at the top of the page or at https://landing.athabascau.ca/login (logins are secure and encrypted)
Posts made here are the responsibility of their owners and may not reflect the views of Athabasca University.