From ce89e09b8fd8341e4d77253038d22266467b75e5 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 2 Sep 2026 07:25:16 +0000 Subject: [PATCH] Port pycchess UI to Python 3 so the app can run on modern systems. The original client required Python 2.7; this updates syntax, integer division, subprocess text mode, and socket encoding for Python 3 and pygame 2. Co-authored-by: Cong Zhang --- README.md | 6 +++--- pycchess/cchess.py | 24 ++++++++++++------------ pycchess/chessboard.py | 22 +++++++++++----------- pycchess/chessman.py | 2 +- pycchess/chessnet.py | 18 ++++++++++-------- pycchess/common.py | 6 +++--- 6 files changed, 40 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 1cf5d86..be1e855 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ A chinese chess engine > require -* `python-2.7.x`: -* `pygame-1.9.x`: +* `python-3.x`: +* `pygame-2.x`: Hint: install pygame on OS X Lion @@ -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 diff --git a/pycchess/cchess.py b/pycchess/cchess.py index cc9ed02..7565053 100755 --- a/pycchess/cchess.py +++ b/pycchess/cchess.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # pycchess - just another chinese chess UI @@ -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 @@ -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)) @@ -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) @@ -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 @@ -113,7 +113,7 @@ def quitGame(): chessboard.fin.flush() p.terminate() - print '>> quit game' + print('>> quit game') sys.exit() def runGame(): @@ -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: @@ -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: @@ -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': @@ -201,7 +201,7 @@ def runGame(): win_side = 'BLACK' else: win_side = 'RED' - print '>>', win_side, 'win' + print('>>', win_side, 'win') moved = False diff --git a/pycchess/chessboard.py b/pycchess/chessboard.py index e69f4c6..d183e28 100644 --- a/pycchess/chessboard.py +++ b/pycchess/chessboard.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # pycchess - just another chinese chess UI @@ -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: @@ -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 @@ -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) @@ -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() diff --git a/pycchess/chessman.py b/pycchess/chessman.py index b92a36c..e9eef93 100644 --- a/pycchess/chessman.py +++ b/pycchess/chessman.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # pycchess - just another chinese chess UI diff --git a/pycchess/chessnet.py b/pycchess/chessnet.py index b91c5c7..4beb94b 100644 --- a/pycchess/chessnet.py +++ b/pycchess/chessnet.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # pycchess - just another chinese chess UI @@ -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() @@ -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() diff --git a/pycchess/common.py b/pycchess/common.py index bf27cf3..1c2e3ad 100644 --- a/pycchess/common.py +++ b/pycchess/common.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 # -*- coding: utf-8 -*- # pycchess - just another chinese chess UI @@ -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