Skip to content

Commit 01d12f2

Browse files
committed
Discourage bots from killing themselves
1 parent a77ed63 commit 01d12f2

10 files changed

+57
-22
lines changed

LightningBot.py

+43-5
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,6 @@ def positionIsBlocked(self, position):
325325
def rotateMoveDirection(self, move_direction, rotation):
326326
return (move_direction + rotation) % 4
327327

328-
# Return the new position after moving in move_direction
329-
def rotateMoveDirection(self, position, move_direction):
330-
return (move_direction + rotation) % 4
331-
332-
333328
# Return the direction that leads to the longest possible path
334329
def directionToLongestPath(self):
335330

@@ -380,3 +375,46 @@ def longestPathDepth(self, position, direction, blocked_tiles, limit = 12):
380375
max_depth = depth
381376

382377
return max_depth
378+
379+
380+
# Return a list of directions which are open
381+
def allowedDirections(self):
382+
position = self.game_bots[self.bot_name]['position']
383+
direction = self.game_bots[self.bot_name]['direction']
384+
allowed_directions = []
385+
for try_direction in [0, 1, 2, 3]:
386+
if not self.positionIsBlocked(self.getNextPosition(position, try_direction)) and try_direction != (direction + 2) % 4:
387+
allowed_directions.append(try_direction)
388+
389+
return allowed_directions
390+
391+
392+
# If the proposed move would lose the game, try to return a move which doesn't
393+
def avoidLosingMove(self, move_direction):
394+
395+
direction = self.game_bots[self.bot_name]['direction']
396+
397+
# Not allowed to go backwards
398+
if move_direction == (direction + 2) % 4:
399+
print('Trying to go backwards!!')
400+
401+
# Reverse to fix
402+
move_direction = direction
403+
404+
405+
# Return the move if ok
406+
allowed_directions = self.allowedDirections()
407+
if move_direction in allowed_directions:
408+
return move_direction
409+
410+
print('allowed', allowed_directions)
411+
412+
# Return the next found move that is ok
413+
for try_direction in [0, 1, 2, 3]:
414+
if try_direction in allowed_directions:
415+
return try_direction
416+
417+
# Surrender
418+
self.move(-1)
419+
raise Exception('No moves left, surrendered.')
420+

basic.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from LightningBot import LightningBot
77
from random import randint
8-
from sys import argv
98

109
# Initialize bot and connect to a game
1110
bot = LightningBot(
@@ -14,7 +13,6 @@
1413
bot_name = 'Basic' + '%04d' % randint(0, 9999),
1514

1615
# Or token for ranked server, supplied as first command line argument
17-
api_token = argv[1] if len(argv) > 1 else None,
1816
#api_token = '00000000000000000000',
1917

2018
# Disable the interactive output to run in the background or multiple bots in parallel in the same terminal
@@ -33,6 +31,10 @@
3331
if bot.turn_number % bot.game_size == 0:
3432
bot.move((move_direction + 1) % 4)
3533

36-
# Otherwise just move in this direction
34+
# Otherwise just go straight
3735
else:
36+
37+
# But avoid losing
38+
move_direction = bot.avoidLosingMove(move_direction)
39+
3840
bot.move(move_direction)

fibonacci.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55

66
from LightningBot import LightningBot
77
from random import randint
8-
from sys import argv
98

109
bot = LightningBot(
1110
bot_name = 'Fibnci' + '%04d' % randint(0, 9999),
12-
api_token = argv[1] if len(argv) > 1 else None
1311
)
1412

1513
move_direction = randint(0, 3)
@@ -34,4 +32,6 @@
3432
side_length = 0
3533
turn_number += 1
3634

35+
move_direction = bot.avoidLosingMove(move_direction)
36+
3737
bot.move(move_direction)

hilbert.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# Move along a hilbert curve which fills the board
44

55
from LightningBot import LightningBot
6-
from random import randint, shuffle
7-
from sys import argv
6+
from random import randint
87

98
bot = LightningBot(
109
bot_name = 'Hilbert' + '%03d' % randint(0, 999),

loser.py

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from LightningBot import LightningBot
66
from random import randint
7-
from sys import argv
87

98
bot = LightningBot(
109
bot_name = 'Loser' + '%05d' % randint(0, 99999),

rando.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from LightningBot import LightningBot
66
from random import randint
7-
from sys import argv
87

98
bot = LightningBot(
109
bot_name = 'Rando' + '%04d' % randint(0, 9999),
@@ -18,4 +17,6 @@
1817

1918
move_direction = (move_direction + turn_direction) % 4
2019

20+
move_direction = bot.avoidLosingMove(move_direction)
21+
2122
bot.move(move_direction)

snowflake.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
from LightningBot import LightningBot
66
from random import randint, shuffle
7-
from sys import argv
87

98
bot = LightningBot(
109
bot_name = 'SnwFlk' + '%04d' % randint(0, 9999),
11-
api_token = argv[1] if len(argv) > 1 else None
1210
)
1311

1412

@@ -42,5 +40,7 @@
4240
if turn_direction != 0:
4341
move_direction = (move_direction + turn_direction) % 4
4442

43+
move_direction = bot.avoidLosingMove(move_direction)
44+
4545
# Move
4646
bot.move(move_direction)

spiral.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
from LightningBot import LightningBot
66
from random import randint
7-
from sys import argv
87

98
bot = LightningBot(
109
bot_name = 'Spiral' + '%04d' % randint(0, 9999),
11-
api_token = argv[1] if len(argv) > 1 else None
1210
)
1311

1412
move_direction = randint(0, 3)
@@ -32,4 +30,6 @@
3230
if turn_number > 0 and turn_number % 2 == 1:
3331
turn_after += 1
3432

33+
move_direction = bot.avoidLosingMove(move_direction)
34+
3535
bot.move(move_direction)

stepdown.py

-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44

55
from LightningBot import LightningBot
66
from random import randint
7-
from sys import argv
87

98
bot = LightningBot(
109
bot_name = 'StpDwn' + '%04d' % randint(0, 9999),
11-
api_token = argv[1] if len(argv) > 1 else None
1210
)
1311

1412
while bot.waitForNextTurnDirections():

swerve.py

-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@
55
from LightningBot import LightningBot
66
from random import randint
77
from pprint import pprint
8-
from sys import argv
98

109
bot = LightningBot(
1110
bot_name = 'Swerve' + '%04d' % randint(0, 9999),
12-
api_token = argv[1] if len(argv) > 1 else None,
1311
)
1412

1513
move_direction = randint(0, 3)

0 commit comments

Comments
 (0)