def test_valid_black_move_rook(self): board_string_sample_black_rook = " QQQ QrQ QQQ r " valid_black_rooks_moves_sample = [[2, 0, 3, 1, 5], [2, 1, 3, 1, 5], [2, 2, 3, 1, 5], [3, 0, 3, 1, 5], [3, 2, 3, 1, 5], [4, 0, 3, 1, 5], [4, 1, 3, 1, 5], [4, 2, 3, 1, 5]] get_board_sample_8 = get_board(board_string_sample_black_rook) result = valid_white_move(get_board_sample_8) self.assertEqual(result, valid_black_rooks_moves_sample)
def test_valid_white_move_bishop(self): board_string_sample_bishop = " qqq qBq qqq " valid_white_bishops_moves_sample = [[12, 1, 11, 0, 10], [12, 1, 13, 0, 10], [12, 1, 11, 2, 10], [12, 1, 13, 2, 10]] get_board_sample_4 = get_board(board_string_sample_bishop) result = valid_white_move(get_board_sample_4) self.assertEqual(result, valid_white_bishops_moves_sample)
def test_valid_white_move_pawn(self): board_string_sample_pawns = " P q q P P " valid_white_pawns_moves_sample = [[7, 7, 6, 7, 4], [7, 7, 6, 7, 1], [12, 1, 11, 0, 10], [12, 1, 11, 2, 10], [12, 1, 10, 1, 3], [12, 1, 11, 1, 1], [13, 6, 11, 6, 3], [13, 6, 12, 6, 1]] get_board_sample_1 = get_board(board_string_sample_pawns) result = valid_white_move(get_board_sample_1) self.assertEqual(result, valid_white_pawns_moves_sample)
def test_valid_white_move_rook(self): board_string_sample_rook = " R q qRq q " valid_white_rooks_moves_sample = [[8, 2, 7, 2, 1], [8, 2, 8, 3, 1], [8, 2, 8, 1, 1], [8, 2, 9, 2, 2], [12, 1, 11, 1, 10], [12, 1, 12, 2, 10], [12, 1, 12, 0, 10], [12, 1, 13, 1, 10]] get_board_sample_3 = get_board(board_string_sample_rook) result = valid_white_move(get_board_sample_3) self.assertEqual(result, valid_white_rooks_moves_sample)
def test_valid_white_move_queen(self): board_string_sample_queens = " Q qqq qQq qqq " valid_white_queens_moves_sample = [[12, 1, 11, 1, 5], [12, 1, 12, 2, 5], [12, 1, 12, 0, 5], [12, 1, 13, 1, 5], [12, 1, 11, 0, 5], [12, 1, 13, 0, 5], [12, 1, 11, 2, 5], [12, 1, 13, 2, 5]] get_board_sample_2 = get_board(board_string_sample_queens) result = valid_white_move(get_board_sample_2) self.assertEqual(result, valid_white_queens_moves_sample)
def test_valid_white_move_king(self): board_string_sample_kings = " K qqq qKq qqq " valid_white_kings_moves_sample = [[8, 2, 7, 2, 1], [8, 2, 8, 3, 1], [8, 2, 8, 1, 1], [8, 2, 9, 2, 2], [8, 2, 7, 1, 1], [8, 2, 9, 1, 1], [8, 2, 7, 3, 1], [8, 2, 9, 3, 1], [12, 1, 11, 1, 10], [12, 1, 12, 2, 10], [12, 1, 12, 0, 10], [12, 1, 13, 1, 10], [12, 1, 11, 0, 10], [12, 1, 13, 0, 10], [12, 1, 11, 2, 10], [12, 1, 13, 2, 10]] get_board_sample_5 = get_board(board_string_sample_kings) result = valid_white_move(get_board_sample_5) self.assertEqual(result, valid_white_kings_moves_sample)
async def play(websocket): while True: try: response = await websocket.recv() print(f"< {response}") data = json.loads(response) if data['event'] == 'update_user_list': pass if data['event'] == 'gameover': pass if data['event'] == 'ask_challenge': if data['data']['username'] == 'dinok': await send( websocket, 'accept_challenge', { 'board_id': data['data']['board_id'], }, ) if data['event'] == 'your_turn': #import ipdb;ipdb.set_trace() board = data['data']['board'] b = get_board(board) player_color = data['data']['actual_turn'] x = valid_white_move(b) y = valid_black_move(b) j = best_move_white(x) k = best_move_white(y) print('MY COLOR :', data['data']['actual_turn']) print('MOVES LEFT :', data['data']['move_left']) print('OPONENT :', data['data']['opponent_username']) #print('WHITE',x) #print('BLACK',y) if player_color == 'white': #import ipdb;ipdb.set_trace() await send( websocket, 'move', { 'board_id': data['data']['board_id'], 'turn_token': data['data']['turn_token'], 'from_row': j[0][0], 'from_col': j[0][1], 'to_row': j[0][2], 'to_col': j[0][3], }, ) else: await send( websocket, 'move', { 'board_id': data['data']['board_id'], 'turn_token': data['data']['turn_token'], 'from_row': k[0][0], 'from_col': k[0][1], 'to_row': k[0][2], 'to_col': k[0][3], }, ) except Exception as e: print('error {}'.format(str(e))) break # force login again