class TwoPlayerChess: def __init__(self): self.white = ChessGame(10) self.black = ChessGame(20, initBoard=self.white.board) def makeWhiteMove(self, move, checkLegal=False): return self.white.makeMove(move, checkLegal) def makeBlackMove(self, move, checkLegal=False): return self.black.makeMove(move, checkLegal) def printBoard(self): print(self.white) def kingStatus( self): # 0: neither, 1: white king, 2: black king, 3: both kings x = 1 if self.white.moveGen.getKingPos() else 0 y = 2 if self.black.moveGen.getKingPos() else 0 return x + y def printGameStatus(self): stat = self.kingStatus() if stat == 0: print('You done goofed, neither have a king.') elif stat == 1: print('White won!') elif stat == 2: print('Black won!') else: pass
def run_game(player2, move_num=20): player1 = RandomAI() game = ChessGame(player1, player2) for _ in range(move_num): print(game) game.make_move() return game
def playNTimes(self, n): winStats = [0, 0] for i in range(n): print('Currently playing:', i) self.playUntilDone(printStuff=False) stat = self.kingStatus() if stat == 1: winStats[0] += 1 elif stat == 2: winStats[1] += 1 else: if self.white.moveEval.boardHeuristic( ) > self.black.moveEval.boardHeuristic(): winStats[0] += 1 else: winStats[1] += 1 self.white = ChessGame(10) self.black = ChessGame(20, initBoard=self.white.board) print('\nWin Count (%) :') print('White:', winStats[0], ' (', winStats[0] / n, ')') print('Black:', winStats[1], ' (', winStats[1] / n, ')') return winStats
class ChessGui: def __init__(self, player1, player2): self.player1 = player1 self.player2 = player2 self.game = ChessGame(player1, player2) self.app = QApplication(sys.argv) self.svgWidget = QtSvg.QSvgWidget() self.svgWidget.setGeometry(50, 50, 400, 400) self.svgWidget.show() def start(self): self.timer = QTimer() self.timer.timeout.connect(self.make_move) self.timer.start(10) self.display_board() def display_board(self): svgboard = chess.svg.board(self.game.board) svgbytes = QByteArray() svgbytes.append(svgboard) self.svgWidget.load(svgbytes) def make_move(self): print("making move, white turn " + str(self.game.board.turn)) self.game.make_move() self.display_board()
def __init__(self, aPlayer1, aPlayer2): self.player1 = aPlayer1 self.player2 = aPlayer2 self.game = ChessGame(aPlayer1, aPlayer2) self.app = QApplication(sys.argv) self.svgWidget = QtSvg.QSvgWidget() self.svgWidget.setGeometry(50, 50, 400, 400) self.svgWidget.show()
def test_3(self): # Arrange game = ChessGame('7k/8/2P1P3/1K3P2/3N4/1P3P2/2P1P3/8 w - - 0 50') expected = '0' # Act actual = game.get_knight_possible_moves() # Assert self.assertEqual(expected, actual)
def test_4(self): # Arrange game = ChessGame('k6b/6b1/5b2/4b3/3b4/2b5/1b6/b6K b - - 0 50') expected = '24\nb2a3 b2c1\nc3a5 c3b4 c3d2 c3e1\nd4a7 d4b6 d4c5 d4e3 d4f2 d4g1\ne5b8 e5c7 e5d6 e5f4 e5g3 e5h2\nf6d8 f6e7 f6g5 f6h4\ng7f8 g7h6' # Act actual = game.get_bishop_possible_moves() # Assert self.assertEqual(expected, actual)
def test_0(self): # Arrange game = ChessGame('2k5/8/8/4N3/8/8/P7/2K5 w - - 6 19') expected = '8\ne5c4 e5c6 e5d3 e5d7 e5f3 e5f7 e5g4 e5g6' # Act actual = game.get_knight_possible_moves() # Assert self.assertEqual(expected, actual)
def test_13(self): # Arrange game = ChessGame('rnbqkbnr/pppp2pp/8/3Pp3/8/4p2P/PPP2PP1/RNBQKBNR w KQkq e6 0 5') move_str = 'd5e6' expected = 'rnbqkbnr/pppp2pp/4P3/8/8/4p2P/PPP2PP1/RNBQKBNR b KQkq - 0 5' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_12(self): # Arrange game = ChessGame('rnbqkbnr/ppppp1pp/8/8/3PPp2/7P/PPP2PP1/RNBQKBNR b KQkq e3 0 3') move_str = 'f4e3' expected = 'rnbqkbnr/ppppp1pp/8/8/3P4/4p2P/PPP2PP1/RNBQKBNR w KQkq - 0 4' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_11(self): # Arrange game = ChessGame('rnbqkbnr/1pppp1pp/p7/3P4/4Pp2/7P/PPP2PP1/RNBQKBNR b KQkq - 0 4') move_str = 'e7e5' expected = 'rnbqkbnr/1ppp2pp/p7/3Pp3/4Pp2/7P/PPP2PP1/RNBQKBNR w KQkq e6 0 5' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_9(self): # Arrange game = ChessGame('rnbqkbnr/p1p1p1p1/8/PpP1P3/3p1pPp/8/1P1P1P1P/RNBQKBNR w KQkq b6 0 8') move_str = 'c5b6' expected = 'rnbqkbnr/p1p1p1p1/1P6/P3P3/3p1pPp/8/1P1P1P1P/RNBQKBNR b KQkq - 0 8' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_4(self): # Arrange game = ChessGame('r3k2r/pppppppp/8/N7/8/8/PPPPPPPP/R3K2R b KQkq - 0 16') move_str = 'e8f8' expected = 'r4k1r/pppppppp/8/N7/8/8/PPPPPPPP/R3K2R w KQ - 1 17' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_0(self): # Arrange game = ChessGame('r4rk1/pppppppp/8/N7/8/8/PPPPPPPP/R3K2R w KQ - 1 17') move_str = 'e1g1' expected = 'r4rk1/pppppppp/8/N7/8/8/PPPPPPPP/R4RK1 b - - 2 17' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_9(self): # Arrange game = ChessGame('r3k2r/pppppp1p/8/N7/8/8/PPPPPPpP/R3K2R b KQkq - 0 1') move_str = 'g2h1r' expected = 'r3k2r/pppppp1p/8/N7/8/8/PPPPPP1P/R3K2r w Qkq - 0 2' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_3(self): # Arrange game = ChessGame('rnbqkbnr/ppp1pppp/8/4P3/2Pp4/8/PP1P1PPP/RNBQKBNR b KQkq c3 0 3') move_str = 'f7f6' expected = 'rnbqkbnr/ppp1p1pp/5p2/4P3/2Pp4/8/PP1P1PPP/RNBQKBNR w KQkq - 0 4' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_7(self): # Arrange game = ChessGame('rnbqkbnr/ppp1p1p1/8/P1P1P3/3p1pPp/8/1P1P1P1P/RNBQKBNR b KQkq g3 0 7') move_str = 'b7b5' expected = 'rnbqkbnr/p1p1p1p1/8/PpP1P3/3p1pPp/8/1P1P1P1P/RNBQKBNR w KQkq b6 0 8' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_0(self): # Arrange game = ChessGame( 'rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2') expected = '5\nf1a6 f1b5 f1c4 f1d3 f1e2' # Act actual = game.get_bishop_possible_moves() # Assert self.assertEqual(expected, actual)
def test_0(self): # Arrange game = ChessGame('k7/r7/b7/q7/N7/B7/R7/K7 w - - 15 48') move_str = 'a2h2' expected = 'k7/r7/b7/q7/N7/B7/7R/K7 b - - 16 48' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_3(self): # Arrange game = ChessGame( '6k1/8/1P1P1P2/B1B1B3/1B1K1P2/B1B1B3/1P1P1P2/8 w - - 0 50') expected = '0' # Act actual = game.get_bishop_possible_moves() # Assert self.assertEqual(expected, actual)
def test_9(self): # Arrange game = ChessGame('1Q4r1/1bp1k1pp/4P3/8/2n5/2N3K1/1p2R1PP/8 b - - 3 30') move_str = 'b2b1r' expected = '1Q4r1/1bp1k1pp/4P3/8/2n5/2N3K1/4R1PP/1r6 w - - 0 31' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_2(self): # Arrange game = ChessGame( 'r2qk2r/ppp2ppp/2np1n2/2bNp1B1/2B1P1b1/3P1N2/PPP2PPP/R2QK2R b KQkq - 3 7' ) expected = '13\nc5a3 c5b4 c5b6 c5d4 c5e3 c5f2\ng4c8 g4d7 g4e6 g4f3 g4f5 g4h3 g4h5' # Act actual = game.get_bishop_possible_moves() # Assert self.assertEqual(expected, actual)
def test_1(self): # Arrange game = ChessGame( 'r2qk2r/ppp2ppp/2np1n2/2b1p1B1/2B1P1b1/2NP1N2/PPP2PPP/R2QK2R w KQkq - 2 7' ) expected = '13\nc4a6 c4b3 c4b5 c4d5 c4e6 c4f7\ng5c1 g5d2 g5e3 g5f4 g5f6 g5h4 g5h6' # Act actual = game.get_bishop_possible_moves() # Assert self.assertEqual(expected, actual)
def test_9(self): # Arrange game = ChessGame( 'rnb1kbnr/pp2pppp/2p5/8/8/2N5/PPPP1PBP/R1BQK1NR w KQkq - 0 5') move_str = 'g2c6' expected = 'rnb1kbnr/pp2pppp/2B5/8/8/2N5/PPPP1P1P/R1BQK1NR b KQkq - 0 5' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
class ConsoleUserInterface: def __init__(self): player1 = Player('Helen', 'P') player2 = Player('Bob', 'M') self.game = ChessGame(player1, player2) self.output = sys.stdout self.input = sys.stdin # Draw self.game.board and the state of the game def show(self): print(HORIZONTAL_BORDER, file=self.output) for y in range(BOARD_SIZE, 0, -1): print(y, '|', end='', sep='', file=self.output) for x in range(BOARD_SIZE): if self.game.board.is_empty(x, y-1): print(" |", end='', file=self.output) else: piece = self.game.board.get_piece(x, y-1) print(piece.get_color().get_short_name(), end='', file=self.output) print(piece.get_type().get_short_name(), end='|', file=self.output) if y > 1: print('\n '+"+--"*8+"+", file=self.output) print('\n', HORIZONTAL_BORDER, sep='', file=self.output) print(' ', ' '.join(LETTERS_ON_BOARD), file=self.output) print(self.game.whose_turn(), 'turn', file=self.output) def loop(self): print('Please, use the international notation for game and magic word \'exit\', if you want finish', file=self.output) while 1: self.show() # next_step = input('What is next step?\n') print('What is the next step?', file=self.output) next_step = self.input.readline().rstrip() print(next_step) if len(next_step) == 5: try: (source_x, source_y, destination_x, destination_y) = check_good_input(next_step, 0, 1, 3, 4) except ChessException as e: print('Error:', e, file=self.output) if 5 < len(next_step) < 8: try: (source_x, source_y, destination_x, destination_y) = check_good_input(next_step, 1, 2, 4, 5) except ChessException as e: print('Error:', e, file=self.output) if next_step == 'exit': break if self.game.whose_turn() == self.game.board.get_piece(source_x, source_y).get_color(): try: self.game.move(source_x, source_y, destination_x, destination_y) except ChessException as e: print('Error:', e, file=self.output) else: print('To turn!', file=self.output)
def test_7(self): # Arrange game = ChessGame( 'rnb1kbnr/ppp1pppp/8/8/8/2N5/PPPP1PqP/R1BQKBNR w KQkq - 0 4') move_str = 'f1g2' expected = 'rnb1kbnr/ppp1pppp/8/8/8/2N5/PPPP1PBP/R1BQK1NR b KQkq - 0 4' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_5(self): # Arrange game = ChessGame( 'rnb1kbnr/ppp1pppp/8/3q4/8/8/PPPP1PPP/RNBQKBNR w KQkq - 0 3') move_str = 'b1f3' expected = 'rnb1kbnr/ppp1pppp/8/3q4/8/5N2/PPPP1PPP/R1BQKBNR b KQkq - 1 3' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_4(self): # Arrange game = ChessGame( 'rnbqkbnr/ppp1pppp/8/3P4/8/8/PPPP1PPP/RNBQKBNR b KQkq - 0 2') move_str = 'd8d5' expected = 'rnb1kbnr/ppp1pppp/8/3q4/8/8/PPPP1PPP/RNBQKBNR w KQkq - 0 3' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_2(self): # Arrange game = ChessGame( 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1') move_str = 'd7d5' expected = 'rnbqkbnr/ppp1pppp/8/3p4/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def test_7(self): # Arrange game = ChessGame( 'r1bqkb1r/pppppppp/5n2/6B1/3Q4/2N5/PPP1PPPP/R3KBNR b KQkq - 2 4') move_str = 'h8g8' expected = 'r1bqkb1r/pppppppp/5n2/6B1/3Q4/2N5/PPP1PPPP/R3KBNR w KQkq - 3 5' # Act game.move(move_str) # Assert self.assertEqual(expected, str(game.fen))
def move(): global games session = get_session() game = games[session] try: source_x = int(request.form['source_x']) source_y = int(request.form['source_y']) destination_x = int(request.form['destination_x']) destination_y = int(request.form['destination_y']) except: return json.dumps({'status': 'Error', 'message': 'Invalid arguments (move)'}) try: game.move(source_x, source_y, destination_x, destination_y) is_check = 0 if game.is_black_check: is_check = ChessColor.Black.value elif game.is_white_check: is_check = ChessColor.White.value is_mate = 0 if game.is_mate(ChessColor.Black): is_mate = ChessColor.Black.value elif game.is_mate(ChessColor.White): is_mate = ChessColor.White.value if is_mate: game = ChessGame(Player('Player', 'White'), Player('Player', 'Black')) games[session] = game resp = make_response(json.dumps({'status': 'Ok', 'is_check': is_check, 'is_mate': is_mate, 'turn': str(game.whose_turn()), 'board': game.board.to_json_array() })) resp.set_cookie('session', session) return resp # return 'Ok' except ChessException as e: return json.dumps({'status': 'Error', 'message': 'Movement problem: %s' % e})
def __init__(self): player1 = Player('Helen', 'P') player2 = Player('Bob', 'M') self.font_color = 'AntiqueWhite2' self.game = ChessGame(player1, player2) self.main_window = Tk() self.main_window.configure(background=self.font_color) self.main_window.title("Chess") self.CHESS_PIECE_IMAGES = {} self.empty_image = ImageTk.PhotoImage(Image.open('Empty_Image.png')) for piece_type in ChessPieceType: for color in ChessColor: self.CHESS_PIECE_IMAGES[str(color)+'_'+str(piece_type)] = Image.open(str(color)+'_'+str(piece_type)+'.png') self.first_chose_was_done = False self.source_coordinates = None self.check_shown = False
from time import time from ChessGame import ChessGame from ChessBoard import ChessBoard import matplotlib.pyplot as plt chessGame = ChessGame() delays = [] for depth in range(1, 11): t = time() board = chessGame.start(ChessBoard(), depth, False) t = time() - t #board.printBoard() print t delays.append(t) plt.plot(delays) plt.ylabel('Game time') plt.xlabel('Maximum depth') plt.savefig('delays.png') plt.show()
class WindowUserInterface: def __init__(self): player1 = Player('Helen', 'P') player2 = Player('Bob', 'M') self.font_color = 'AntiqueWhite2' self.game = ChessGame(player1, player2) self.main_window = Tk() self.main_window.configure(background=self.font_color) self.main_window.title("Chess") self.CHESS_PIECE_IMAGES = {} self.empty_image = ImageTk.PhotoImage(Image.open('Empty_Image.png')) for piece_type in ChessPieceType: for color in ChessColor: self.CHESS_PIECE_IMAGES[str(color)+'_'+str(piece_type)] = Image.open(str(color)+'_'+str(piece_type)+'.png') self.first_chose_was_done = False self.source_coordinates = None self.check_shown = False # self.result_notation_text = '' def get_image(self, chess_piece): return self.CHESS_PIECE_IMAGES[str(chess_piece)] def get_square_tk_color(self, i, j): if self.game.board.get_square_color(i, j) == ChessColor.Black: return 'sienna' else: return 'khaki' def set_boards(self): for x in range(1, BOARD_SIZE+1): top_letter = Label(self.main_window, text=LETTERS_ON_BOARD[x-1], bg=self.font_color) top_letter.grid(row=0, column=x) down_letter = Label(self.main_window, text=LETTERS_ON_BOARD[x-1], bg=self.font_color) down_letter.grid(row=9, column=x) for y in range(BOARD_SIZE, 0, -1): left_letter = Label(self.main_window, text=y, bg=self.font_color) left_letter.grid(row=abs(y-BOARD_SIZE)+1, column=0) right_letter = Label(self.main_window, text=y, bg=self.font_color) right_letter.grid(row=abs(y-BOARD_SIZE)+1, column=BOARD_SIZE+1) def set_or_destroy_check_message(self): if self.was_mate(): return 0 for color in ChessColor: if self.game.is_check(color) and self.check_shown == False: self.check_message = Label(self.frame_for_data, text='Check for '+str(color), bg=self.font_color, width=20, height=1, font="Verdana 13 bold") self.check_message.grid(row=0, column=1) self.check_shown = True if self.check_shown and not (self.game.is_white_check or self.game.is_black_check): self.check_message.destroy() self.check_shown = False def was_mate(self): for color in ChessColor: if self.game.is_mate(color): self.end_of_game(color.get_another_color()) return True def save_notation(self): # with open("notation.txt", "w") as out: # print(self.game.result_notation_text, file=out) self.save_file(self.game.result_notation_text) def open_new_game(self): self.main_window.destroy() new = WindowUserInterface() new.show() def end_of_game(self, winner_color): frame_for_mate_message = Frame(self.main_window, bg='', height=50) frame_for_mate_message.grid(row=1, column=1, columnspan=8, rowspan=8) mate_message = Label(frame_for_mate_message, text=str(winner_color)+' win!', padx=80, font="Verdana 40 bold") mate_message.pack(side='top') # save_button = Button(frame_for_mate_message, text='Save notation', command=self.save_notation) # save_button.pack(side='left') # # new_game_button = Button(frame_for_mate_message, text='New game', command=self.open_new_game) # new_game_button.pack(side='right') for i in range(BOARD_SIZE): for j in range(BOARD_SIZE): self.labels[i+1][j+1].unbind("<Button 1>") def click_handler(self, x, y): if self.first_chose_was_done: (source_x, source_y) = self.source_coordinates destination_x, destination_y = (x, y) if (source_x, source_y) == (destination_x, destination_y): self.source_coordinates = None self.first_chose_was_done = False return 0 if not (self.game.board.is_empty(source_x, source_y) or self.game.board.is_empty(destination_x, destination_y)): if self.game.board.get_piece(source_x, source_y).get_color() == self.game.board.get_piece(destination_x, destination_y).get_color(): self.source_coordinates = (destination_x, destination_y) self.first_chose_was_done = True return 0 if self.game.is_move_correct(source_x, source_y, destination_x, destination_y): self.first_chose_was_done = False self.game.previous_board = copy.deepcopy(self.game.board) self.game.move(source_x, source_y, destination_x, destination_y) self.set_or_destroy_check_message() self.show_this_squares(self.game.squares_changed_last_move()) self.show_whose_turn() # print('From '+str(source_x)+str(source_y)+'to'+str(destination_x)+str(destination_y)) else: self.source_coordinates = None self.first_chose_was_done = False self.show_mistake('Wrong move!') else: if self.game.board.is_empty(x, y): self.show_mistake('You can\'t go from empty square') else: if self.game.whose_turn() != self.game.board.get_piece(x, y).get_color(): self.show_mistake(str(self.game.whose_turn())+' player\'s turn!') else: self.first_chose_was_done = True self.source_coordinates = (x, y) def show_mistake(self, message): mistake_window = Toplevel() mistake_message = Label(mistake_window, text=message) mistake_message.pack() mistake_window.geometry('200x90+700+355') def show_whose_turn(self): self.area_whose_turn['text'] = 'Turn for ' + str(self.game.whose_turn()) def save_file(self, saving_text): file = tkFileDialog.SaveAs(self.main_window, filetypes=[('*.txt files', '.txt')]).show() if file == '': return if not file.endswith(".txt"): file += ".txt" open(file, 'wt').write(saving_text) def save_game(self): encoded_game = jsonpickle.encode(self.game) # file = tkFileDialog.SaveAs(self.main_window, filetypes=[('*.txt files', '.txt')]).show() # if file == '': # return # if not file.endswith(".txt"): # file += ".txt" # open(file, 'wt').write(encoded_game) self.save_file(encoded_game) def recursive_function_for_fields(self, pending_object, object_in_normal_game): a = {} try: a = object_in_normal_game.__dict__.keys() except AttributeError: pass for field in a: if field.startswith('__') and field.endswith('__'): continue try: if type(getattr(pending_object, field)) != type(getattr(object_in_normal_game, field)): print('False1') return False except AttributeError: print('False2') return False print(field) if not self.recursive_function_for_fields(getattr(pending_object, field), getattr(object_in_normal_game, field)): return False return True def check_board(self, pending_board): for i in range(BOARD_SIZE): for j in range(BOARD_SIZE): if not pending_board.is_empty(i, j): pending_piece = pending_board.get_piece(i, j) if not self.recursive_function_for_fields(pending_piece, ChessPiece(ChessColor.White, ChessPieceType.Bishop)): print('False8') return False return True def loaded_game_is_correct(self): game_for_check = ChessGame(Player('A', 'B'), Player('C', 'D')) if type(self.game) != ChessGame: return False if not self.recursive_function_for_fields(self.game, game_for_check): return False if not self.check_board(self.game.board): return False if type(self.game.board) != ChessBoard or type(self.game.previous_board) != ChessBoard: print('False5') return False if type(self.game.player_black) != Player or type(self.game.player_white) != Player: print('False6') return False return True def load_game(self): encoded_game = self.load_file() if encoded_game is None: return try: self.game = jsonpickle.decode(encoded_game) except Exception: self.show_mistake('Can\'t open game file') return if not self.loaded_game_is_correct(): self.show_mistake('Object in file is not a chess game') return try: self.start_frame.destroy() except AttributeError: pass self.show() def load_file(self): file = tkFileDialog.Open(self.main_window, filetypes=[('*.txt files', '.txt')]).show() if file == '': return return open(file, 'r').read() def place_main_window(self, width, height): screen_width = self.main_window.winfo_screenwidth() screen_height = self.main_window.winfo_screenheight() x_coordinate = screen_width/2-width/2 y_coordinate = screen_height/2 - height/2-20 self.main_window.geometry('%dx%d+%d+%d' % (width, height, x_coordinate, y_coordinate)) def show_this_squares(self, array): for (i, j) in array: if not self.game.board.is_empty(i, j): piece = self.game.board.get_piece(i, j) img = self.get_image(piece) img = ImageTk.PhotoImage(img) self.labels[i+1][j+1]['image'] = img self.labels[i+1][j+1].image = img else: self.labels[i+1][j+1]['image'] = self.empty_image self.labels[i+1][j+1].image = self.empty_image def show(self): self.frames = [[Frame(self.main_window, height=SQUARE_SIZE, width=SQUARE_SIZE, bd=0) for j in range(BOARD_SIZE + 1)] for i in range(BOARD_SIZE + 1)] self.labels = [[Label(self.frames[i][j], width=SQUARE_SIZE-10, height=SQUARE_SIZE-10) for j in range(BOARD_SIZE + 1)] for i in range(BOARD_SIZE+1)] self.set_boards() for i in range(BOARD_SIZE): for j in range(BOARD_SIZE): handler = lambda g, ii=i, jj=j: self.click_handler(ii, jj) self.frames[i+1][j+1]['bg'] = self.get_square_tk_color(i, j) self.frames[i+1][j+1].bind("<Button-1>", handler) self.frames[i+1][j+1].grid(row=abs(j+1-BOARD_SIZE)+1, column=i+1, sticky=S+W+N+E) self.labels[i+1][j+1]['bg'] = self.get_square_tk_color(i, j) self.labels[i+1][j+1].bind("<Button-1>", handler) self.labels[i+1][j+1].pack(fill='both') self.frame_for_data = Frame(self.main_window, bd=1, bg=self.font_color) self.frame_for_data.grid(column=1, row=10, columnspan=12, sticky=N) self.area_whose_turn = Label(self.frame_for_data, text='Turn for ' + str(self.game.whose_turn()), bg=self.font_color, width=59) self.area_whose_turn.grid(row=1, column=1, rowspan=1) self.save_button = Button(self.frame_for_data, text='Save game', command=self.save_game, width=11) self.save_button.grid(row=0, column=2) self.open_new_button = Button(self.frame_for_data, text='Open new game', command=self.open_new_game) self.open_new_button.grid(row=1, column=0, rowspan=1) self.place_main_window(618, 720) self.load_game_button = Button(self.frame_for_data, text='Load game', command=self.load_game, width=11) self.load_game_button.grid(row=1, column=2) self.save_notation_button = Button(self.frame_for_data, text='Save notation', command=self.save_notation, width=11) self.save_notation_button.grid(row=2, column=2) squares_array = [] for i in range(BOARD_SIZE): for j in range(BOARD_SIZE): squares_array.append((i, j)) self.show_this_squares(squares_array) def show_start_view(self): self.start_frame = Frame(self.main_window, bg=self.font_color, bd=16) self.place_main_window(220, 60) self.start_frame.grid(row=0, column=0) load_button = Button(self.start_frame, text='load saved game', command=self.load_game) load_button.grid(row=1, column=2) new_game_button = Button(self.start_frame, text='start new game', command=self.open_new_game) new_game_button.grid(row=1, column=1) def loop(self): self.main_window.mainloop() pass
def __init__(self): player1 = Player('Helen', 'P') player2 = Player('Bob', 'M') self.game = ChessGame(player1, player2) self.output = sys.stdout self.input = sys.stdin