Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Michele Volpato
Alnos
Commits
e29dc4f5
Commit
e29dc4f5
authored
Nov 12, 2015
by
Michele
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed bug in teacher and made tictactoe.py fully deterministic
parent
b01f7ce5
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
30 deletions
+39
-30
examples/tictactoe/tictacteacher.py
examples/tictactoe/tictacteacher.py
+23
-18
examples/tictactoe/tictactoe.py
examples/tictactoe/tictactoe.py
+16
-12
No files found.
examples/tictactoe/tictacteacher.py
View file @
e29dc4f5
...
...
@@ -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
):
...
...
examples/tictactoe/tictactoe.py
View file @
e29dc4f5
...
...
@@ -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'
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment