-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Why this change was necessary: * Norms are important * Camel case instead of dash * for loops instead of enumerate * change some file names * import grouped and ordered correctly * warning about spacing and hyphens
- Loading branch information
1 parent
fe1e101
commit 66c32a4
Showing
31 changed files
with
429 additions
and
334 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
"""The HLT class to handle the connection""" | ||
import socket | ||
|
||
from public.hlt import GameMap, translate_cardinal | ||
|
||
|
||
class HLT: | ||
"""The HLT class to handle the connection""" | ||
def __init__(self, port): | ||
_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
_connection.connect(('localhost', port)) | ||
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
connection.connect(('localhost', port)) | ||
print('Connected to intermediary on port #' + str(port)) | ||
self._connection = _connection | ||
self.connection = connection | ||
|
||
def get_string(self): | ||
newString = "" | ||
new_string = "" | ||
buffer = '\0' | ||
while True: | ||
buffer = self._connection.recv(1).decode('ascii') | ||
buffer = self.connection.recv(1).decode('ascii') | ||
if buffer != '\n': | ||
newString += str(buffer) | ||
new_string += str(buffer) | ||
else: | ||
return newString | ||
return new_string | ||
|
||
def sendString(self, s): | ||
def send_string(self, s): | ||
s += '\n' | ||
self._connection.sendall(bytes(s, 'ascii')) | ||
self.connection.sendall(bytes(s, 'ascii')) | ||
|
||
def get_init(self): | ||
myID = int(self.get_string()) | ||
my_id = int(self.get_string()) | ||
game_map = GameMap(self.get_string(), self.get_string(), self.get_string()) | ||
return myID, game_map | ||
return my_id, game_map | ||
|
||
def send_init(self, name): | ||
self.sendString(name) | ||
self.send_string(name) | ||
|
||
def send_frame(self, moves): | ||
self.sendString(' '.join( | ||
self.send_string(' '.join( | ||
str(move.square.x) + ' ' + str(move.square.y) + ' ' + str(translate_cardinal(move.direction)) for move in | ||
moves)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,59 @@ | ||
""" | ||
To be launched by the Halite program as an intermediary, | ||
in order to enable a pipe player to join. | ||
""" | ||
import socket | ||
import sys | ||
|
||
# logging.basicConfig(filename='example.log', level=logging.DEBUG) | ||
|
||
try: | ||
# Connect | ||
# logging.warning("connecting") | ||
socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
socket_.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
socket_.bind(('localhost', int(sys.argv[1]))) # This is where the port is selected | ||
socket_.listen(1) | ||
connection, _ = socket_.accept() | ||
|
||
|
||
# IO Functions | ||
def sendStringPipe(toBeSent): | ||
sys.stdout.write(toBeSent + '\n') | ||
def send_string_pipe(to_be_sent): | ||
sys.stdout.write(to_be_sent + '\n') | ||
sys.stdout.flush() | ||
|
||
|
||
def getStringPipe(): | ||
str = sys.stdin.readline().rstrip('\n') | ||
return (str) | ||
def get_string_pipe(): | ||
str_pipe = sys.stdin.readline().rstrip('\n') | ||
return str_pipe | ||
|
||
|
||
def sendStringSocket(toBeSent): | ||
global connection | ||
toBeSent += '\n' | ||
connection.sendall(bytes(toBeSent, 'ascii')) | ||
def send_string_socket(to_be_sent): | ||
to_be_sent += '\n' | ||
connection.sendall(bytes(to_be_sent, 'ascii')) | ||
|
||
|
||
def getStringSocket(): | ||
global connection | ||
newString = "" | ||
def get_string_socket(): | ||
new_string = "" | ||
buffer = '\0' | ||
while True: | ||
buffer = connection.recv(1).decode('ascii') | ||
if buffer != '\n': | ||
newString += str(buffer) | ||
new_string += str(buffer) | ||
else: | ||
return newString | ||
return new_string | ||
|
||
|
||
while True: | ||
# Handle Init IO | ||
sendStringSocket(getStringPipe()) # Player ID | ||
sendStringSocket(getStringPipe()) # Map Dimensions | ||
sendStringSocket(getStringPipe()) # Productions | ||
sendStringSocket(getStringPipe()) # Starting Map | ||
sendStringPipe(getStringSocket()) # Player Name / Ready Response | ||
send_string_socket(get_string_pipe()) # Player ID | ||
send_string_socket(get_string_pipe()) # Map Dimensions | ||
send_string_socket(get_string_pipe()) # Productions | ||
send_string_socket(get_string_pipe()) # Starting Map | ||
send_string_pipe(get_string_socket()) # Player Name / Ready Response | ||
|
||
# Run Frame Loop | ||
while (getStringPipe() == 'Get map and play!'): # while True: | ||
sendStringSocket('Get map and play!') | ||
sendStringSocket(getStringPipe()) # Frame Map | ||
sendStringPipe(getStringSocket()) # Move List | ||
sendStringSocket('Stop playing!') | ||
while get_string_pipe() == 'Get map and play!': # while True: | ||
send_string_socket('Get map and play!') | ||
send_string_socket(get_string_pipe()) # Frame Map | ||
send_string_pipe(get_string_socket()) # Move List | ||
send_string_socket('Stop playing!') | ||
|
||
except Exception as e: | ||
except ConnectionError as e: | ||
# logging.warning(traceback.format_exc()) | ||
pass |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
import sys | ||
"""The MyBot.py file that executes the TrainedBot.py""" | ||
import os | ||
import sys | ||
|
||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) | ||
try: | ||
from public.models.bot.TrainedBot import TrainedBot | ||
from networking.hlt_networking import HLT | ||
except: | ||
raise | ||
|
||
mode = 'server' if (len(sys.argv) == 1) else 'local' | ||
|
||
if mode == 'server' or sys.argv[1]=='slave': # 'server' mode | ||
if mode == 'server' or sys.argv[1] == 'slave': # 'server' mode | ||
import hlt | ||
else: # 'local' mode | ||
import context | ||
|
||
port = int(sys.argv[1]) if len(sys.argv) > 1 else 2000 | ||
hlt = context.HLT(port=port) | ||
|
||
from public.models.bot.trainedBot import TrainedBot | ||
hlt = HLT(port=port) | ||
|
||
bot = TrainedBot() | ||
|
||
while True: | ||
myID, game_map = hlt.get_init() | ||
my_id, game_map = hlt.get_init() | ||
hlt.send_init("MyBot") | ||
bot.setID(myID) | ||
bot.set_id(my_id) | ||
|
||
while (mode == 'server' or hlt.get_string() == 'Get map and play!'): | ||
while mode == 'server' or hlt.get_string() == 'Get map and play!': | ||
game_map.get_frame(hlt.get_string()) | ||
moves = bot.compute_moves(game_map) | ||
hlt.send_frame(moves) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,29 @@ | ||
import sys | ||
"""The Opponent.py file that executes the ImprovedBot.py""" | ||
import os | ||
import sys | ||
|
||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) | ||
try: | ||
from public.models.bot.ImprovedBot import ImprovedBot | ||
from networking.hlt_networking import HLT | ||
except: | ||
raise | ||
|
||
mode = 'server' if (len(sys.argv) == 1) else 'local' | ||
|
||
if mode == 'server' or sys.argv[1]=='slave': # 'server' mode | ||
if mode == 'server' or sys.argv[1] == 'slave': # 'server' mode | ||
import hlt | ||
else: # 'local' mode | ||
import context | ||
|
||
port = int(sys.argv[1]) if len(sys.argv) > 1 else 2000 | ||
hlt = context.HLT(port=port) | ||
|
||
from public.models.bot.improvedBot import ImprovedBot | ||
hlt = HLT(port=port) | ||
|
||
bot = ImprovedBot() | ||
|
||
while True: | ||
myID, game_map = hlt.get_init() | ||
my_id, game_map = hlt.get_init() | ||
hlt.send_init("OpponentBot") | ||
bot.setID(myID) | ||
bot.set_id(my_id) | ||
|
||
while (mode == 'server' or hlt.get_string() == 'Get map and play!'): | ||
while mode == 'server' or hlt.get_string() == 'Get map and play!': | ||
game_map.get_frame(hlt.get_string()) | ||
moves = bot.compute_moves(game_map) | ||
hlt.send_frame(moves) |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.