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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ A chinese chess engine

> require

* `python-2.7.x`: <http://python.org>
* `pygame-1.9.x`: <http://pygame.org>
* `python-3.x`: <http://python.org>
* `pygame-2.x`: <http://pygame.org>

Hint: install pygame on OS X Lion

Expand All @@ -20,7 +20,7 @@ Hint: install pygame on OS X Lion
```
$ git clone git://github.com/timebug/harmless.git
$ make && make install
$ cd pycchess && python cchess.py
$ cd pycchess && python3 cchess.py
```

### Windows User
Expand Down
24 changes: 12 additions & 12 deletions pycchess/cchess.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env python
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# pycchess - just another chinese chess UI
Expand Down Expand Up @@ -28,7 +28,7 @@
import sys
from subprocess import PIPE, Popen
from threading import Thread
from Queue import Queue, Empty
from queue import Queue, Empty

ON_POSIX = 'posix' in sys.builtin_module_names

Expand All @@ -52,13 +52,13 @@ def enqueue_output(out, queue):
pygame.display.set_caption("black")
chessboard.side = BLACK
else:
print '>> quit game'
print('>> quit game')
sys.exit()

chessboard.net.NET_HOST = sys.argv[2]

elif len(sys.argv) == 1:
p = Popen("./harmless", stdin=PIPE, stdout=PIPE, close_fds=ON_POSIX)
p = Popen("./harmless", stdin=PIPE, stdout=PIPE, close_fds=ON_POSIX, text=True)
(chessboard.fin, chessboard.fout) = (p.stdin, p.stdout)
q = Queue()
t = Thread(target=enqueue_output, args=(chessboard.fout, q))
Expand All @@ -82,7 +82,7 @@ def enqueue_output(out, queue):
pygame.display.set_caption("harmless")
chessboard.side = RED
else:
print '>> quit game'
print('>> quit game')
sys.exit()

chessboard.fen_parse(fen_str)
Expand All @@ -97,7 +97,7 @@ def newGame():

chessboard.fin.write("setoption newgame\n")
chessboard.fin.flush()
print '>> new game'
print('>> new game')

chessboard.fen_parse(fen_str)
init = True
Expand All @@ -113,7 +113,7 @@ def quitGame():
chessboard.fin.flush()
p.terminate()

print '>> quit game'
print('>> quit game')
sys.exit()

def runGame():
Expand All @@ -137,8 +137,8 @@ def runGame():
break
if y < BORDER or y > (HEIGHT - BORDER):
break
x = (x - BORDER) / SPACE
y = (y - BORDER) / SPACE
x = (x - BORDER) // SPACE
y = (y - BORDER) // SPACE
if not waiting and not chessboard.over:
moved = chessboard.move_chessman(x, y)
if chessboard.mode == NETWORK and moved:
Expand All @@ -152,7 +152,7 @@ def runGame():
if moved:
if chessboard.mode is NETWORK:
move_str = chessboard.net.get_move()
if move_str is not 'quit':
if move_str != 'quit':
# print 'recv move: %s' % move_str
move_arr = str_to_move(move_str)
else:
Expand All @@ -176,7 +176,7 @@ def runGame():
win_side = 'BLACK'
else:
win_side = 'RED'
print '>>', win_side, 'win'
print('>>', win_side, 'win')

return
elif output[0:8] == 'bestmove':
Expand All @@ -201,7 +201,7 @@ def runGame():
win_side = 'BLACK'
else:
win_side = 'RED'
print '>>', win_side, 'win'
print('>>', win_side, 'win')

moved = False

Expand Down
22 changes: 11 additions & 11 deletions pycchess/chessboard.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env python
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# pycchess - just another chinese chess UI
Expand Down Expand Up @@ -73,18 +73,18 @@ def get_fen(self):
for j in range(10):
for i in range(9):
if (i, j) in self.board.keys():
if count is not 0:
if count != 0:
fen_str += str(count)
count = 0
chessman = self.board[(i, j)]
ch = get_char(chessman.kind, chessman.color)

if ch is not '':
if ch != '':
fen_str += ch
else:
count += 1

if count is not 0:
if count != 0:
fen_str += str(count)
count = 0
if j < 9:
Expand Down Expand Up @@ -308,18 +308,18 @@ def check(self, side):
def can_move(self, chessman, x, y):
ok = True
if chessman.kind == BISHOP:
m_x = (chessman.x + x) / 2
m_y = (chessman.y + y) / 2
m_x = (chessman.x + x) // 2
m_y = (chessman.y + y) // 2
if (m_x, m_y) in self.board.keys():
ok = False

if chessman.kind == KNIGHT:
if abs(chessman.x - x) == 2:
m_x = (chessman.x + x) / 2
m_x = (chessman.x + x) // 2
m_y = chessman.y
if abs(chessman.y - y) == 2:
m_x = chessman.x
m_y = (chessman.y + y) / 2
m_y = (chessman.y + y) // 2
if (m_x, m_y) in self.board.keys():
ok = False

Expand Down Expand Up @@ -356,10 +356,10 @@ def move_chessman(self, x, y):
if chessman.color == self.side:
flag = True
else:
if self.selected is ():
if self.selected == ():
return False

if self.selected is ():
if self.selected == ():
if flag:
self.selected = (x, y)

Expand Down Expand Up @@ -399,7 +399,7 @@ def move_chessman(self, x, y):
if self.net is not None:
self.net.send_move(move_str)
else:
print 'self.net is None'
print('self.net is None')

if self.mode == AI:
fen_str = self.get_fen()
Expand Down
2 changes: 1 addition & 1 deletion pycchess/chessman.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env python
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# pycchess - just another chinese chess UI
Expand Down
18 changes: 10 additions & 8 deletions pycchess/chessnet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env python
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# pycchess - just another chinese chess UI
Expand Down Expand Up @@ -30,14 +30,14 @@ def send_move(self, move):

try:
s.connect((self.NET_HOST, self.NET_PORT))
except socket.error, e:
print "Couldn't find your port: %s" % e
except socket.error as e:
print("Couldn't find your port: %s" % e)
sys.exit(1)

try:
s.send(move)
except socket.error, e:
print "Error sending data (detected by shutdown): %s" % e
s.send(move.encode() if isinstance(move, str) else move)
except socket.error as e:
print("Error sending data (detected by shutdown): %s" % e)
sys.exit(1)

s.close()
Expand All @@ -59,8 +59,10 @@ def get_move(self):
continue
try:
move = clientsock.recv(1024)
except socket.error, e:
print "Error receiving data: %s" % e
if isinstance(move, bytes):
move = move.decode()
except socket.error as e:
print("Error receiving data: %s" % e)
sys.exit(1)
except:
traceback.print_exc()
Expand Down
6 changes: 3 additions & 3 deletions pycchess/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env python
#! /usr/bin/env python3
# -*- coding: utf-8 -*-

# pycchess - just another chinese chess UI
Expand Down Expand Up @@ -119,6 +119,6 @@ def __init__(self, p, n):
def load_sound(name):
try:
sound = pygame.mixer.Sound(name)
except pygame.error, message:
raise SystemExit, message
except pygame.error as message:
raise SystemExit(message)
return sound