def pygame_mainloop(): chess_board = ChessBoard() player = 0 chess_board.set_allowed_color(player) screen = pygame.display.set_mode((640, 480)) board_image, chess_image = load_images() selected = surface() is_selected_cell = False selected_cell = Point(None, None) offset = 120 print ("player's", player, "turn") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return elif event.type == pygame.MOUSEBUTTONUP: new_position = make_a_point_from_mouse(event, offset) if is_into_board(new_position): new_position.normalize_point() if is_selected_cell: is_selected_cell = False can_play = chess_board.play(selected_cell, new_position) if can_play: player = change_player(player) chess_board.set_allowed_color(player) print ("player's", player, "turn") else: print("Invalid move!!! Try again") selected_cell.set_point(None, None) else: selected_cell.set_point( new_position.get_x(), new_position.get_y()) if chess_board.allowed_selection(selected_cell, player): is_selected_cell = True else: selected_cell.set_point(None, None) screen.fill((109, 165, 165)) screen.blit(board_image, (offset, offset)) if selected_cell.get_x() is not None and \ selected_cell.get_y() is not None: screen.blit(selected, (selected_cell.get_x()*32 + offset + 2, selected_cell.get_y()*24 + offset + 2)) prepare_board_to_update(chess_board, screen, chess_image, offset) pygame.display.update() time.sleep(0.04) if chess_board.is_end(): print("The winner is Player {0}".format((player + 1) % 2)) time.sleep(4) return
def test_set_allowed_color_of_player(self): board = ChessBoard() player = 0 board.set_allowed_color(player) # Test 1 & 2 self.assertEqual(board.get_allawod_color(), "w") self.assertNotEqual(board.get_allawod_color(), "b") # Test 3 player = 1 board.set_allowed_color(player) self.assertEqual(board.get_allawod_color(), "b")
def main(): cb = ChessBoard() color = ('w', 'b') player = 0 cb.set_allowed_color(color[player]) while(1): print '-------------------------------------\n', str(cb), '\n-------------------------------------\n' inp = raw_input("Player%s's move: " % (player,)) try: sx, sy, dx, dy = inp.split(' ') except: if inp == 'exit': break if not cb.move(int(sx), int(sy), int(dx), int(dy)): print 'Invalid move' continue player = (player + 1) % 2 cb.set_allowed_color(color[player])
def test_valide_selected_cell(self): board = ChessBoard() player = 0 board.set_allowed_color(player) selected_figure = Point(5, 5) # Test 1 selected_figure.set_info(" ") self.assertTrue(board._invalid_selection(selected_figure)) # Test 2 selected_figure.set_info("wp") self.assertFalse(board._invalid_selection(selected_figure)) # Test 3 player = 1 board.set_allowed_color(player) selected_figure.set_info("wp") self.assertTrue(board._invalid_selection(selected_figure))
def test_play(self): board = ChessBoard() player = 0 board.set_allowed_color(player) # Test 1 white_figure = Point(2, 6, "wp") new_position = Point(2, 4) self.assertTrue(board.play(white_figure, new_position)) # Test 2 selected_figure = Point(-1, -1, " ") new_position = Point(2, 4) self.assertFalse(board.play(selected_figure, new_position)) # Test 3 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'], ['bp', 'bp', 'bp', ' ', 'bp', 'bp', 'bp', 'bp'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', 'bp', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) board.set_allowed_color(1) black_pawn = Point(3, 5, "bp") new_position = Point(4, 6) self.assertTrue(board.play(black_pawn, new_position)) # Test 4 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'], ['bp', 'bp', 'bp', ' ', 'bp', 'bp', 'bp', 'bp'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', 'bp', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) black_pawn = Point(3, 5, "bp") new_position = Point(4, 5) self.assertFalse(board.play(black_pawn, new_position)) # Test 5 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'], ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'], [' ', ' ', ' ', 'wp', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', ' ', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) board.set_allowed_color(0) white_figure = Point(3, 2, "wp") new_position = Point(4, 1) self.assertTrue(board.play(white_figure, new_position)) # Test 6 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'], ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'], [' ', ' ', ' ', 'wp', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', ' ', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) white_figure = Point(3, 2, "wp") new_position = Point(4, 2) self.assertFalse(board.play(white_figure, new_position)) # Test 7 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'], ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', ' '], [' ', ' ', ' ', 'wp', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', 'bp'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', ' ', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) white_rock = Point(7, 7, "wr") new_position = Point(7, 1) self.assertFalse(board.play(white_rock, new_position)) # Test 8 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'], ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', ' '], [' ', ' ', ' ', 'wp', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', 'wp'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', ' ', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) white_rock = Point(7, 7, "wr") new_position = Point(7, 1) self.assertFalse(board.play(white_rock, new_position)) # Test 9 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', 'br'], ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', ' '], [' ', ' ', ' ', 'wp', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', 'wp'], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', ' ', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) white_knight = Point(6, 7, "wn") new_position = Point(4, 6) self.assertFalse(board.play(white_knight, new_position)) # Test 10 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', ' '], ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'wp'], [' ', ' ', ' ', 'wp', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', ' ', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) white_pawn = Point(7, 1, "wp") new_position = Point(7, 0) self.assertTrue(board.play(white_pawn, new_position)) # Test 11 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', ' '], ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'wp'], [' ', ' ', ' ', 'wp', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', ' ', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) white_pawn = Point(7, 1, "wp") new_position = Point(6, 0) self.assertTrue(board.play(white_pawn, new_position)) # Test 12 testBoard = [['br', 'bn', 'bb', 'bq', 'bk', 'bb', 'bn', ' '], ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'wp'], [' ', ' ', ' ', 'wp', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '], ['wp', 'wp', 'wp', ' ', 'wp', 'wp', 'wp', ' '], ['wr', 'wn', 'wb', 'wq', 'wk', 'wb', 'wn', 'wr']] set_board(board, testBoard) white_pawn = Point(3, 3, " ") new_position = Point(6, 0) self.assertFalse(board.play(white_pawn, new_position))
def pygame_mainloop(): #pygame board test cb = ChessBoard() color = ('w', 'b') player = 0 cb.set_allowed_color(color[player]) screen = pygame.display.set_mode((640, 480)) board, chess = load_images() selected=pygame.Surface((32,24),pygame.SWSURFACE|pygame.SRCALPHA,32) #selected.set_alpha(60) selected.fill((255,255,0)) selection = False sx, sy = None, None offset = 22 print 'player\'s', player, 'turn' while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: return elif event.type == pygame.MOUSEBUTTONUP: x, y = event.pos x -= offset y -= offset if 0 <= x <= 255 and 0 <= y <= 191: x /= 32 y /= 24 if selection: selection = False if cb.move(int(sx), int(sy), int(x), int(y)): player = (player + 1) % 2 cb.set_allowed_color(color[player]) print 'player\'s', player, 'turn' sx, sy = None, None else: sx, sy = x, y if cb.getPiece(x, y)[0] == color[player]: selection = True else: sx, sy = None, None screen.fill((109,165, 165)) screen.blit(board, (offset, offset)) if sx != None and sy != None: screen.blit(selected, (sx*32+offset+2, sy*24+offset+2)) for y, col in enumerate(cb.getBoard()): for x, piece in enumerate(col): if piece[0] != ' ': screen.blit(chess[piece], (x*32+offset, y*24+offset-12)) pygame.display.update() time.sleep(0.04)