Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
import socket
import time
from controller_config import config
import sys

# Arguably dangerous to assume these exist, but OK to crash if they don't
# since these values are necessary
HOST = config['robot']['ip']
if len(sys.argv)>1:
HOST = sys.argv[2]
else:
HOST = config['robot']['ip']

PORT = config['robot']['port']

# Socket for connecting to robot
Expand All @@ -21,8 +26,10 @@

# Open connection
def open():
global s
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Connecting to {}:{}".format(HOST, PORT))
s.connect((HOST, PORT))
conn_open = True
except Exception as exception:
Expand All @@ -35,20 +42,20 @@ def close():
conn_open = False

# The following several functions are to convert the given commands into a later seperatable string
def move_piece(cellA,cellB):
message = 'move_piece;' + str(cellA) + ';' + str(cellB)
def move_piece(cellA,cellB, piece_type):
message = 'move_piece;' + str(cellA) + ';' + str(cellB) + ';' + str(piece_type)
send(message)

def move(cellA, cellB):
message = 'move;' + str(cellA) + ';' + str(cellB)
def move(cellA, cellB, piece_type):
message = 'move;' + str(cellA) + ';' + str(cellB) + ';' + str(piece_type)
send(message)

def take_piece(cellA, cellB, piece):
message = 'take_piece;' + str(cellA) + ';' + str(cellB) + ';' + str(piece)
def take_piece(cellA, cellB, piece, piece_typeA, piece_typeB):
message = 'take_piece;' + str(cellA) + ';' + str(cellB) + ';' + str(piece) + ';' + str(piece_typeA) + ';' + str(piece_typeB)
send(message)

def perform_castling_at(cellA, cellB, cellC, cellD):
message = 'perform_castling_at;' + str(cellA) + ';' + str(cellB) + ';' + str(cellC) + ';' + str(cellD)
def perform_castling_at(cellA, cellB, cellC, cellD, piece_typeA, piece_typeB):
message = 'perform_castling_at;' + str(cellA) + ';' + str(cellB) + ';' + str(cellC) + ';' + str(cellD) + ';' + str(piece_typeA) + ';' + str(piece_typeB)
send(message)

def en_passant(cellA, cellB, cellTake, piece):
Expand Down
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ robot:
port: 64432

controller:
id: 'CHANGE_ME'
id: 'kevin'
version: '0.0.1'
26 changes: 25 additions & 1 deletion controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
import json
import internal_logic
from controller_config import config
import client as cl
from square_piece import square_to_piece

# Arguably dangerous to assume these exist, but OK to crash if they don't
# since these values are necessary
controller_id = config['controller']['id']
version = config['controller']['version']
controller_id = config['controller']['id']

if len(sys.argv)>1:
controller_id = sys.argv[1]

req = communication.Requester(controller_id, version)

def control_loop():

ply_count = 0

while True:
Expand All @@ -24,6 +30,24 @@ def control_loop():
jsonfile = r.json()
print(jsonfile)

if(jsonfile['game_over']['game_over']) {
piece_list = ["a1","b1","c1","d1","e1","f1","g1","h1",
"a2","b2","c2","d2","e2","f2","g2","h2",
"a7","b7","c7","d7","e7","f7","g7","h7",
"a8","b8","c8","d8","e8","f8","g8","h8",]
for square, i in enumerate(jsonfile["initial_positions"]):
initial_position = reset.reset_board(square, jsonfile['initial_positions'])
piece_list.remove(initial_position)
if piece_list != None:
for initial_pos, i in enumerate(piece_list):
col, row = square[:1].upper(), int(square[1:])
cell = (col,row)
buffer_cell = (8 + (row % 4), col
piece = square_to_piece[initial_pos]
cl.move_piece(buffer_cell, cell, piece)
break
}

for actions, i in enumerate(jsonfile["history"]):
ply_count = internal_logic.parseJson(jsonfile["history"][actions],ply_count)
r = req.sendResponse(ply_count)
Expand Down
27 changes: 27 additions & 0 deletions reset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import json
import client as cl
from square_piece import square_to_piece

# Close connection to robot
def close():
cl.close()

# Function to select the appropriate move based on the json data (move_data)
# move_data has the form jsondata["history"][n], where n is the move identifier
def reset_board(square, piece_map):

print(square)
initial_position = piece_map[square]


if square != initial_position:
piece = square_to_piece[initial_position]

rowA, colA = square[:1].upper(), int(square[1:])
cellA = (rowA,colA)

rowB, colB = initial_position[:1].upper(), int(initial_position[1:])
cellB = (rowB,colB)

cl.move_piece(cellA, cellB, piece)
return initial_position
34 changes: 34 additions & 0 deletions square_piece.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
square_to_piece = {}

square_to_piece["a1"] = 'r'
square_to_piece["b1"] = 'n'
square_to_piece["c1"] = 'b'
square_to_piece["d1"] = 'q'
square_to_piece["e1"] = 'k'
square_to_piece["f1"] = 'b'
square_to_piece["g1"] = 'n'
square_to_piece["h1"] = 'r'
square_to_piece["a2"] = 'p'
square_to_piece["b2"] = 'p'
square_to_piece["c2"] = 'p'
square_to_piece["d2"] = 'p'
square_to_piece["e2"] = 'p'
square_to_piece["f2"] = 'p'
square_to_piece["g2"] = 'p'
square_to_piece["h2"] = 'p'
square_to_piece["a7"] = 'p'
square_to_piece["b7"] = 'p'
square_to_piece["c7"] = 'p'
square_to_piece["d7"] = 'p'
square_to_piece["e7"] = 'p'
square_to_piece["f7"] = 'p'
square_to_piece["g7"] = 'p'
square_to_piece["h7"] = 'p'
square_to_piece["a8"] = 'r'
square_to_piece["b8"] = 'n'
square_to_piece["c8"] = 'b'
square_to_piece["d8"] = 'q'
square_to_piece["e8"] = 'k'
square_to_piece["f8"] = 'b'
square_to_piece["g8"] = 'n'
square_to_piece["h8"] = 'r'