Commit e29dc4f5 authored by Michele's avatar Michele

fixed bug in teacher and made tictactoe.py fully deterministic

parent b01f7ce5
......@@ -59,24 +59,25 @@ class TicTacToeTeacher(AbstractTeacher):
# Reply to a membership/output query
# trace is a list of inputs and or outputs
def process(self, trace):
print(ddd)
# reset SUL
reset = self.reset()
if not reset:
logger.error("Cannot reset.")
return False
output = None
if len(trace) == 0:
# Get output for the empty trace
ready = select.select([self._socket], [], [], 1) # 1 second timeout, for quiescence
output = 'delta'
if ready[0]:
output = self._socket.recv(1024).decode("utf-8")
for action in trace:
# TODO finish here, maybe not needed
print(ddd)
return None
return output
# reset = self.reset()
# if not reset:
# logger.error("Cannot reset.")
# return False
#
# output = None
# if len(trace) == 0:
# # Get output for the empty trace
# ready = select.select([self._socket], [], [], 1) # 1 second timeout, for quiescence
# output = 'delta'
# if ready[0]:
# output = self._socket.recv(1024).decode("utf-8")
# for action in trace:
# # TODO finish here, maybe not needed
# print(ddd)
# return None
# return output
# Provide a sequence of inputs (possibly one or even zero) end get an output
# Tic Tac Toe is a mealy machine like game, there is alternation of input and
......@@ -84,7 +85,11 @@ class TicTacToeTeacher(AbstractTeacher):
def oneOutput(self, actions):
for i in range(len(actions)):
self._socket.sendall(bytes(str(actions[i]), 'UTF-8'))
return self.output()
output = self.output()
print(actions)
print(output)
return output
# Provide output from current state
def output(self):
......
......@@ -22,9 +22,6 @@
# Copyright Stephen Ostermiller 2002-2014
import random
# make ot deterministic for the moment
# TODO: comment following line
random.seed(100)
import socket
import sys
import logging
......@@ -88,8 +85,10 @@ def moveRandom(moves):
numMoves += 1
if numMoves > 0:
moveNum = random.sample(range(numMoves), 1)[0]
numMoves = 0;
# TODO system is deterministic.
#moveNum = random.sample(range(numMoves), 1)[0]
moveNum = 1
numMoves = 0
for j in range(9):
if ((moves & (1<<j)) != 0):
numMoves += 1
......@@ -367,7 +366,7 @@ if __name__ == "__main__":
HOST = 'localhost'
PORT = 29000 # Arbitrary non-privileged port
logging.basicConfig(level=logging.INFO)
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
......@@ -414,10 +413,6 @@ if __name__ == "__main__":
#nice_print()
state = getState()
winner = detectWin(state)
if winner != 0:
logger.debug("Sending: END ")
conn.sendall(bytes("END", 'UTF-8'))
newGame()
# build an output
# _O_X_O_X_ stands for
......@@ -425,8 +420,17 @@ if __name__ == "__main__":
# | X | _ | O |
# | _ | X | _ |
board = in_line_board()
logger.debug("Sending: " + board)
conn.sendall(bytes(board, 'UTF-8'))
if winner != 0:
logger.debug("Sending: END ")
board = board + " END"
conn.sendall(bytes(board, 'UTF-8'))
newGame()
else:
logger.debug("Sending: " + board)
conn.sendall(bytes(board, 'UTF-8'))
logger.debug("Sending: EXIT")
conn.sendall(bytes("EXIT", 'UTF-8'))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment