Halite Home

Understanding dynamics of a move


#1

I am going through the learning section about programming a bot and have questions about the final example code presented:

For lines 3-9 I need some clarification about what would actually happen here.

a) In line 3 is there a sequence it goes through when you say "CARDINALS"? (e.g., NORTH, SOUTH, EAST, WEST)?

b) Let's say you are on location (1,1) and it finds that the neighbor location SOUTH is !=myID and neighbor_site.strenght < site.strength, does it move the piece SOUTH to (1, 2) during that run of the for loop? If so, does it continue with the sequence in the for loop even though that space is now at a strength of 0? Now that it is on (1,2), when the loop below gets to (1,2) does it see that new piece there now?

1 def move(location):
2 site = gameMap.getSite(location)
3 for d in CARDINALS:
4 neighbour_site = gameMap.getSite(location, d)
5 if neighbour_site.owner != myID and neighbour_site.strength < site.strength:
6 return Move(location, d)
7 if site.strength < site.production * 5:
8 return Move(location, STILL)
9 return Move(location, NORTH if random.random() > 0.5 else WEST)
10
11 while True:
12 moves = []
13 gameMap = getFrame()
14 for y in range(gameMap.height):
15 for x in range(gameMap.width):
16 location = Location(x,y)
17 if gameMap.getSite(location).owner == myID:
18 moves.append(move(location))
19 sendFrame(moves)


#2

No, it doesn't continue the for loop, because it returns from the function with the return statement.


#3

Oh yea good point. What about the sequencing of direction in the loop?


#4

I suggest looking in the header files to understand everything. I have the c++ code, so I dont know if its the same, but I'll be basing my assumptions on that.
Cardinals is just an array of {1, 2, 3, 4}, which represent the 4 cardinal directions a piece can move in.
So saying 'for d in CARDINALS' means that you will iterate through all the directions, represented by d.

It's important to understand that in this game loop, you are not making any changes to the actual board that the server has! Only the server can make those changes! The player simply gets the updated map (frame) each round, calculates what it should do, then sends the set/list/whatever of those moves to the server to be dealt with. No actual update happens on the "real" map until a round is complete and the server starts making changes based on the moves it received and the rules of the game.

So no, your strength will never change, nor will any moves be made, while you are in your logic part. Nothing will change until the next frame. The function returns the move it wants to make to be appended to the list of moves. When done, it sends the list to cout for the server to read.


#5

I was literally just looking at that header file when I saw this come through. Thanks. Yea it looks like it goes N, E, S, W.

Thank you also for the explanation of how the moves work. Makes sense.


#6

Another related question. How could you ever have an opponent cardinal to you when your move starts? If you moved to a spot on the turn before and you ended up next to an opponent wouldn't you kill them or they would kill you depending on relative strengths?


#7

I think you're right about that.