Skip to content
Snippets Groups Projects
Commit cf46f5e9 authored by Bariatti Francesco's avatar Bariatti Francesco
Browse files

Updated tools

parent 434c94c4
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
#-*- encoding: utf-8 -*-
def board2Bitboard(board):
"""
Convert a real board to its bitboard representation.
:param board: a sequence of 1, 2 and 3 that tells how many fish are in every tile. A character that isn't 1, 2 or 3 is considered water. Newlines, pipes and spaces are ignored.
:type board: str
"""
board = board.replace("\n", "")
board = board.replace("|", "")
board = board.replace(" ", "")
print(board)
if len(board) != 60:
print("Warning: board is not 60 char long!")
ones = 0
twos = 0
threes = 0
for c in board:
if c == '1':
ones |= 1
elif c == '2':
twos |= 1
elif c == '3':
threes |= 1
ones = ones << 1
twos = twos << 1
threes = threes << 1
ones = ones >> 1
twos = twos >> 1
threes = threes >> 1
return (ones, twos, threes)
if __name__ == "__main__":
try:
while True:
board = input("Enter board: ")
ones, twos, threes = board2Bitboard(board)
print("one_fish: {}\ntwo_fish: {}\nthree_fish: {}".format(ones, twos, threes))
except KeyboardInterrupt:
print("\nMay the fish be with you.")
...@@ -13,24 +13,22 @@ if __name__ == "__main__": ...@@ -13,24 +13,22 @@ if __name__ == "__main__":
#READ #READ
readloop = True readloop = True
comments = [] comments = []
json_data = [] json_data = ""
brackets_count = 0 brackets_count = 0
while readloop: while readloop:
line = program.stdout.readline() line = program.stdout.readline()
#print(line) print(line)
if line.startswith("{"): #in json or entering json line = line.replace("Red move: ", "")
brackets_count += 1 line = line.replace("Blue move: ", "")
if brackets_count > 0: #We are in the middle of json data if line.startswith("{"): #Reading json
json_data.append(line) json_data = line
else: else:
comments.append(line) comments.append(line)
if line.startswith("}"):
brackets_count -= 1
if line == "\n": if line == "\n":
readloop = False readloop = False
#PRINT STATE #PRINT STATE
print("Comments: {}".format(''.join(comments))) print("Comments: {}".format(''.join(comments)))
state = json.loads(''.join(json_data)) state = json.loads(json_data)
drawState.drawBitboard(state["bitboards"]["onefish"],state["bitboards"]["twofish"],state["bitboards"]["threefish"]) drawState.drawBitboard(state["bitboards"]["onefish"],state["bitboards"]["twofish"],state["bitboards"]["threefish"])
print("Red penguins (Red score: {}, Total moves: {} [0..{}])".format(state["score"]["red"], state["nb_moves"]["red"], state["nb_moves"]["red"]-1)) print("Red penguins (Red score: {}, Total moves: {} [0..{}])".format(state["score"]["red"], state["nb_moves"]["red"], state["nb_moves"]["red"]-1))
for i in range(4): for i in range(4):
...@@ -45,6 +43,8 @@ if __name__ == "__main__": ...@@ -45,6 +43,8 @@ if __name__ == "__main__":
move = input("Enter Blue move: ") move = input("Enter Blue move: ")
program.stdin.write(move+"\n") program.stdin.write(move+"\n")
program.stdin.flush() program.stdin.flush()
except BrokenPipeError:
print("Game end")
except KeyboardInterrupt: except KeyboardInterrupt:
print("\nMay the fish be with you.") print("\nMay the fish be with you.")
finally: finally:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment